-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathnode_profiling.cc
More file actions
130 lines (115 loc) Β· 4.3 KB
/
node_profiling.cc
File metadata and controls
130 lines (115 loc) Β· 4.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "node_profiling.h"
#include "json_utils.h"
#include "util.h"
#include <memory>
namespace node {
using v8::AllocationProfile;
using v8::HandleScope;
using v8::HeapProfiler;
using v8::Isolate;
using v8::Value;
static void BuildHeapProfileNode(Isolate* isolate,
const AllocationProfile::Node* node,
JSONWriter* writer) {
size_t selfSize = 0;
for (const auto& allocation : node->allocations)
selfSize += allocation.size * allocation.count;
writer->json_keyvalue("selfSize", selfSize);
writer->json_keyvalue("id", node->node_id);
writer->json_objectstart("callFrame");
writer->json_keyvalue("scriptId", node->script_id);
writer->json_keyvalue("lineNumber", node->line_number - 1);
writer->json_keyvalue("columnNumber", node->column_number - 1);
Utf8Value name(isolate, node->name);
Utf8Value script_name(isolate, node->script_name);
writer->json_keyvalue("functionName", *name);
writer->json_keyvalue("url", *script_name);
writer->json_objectend();
writer->json_arraystart("children");
for (const auto* child : node->children) {
writer->json_start();
BuildHeapProfileNode(isolate, child, writer);
writer->json_end();
}
writer->json_arrayend();
}
bool SerializeHeapProfile(Isolate* isolate, std::ostringstream& out_stream) {
HandleScope scope(isolate);
HeapProfiler* profiler = isolate->GetHeapProfiler();
std::unique_ptr<AllocationProfile> profile(profiler->GetAllocationProfile());
if (!profile) {
return false;
}
profiler->StopSamplingHeapProfiler();
JSONWriter writer(out_stream, true);
writer.json_start();
writer.json_arraystart("samples");
for (const auto& sample : profile->GetSamples()) {
writer.json_start();
writer.json_keyvalue("size", sample.size * sample.count);
writer.json_keyvalue("nodeId", sample.node_id);
writer.json_keyvalue("ordinal", static_cast<double>(sample.sample_id));
writer.json_end();
}
writer.json_arrayend();
writer.json_objectstart("head");
BuildHeapProfileNode(isolate, profile->GetRootNode(), &writer);
writer.json_objectend();
writer.json_end();
return true;
}
HeapProfileOptions ParseHeapProfileOptions(
const v8::FunctionCallbackInfo<Value>& args) {
HeapProfileOptions options;
CHECK_LE(args.Length(), 3);
if (args.Length() > 0) {
CHECK(args[0]->IsNumber());
options.sample_interval =
static_cast<uint64_t>(args[0].As<v8::Number>()->Value());
}
if (args.Length() > 1) {
CHECK(args[1]->IsInt32());
options.stack_depth = args[1].As<v8::Int32>()->Value();
}
if (args.Length() > 2) {
CHECK(args[2]->IsUint32());
options.flags = static_cast<v8::HeapProfiler::SamplingFlags>(
args[2].As<v8::Uint32>()->Value());
}
return options;
}
CpuProfileOptions ParseCpuProfileOptions(
const v8::FunctionCallbackInfo<Value>& args) {
CpuProfileOptions options;
CHECK_LE(args.Length(), 2);
if (args.Length() > 0) {
CHECK(args[0]->IsInt32());
options.sampling_interval_us = args[0].As<v8::Int32>()->Value();
}
if (args.Length() > 1) {
CHECK(args[1]->IsUint32());
options.max_samples = args[1].As<v8::Uint32>()->Value();
}
return options;
}
} // namespace node