diff --git a/bindings/profiler.cc b/bindings/profiler.cc index d88346ff..318cde82 100644 --- a/bindings/profiler.cc +++ b/bindings/profiler.cc @@ -14,44 +14,42 @@ * limitations under the License. */ -#include "v8-profiler.h" -#include "nan.h" #include +#include "nan.h" +#include "v8-profiler.h" + using namespace v8; // Sampling Heap Profiler Local TranslateAllocationProfile(AllocationProfile::Node* node) { Local js_node = Nan::New(); - js_node->Set(Nan::New("name").ToLocalChecked(), - node->name); + js_node->Set(Nan::New("name").ToLocalChecked(), node->name); js_node->Set(Nan::New("scriptName").ToLocalChecked(), - node->script_name); + node->script_name); js_node->Set(Nan::New("scriptId").ToLocalChecked(), - Nan::New(node->script_id)); + Nan::New(node->script_id)); js_node->Set(Nan::New("lineNumber").ToLocalChecked(), - Nan::New(node->line_number)); + Nan::New(node->line_number)); js_node->Set(Nan::New("columnNumber").ToLocalChecked(), - Nan::New(node->column_number)); + Nan::New(node->column_number)); Local children = Nan::New(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("children").ToLocalChecked(), - children); + js_node->Set(Nan::New("children").ToLocalChecked(), children); Local allocations = Nan::New(node->allocations.size()); for (size_t i = 0; i < node->allocations.size(); i++) { AllocationProfile::Allocation alloc = node->allocations[i]; Local js_alloc = Nan::New(); js_alloc->Set(Nan::New("sizeBytes").ToLocalChecked(), - Nan::New(alloc.size)); + Nan::New(alloc.size)); js_alloc->Set(Nan::New("count").ToLocalChecked(), - Nan::New(alloc.count)); + Nan::New(alloc.count)); allocations->Set(i, js_alloc); } - js_node->Set(Nan::New("allocations").ToLocalChecked(), - allocations); + js_node->Set(Nan::New("allocations").ToLocalChecked(), allocations); return js_node; } @@ -72,8 +70,8 @@ NAN_METHOD(StartSamplingHeapProfiler) { int stack_depth = info[1].As()->IntegerValue(); #endif - info.GetIsolate()->GetHeapProfiler()-> - StartSamplingHeapProfiler(sample_interval, stack_depth); + info.GetIsolate()->GetHeapProfiler()->StartSamplingHeapProfiler( + sample_interval, stack_depth); } else { info.GetIsolate()->GetHeapProfiler()->StartSamplingHeapProfiler(); } @@ -89,16 +87,15 @@ NAN_METHOD(StopSamplingHeapProfiler) { // getAllocationProfile(): AllocationProfileNode NAN_METHOD(GetAllocationProfile) { std::unique_ptr 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 +// This profiler exists for the lifetime of the program. Not calling // CpuProfiler::Dispose() is intentional. CpuProfiler* cpuProfiler = CpuProfiler::New(v8::Isolate::GetCurrent()); #else @@ -106,9 +103,9 @@ CpuProfiler* cpuProfiler = v8::Isolate::GetCurrent()->GetCpuProfiler(); #endif Local CreateTimeNode(Local name, Local scriptName, - Local scriptId, Local lineNumber, - Local columnNumber, Local hitCount, - Local children) { + Local scriptId, Local lineNumber, + Local columnNumber, + Local hitCount, Local children) { Local js_node = Nan::New(); js_node->Set(Nan::New("name").ToLocalChecked(), name); js_node->Set(Nan::New("scriptName").ToLocalChecked(), scriptName); @@ -120,13 +117,12 @@ Local CreateTimeNode(Local name, Local scriptName, return js_node; } - #if NODE_MODULE_VERSION > NODE_11_0_MODULE_VERSION Local TranslateLineNumbersTimeProfileNode(const CpuProfileNode* parent, - const CpuProfileNode* node); + const CpuProfileNode* node); Local GetLineNumberTimeProfileChildren(const CpuProfileNode* parent, - const CpuProfileNode* node) { + const CpuProfileNode* node) { unsigned int index = 0; Local children; int32_t count = node->GetChildrenCount(); @@ -138,53 +134,47 @@ Local GetLineNumberTimeProfileChildren(const CpuProfileNode* parent, node->GetLineTicks(&entries[0], hitLineCount); children = Nan::New(count + hitLineCount); for (const CpuProfileNode::LineTick entry : entries) { - children->Set(index++, CreateTimeNode( - node->GetFunctionName(), - node->GetScriptResourceName(), - Nan::New(node->GetScriptId()), - Nan::New(entry.line), - Nan::New(0), - Nan::New(entry.hit_count), - Nan::New(0) - )); + children->Set( + index++, CreateTimeNode( + node->GetFunctionName(), node->GetScriptResourceName(), + Nan::New(node->GetScriptId()), + Nan::New(entry.line), Nan::New(0), + Nan::New(entry.hit_count), Nan::New(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(count + 1); - children->Set(index++, CreateTimeNode( - node->GetFunctionName(), - node->GetScriptResourceName(), - Nan::New(node->GetScriptId()), - Nan::New(node->GetLineNumber()), - Nan::New(node->GetColumnNumber()), - Nan::New(hitCount), - Nan::New(0) - )); + children->Set( + index++, + CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(), + Nan::New(node->GetScriptId()), + Nan::New(node->GetLineNumber()), + Nan::New(node->GetColumnNumber()), + Nan::New(hitCount), Nan::New(0))); } else { children = Nan::New(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 TranslateLineNumbersTimeProfileNode(const CpuProfileNode* parent, - const CpuProfileNode* node) { - return CreateTimeNode(parent->GetFunctionName(), - parent->GetScriptResourceName(), - Nan::New(parent->GetScriptId()), - Nan::New(node->GetLineNumber()), - Nan::New(node->GetColumnNumber()), - Nan::New(0), - GetLineNumberTimeProfileChildren(parent, node)); + const CpuProfileNode* node) { + return CreateTimeNode( + parent->GetFunctionName(), parent->GetScriptResourceName(), + Nan::New(parent->GetScriptId()), + Nan::New(node->GetLineNumber()), + Nan::New(node->GetColumnNumber()), Nan::New(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 TranslateLineNumbersTimeProfileRoot(const CpuProfileNode* node) { @@ -207,15 +197,11 @@ Local TranslateLineNumbersTimeProfileRoot(const CpuProfileNode* node) { } } - return CreateTimeNode( - node->GetFunctionName(), - node->GetScriptResourceName(), - Nan::New(node->GetScriptId()), - Nan::New(node->GetLineNumber()), - Nan::New(node->GetColumnNumber()), - Nan::New(0), - children - ); + return CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(), + Nan::New(node->GetScriptId()), + Nan::New(node->GetLineNumber()), + Nan::New(node->GetColumnNumber()), + Nan::New(0), children); } #endif @@ -226,41 +212,36 @@ Local TranslateTimeProfileNode(const CpuProfileNode* node) { children->Set(i, TranslateTimeProfileNode(node->GetChild(i))); } - return CreateTimeNode( - node->GetFunctionName(), - node->GetScriptResourceName(), - Nan::New(node->GetScriptId()), - Nan::New(node->GetLineNumber()), - Nan::New(node->GetColumnNumber()), - Nan::New(node->GetHitCount()), - children - ); + return CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(), + Nan::New(node->GetScriptId()), + Nan::New(node->GetLineNumber()), + Nan::New(node->GetColumnNumber()), + Nan::New(node->GetHitCount()), children); } -Local TranslateTimeProfile(const CpuProfile* profile, bool hasDetailedLines) { +Local TranslateTimeProfile(const CpuProfile* profile, + bool includeLineInfo) { Local js_profile = Nan::New(); js_profile->Set(Nan::New("title").ToLocalChecked(), - profile->GetTitle()); + profile->GetTitle()); #if NODE_MODULE_VERSION > NODE_11_0_MODULE_VERSION - if (hasDetailedLines) { + if (includeLineInfo) { js_profile->Set( - Nan::New("topDownRoot").ToLocalChecked(), - TranslateLineNumbersTimeProfileRoot(profile->GetTopDownRoot())); + Nan::New("topDownRoot").ToLocalChecked(), + TranslateLineNumbersTimeProfileRoot(profile->GetTopDownRoot())); } else { - js_profile->Set( - Nan::New("topDownRoot").ToLocalChecked(), - TranslateTimeProfileNode(profile->GetTopDownRoot())); + js_profile->Set(Nan::New("topDownRoot").ToLocalChecked(), + TranslateTimeProfileNode(profile->GetTopDownRoot())); } #else - js_profile->Set( - Nan::New("topDownRoot").ToLocalChecked(), - TranslateTimeProfileNode(profile->GetTopDownRoot())); + js_profile->Set(Nan::New("topDownRoot").ToLocalChecked(), + TranslateTimeProfileNode(profile->GetTopDownRoot())); #endif js_profile->Set(Nan::New("startTime").ToLocalChecked(), - Nan::New(profile->GetStartTime())); + Nan::New(profile->GetStartTime())); js_profile->Set(Nan::New("endTime").ToLocalChecked(), - Nan::New(profile->GetEndTime())); + Nan::New(profile->GetEndTime())); return js_profile; } @@ -268,7 +249,7 @@ Local TranslateTimeProfile(const CpuProfile* profile, bool hasDetailedLin // 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."); @@ -280,11 +261,11 @@ NAN_METHOD(StartProfiling) { Local name = Nan::MaybeLocal(info[0].As()).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(info[1].As()).ToLocalChecked()->Value(); @@ -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."); @@ -313,12 +294,12 @@ NAN_METHOD(StopProfiling) { } Local name = Nan::MaybeLocal(info[0].As()).ToLocalChecked(); - bool includedLineInfo = + bool includeLineInfo = Nan::MaybeLocal(info[1].As()).ToLocalChecked()->Value(); CpuProfile* profile = cpuProfiler->StopProfiling(name); Local translated_profile = - TranslateTimeProfile(profile, includedLineInfo); + TranslateTimeProfile(profile, includeLineInfo); profile->Delete(); info.GetReturnValue().Set(translated_profile); } @@ -334,26 +315,32 @@ NAN_METHOD(SetSamplingInterval) { cpuProfiler->SetSamplingInterval(us); } - NAN_MODULE_INIT(InitAll) { Local timeProfiler = Nan::New(); Nan::Set(timeProfiler, Nan::New("startProfiling").ToLocalChecked(), - Nan::GetFunction(Nan::New(StartProfiling)).ToLocalChecked()); + Nan::GetFunction(Nan::New(StartProfiling)) + .ToLocalChecked()); Nan::Set(timeProfiler, Nan::New("stopProfiling").ToLocalChecked(), - Nan::GetFunction(Nan::New(StopProfiling)).ToLocalChecked()); + Nan::GetFunction(Nan::New(StopProfiling)) + .ToLocalChecked()); Nan::Set(timeProfiler, Nan::New("setSamplingInterval").ToLocalChecked(), - Nan::GetFunction(Nan::New(SetSamplingInterval)).ToLocalChecked()); + Nan::GetFunction(Nan::New(SetSamplingInterval)) + .ToLocalChecked()); target->Set(Nan::New("timeProfiler").ToLocalChecked(), timeProfiler); Local heapProfiler = Nan::New(); - Nan::Set(heapProfiler, Nan::New("startSamplingHeapProfiler").ToLocalChecked(), - Nan::GetFunction(Nan::New(StartSamplingHeapProfiler)).ToLocalChecked()); - Nan::Set(heapProfiler, Nan::New("stopSamplingHeapProfiler").ToLocalChecked(), - Nan::GetFunction(Nan::New(StopSamplingHeapProfiler)).ToLocalChecked()); + Nan::Set( + heapProfiler, Nan::New("startSamplingHeapProfiler").ToLocalChecked(), + Nan::GetFunction(Nan::New(StartSamplingHeapProfiler)) + .ToLocalChecked()); + Nan::Set( + heapProfiler, Nan::New("stopSamplingHeapProfiler").ToLocalChecked(), + Nan::GetFunction(Nan::New(StopSamplingHeapProfiler)) + .ToLocalChecked()); Nan::Set(heapProfiler, Nan::New("getAllocationProfile").ToLocalChecked(), - Nan::GetFunction(Nan::New(GetAllocationProfile)).ToLocalChecked()); + Nan::GetFunction(Nan::New(GetAllocationProfile)) + .ToLocalChecked()); target->Set(Nan::New("heapProfiler").ToLocalChecked(), heapProfiler); } NODE_MODULE(google_cloud_profiler, InitAll); - diff --git a/ts/src/time-profiler.ts b/ts/src/time-profiler.ts index 02969bad..bec5ecbf 100644 --- a/ts/src/time-profiler.ts +++ b/ts/src/time-profiler.ts @@ -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. */