Add node bindings
Ben Alpert committed
Sep 19, 2015 at 10:59 UTC
b3bccf22735fc96ade8415b2be02b969779bc118
5 files changed
+99
scripts/perf-counters/README.md
new
+16
@@ -0,0 +1,16 @@
1
+# perf-counters
2
+
3
+Lightweight bindings to Linux perf event counters.
4
+
5
+```
6
+$ node
7
+> var PerfCounters = require('perf-counters');
8
+> PerfCounters.init();
9
+> var start = PerfCounters.getCounters(); console.log('test'); var end = PerfCounters.getCounters();
10
+test
11
+> start
12
+{ instructions: 1382, loads: 421, stores: 309 }
13
+> end
14
+{ instructions: 647633, loads: 195771, stores: 133246 }
15
+>
16
+```
scripts/perf-counters/binding.gyp
new
+15
@@ -0,0 +1,15 @@
1
+{
2
+ "targets": [
3
+ {
4
+ "target_name": "perfcounters",
5
+ "sources": [
6
+ "src/hardware-counter.cpp",
7
+ "src/perf-counters.cpp",
8
+ "src/thread-local.cpp",
9
+ ],
10
+ "cflags": [
11
+ "-Wno-sign-compare",
12
+ ],
13
+ },
14
+ ],
15
+}
scripts/perf-counters/index.js
new
+3
@@ -0,0 +1,3 @@
1
+'use strict';
2
+
3
+module.exports = require('bindings')('perfcounters');
scripts/perf-counters/package.json
new
+10
@@ -0,0 +1,10 @@
1
+{
2
+ "name": "perf-counters",
3
+ "version": "0.1.2",
4
+ "description": "Lightweight bindings to Linux perf event counters.",
5
+ "main": "index.js",
6
+ "license": "BSD-3-Clause",
7
+ "dependencies": {
8
+ "bindings": "^1.2.1"
9
+ }
10
+}
scripts/perf-counters/src/perf-counters.cpp
new
+55
@@ -0,0 +1,55 @@
1
+/**
2
+ * Copyright 2013-2015, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+
10
+#include <node.h>
11
+
12
+#include "hardware-counter.h"
13
+
14
+namespace PerfCounters {
15
+
16
+using HPHP::HardwareCounter;
17
+
18
+void Init(const v8::FunctionCallbackInfo<v8::Value>& args) {
19
+ // TODO: Allow customizing recorded events
20
+ bool enable = true;
21
+ std::string events = "";
22
+ bool recordSubprocesses = false;
23
+ HardwareCounter::Init(enable, events, recordSubprocesses);
24
+ HardwareCounter::s_counter.getCheck();
25
+}
26
+
27
+void GetCounters(const v8::FunctionCallbackInfo<v8::Value>& args) {
28
+ v8::Isolate* isolate = args.GetIsolate();
29
+ v8::Local<v8::Object> obj = v8::Object::New(isolate);
30
+ std::pair<v8::Isolate*, v8::Local<v8::Object>> pair(isolate, obj);
31
+
32
+ HardwareCounter::GetPerfEvents(
33
+ [](const std::string& key, int64_t value, void* data) {
34
+ std::pair<v8::Isolate*, v8::Local<v8::Object>>& pair =
35
+ *reinterpret_cast<std::pair<v8::Isolate*, v8::Local<v8::Object>>*>(data);
36
+ v8::Isolate* isolate = pair.first;
37
+ v8::Local<v8::Object> obj = pair.second;
38
+ obj->Set(
39
+ v8::String::NewFromUtf8(isolate, key.c_str()),
40
+ v8::Number::New(isolate, value)
41
+ );
42
+ },
43
+ &pair);
44
+
45
+ args.GetReturnValue().Set(obj);
46
+}
47
+
48
+void InitModule(v8::Local<v8::Object> exports) {
49
+ NODE_SET_METHOD(exports, "init", Init);
50
+ NODE_SET_METHOD(exports, "getCounters", GetCounters);
51
+}
52
+
53
+NODE_MODULE(perfcounters, InitModule)
54
+
55
+}