Skip to content
Open
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
16 changes: 0 additions & 16 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
system-test/busybench/package-lock.json
system-test/busybench-js/package-lock.json
prebuilds
tsconfig.tsbuildinfo
6 changes: 3 additions & 3 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
// limitations under the License.

module.exports = {
endOfLine:"auto",
...require('gts/.prettierrc.json')
}
endOfLine: 'auto',
...require('gts/.prettierrc.json'),
};
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ Install [`pprof`][npm-url] with `npm` or add to your `package.json`.
pprof -http=: heap.pb.gz
```

* Collecting a heap profile with V8 allocation profile format:
* Collecting a heap profile with V8 allocation profile format:
```javascript
const profile = await pprof.heap.v8Profile();
const profile = pprof.heap.v8Profile(pprof.heap.convertProfile);
```
`v8Profile` accepts a callback and returns its result. Allocation nodes
are only valid during the callback, so copy/transform what you need
before returning. `heap.convertProfile` performs that conversion during
the callback, and `heap.profile()` uses it under the hood.

[build-image]: https://github.com/Datadog/pprof-nodejs/actions/workflows/build.yml/badge.svg?branch=main
[build-url]: https://github.com/Datadog/pprof-nodejs/actions/workflows/build.yml
Expand Down
5 changes: 4 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"bindings/thread-cpu-clock.cc",
"bindings/translate-heap-profile.cc",
"bindings/translate-time-profile.cc",
"bindings/binding.cc"
"bindings/binding.cc",
"bindings/map-get.cc",
"bindings/allocation-profile-node.cc"
],
"include_dirs": [
"bindings",
Expand All @@ -42,6 +44,7 @@
"bindings/translate-heap-profile.cc",
"bindings/translate-time-profile.cc",
"bindings/test/binding.cc",
"bindings/allocation-profile-node.cc"
],
"include_dirs": [
"bindings",
Expand Down
132 changes: 132 additions & 0 deletions bindings/allocation-profile-node.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2026 Datadog, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "allocation-profile-node.hh"
#include "per-isolate-data.hh"

using namespace v8;

namespace dd {

template <typename F>
void AllocationProfileNodeView::mapAllocationProfileNode(
const Nan::PropertyCallbackInfo<Value>& info, F&& mapper) {
auto* node = static_cast<AllocationProfile::Node*>(
Nan::GetInternalFieldPointer(info.Holder(), 0));
info.GetReturnValue().Set(mapper(node));
}

NAN_MODULE_INIT(AllocationProfileNodeView::Init) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>();
tpl->SetClassName(Nan::New("AllocationProfileNode").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

auto inst = tpl->InstanceTemplate();
Nan::SetAccessor(inst, Nan::New("name").ToLocalChecked(), GetName);
Nan::SetAccessor(
inst, Nan::New("scriptName").ToLocalChecked(), GetScriptName);
Nan::SetAccessor(inst, Nan::New("scriptId").ToLocalChecked(), GetScriptId);
Nan::SetAccessor(
inst, Nan::New("lineNumber").ToLocalChecked(), GetLineNumber);
Nan::SetAccessor(
inst, Nan::New("columnNumber").ToLocalChecked(), GetColumnNumber);
Nan::SetAccessor(
inst, Nan::New("allocations").ToLocalChecked(), GetAllocations);
Nan::SetAccessor(inst, Nan::New("children").ToLocalChecked(), GetChildren);

PerIsolateData::For(Isolate::GetCurrent())
->AllocationNodeConstructor()
.Reset(Nan::GetFunction(tpl).ToLocalChecked());
}

Local<Object> AllocationProfileNodeView::New(AllocationProfile::Node* node) {
auto* isolate = Isolate::GetCurrent();

Local<Function> constructor =
Nan::New(PerIsolateData::For(isolate)->AllocationNodeConstructor());

Local<Object> obj = Nan::NewInstance(constructor).ToLocalChecked();

Nan::SetInternalFieldPointer(obj, 0, node);

return obj;
}

NAN_GETTER(AllocationProfileNodeView::GetName) {
mapAllocationProfileNode(
info, [](AllocationProfile::Node* node) { return node->name; });
}

NAN_GETTER(AllocationProfileNodeView::GetScriptName) {
mapAllocationProfileNode(
info, [](AllocationProfile::Node* node) { return node->script_name; });
}

NAN_GETTER(AllocationProfileNodeView::GetScriptId) {
mapAllocationProfileNode(
info, [](AllocationProfile::Node* node) { return node->script_id; });
}

NAN_GETTER(AllocationProfileNodeView::GetLineNumber) {
mapAllocationProfileNode(
info, [](AllocationProfile::Node* node) { return node->line_number; });
}

NAN_GETTER(AllocationProfileNodeView::GetColumnNumber) {
mapAllocationProfileNode(
info, [](AllocationProfile::Node* node) { return node->column_number; });
}

NAN_GETTER(AllocationProfileNodeView::GetAllocations) {
mapAllocationProfileNode(info, [](AllocationProfile::Node* node) {
auto* isolate = Isolate::GetCurrent();
auto context = isolate->GetCurrentContext();

const auto& allocations = node->allocations;
Local<Array> arr = Array::New(isolate, allocations.size());
auto sizeBytes = String::NewFromUtf8Literal(isolate, "sizeBytes");
auto count = String::NewFromUtf8Literal(isolate, "count");

for (size_t i = 0; i < allocations.size(); i++) {
const auto& alloc = allocations[i];
Local<Object> alloc_obj = Object::New(isolate);
Nan::Set(alloc_obj,
sizeBytes,
Number::New(isolate, static_cast<double>(alloc.size)));
Nan::Set(alloc_obj,
count,
Number::New(isolate, static_cast<double>(alloc.count)));
arr->Set(context, i, alloc_obj).Check();
}
return arr;
});
}

NAN_GETTER(AllocationProfileNodeView::GetChildren) {
mapAllocationProfileNode(info, [](AllocationProfile::Node* node) {
auto* isolate = Isolate::GetCurrent();
auto context = isolate->GetCurrentContext();

const auto& children = node->children;
Local<Array> arr = Array::New(isolate, children.size());
for (size_t i = 0; i < children.size(); i++) {
arr->Set(context, i, AllocationProfileNodeView::New(children[i])).Check();
}
return arr;
});
}

} // namespace dd
44 changes: 44 additions & 0 deletions bindings/allocation-profile-node.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2026 Datadog, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

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

namespace dd {

class AllocationProfileNodeView {
public:
static NAN_MODULE_INIT(Init);

static v8::Local<v8::Object> New(v8::AllocationProfile::Node* node);

private:
template <typename F>
static void mapAllocationProfileNode(
const Nan::PropertyCallbackInfo<v8::Value>& info, F&& mapper);

static NAN_GETTER(GetName);
static NAN_GETTER(GetScriptName);
static NAN_GETTER(GetScriptId);
static NAN_GETTER(GetLineNumber);
static NAN_GETTER(GetColumnNumber);
static NAN_GETTER(GetAllocations);
static NAN_GETTER(GetChildren);
};

} // namespace dd
2 changes: 2 additions & 0 deletions bindings/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <node.h>
#include <v8.h>

#include "allocation-profile-node.hh"
#include "profilers/heap.hh"
#include "profilers/wall.hh"

Expand Down Expand Up @@ -47,6 +48,7 @@ NODE_MODULE_INIT(/* exports, module, context */) {
#pragma GCC diagnostic pop
#endif

dd::AllocationProfileNodeView::Init(exports);
dd::HeapProfiler::Init(exports);
dd::WallProfiler::Init(exports);
Nan::SetMethod(exports, "getNativeThreadId", GetNativeThreadId);
Expand Down
Loading