diff --git a/scripts/perf-counters/README.md b/scripts/perf-counters/README.md new file mode 100644 index 0000000000..48fa7ec753 --- /dev/null +++ b/scripts/perf-counters/README.md @@ -0,0 +1,16 @@ +# perf-counters + +Lightweight bindings to Linux perf event counters. + +``` +$ node +> var PerfCounters = require('perf-counters'); +> PerfCounters.init(); +> var start = PerfCounters.getCounters(); console.log('test'); var end = PerfCounters.getCounters(); +test +> start +{ instructions: 1382, loads: 421, stores: 309 } +> end +{ instructions: 647633, loads: 195771, stores: 133246 } +> +``` diff --git a/scripts/perf-counters/binding.gyp b/scripts/perf-counters/binding.gyp new file mode 100644 index 0000000000..397d2c7f14 --- /dev/null +++ b/scripts/perf-counters/binding.gyp @@ -0,0 +1,15 @@ +{ + "targets": [ + { + "target_name": "perfcounters", + "sources": [ + "src/hardware-counter.cpp", + "src/perf-counters.cpp", + "src/thread-local.cpp", + ], + "cflags": [ + "-Wno-sign-compare", + ], + }, + ], +} diff --git a/scripts/perf-counters/index.js b/scripts/perf-counters/index.js new file mode 100644 index 0000000000..5329a141d5 --- /dev/null +++ b/scripts/perf-counters/index.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('bindings')('perfcounters'); diff --git a/scripts/perf-counters/package.json b/scripts/perf-counters/package.json new file mode 100644 index 0000000000..4c362cea36 --- /dev/null +++ b/scripts/perf-counters/package.json @@ -0,0 +1,10 @@ +{ + "name": "perf-counters", + "version": "0.1.2", + "description": "Lightweight bindings to Linux perf event counters.", + "main": "index.js", + "license": "BSD-3-Clause", + "dependencies": { + "bindings": "^1.2.1" + } +} diff --git a/scripts/perf-counters/src/perf-counters.cpp b/scripts/perf-counters/src/perf-counters.cpp new file mode 100644 index 0000000000..3be6d0ece3 --- /dev/null +++ b/scripts/perf-counters/src/perf-counters.cpp @@ -0,0 +1,55 @@ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#include + +#include "hardware-counter.h" + +namespace PerfCounters { + +using HPHP::HardwareCounter; + +void Init(const v8::FunctionCallbackInfo& args) { + // TODO: Allow customizing recorded events + bool enable = true; + std::string events = ""; + bool recordSubprocesses = false; + HardwareCounter::Init(enable, events, recordSubprocesses); + HardwareCounter::s_counter.getCheck(); +} + +void GetCounters(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); + v8::Local obj = v8::Object::New(isolate); + std::pair> pair(isolate, obj); + + HardwareCounter::GetPerfEvents( + [](const std::string& key, int64_t value, void* data) { + std::pair>& pair = + *reinterpret_cast>*>(data); + v8::Isolate* isolate = pair.first; + v8::Local obj = pair.second; + obj->Set( + v8::String::NewFromUtf8(isolate, key.c_str()), + v8::Number::New(isolate, value) + ); + }, + &pair); + + args.GetReturnValue().Set(obj); +} + +void InitModule(v8::Local exports) { + NODE_SET_METHOD(exports, "init", Init); + NODE_SET_METHOD(exports, "getCounters", GetCounters); +} + +NODE_MODULE(perfcounters, InitModule) + +}