-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agents: add support for start/stopTrackingHeapObjects
This patch implements triggers for `startTrackingHeapObjects` and `stopTrackingHeapObjects` in the heap profiler. The implementation is based on what the inspector does: The `startTrackingHeapObjects` will simply activate that in the HeapProfiler and; the `stopTrackingHeapObjects` will create a heap snapshot and deactivate the `TrackingHeapObjects` in the Heap profiler. This patch also includes the `trackAllocations` option supported. Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
- Loading branch information
Showing
7 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <node.h> | ||
#include <v8.h> | ||
#include <uv.h> | ||
#include <nsolid.h> | ||
|
||
#include <assert.h> | ||
#include <map> | ||
|
||
using v8::FunctionCallbackInfo; | ||
using v8::Uint32; | ||
using v8::Value; | ||
|
||
std::map<uint64_t, std::string> snapshots; | ||
|
||
static void got_snapshot(int status, | ||
std::string snapshot, | ||
uint64_t thread_id) { | ||
assert(status == 0); | ||
snapshots[thread_id] += snapshot; | ||
} | ||
|
||
static void StartTrackingHeapObjects(const FunctionCallbackInfo<Value>& args) { | ||
assert(args[0]->IsUint32()); | ||
// Track allocations | ||
assert(args[1]->IsBoolean()); | ||
|
||
uint64_t thread_id = args[0].As<Uint32>()->Value(); | ||
bool track_allocations = args[1]->BooleanValue(args.GetIsolate()); | ||
|
||
node::nsolid::Snapshot::StartTrackingHeapObjects( | ||
node::nsolid::GetEnvInst(thread_id), track_allocations, thread_id); | ||
} | ||
|
||
static void StopTrackingHeapObjects(const FunctionCallbackInfo<Value>& args) { | ||
assert(args[0]->IsUint32()); | ||
// Redacted heap snapshot | ||
assert(args[1]->IsBoolean()); | ||
|
||
uint64_t thread_id = args[0].As<Uint32>()->Value(); | ||
bool redacted = args[1]->BooleanValue(args.GetIsolate()); | ||
|
||
node::nsolid::Snapshot::StopTrackingHeapObjects( | ||
node::nsolid::GetEnvInst(thread_id), redacted, got_snapshot, thread_id); | ||
} | ||
|
||
static void at_exit_cb() { | ||
for (const auto& pair : snapshots) { | ||
assert(pair.second.size() > 0); | ||
} | ||
} | ||
|
||
NODE_MODULE_INIT(/* exports, module, context */) { | ||
NODE_SET_METHOD( | ||
exports, "startTrackingHeapObjects", StartTrackingHeapObjects); | ||
NODE_SET_METHOD(exports, "stopTrackingHeapObjects", StopTrackingHeapObjects); | ||
node::nsolid::SharedEnvInst envinst = node::nsolid::GetLocalEnvInst(context); | ||
if (node::nsolid::IsMainThread(envinst)) { | ||
atexit(at_exit_cb); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
'targets': [{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.cc' ], | ||
'includes': ['../common.gypi'], | ||
'target_defaults': { | ||
'default_configuration': 'Release', | ||
'configurations': { | ||
'Debug': { | ||
'defines': [ 'DEBUG', '_DEBUG' ], | ||
'cflags': [ '-g', '-O0', '-fstandalone-debug' ], | ||
} | ||
}, | ||
}, | ||
}], | ||
} |
43 changes: 43 additions & 0 deletions
43
test/addons/nsolid-track-heap-objects/nsolid-track-heap-objects.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const { buildType, mustCall, skip } = require('../../common'); | ||
const assert = require('assert'); | ||
const bindingPath = require.resolve(`./build/${buildType}/binding`); | ||
const binding = require(bindingPath); | ||
const { Worker, isMainThread, parentPort } = require('worker_threads'); | ||
|
||
if (process.env.NSOLID_COMMAND) | ||
skip('required to run without the Console'); | ||
|
||
if (!isMainThread && +process.argv[2] !== process.pid) | ||
skip('Test must first run as the main thread'); | ||
|
||
if (!isMainThread) { | ||
parentPort.postMessage('hi'); | ||
if (process.argv[3] === 'spin') { | ||
const t = 0; | ||
while (Date.now() - t < 500); | ||
} else if (process.argv[3] === 'timeout') { | ||
setTimeout(() => {}, 500); | ||
} | ||
return; | ||
} | ||
|
||
process.on('beforeExit', mustCall()); | ||
|
||
createWorker('spin', () => { | ||
createWorker('timeout'); | ||
}); | ||
|
||
function createWorker(arg, cb) { | ||
const worker = new Worker(__filename, { argv: [process.pid, arg] }); | ||
worker.once('message', mustCall((msg) => { | ||
assert.strictEqual(msg, 'hi'); | ||
binding.startTrackingHeapObjects(worker.threadId, false); | ||
binding.stopTrackingHeapObjects(worker.threadId, false); | ||
})); | ||
worker.on('exit', mustCall(() => { | ||
if (cb) | ||
setImmediate(cb); | ||
})); | ||
} |