Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions bindings/profilers/heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,33 @@

#include <chrono>
#include <memory>
#include <mutex>
#include <unordered_set>
#include <vector>

#include <node.h>
#include <v8-profiler.h>

namespace dd {

// Track which isolates have cleanup hooks registered for heap profiler
static std::unordered_set<v8::Isolate*> g_heap_profiler_isolates;
static std::mutex g_heap_profiler_mutex;

// Cleanup hook to stop heap profiler before isolate is destroyed
static void HeapProfilerCleanupHook(void* data) {
auto isolate = static_cast<v8::Isolate*>(data);
{
const std::lock_guard<std::mutex> lock(g_heap_profiler_mutex);
g_heap_profiler_isolates.erase(isolate);
}
// Stop the sampling heap profiler to prevent crash during V8 teardown
auto heap_profiler = isolate->GetHeapProfiler();
if (heap_profiler) {
heap_profiler->StopSamplingHeapProfiler();
}
}

static size_t NearHeapLimit(void* data,
size_t current_heap_limit,
size_t initial_heap_limit);
Expand Down Expand Up @@ -497,6 +517,19 @@ size_t NearHeapLimit(void* data,
}

NAN_METHOD(HeapProfiler::StartSamplingHeapProfiler) {
auto isolate = info.GetIsolate();

// Register cleanup hook if not already registered for this isolate
{
const std::lock_guard<std::mutex> lock(g_heap_profiler_mutex);
if (g_heap_profiler_isolates.find(isolate) ==
g_heap_profiler_isolates.end()) {
node::AddEnvironmentCleanupHook(
isolate, HeapProfilerCleanupHook, isolate);
g_heap_profiler_isolates.insert(isolate);
}
}

if (info.Length() == 2) {
if (!info[0]->IsUint32()) {
return Nan::ThrowTypeError("First argument type must be uint32.");
Expand All @@ -508,10 +541,10 @@ NAN_METHOD(HeapProfiler::StartSamplingHeapProfiler) {
uint64_t sample_interval = info[0].As<v8::Integer>()->Value();
int stack_depth = info[1].As<v8::Integer>()->Value();

info.GetIsolate()->GetHeapProfiler()->StartSamplingHeapProfiler(
sample_interval, stack_depth);
isolate->GetHeapProfiler()->StartSamplingHeapProfiler(sample_interval,
stack_depth);
} else {
info.GetIsolate()->GetHeapProfiler()->StartSamplingHeapProfiler();
isolate->GetHeapProfiler()->StartSamplingHeapProfiler();
}
}

Expand All @@ -521,6 +554,15 @@ NAN_METHOD(HeapProfiler::StopSamplingHeapProfiler) {
auto isolate = info.GetIsolate();
isolate->GetHeapProfiler()->StopSamplingHeapProfiler();
PerIsolateData::For(isolate)->GetHeapProfilerState().reset();

// Remove cleanup hook since profiler is explicitly stopped
{
const std::lock_guard<std::mutex> lock(g_heap_profiler_mutex);
if (g_heap_profiler_isolates.erase(isolate) == 1) {
node::RemoveEnvironmentCleanupHook(
isolate, HeapProfilerCleanupHook, isolate);
}
}
}

// Signature:
Expand Down
31 changes: 20 additions & 11 deletions bindings/profilers/wall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ struct TimeTicks {
static int64_t Now();
};
} // namespace base
#if NODE_MAJOR_VERSION >= 24
#if NODE_MAJOR_VERSION >= 22

// Available from 22.7.0
#define DD_WALL_USE_CPED true

namespace internal {
#if NODE_MAJOR_VERSION == 24
#if NODE_MAJOR_VERSION < 25
struct HandleScopeData {
v8::internal::Address* next;
v8::internal::Address* limit;
};
#endif
#endif // NODE_MAJOR_VERSION < 25
#if NODE_MAJOR_VERSION >= 24
constexpr int kHandleBlockSize = v8::internal::KB - 2;
#endif // NODE_MAJOR_VERSION >= 24
} // namespace internal
#endif
#else // NODE_MAJOR_VERSION >= 22
#define DD_WALL_USE_CPED false
#endif //
} // namespace v8

static int64_t Now() {
Expand All @@ -61,18 +69,14 @@ static int64_t Now() {

#else
#define DD_WALL_USE_SIGPROF false
#define DD_WALL_USE_CPED false

static int64_t Now() {
return 0;
};

#endif

#if NODE_MAJOR_VERSION >= 23
#define DD_WALL_USE_CPED true
#else
#define DD_WALL_USE_CPED false
#endif

using namespace v8;

namespace dd {
Expand Down Expand Up @@ -905,7 +909,12 @@ NAN_METHOD(WallProfiler::New) {
#if !DD_WALL_USE_CPED
if (useCPED) {
return Nan::ThrowTypeError(
"useCPED is not supported on this Node.js version.");
#ifndef _WIN32
"useCPED is not supported on this Node.js version."
#else
"useCPED is not supported on Windows."
#endif
);
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/pprof",
"version": "5.12.0",
"version": "5.13.0",
"description": "pprof support for Node.js",
"repository": {
"type": "git",
Expand Down
1 change: 0 additions & 1 deletion ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const time = {
v8ProfilerStuckEventLoopDetected:
timeProfiler.v8ProfilerStuckEventLoopDetected,
getState: timeProfiler.getState,
getMetrics: timeProfiler.getMetrics,
constants: timeProfiler.constants,
};

Expand Down
6 changes: 4 additions & 2 deletions ts/test/test-time-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import {satisfies} from 'semver';
import assert from 'assert';

const useCPED =
satisfies(process.versions.node, '>=24.0.0') &&
!process.execArgv.includes('--no-async-context-frame');
(satisfies(process.versions.node, '>=24.0.0') &&
!process.execArgv.includes('--no-async-context-frame')) ||
(satisfies(process.versions.node, '>=22.7.0') &&
process.execArgv.includes('--experimental-async-context-frame'));

const collectAsyncId = satisfies(process.versions.node, '>=24.0.0');

Expand Down
7 changes: 5 additions & 2 deletions ts/test/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ const intervalMicros = 10000;
const withContexts =
process.platform === 'darwin' || process.platform === 'linux';
const useCPED =
satisfies(process.versions.node, '>=24.0.0') &&
!process.execArgv.includes('--no-async-context-frame');
withContexts &&
((satisfies(process.versions.node, '>=24.0.0') &&
!process.execArgv.includes('--no-async-context-frame')) ||
(satisfies(process.versions.node, '>=22.7.0') &&
process.execArgv.includes('--experimental-async-context-frame')));
const collectAsyncId =
withContexts && satisfies(process.versions.node, '>=24.0.0');

Expand Down
7 changes: 5 additions & 2 deletions ts/test/worker2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ const withContexts =
process.platform === 'darwin' || process.platform === 'linux';

const useCPED =
satisfies(process.versions.node, '>=24.0.0') &&
!process.execArgv.includes('--no-async-context-frame');
withContexts &&
((satisfies(process.versions.node, '>=24.0.0') &&
!process.execArgv.includes('--no-async-context-frame')) ||
(satisfies(process.versions.node, '>=22.7.0') &&
process.execArgv.includes('--experimental-async-context-frame')));

const collectAsyncId =
withContexts && satisfies(process.versions.node, '>=24.0.0');
Expand Down