8000 chore: address follow-up comments to #69 by nolanmar511 · Pull Request #73 · google/pprof-nodejs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: address follow-up comments to #69 #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2019
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
193 changes: 90 additions & 103 deletions bindings/profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,42 @@
* limitations under the License.
*/

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

#include "nan.h"
#include "v8-profiler.h"

using namespace v8;

// Sampling Heap Profiler

Local<Value> TranslateAllocationProfile(AllocationProfile::Node* node) {
Local<Object> js_node = Nan::New<Object>();
js_node->Set(Nan::New<String>("name").ToLocalChecked(),
node->name);
js_node->Set(Nan::New<String>("name").ToLocalChecked(), node->name);
js_node->Set(Nan::New<String>("scriptName").ToLocalChecked(),
node->script_name);
node->script_name);
js_node->Set(Nan::New<String>("scriptId").ToLocalChecked(),
Nan::New<Integer>(node->script_id));
Nan::New<Integer>(node->script_id));
js_node->Set(Nan::New<String>("lineNumber").ToLocalChecked(),
Nan::New<Integer>(node->line_number));
Nan::New<Integer>(node->line_number));
js_node->Set(Nan::New<String>("columnNumber").ToLocalChecked(),
Nan::New<Integer>(node->column_number));
Nan::New<Integer>(node->column_number));
Local<Array> children = Nan::New<Array>(node->children.size());
for (size_t i = 0; i < node->children.size(); i++) {
children->Set(i, TranslateAllocationProfile(node->children[i]));
}
js_node->Set(Nan::New<String>("children").ToLocalChecked(),
children);
js_node->Set(Nan::New<String>("children").ToLocalChecked(), children);
Local<Array> allocations = Nan::New<Array>(node->allocations.size());
for (size_t i = 0; i < node->allocations.size(); i++) {
AllocationProfile::Allocation alloc = node->allocations[i];
Local<Object> js_alloc = Nan::New<Object>();
js_alloc->Set(Nan::New<String>("sizeBytes").ToLocalChecked(),
Nan::New<Number>(alloc.size));
Nan::New<Number>(alloc.size));
js_alloc->Set(Nan::New<String>("count").ToLocalChecked(),
Nan::New<Number>(alloc.count));
Nan::New<Number>(alloc.count));
allocations->Set(i, js_alloc);
}
js_node->Set(Nan::New<String>("allocations").ToLocalChecked(),
allocations);
js_node->Set(Nan::New<String>("allocations").ToLocalChecked(), allocations);
return js_node;
}

Expand All @@ -72,8 +70,8 @@ NAN_METHOD(StartSamplingHeapProfiler) {
int stack_depth = info[1].As<Integer>()->IntegerValue();
#endif

info.GetIsolate()->GetHeapProfiler()->
StartSamplingHeapProfiler(sample_interval, stack_depth);
info.GetIsolate()->GetHeapProfiler()->StartSamplingHeapProfiler(
sample_interval, stack_depth);
} else {
info.GetIsolate()->GetHeapProfiler()->StartSamplingHeapProfiler();
}
Expand All @@ -89,26 +87,25 @@ NAN_METHOD(StopSamplingHeapProfiler) {
// getAllocationProfile(): AllocationProfileNode
NAN_METHOD(GetAllocationProfile) {
std::unique_ptr<v8::AllocationProfile> profile(
info.GetIsolate()->GetHeapProfiler()->GetAllocationProfile());
info.GetIsolate()->GetHeapProfiler()->GetAllocationProfile());
AllocationProfile::Node* root = profile->GetRootNode();
info.GetReturnValue().Set(TranslateAllocationProfile(root));
}


// Time profiler

#if NODE_MODULE_VERSION > NODE_8_0_MODULE_VERSION
// This profiler exists for the lifetime of the program. Not calling
8000 // This profiler exists for the lifetime of the program. Not calling
// CpuProfiler::Dispose() is intentional.
CpuProfiler* cpuProfiler = CpuProfiler::New(v8::Isolate::GetCurrent());
#else
CpuProfiler* cpuProfiler = v8::Isolate::GetCurrent()->GetCpuProfiler();
#endif

Local<Object> CreateTimeNode(Local<String> name, Local<String> scriptName,
Local<Integer> scriptId, Local<Integer> lineNumber,
Local<Integer> columnNumber, Local<Integer> hitCount,
Local<Array> children) {
Local<Integer> scriptId, Local<Integer> lineNumber,
Local<Integer> columnNumber,
Local<Integer> hitCount, Local<Array> children) {
Local<Object> js_node = Nan::New<Object>();
js_node->Set(Nan::New<String>("name").ToLocalChecked(), name);
js_node->Set(Nan::New<String>("scriptName").ToLocalChecked(), scriptName);
Expand All @@ -120,13 +117,12 @@ Local<Object> CreateTimeNode(Local<String> name, Local<String> scriptName,
return js_node;
}


#if NODE_MODULE_VERSION > NODE_11_0_MODULE_VERSION
Local<Object> TranslateLineNumbersTimeProfileNode(const CpuProfileNode* parent,
const CpuProfileNode* node);
const CpuProfileNode* node);

Local<Array> GetLineNumberTimeProfileChildren(const CpuProfileNode* parent,
const CpuProfileNode* node) {
const CpuProfileNode* node) {
unsigned int index = 0;
Local<Array> children;
int32_t count = node->GetChildrenCount();
Expand All @@ -138,53 +134,47 @@ Local<Array> GetLineNumberTimeProfileChildren(const CpuProfileNode* parent,
node->GetLineTicks(&entries[0], hitLineCount);
children = Nan::New<Array>(count + hitLineCount);
for (const CpuProfileNode::LineTick entry : entries) {
children->Set(index++, CreateTimeNode(
node->GetFunctionName(),
node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(entry.line),
Nan::New<Integer>(0),
Nan::New<Integer>(entry.hit_count),
Nan::New<Array>(0)
));
children->Set(
index++, CreateTimeNode(
node->GetFunctionName(), node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(entry.line), Nan::New<Integer>(0),
Nan::New<Integer>(entry.hit_count), Nan::New<Array>(0)));
}
} else if (hitCount > 0) {
// Handle nodes for pseudo-functions like "process" and "garbage collection"
// which do not have hit line counts.
children = Nan::New<Array>(count + 1);
children->Set(index++, CreateTimeNode(
node->GetFunctionName(),
node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()),
Nan::New<Integer>(hitCount),
Nan::New<Array>(0)
));
children->Set(
index++,
CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()),
Nan::New<Integer>(hitCount), Nan::New<Array>(0)));
} else {
children = Nan::New<Array>(count);
}

for (int32_t i = 0; i < count; i++) {
children->Set(index++, TranslateLineNumbersTimeProfileNode(node,
node->GetChild(i)));
children->Set(index++,
TranslateLineNumbersTimeProfileNode(node, node->GetChild(i)));
};

return children;
}

Local<Object> TranslateLineNumbersTimeProfileNode(const CpuProfileNode* parent,
const CpuProfileNode* node) {
return CreateTimeNode(parent->GetFunctionName(),
parent->GetScriptResourceName(),
Nan::New<Integer>(parent->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()),
Nan::New<Integer>(0),
GetLineNumberTimeProfileChildren(parent, node));
const CpuProfileNode* node) {
return CreateTimeNode(
parent->GetFunctionName(), parent->GetScriptResourceName(),
Nan::New<Integer>(parent->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()), Nan::New<Integer>(0),
GetLineNumberTimeProfileChildren(parent, node));
}

// In profiles with line-level accurate line numbers, a node's line number
// In profiles with line level accurate line numbers, a node's line number
// and column number refer to the line/column from which the function was
// called.
Local<Value> TranslateLineNumbersTimeProfileRoot(const CpuProfileNode* node) {
Expand All @@ -207,15 +197,11 @@ Local<Value> TranslateLineNumbersTimeProfileRoot(const CpuProfileNode* node) {
}
}

return CreateTimeNode(
node->GetFunctionName(),
node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()),
Nan::New<Integer>(0),
children
);
return CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()),
Nan::New<Integer>(0), children);
}
#endif

Expand All @@ -226,49 +212,44 @@ Local<Value> TranslateTimeProfileNode(const CpuProfileNode* node) {
children->Set(i, TranslateTimeProfileNode(node->GetChild(i)));
}

return CreateTimeNode(
node->GetFunctionName(),
node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()),
Nan::New<Integer>(node->GetHitCount()),
children
);
return CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(),
Nan::New<Integer>(node->GetScriptId()),
Nan::New<Integer>(node->GetLineNumber()),
Nan::New<Integer>(node->GetColumnNumber()),
Nan::New<Integer>(node->GetHitCount()), children);
}

Local<Value> TranslateTimeProfile(const CpuProfile* profile, bool hasDetailedLines) {
Local<Value> TranslateTimeProfile(const CpuProfile* profile,
bool includeLineInfo) {
Local<Object> js_profile = Nan::New<Object>();
js_profile->Set(Nan::New<String>("title").ToLocalChecked(),
profile->GetTitle());
profile->GetTitle());

#if NODE_MODULE_VERSION > NODE_11_0_MODULE_VERSION
if (hasDetailedLines) {
if (includeLineInfo) {
js_profile->Set(
Nan::New<String>("topDownRoot").ToLocalChecked(),
TranslateLineNumbersTimeProfileRoot(profile->GetTopDownRoot()));
Nan::New<String>("topDownRoot").ToLocalChecked(),
TranslateLineNumbersTimeProfileRoot(profile->GetTopDownRoot()));
} else {
js_profile->Set(
Nan::New<String>("topDownRoot").ToLocalChecked(),
TranslateTimeProfileNode(profile->GetTopDownRoot()));
js_profile->Set(Nan::New<String>("topDownRoot").ToLocalChecked(),
TranslateTimeProfileNode(profile->GetTopDownRoot()));
}
#else
js_profile->Set(
Nan::New<String>("topDownRoot").ToLocalChecked(),
TranslateTimeProfileNode(profile->GetTopDownRoot()));
js_profile->Set(Nan::New<String>("topDownRoot").ToLocalChecked(),
TranslateTimeProfileNode(profile->GetTopDownRoot()));
#endif
js_profile->Set(Nan::New<String>("startTime").ToLocalChecked(),
Nan::New<Number>(profile->GetStartTime()));
Nan::New<Number>(profile->GetStartTime()));
js_profile->Set(Nan::New<String>("endTime").ToLocalChecked(),
Nan::New<Number>(profile->GetEndTime()));
Nan::New<Number>(profile->GetEndTime()));
return js_profile;
}

// Signature:
// startProfiling(runName: string, includeLineInfo: boolean)
NAN_METHOD(StartProfiling) {
if (info.Length() != 2) {
return Nan::ThrowTypeError("StartProfling must have two arguments.");
return Nan::ThrowTypeError("StartProfiling must have two arguments.");
}
if (!info[0]->IsString()) {
return Nan::ThrowTypeError("First argument must be a string.");
Expand All @@ -280,11 +261,11 @@ NAN_METHOD(StartProfiling) { 5D32
Local<String> name =
Nan::MaybeLocal<String>(info[0].As<String>()).ToLocalChecked();

// Sample counts and timestamps are not used, so we do not need to record
// samples.
bool recordSamples = false;
// Sample counts and timestamps are not used, so we do not need to record
// samples.
const bool recordSamples = false;

// Line-level accurate line information is not available in Node 11 or earlier.
// Line level accurate line information is not available in Node 11 or earlier.
#if NODE_MODULE_VERSION > NODE_11_0_MODULE_VERSION
bool includeLineInfo =
Nan::MaybeLocal<Boolean>(info[1].As<Boolean>()).ToLocalChecked()->Value();
Expand All @@ -300,7 +281,7 @@ bool recordSamples = false;
}

// Signature:
// stopProfiling(runName: string, includedLineInfo: boolean): TimeProfile
// stopProfiling(runName: string, includeLineInfo: boolean): TimeProfile
NAN_METHOD(StopProfiling) {
if (info.Length() != 2) {
return Nan::ThrowTypeError("StopProfling must have two arguments.");
Expand All @@ -313,12 +294,12 @@ NAN_METHOD(StopProfiling) {
}
Local<String> name =
Nan::MaybeLocal<String>(info[0].As<String>()).ToLocalChecked();
bool includedLineInfo =
bool includeLineInfo =
Nan::MaybeLocal<Boolean>(info[1].As<Boolean>()).ToLocalChecked()->Value();

CpuProfile* profile = cpuProfiler->StopProfiling(name);
Local<Value> translated_profile =
TranslateTimeProfile(profile, includedLineInfo);
TranslateTimeProfile(profile, includeLineInfo);
profile->Delete();
info.GetReturnValue().Set(translated_profile);
}
Expand All @@ -334,26 +315,32 @@ NAN_METHOD(SetSamplingInterval) {
cpuProfiler->SetSamplingInterval(us);
}


NAN_MODULE_INIT(InitAll) {
Local<Object> timeProfiler = Nan::New<Object>();
Nan::Set(timeProfiler, Nan::New("startProfiling").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(StartProfiling)).ToLocalChecked());
Nan::GetFunction(Nan::New<FunctionTemplate>(StartProfiling))
.ToLocalChecked());
Nan::Set(timeProfiler, Nan::New("stopProfiling").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(StopProfiling)).ToLocalChecked());
Nan::GetFunction(Nan::New<FunctionTemplate>(StopProfiling))
.ToLocalChecked());
Nan::Set(timeProfiler, Nan::New("setSamplingInterval").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(SetSamplingInterval)).ToLocalChecked());
Nan::GetFunction(Nan::New<FunctionTemplate>(SetSamplingInterval))
.ToLocalChecked());
target->Set(Nan::New<String>("timeProfiler").ToLocalChecked(), timeProfiler);

Local<Object> heapProfiler = Nan::New<Object>();
Nan::Set(heapProfiler, Nan::New("startSamplingHeapProfiler").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(StartSamplingHeapProfiler)).ToLocalChecked());
Nan::Set(heapProfiler, Nan::New("stopSamplingHeapProfiler").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(StopSamplingHeapProfiler)).ToLocalChecked());
Nan::Set(
heapProfiler, Nan::New("startSamplingHeapProfiler").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(StartSamplingHeapProfiler))
.ToLocalChecked());
Nan::Set(
heapProfiler, Nan::New("stopSamplingHeapProfiler").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(StopSamplingHeapProfiler))
.ToLocalChecked());
Nan::Set(heapProfiler, Nan::New("getAllocationProfile").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(GetAllocationProfile)).ToLocalChecked());
Nan::GetFunction(Nan::New<FunctionTemplate>(GetAllocationProfile))
.ToLocalChecked());
target->Set(Nan::New<String>("heapProfiler").ToLocalChecked(), heapProfiler);
}

NODE_MODULE(google_cloud_profiler, InitAll);

2 changes: 1 addition & 1 deletion ts/src/time-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface TimeProfilerOptions {

/**
* This configuration option is experimental.
* When set to true, functions will be aggregated at the line-level, rather
* When set to true, functions will be aggregated at the line level, rather
* than at the function level.
* This defaults to false.
*/
Expand Down
0