Skip to content

Commit

Permalink
chore: retry docker run, instead of commands in build.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanmar511 committed Jun 24, 2019
1 parent 138d5bd commit 54539af
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 190 deletions.
187 changes: 87 additions & 100 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
// 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 includedLineInfo) {
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 (includedLineInfo) {
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) {
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 Down Expand Up @@ -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);

Loading

0 comments on commit 54539af

Please sign in to comment.