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
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
18 changes: 0 additions & 18 deletions bindings/profilers/heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,23 +488,6 @@ NAN_METHOD(HeapProfiler::StopSamplingHeapProfiler) {
}
}

// Signature:
// getAllocationProfile(): AllocationProfileNode
NAN_METHOD(HeapProfiler::GetAllocationProfile) {
auto isolate = info.GetIsolate();
std::unique_ptr<v8::AllocationProfile> profile(
isolate->GetHeapProfiler()->GetAllocationProfile());
if (!profile) {
return Nan::ThrowError("Heap profiler is not enabled.");
}
v8::AllocationProfile::Node* root = profile->GetRootNode();
auto state = PerIsolateData::For(isolate)->GetHeapProfilerState();
if (state) {
state->OnNewProfile();
}
info.GetReturnValue().Set(TranslateAllocationProfile(root));
}

// mapAllocationProfile(callback): callback result
NAN_METHOD(HeapProfiler::MapAllocationProfile) {
if (info.Length() < 1 || !info[0]->IsFunction()) {
Expand Down Expand Up @@ -596,7 +579,6 @@ NAN_MODULE_INIT(HeapProfiler::Init) {
heapProfiler, "startSamplingHeapProfiler", StartSamplingHeapProfiler);
Nan::SetMethod(
heapProfiler, "stopSamplingHeapProfiler", StopSamplingHeapProfiler);
Nan::SetMethod(heapProfiler, "getAllocationProfile", GetAllocationProfile);
Nan::SetMethod(heapProfiler, "mapAllocationProfile", MapAllocationProfile);
Nan::SetMethod(heapProfiler, "monitorOutOfMemory", MonitorOutOfMemory);
Nan::Set(target,
Expand Down
4 changes: 0 additions & 4 deletions bindings/profilers/heap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class HeapProfiler {
// stopSamplingHeapProfiler()
static NAN_METHOD(StopSamplingHeapProfiler);

// Signature:
// getAllocationProfile(): AllocationProfileNode
static NAN_METHOD(GetAllocationProfile);

// Signature:
// mapAllocationProfile(callback): callback result
static NAN_METHOD(MapAllocationProfile);
Expand Down
29 changes: 0 additions & 29 deletions bindings/translate-heap-profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,6 @@ class HeapProfileTranslator : ProfileTranslator {
#undef X

public:
v8::Local<v8::Value> TranslateAllocationProfile(
v8::AllocationProfile::Node* node) {
v8::Local<v8::Array> children = NewArray(node->children.size());
for (size_t i = 0; i < node->children.size(); i++) {
Set(children, i, TranslateAllocationProfile(node->children[i]));
}

v8::Local<v8::Array> allocations = NewArray(node->allocations.size());
for (size_t i = 0; i < node->allocations.size(); i++) {
auto alloc = node->allocations[i];
Set(allocations,
i,
CreateAllocation(NewNumber(alloc.count), NewNumber(alloc.size)));
}

return CreateNode(node->name,
node->script_name,
NewInteger(node->script_id),
NewInteger(node->line_number),
NewInteger(node->column_number),
children,
allocations);
}

v8::Local<v8::Value> TranslateAllocationProfile(Node* node) {
v8::Local<v8::Array> children = NewArray(node->children.size());
for (size_t i = 0; i < node->children.size(); i++) {
Expand Down Expand Up @@ -142,11 +118,6 @@ std::shared_ptr<Node> TranslateAllocationProfileToCpp(
return new_node;
}

v8::Local<v8::Value> TranslateAllocationProfile(
v8::AllocationProfile::Node* node) {
return HeapProfileTranslator().TranslateAllocationProfile(node);
}

v8::Local<v8::Value> TranslateAllocationProfile(Node* node) {
return HeapProfileTranslator().TranslateAllocationProfile(node);
}
Expand Down
3 changes: 0 additions & 3 deletions bindings/translate-heap-profile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,4 @@ std::shared_ptr<Node> TranslateAllocationProfileToCpp(
v8::AllocationProfile::Node* node);

v8::Local<v8::Value> TranslateAllocationProfile(Node* node);
v8::Local<v8::Value> TranslateAllocationProfile(
v8::AllocationProfile::Node* node);

} // namespace dd
4 changes: 0 additions & 4 deletions ts/src/heap-profiler-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ export function stopSamplingHeapProfiler() {
profiler.heapProfiler.stopSamplingHeapProfiler();
}

export function getAllocationProfile(): AllocationProfileNode {
return profiler.heapProfiler.getAllocationProfile();
}

export function mapAllocationProfile<T>(
callback: (root: AllocationProfileNode) => T
): T {
Expand Down
43 changes: 3 additions & 40 deletions ts/src/heap-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {Profile} from 'pprof-format';

import {
getAllocationProfile,
mapAllocationProfile,
startSamplingHeapProfiler,
stopSamplingHeapProfiler,
Expand All @@ -34,20 +33,6 @@ import {isMainThread} from 'worker_threads';
let enabled = false;
let heapIntervalBytes = 0;
let heapStackDepth = 0;

/*
* Collects a heap profile when heapProfiler is enabled. Otherwise throws
* an error.
*
* Data is returned in V8 allocation profile format.
*/
export function v8Profile(): AllocationProfileNode {
if (!enabled) {
throw new Error('Heap profiler is not enabled.');
}
return getAllocationProfile();
}

/**
* Collects a heap profile when heapProfiler is enabled. Otherwise throws
* an error.
Expand All @@ -59,35 +44,13 @@ export function v8Profile(): AllocationProfileNode {
* @param callback - function to convert the heap profiler to a converted profile
* @returns <T> converted profile
*/
export function v8ProfileV2<T>(
callback: (root: AllocationProfileNode) => T
): T {
export function v8Profile<T>(callback: (root: AllocationProfileNode) => T): T {
if (!enabled) {
throw new Error('Heap profiler is not enabled.');
}
return mapAllocationProfile(callback);
}

/**
* Collects a profile and returns it serialized in pprof format.
* Throws if heap profiler is not enabled.
*
* @param ignoreSamplePath
* @param sourceMapper
*/
export function profile(
ignoreSamplePath?: string,
sourceMapper?: SourceMapper,
generateLabels?: GenerateAllocationLabelsFunction
): Profile {
return convertProfile(
v8Profile(),
ignoreSamplePath,
sourceMapper,
generateLabels
);
}

export function convertProfile(
rootNode: AllocationProfileNode,
ignoreSamplePath?: string,
Expand Down Expand Up @@ -130,12 +93,12 @@ export function convertProfile(
* @param sourceMapper
* @param generateLabels
*/
export function profileV2(
export function profile(
ignoreSamplePath?: string,
sourceMapper?: SourceMapper,
generateLabels?: GenerateAllocationLabelsFunction
): Profile {
return v8ProfileV2(root => {
return v8Profile(root => {
return convertProfile(root, ignoreSamplePath, sourceMapper, generateLabels);
});
}
Expand Down
1 change: 0 additions & 1 deletion ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const heap = {
start: heapProfiler.start,
stop: heapProfiler.stop,
profile: heapProfiler.profile,
profileV2: heapProfiler.profileV2,
convertProfile: heapProfiler.convertProfile,
v8Profile: heapProfiler.v8Profile,
monitorOutOfMemory: heapProfiler.monitorOutOfMemory,
Expand Down
112 changes: 0 additions & 112 deletions ts/test/heap-memory-worker.ts

This file was deleted.

Loading