@datadog/pprof 0.2.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/binding.gyp +130 -31
  2. package/bindings/binding.cc +13 -0
  3. package/bindings/code-event-record.cc +115 -0
  4. package/bindings/code-event-record.hh +67 -0
  5. package/bindings/code-map.cc +112 -0
  6. package/bindings/code-map.hh +47 -0
  7. package/bindings/cpu-time.cc +89 -0
  8. package/bindings/cpu-time.hh +35 -0
  9. package/bindings/location.cc +117 -0
  10. package/bindings/location.hh +43 -0
  11. package/bindings/per-isolate-data.cc +42 -0
  12. package/bindings/per-isolate-data.hh +26 -0
  13. package/bindings/profilers/cpu.cc +285 -0
  14. package/bindings/profilers/cpu.hh +78 -0
  15. package/bindings/profilers/heap.cc +112 -0
  16. package/bindings/profilers/heap.hh +40 -0
  17. package/bindings/profilers/wall.cc +306 -0
  18. package/bindings/profilers/wall.hh +28 -0
  19. package/bindings/sample.cc +160 -0
  20. package/bindings/sample.hh +48 -0
  21. package/bindings/test/binding.cc +59 -0
  22. package/bindings/test/code-event-record.test.cc +81 -0
  23. package/bindings/test/code-event-record.test.hh +5 -0
  24. package/bindings/test/code-map.test.cc +77 -0
  25. package/bindings/test/code-map.test.hh +5 -0
  26. package/bindings/test/cpu-time.test.cc +30 -0
  27. package/bindings/test/cpu-time.test.hh +5 -0
  28. package/bindings/test/location.test.cc +39 -0
  29. package/bindings/test/location.test.hh +5 -0
  30. package/bindings/test/profilers/cpu.test.cc +88 -0
  31. package/bindings/test/profilers/cpu.test.hh +5 -0
  32. package/bindings/test/sample.test.cc +128 -0
  33. package/bindings/test/sample.test.hh +5 -0
  34. package/bindings/test/tap.h +830 -0
  35. package/bindings/wrap.hh +20 -0
  36. package/out/src/cpu-profiler-bindings.d.ts +1 -0
  37. package/out/src/cpu-profiler-bindings.js +23 -0
  38. package/out/src/cpu-profiler-bindings.js.map +1 -0
  39. package/out/src/cpu-profiler.d.ts +19 -0
  40. package/out/src/cpu-profiler.js +94 -0
  41. package/out/src/cpu-profiler.js.map +1 -0
  42. package/out/src/index.d.ts +2 -0
  43. package/out/src/index.js +3 -1
  44. package/out/src/index.js.map +1 -1
  45. package/out/src/profile-encoder.js +3 -14
  46. package/out/src/profile-encoder.js.map +1 -1
  47. package/out/src/profile-serializer.d.ts +9 -1
  48. package/out/src/profile-serializer.js +73 -1
  49. package/out/src/profile-serializer.js.map +1 -1
  50. package/out/src/sourcemapper/sourcemapper.js +81 -88
  51. package/out/src/sourcemapper/sourcemapper.js.map +1 -1
  52. package/out/src/time-profiler.d.ts +1 -1
  53. package/out/src/time-profiler.js +40 -34
  54. package/out/src/time-profiler.js.map +1 -1
  55. package/out/src/v8-types.d.ts +38 -0
  56. package/out/third_party/cloud-debug-nodejs/src/agent/io/scanner.js +6 -17
  57. package/out/third_party/cloud-debug-nodejs/src/agent/io/scanner.js.map +1 -1
  58. package/package-lock.json +5627 -0
  59. package/package.json +12 -14
  60. package/prebuilds/darwin-arm64/node-102.node +0 -0
  61. package/prebuilds/darwin-arm64/node-108.node +0 -0
  62. package/prebuilds/darwin-arm64/node-72.node +0 -0
  63. package/prebuilds/darwin-arm64/node-79.node +0 -0
  64. package/prebuilds/darwin-arm64/node-83.node +0 -0
  65. package/prebuilds/darwin-arm64/node-88.node +0 -0
  66. package/prebuilds/darwin-arm64/node-93.node +0 -0
  67. package/prebuilds/darwin-ia32/node-102.node +0 -0
  68. package/prebuilds/darwin-ia32/node-108.node +0 -0
  69. package/prebuilds/darwin-ia32/node-72.node +0 -0
  70. package/prebuilds/darwin-ia32/node-79.node +0 -0
  71. package/prebuilds/darwin-ia32/node-83.node +0 -0
  72. package/prebuilds/darwin-ia32/node-88.node +0 -0
  73. package/prebuilds/darwin-ia32/node-93.node +0 -0
  74. package/prebuilds/darwin-x64/node-102.node +0 -0
  75. package/prebuilds/darwin-x64/node-108.node +0 -0
  76. package/prebuilds/darwin-x64/node-72.node +0 -0
  77. package/prebuilds/darwin-x64/node-79.node +0 -0
  78. package/prebuilds/darwin-x64/node-83.node +0 -0
  79. package/prebuilds/darwin-x64/node-88.node +0 -0
  80. package/prebuilds/darwin-x64/node-93.node +0 -0
  81. package/prebuilds/linux-ia32/node-72.node +0 -0
  82. package/prebuilds/linux-ia32/node-79.node +0 -0
  83. package/prebuilds/linux-x64/node-102.node +0 -0
  84. package/prebuilds/linux-x64/node-108.node +0 -0
  85. package/prebuilds/linux-x64/node-72.node +0 -0
  86. package/prebuilds/linux-x64/node-79.node +0 -0
  87. package/prebuilds/linux-x64/node-83.node +0 -0
  88. package/prebuilds/linux-x64/node-88.node +0 -0
  89. package/prebuilds/linux-x64/node-93.node +0 -0
  90. package/prebuilds/win32-ia32/node-102.node +0 -0
  91. package/prebuilds/win32-ia32/node-72.node +0 -0
  92. package/prebuilds/win32-ia32/node-79.node +0 -0
  93. package/prebuilds/win32-ia32/node-83.node +0 -0
  94. package/prebuilds/win32-ia32/node-88.node +0 -0
  95. package/prebuilds/win32-ia32/node-93.node +0 -0
  96. package/prebuilds/win32-x64/node-102.node +0 -0
  97. package/prebuilds/win32-x64/node-108.node +0 -0
  98. package/prebuilds/win32-x64/node-72.node +0 -0
  99. package/prebuilds/win32-x64/node-79.node +0 -0
  100. package/prebuilds/win32-x64/node-83.node +0 -0
  101. package/prebuilds/win32-x64/node-88.node +0 -0
  102. package/prebuilds/win32-x64/node-93.node +0 -0
  103. package/scripts/require-package-json.js +11 -11
  104. package/scripts/should_rebuild.js +3 -3
  105. package/bindings/profiler.cc +0 -384
  106. package/scripts/preinstall.js +0 -36
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Copyright 2018 Google Inc. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #pragma once
18
+
19
+ #include <nan.h>
20
+
21
+ namespace dd {
22
+
23
+ class HeapProfiler {
24
+ public:
25
+ // Signature:
26
+ // startSamplingHeapProfiler()
27
+ static NAN_METHOD(StartSamplingHeapProfiler);
28
+
29
+ // Signature:
30
+ // stopSamplingHeapProfiler()
31
+ static NAN_METHOD(StopSamplingHeapProfiler);
32
+
33
+ // Signature:
34
+ // getAllocationProfile(): AllocationProfileNode
35
+ static NAN_METHOD(GetAllocationProfile);
36
+
37
+ static NAN_MODULE_INIT(Init);
38
+ };
39
+
40
+ } // namespace dd
@@ -0,0 +1,306 @@
1
+ /**
2
+ * Copyright 2018 Google Inc. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <memory>
18
+
19
+ #include <node.h>
20
+ #include <nan.h>
21
+ #include <v8-profiler.h>
22
+
23
+ #include "wall.hh"
24
+
25
+ using namespace v8;
26
+
27
+ namespace dd {
28
+
29
+ Local<Object> CreateTimeNode(Local<String> name, Local<String> scriptName,
30
+ Local<Integer> scriptId, Local<Integer> lineNumber,
31
+ Local<Integer> columnNumber,
32
+ Local<Integer> hitCount, Local<Array> children) {
33
+ Local<Object> js_node = Nan::New<Object>();
34
+ Nan::Set(js_node, Nan::New<String>("name").ToLocalChecked(), name);
35
+ Nan::Set(js_node, Nan::New<String>("scriptName").ToLocalChecked(),
36
+ scriptName);
37
+ Nan::Set(js_node, Nan::New<String>("scriptId").ToLocalChecked(), scriptId);
38
+ Nan::Set(js_node, Nan::New<String>("lineNumber").ToLocalChecked(),
39
+ lineNumber);
40
+ Nan::Set(js_node, Nan::New<String>("columnNumber").ToLocalChecked(),
41
+ columnNumber);
42
+ Nan::Set(js_node, Nan::New<String>("hitCount").ToLocalChecked(), hitCount);
43
+ Nan::Set(js_node, Nan::New<String>("children").ToLocalChecked(), children);
44
+
45
+ return js_node;
46
+ }
47
+
48
+ Local<Object> TranslateLineNumbersTimeProfileNode(const CpuProfileNode* parent,
49
+ const CpuProfileNode* node);
50
+
51
+ Local<Array> GetLineNumberTimeProfileChildren(const CpuProfileNode* parent,
52
+ const CpuProfileNode* node) {
53
+ unsigned int index = 0;
54
+ Local<Array> children;
55
+ int32_t count = node->GetChildrenCount();
56
+
57
+ unsigned int hitLineCount = node->GetHitLineCount();
58
+ unsigned int hitCount = node->GetHitCount();
59
+ if (hitLineCount > 0) {
60
+ std::vector<CpuProfileNode::LineTick> entries(hitLineCount);
61
+ node->GetLineTicks(&entries[0], hitLineCount);
62
+ children = Nan::New<Array>(count + hitLineCount);
63
+ for (const CpuProfileNode::LineTick entry : entries) {
64
+ Nan::Set(children, index++,
65
+ CreateTimeNode(
66
+ node->GetFunctionName(), node->GetScriptResourceName(),
67
+ Nan::New<Integer>(node->GetScriptId()),
68
+ Nan::New<Integer>(entry.line), Nan::New<Integer>(0),
69
+ Nan::New<Integer>(entry.hit_count), Nan::New<Array>(0)));
70
+ }
71
+ } else if (hitCount > 0) {
72
+ // Handle nodes for pseudo-functions like "process" and "garbage collection"
73
+ // which do not have hit line counts.
74
+ children = Nan::New<Array>(count + 1);
75
+ Nan::Set(
76
+ children, index++,
77
+ CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(),
78
+ Nan::New<Integer>(node->GetScriptId()),
79
+ Nan::New<Integer>(node->GetLineNumber()),
80
+ Nan::New<Integer>(node->GetColumnNumber()),
81
+ Nan::New<Integer>(hitCount), Nan::New<Array>(0)));
82
+ } else {
83
+ children = Nan::New<Array>(count);
84
+ }
85
+
86
+ for (int32_t i = 0; i < count; i++) {
87
+ Nan::Set(children, index++,
88
+ TranslateLineNumbersTimeProfileNode(node, node->GetChild(i)));
89
+ };
90
+
91
+ return children;
92
+ }
93
+
94
+ Local<Object> TranslateLineNumbersTimeProfileNode(const CpuProfileNode* parent,
95
+ const CpuProfileNode* node) {
96
+ return CreateTimeNode(
97
+ parent->GetFunctionName(), parent->GetScriptResourceName(),
98
+ Nan::New<Integer>(parent->GetScriptId()),
99
+ Nan::New<Integer>(node->GetLineNumber()),
100
+ Nan::New<Integer>(node->GetColumnNumber()), Nan::New<Integer>(0),
101
+ GetLineNumberTimeProfileChildren(parent, node));
102
+ }
103
+
104
+ // In profiles with line level accurate line numbers, a node's line number
105
+ // and column number refer to the line/column from which the function was
106
+ // called.
107
+ Local<Value> TranslateLineNumbersTimeProfileRoot(const CpuProfileNode* node) {
108
+ int32_t count = node->GetChildrenCount();
109
+ std::vector<Local<Array>> childrenArrs(count);
110
+ int32_t childCount = 0;
111
+ for (int32_t i = 0; i < count; i++) {
112
+ Local<Array> c = GetLineNumberTimeProfileChildren(node, node->GetChild(i));
113
+ childCount = childCount + c->Length();
114
+ childrenArrs[i] = c;
115
+ }
116
+
117
+ Local<Array> children = Nan::New<Array>(childCount);
118
+ int32_t idx = 0;
119
+ for (int32_t i = 0; i < count; i++) {
120
+ Local<Array> arr = childrenArrs[i];
121
+ for (uint32_t j = 0; j < arr->Length(); j++) {
122
+ Nan::Set(children, idx, Nan::Get(arr, j).ToLocalChecked());
123
+ idx++;
124
+ }
125
+ }
126
+
127
+ return CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(),
128
+ Nan::New<Integer>(node->GetScriptId()),
129
+ Nan::New<Integer>(node->GetLineNumber()),
130
+ Nan::New<Integer>(node->GetColumnNumber()),
131
+ Nan::New<Integer>(0), children);
132
+ }
133
+
134
+ Local<Value> TranslateTimeProfileNode(const CpuProfileNode* node) {
135
+ int32_t count = node->GetChildrenCount();
136
+ Local<Array> children = Nan::New<Array>(count);
137
+ for (int32_t i = 0; i < count; i++) {
138
+ Nan::Set(children, i, TranslateTimeProfileNode(node->GetChild(i)));
139
+ }
140
+
141
+ return CreateTimeNode(node->GetFunctionName(), node->GetScriptResourceName(),
142
+ Nan::New<Integer>(node->GetScriptId()),
143
+ Nan::New<Integer>(node->GetLineNumber()),
144
+ Nan::New<Integer>(node->GetColumnNumber()),
145
+ Nan::New<Integer>(node->GetHitCount()), children);
146
+ }
147
+
148
+ Local<Value> TranslateTimeProfile(const CpuProfile* profile,
149
+ bool includeLineInfo) {
150
+ Local<Object> js_profile = Nan::New<Object>();
151
+ Nan::Set(js_profile, Nan::New<String>("title").ToLocalChecked(),
152
+ profile->GetTitle());
153
+
154
+ #if NODE_MODULE_VERSION > NODE_11_0_MODULE_VERSION
155
+ if (includeLineInfo) {
156
+ Nan::Set(js_profile, Nan::New<String>("topDownRoot").ToLocalChecked(),
157
+ TranslateLineNumbersTimeProfileRoot(profile->GetTopDownRoot()));
158
+ } else {
159
+ Nan::Set(js_profile, Nan::New<String>("topDownRoot").ToLocalChecked(),
160
+ TranslateTimeProfileNode(profile->GetTopDownRoot()));
161
+ }
162
+ #else
163
+ Nan::Set(js_profile, Nan::New<String>("topDownRoot").ToLocalChecked(),
164
+ TranslateTimeProfileNode(profile->GetTopDownRoot()));
165
+ #endif
166
+ Nan::Set(js_profile, Nan::New<String>("startTime").ToLocalChecked(),
167
+ Nan::New<Number>(profile->GetStartTime()));
168
+ Nan::Set(js_profile, Nan::New<String>("endTime").ToLocalChecked(),
169
+ Nan::New<Number>(profile->GetEndTime()));
170
+ return js_profile;
171
+ }
172
+
173
+ static Nan::Persistent<v8::Function> constructor;
174
+
175
+ WallProfiler::WallProfiler(int interval)
176
+ : samplingInterval(interval) {}
177
+
178
+ WallProfiler::~WallProfiler() {
179
+ Dispose();
180
+ }
181
+
182
+ void WallProfiler::Dispose() {
183
+ if (cpuProfiler != nullptr) {
184
+ cpuProfiler->Dispose();
185
+ cpuProfiler = nullptr;
186
+ }
187
+ }
188
+
189
+ NAN_METHOD(WallProfiler::Dispose) {
190
+ WallProfiler* wallProfiler =
191
+ Nan::ObjectWrap::Unwrap<WallProfiler>(info.Holder());
192
+
193
+ wallProfiler->Dispose();
194
+ }
195
+
196
+ NAN_METHOD(WallProfiler::New) {
197
+ if (info.Length() != 1) {
198
+ return Nan::ThrowTypeError("WallProfiler must have one argument.");
199
+ }
200
+ if (!info[0]->IsNumber()) {
201
+ return Nan::ThrowTypeError("Sample rate must be a number.");
202
+ }
203
+
204
+ if (info.IsConstructCall()) {
205
+ int interval =
206
+ Nan::MaybeLocal<Integer>(info[0].As<Integer>()).ToLocalChecked()->Value();
207
+
208
+ WallProfiler* obj = new WallProfiler(interval);
209
+ obj->Wrap(info.This());
210
+ info.GetReturnValue().Set(info.This());
211
+ } else {
212
+ const int argc = 1;
213
+ v8::Local<v8::Value> argv[argc] = {info[0]};
214
+ v8::Local<v8::Function> cons = Nan::New(constructor);
215
+ info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
216
+ }
217
+ }
218
+
219
+ NAN_METHOD(WallProfiler::Start) {
220
+ WallProfiler* wallProfiler =
221
+ Nan::ObjectWrap::Unwrap<WallProfiler>(info.Holder());
222
+
223
+ if (info.Length() != 2) {
224
+ return Nan::ThrowTypeError("Start must have two arguments.");
225
+ }
226
+ if (!info[0]->IsString()) {
227
+ return Nan::ThrowTypeError("Profile name must be a string.");
228
+ }
229
+ if (!info[1]->IsBoolean()) {
230
+ return Nan::ThrowTypeError("Include lines must be a boolean.");
231
+ }
232
+
233
+ Local<String> name =
234
+ Nan::MaybeLocal<String>(info[0].As<String>()).ToLocalChecked();
235
+
236
+ bool includeLines =
237
+ Nan::MaybeLocal<Boolean>(info[1].As<Boolean>()).ToLocalChecked()->Value();
238
+
239
+ auto profiler = wallProfiler->GetProfiler();
240
+
241
+ // Sample counts and timestamps are not used, so we do not need to record
242
+ // samples.
243
+ const bool recordSamples = false;
244
+
245
+ if (includeLines) {
246
+ profiler->StartProfiling(name, CpuProfilingMode::kCallerLineNumbers,
247
+ recordSamples);
248
+ } else {
249
+ profiler->StartProfiling(name, recordSamples);
250
+ }
251
+ }
252
+
253
+ NAN_METHOD(WallProfiler::Stop) {
254
+ WallProfiler* wallProfiler =
255
+ Nan::ObjectWrap::Unwrap<WallProfiler>(info.Holder());
256
+
257
+ if (info.Length() != 2) {
258
+ return Nan::ThrowTypeError("Start must have two arguments.");
259
+ }
260
+ if (!info[0]->IsString()) {
261
+ return Nan::ThrowTypeError("Profile name must be a string.");
262
+ }
263
+ if (!info[1]->IsBoolean()) {
264
+ return Nan::ThrowTypeError("Include lines must be a boolean.");
265
+ }
266
+
267
+ Local<String> name =
268
+ Nan::MaybeLocal<String>(info[0].As<String>()).ToLocalChecked();
269
+
270
+ bool includeLines =
271
+ Nan::MaybeLocal<Boolean>(info[1].As<Boolean>()).ToLocalChecked()->Value();
272
+
273
+ auto profiler = wallProfiler->GetProfiler();
274
+ auto v8_profile = profiler->StopProfiling(name);
275
+ Local<Value> profile = TranslateTimeProfile(v8_profile, includeLines);
276
+ v8_profile->Delete();
277
+
278
+ info.GetReturnValue().Set(profile);
279
+ }
280
+
281
+ NAN_MODULE_INIT(WallProfiler::Init) {
282
+ Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
283
+ Local<String> className = Nan::New("TimeProfiler").ToLocalChecked();
284
+ tpl->SetClassName(className);
285
+ tpl->InstanceTemplate()->SetInternalFieldCount(1);
286
+
287
+ Nan::SetPrototypeMethod(tpl, "start", Start);
288
+ Nan::SetPrototypeMethod(tpl, "dispose", Dispose);
289
+ Nan::SetPrototypeMethod(tpl, "stop", Stop);
290
+
291
+ constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
292
+ Nan::Set(target, className, Nan::GetFunction(tpl).ToLocalChecked());
293
+ }
294
+
295
+ // A new CPU profiler object will be created each time profiling is started
296
+ // to work around https://bugs.chromium.org/p/v8/issues/detail?id=11051.
297
+ v8::CpuProfiler* WallProfiler::GetProfiler() {
298
+ if (cpuProfiler == nullptr) {
299
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
300
+ cpuProfiler = v8::CpuProfiler::New(isolate);
301
+ cpuProfiler->SetSamplingInterval(samplingInterval);
302
+ }
303
+ return cpuProfiler;
304
+ }
305
+
306
+ } // namespace dd
@@ -0,0 +1,28 @@
1
+ #pragma once
2
+
3
+ #include <nan.h>
4
+ #include <v8-profiler.h>
5
+
6
+ namespace dd {
7
+
8
+ class WallProfiler : public Nan::ObjectWrap {
9
+ private:
10
+ int samplingInterval = 0;
11
+ v8::CpuProfiler* cpuProfiler = nullptr;
12
+ ~WallProfiler();
13
+ void Dispose();
14
+
15
+ // A new CPU profiler object will be created each time profiling is started
16
+ // to work around https://bugs.chromium.org/p/v8/issues/detail?id=11051.
17
+ v8::CpuProfiler* GetProfiler();
18
+ public:
19
+ explicit WallProfiler(int interval);
20
+
21
+ static NAN_METHOD(New);
22
+ static NAN_METHOD(Dispose);
23
+ static NAN_METHOD(Start);
24
+ static NAN_METHOD(Stop);
25
+ static NAN_MODULE_INIT(Init);
26
+ };
27
+
28
+ } // namespace dd
@@ -0,0 +1,160 @@
1
+ #include <algorithm>
2
+
3
+ #include <nan.h>
4
+
5
+ #include "per-isolate-data.hh"
6
+ #include "sample.hh"
7
+ #include "location.hh"
8
+ #include "code-map.hh"
9
+
10
+ #include <iostream>
11
+
12
+ #include <uv.h>
13
+
14
+ namespace dd {
15
+
16
+ std::vector<uintptr_t> MakeFrames(v8::Isolate* isolate) {
17
+ static const size_t frames_limit = 255;
18
+ void* frames[frames_limit];
19
+
20
+ v8::SampleInfo sample_info;
21
+ v8::RegisterState register_state;
22
+ register_state.pc = nullptr;
23
+ register_state.fp = &register_state;
24
+ register_state.sp = &register_state;
25
+
26
+ isolate->GetStackSample(register_state, frames, frames_limit, &sample_info);
27
+
28
+ size_t n = sample_info.frames_count;
29
+ std::vector<uintptr_t> output(n);
30
+
31
+ for (size_t i = 0; i < n; i++) {
32
+ output.push_back(reinterpret_cast<uintptr_t>(frames[i]));
33
+ }
34
+
35
+ return output;
36
+ }
37
+
38
+ Sample::Sample(v8::Isolate* isolate,
39
+ std::shared_ptr<LabelWrap> labels,
40
+ std::vector<uintptr_t> frames,
41
+ int64_t cpu_time)
42
+ : labels_(std::move(labels)),
43
+ frames(frames),
44
+ cpu_time(cpu_time) {
45
+ timestamp = uv_hrtime();
46
+ }
47
+
48
+ Sample::Sample(v8::Isolate* isolate,
49
+ std::shared_ptr<LabelWrap> labels,
50
+ int64_t cpu_time)
51
+ : Sample(isolate, std::move(labels), MakeFrames(isolate), cpu_time) {}
52
+
53
+ std::vector<uintptr_t> Sample::GetFrames() {
54
+ return frames;
55
+ }
56
+
57
+ v8::Local<v8::Array> Sample::Symbolize(
58
+ std::shared_ptr<CodeMap> code_map) {
59
+ auto isolate = v8::Isolate::GetCurrent();
60
+ if (!locations_.IsEmpty()) return locations_.Get(isolate);
61
+
62
+ auto locations = Nan::New<v8::Array>();
63
+
64
+ auto ToCodeEventRecord = [code_map](uintptr_t address)
65
+ -> std::shared_ptr<CodeEventRecord> {
66
+ return code_map->Lookup(address);
67
+ };
68
+
69
+ std::deque<std::shared_ptr<CodeEventRecord>> records;
70
+ std::transform(frames.begin(), frames.end(), std::front_inserter(records),
71
+ ToCodeEventRecord);
72
+
73
+ for (auto record : records) {
74
+ if (record) {
75
+ auto location = Location::New(isolate, record);
76
+ Nan::Set(locations, locations->Length(), location->handle()).Check();
77
+ }
78
+ }
79
+
80
+ locations_.Reset(locations);
81
+ return locations;
82
+ }
83
+
84
+ v8::Local<v8::Integer> Sample::GetCpuTime(v8::Isolate* isolate) {
85
+ return v8::Integer::New(isolate, cpu_time);
86
+ }
87
+
88
+ v8::Local<v8::Value> Sample::GetLabels(v8::Isolate* isolate) {
89
+ if (!labels_) return v8::Undefined(isolate);
90
+ return labels_->handle();
91
+ }
92
+
93
+ v8::Local<v8::Array> Sample::GetLocations(v8::Isolate* isolate) {
94
+ if (locations_.IsEmpty()) {
95
+ return Nan::New<v8::Array>();
96
+ }
97
+ return locations_.Get(isolate);
98
+ }
99
+
100
+ NAN_GETTER(Sample::GetCpuTime) {
101
+ Sample* wrap = Nan::ObjectWrap::Unwrap<Sample>(info.Holder());
102
+ info.GetReturnValue().Set(wrap->GetCpuTime(info.GetIsolate()));
103
+ }
104
+
105
+ NAN_GETTER(Sample::GetLabels) {
106
+ Sample* wrap = Nan::ObjectWrap::Unwrap<Sample>(info.Holder());
107
+ info.GetReturnValue().Set(wrap->GetLabels(info.GetIsolate()));
108
+ }
109
+
110
+ NAN_GETTER(Sample::GetLocations) {
111
+ Sample* wrap = Nan::ObjectWrap::Unwrap<Sample>(info.Holder());
112
+ info.GetReturnValue().Set(wrap->GetLocations(info.GetIsolate()));
113
+ }
114
+
115
+ v8::Local<v8::Object> Sample::ToObject(v8::Isolate* isolate) {
116
+ if (!persistent().IsEmpty()) {
117
+ return handle();
118
+ }
119
+
120
+ auto per_isolate = PerIsolateData::For(isolate);
121
+ v8::Local<v8::Function> cons = Nan::New(
122
+ per_isolate->SampleConstructor());
123
+ auto inst = Nan::NewInstance(cons, 0, {}).ToLocalChecked();
124
+
125
+ Wrap(inst);
126
+
127
+ return handle();
128
+ }
129
+
130
+ NAN_MODULE_INIT(Sample::Init) {
131
+ auto class_name = Nan::New<v8::String>("Sample")
132
+ .ToLocalChecked();
133
+
134
+ auto tpl = Nan::New<v8::FunctionTemplate>(nullptr);
135
+ tpl->SetClassName(class_name);
136
+ tpl->InstanceTemplate()
137
+ ->SetInternalFieldCount(1);
138
+
139
+ // auto proto = tpl->PrototypeTemplate();
140
+ auto proto = tpl->InstanceTemplate();
141
+
142
+ Nan::SetAccessor(
143
+ proto,
144
+ Nan::New("cpuTime").ToLocalChecked(),
145
+ GetCpuTime);
146
+ Nan::SetAccessor(
147
+ proto,
148
+ Nan::New("labels").ToLocalChecked(),
149
+ GetLabels);
150
+ Nan::SetAccessor(
151
+ proto,
152
+ Nan::New("locations").ToLocalChecked(),
153
+ GetLocations);
154
+
155
+ auto fn = Nan::GetFunction(tpl).ToLocalChecked();
156
+ auto per_isolate = PerIsolateData::For(target->GetIsolate());
157
+ per_isolate->SampleConstructor().Reset(fn);
158
+ }
159
+
160
+ } // namespace dd
@@ -0,0 +1,48 @@
1
+ #pragma once
2
+
3
+ #include "code-map.hh"
4
+ #include "wrap.hh"
5
+
6
+ #include <node_object_wrap.h>
7
+ #include <v8.h>
8
+
9
+ namespace dd {
10
+
11
+ class Sample : public Nan::ObjectWrap {
12
+ private:
13
+ static const size_t frames_limit = 255;
14
+
15
+ std::shared_ptr<LabelWrap> labels_;
16
+ uint64_t timestamp;
17
+ std::vector<uintptr_t> frames;
18
+ Nan::Global<v8::Array> locations_;
19
+ int64_t cpu_time;
20
+
21
+ public:
22
+ Sample(v8::Isolate* isolate,
23
+ std::shared_ptr<LabelWrap> labels,
24
+ std::vector<uintptr_t> frames,
25
+ int64_t cpu_time);
26
+
27
+ Sample(v8::Isolate* isolate,
28
+ std::shared_ptr<LabelWrap> labels,
29
+ int64_t cpu_time);
30
+
31
+ std::vector<uintptr_t> GetFrames();
32
+ v8::Local<v8::Array> Symbolize(
33
+ std::shared_ptr<CodeMap> code_map);
34
+
35
+ v8::Local<v8::Integer> GetCpuTime(v8::Isolate* isolate);
36
+ v8::Local<v8::Value> GetLabels(v8::Isolate* isolate);
37
+ v8::Local<v8::Array> GetLocations(v8::Isolate* isolate);
38
+
39
+ v8::Local<v8::Object> ToObject(v8::Isolate* isolate);
40
+
41
+ static NAN_GETTER(GetCpuTime);
42
+ static NAN_GETTER(GetLabels);
43
+ static NAN_GETTER(GetLocations);
44
+
45
+ static NAN_MODULE_INIT(Init);
46
+ };
47
+
48
+ } // namespace dd
@@ -0,0 +1,59 @@
1
+ #include <unordered_map>
2
+ #include <sstream>
3
+ #include <cstdlib>
4
+
5
+ #include "node.h"
6
+ #include "v8.h"
7
+ #include "nan.h"
8
+ #include "tap.h"
9
+
10
+ #include "../profilers/cpu.hh"
11
+
12
+ #include "profilers/cpu.test.hh"
13
+ #include "code-event-record.test.hh"
14
+ #include "code-map.test.hh"
15
+ #include "cpu-time.test.hh"
16
+ #include "location.test.hh"
17
+ #include "sample.test.hh"
18
+
19
+ NODE_MODULE_INIT(/* exports, module, context */) {
20
+ // Need to do this so the class templates get constructed
21
+ dd::CpuProfiler::Init(exports);
22
+
23
+ Tap t;
24
+ const char* env_var = std::getenv("TEST");
25
+ std::string name(env_var == nullptr ? "" : env_var);
26
+
27
+ std::unordered_map<std::string, std::function<void(Tap&)>> tests = {
28
+ {"profilers/cpu", test_profilers_cpu_profiler},
29
+ {"code-event-record", test_code_event_record},
30
+ {"code-map", test_code_map},
31
+ {"cpu-time", test_cpu_time},
32
+ {"location", test_location},
33
+ {"sample", test_sample},
34
+ };
35
+
36
+ if (name.empty()) {
37
+ t.plan(tests.size());
38
+ for (auto test : tests) {
39
+ t.test(test.first, test.second);
40
+ }
41
+ } else {
42
+ t.plan(1);
43
+ if (tests.count(name)) {
44
+ t.test(name, tests[name]);
45
+ } else {
46
+ std::ostringstream s;
47
+ s << "Unknown test: " << name;
48
+ t.fail(s.str());
49
+ }
50
+ }
51
+
52
+ // End test and set `process.exitCode`
53
+ int exitCode = t.end();
54
+ auto processKey = Nan::New<v8::String>("process").ToLocalChecked();
55
+ auto process = Nan::Get(context->Global(), processKey).ToLocalChecked();
56
+ Nan::Set(process.As<v8::Object>(),
57
+ Nan::New<v8::String>("exitCode").ToLocalChecked(),
58
+ Nan::New<v8::Number>(exitCode));
59
+ }