Repository navigation
Expand file tree
/
Copy pathcpu_profiler.cc
More file actions
91 lines (71 loc) · 2.78 KB
/
Copy pathcpu_profiler.cc
File metadata and controls
91 lines (71 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <deque>
#include <nan.h>
#include "v8.h"
#include "v8-profiler.h"
namespace cpu_profiler {
const int MAX_DEPTH = 25;
bool is_started = false;
v8::CpuProfiler* cpu_profiler;
static v8::Local<v8::Object> ConvertNode(const v8::CpuProfileNode* node, int depth) {
v8::Local<v8::Object> node_obj = Nan::New<v8::Object>();
v8::Local<v8::Array> children_arr = Nan::New<v8::Array>();
if (depth + 1 < MAX_DEPTH) {
int children_count = node->GetChildrenCount();
for (int i = 0; i < children_count; i++) {
const v8::CpuProfileNode* child = node->GetChild(i);
v8::Local<v8::Object> child_obj = ConvertNode(child, depth + 1);
Nan::Set(children_arr, i, child_obj);
}
}
Nan::Set(node_obj, Nan::New<v8::String>("file_name").ToLocalChecked(), node->GetScriptResourceName());
Nan::Set(node_obj, Nan::New<v8::String>("line_num").ToLocalChecked(), Nan::New<v8::Number>(node->GetLineNumber()));
Nan::Set(node_obj, Nan::New<v8::String>("col_num").ToLocalChecked(), Nan::New<v8::Number>(node->GetColumnNumber()));
Nan::Set(node_obj, Nan::New<v8::String>("func_name").ToLocalChecked(), node->GetFunctionName());
Nan::Set(node_obj, Nan::New<v8::String>("hit_count").ToLocalChecked(), Nan::New<v8::Number>(node->GetHitCount()));
Nan::Set(node_obj, Nan::New<v8::String>("children").ToLocalChecked(), children_arr);
return node_obj;
}
NAN_METHOD(StartCPUProfiler) {
if (is_started) {
return;
}
is_started = true;
const v8::Local<v8::String> title = Nan::New<v8::String>("stackimpact-cpu-profile").ToLocalChecked();
#if V8_MAJOR_VERSION * 1000 + V8_MINOR_VERSION > 5005
cpu_profiler = v8::CpuProfiler::New(info.GetIsolate());
#else
cpu_profiler = info.GetIsolate()->GetCpuProfiler();
#endif
cpu_profiler->SetSamplingInterval(10 * 1000);
cpu_profiler->StartProfiling(title, false);
}
NAN_METHOD(StopCPUProfiler) {
if (!is_started) {
info.GetReturnValue().SetUndefined();
return;
}
is_started = false;
const v8::Local<v8::String> title = Nan::New<v8::String>("stackimpact-cpu-profile").ToLocalChecked();
v8::CpuProfile* cpu_profile = cpu_profiler->StopProfiling(title);
if (cpu_profile != NULL) {
const v8::CpuProfileNode* root = cpu_profile->GetTopDownRoot();
if (root != NULL) {
v8::Local<v8::Object> root_obj = ConvertNode(root, 0);
cpu_profile->Delete();
#if V8_MAJOR_VERSION * 1000 + V8_MINOR_VERSION > 5005
cpu_profiler->Dispose();
#endif
info.GetReturnValue().Set(root_obj);
}
else {
#if V8_MAJOR_VERSION * 1000 + V8_MINOR_VERSION > 5005
cpu_profiler->Dispose();
#endif
info.GetReturnValue().SetUndefined();
}
}
else {
info.GetReturnValue().SetUndefined();
}
}
}