-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathobjecttracker.cpp
More file actions
170 lines (151 loc) · 5.56 KB
/
Copy pathobjecttracker.cpp
File metadata and controls
170 lines (151 loc) · 5.56 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#ifndef BUILDING_NODE_EXTENSION
#define BUILDING_NODE_EXTENSION
#endif
#include "node.h"
#include "v8.h"
#include "v8-profiler.h"
#include "nan.h"
using namespace v8;
// Only perform object tracking on node v0.11 +
#if NODE_VERSION_AT_LEAST(0, 11, 0)
/* Take a heap snapshot and convert it into a histogram giving the counts and sizes
* of every type of object on the heap.
*/
NAN_METHOD(getObjectHistogram)
{
Isolate *isolate = info.GetIsolate();
if (isolate == NULL)
{
return;
}
HeapProfiler *heapProfiler = isolate->GetHeapProfiler();
Local<Context> currentContext = isolate->GetCurrentContext();
#if NODE_VERSION_AT_LEAST(4, 0, 0) // > v4.00+
// Title field removed in Node 4.x
#else
Local<String> snapshotName = String::NewFromUtf8(isolate, "snapshot");
#endif
/* TakeHeapSnapshot used to be called TakeSnapshot
* It's arguments changed so the latest versions don't need a title param.
*/
#if NODE_VERSION_AT_LEAST(0, 11, 0) // > v0.11+
const HeapSnapshot *snapshot = heapProfiler->TakeHeapSnapshot(
#else
const HeapSnapshot *snapshot = heapProfiler->TakeSnapshot(snapshotName);
#endif
#if NODE_VERSION_AT_LEAST(4, 0, 0) // > v4.0+
// Title field removed in Node 4.x
#else
snapshotName
#endif
);
/* Build a simple histogram from the heap snapshot. */
Local<Object> histogram = Object::New(isolate);
#if NODE_VERSION_AT_LEAST(13, 0, 0) // > v13.0+
/* Declare our tuple keys outside the loop. */
Local<String> countName = String::NewFromUtf8(isolate, "count").ToLocalChecked();
Local<String> sizeName = String::NewFromUtf8(isolate, "size").ToLocalChecked();
/* v8-profiler.h says that kObject is "A JS object (except for arrays and strings)."
* so we should include strings and arrays as objects in the histogram as they are
* objects as the user understands them.
* When you take a heap dump in Chrome dev tools and then view it the
* names "(string)" and "(array)" are used for these, so that's what we'll show the user.
*/
Local<String> stringName = String::NewFromUtf8(isolate, "(string)").ToLocalChecked();
Local<String> arrayName = String::NewFromUtf8(isolate, "(array)").ToLocalChecked();
#else
Local<String> countName = String::NewFromUtf8(isolate, "count");
Local<String> sizeName = String::NewFromUtf8(isolate, "size");
Local<String> stringName = String::NewFromUtf8(isolate, "(string)");
Local<String> arrayName = String::NewFromUtf8(isolate, "(array)");
#endif
/* Walk every node by index (not id) */
for (int i = 0; i < snapshot->GetNodesCount(); i++)
{
const HeapGraphNode *node = snapshot->GetNode(i);
Local<String> name;
switch (node->GetType())
{
case HeapGraphNode::kObject:
name = node->GetName();
break;
case HeapGraphNode::kString:
name = stringName;
break;
case HeapGraphNode::kArray:
name = arrayName;
break;
default:
continue;
}
Local<Value> tupleval = Nan::Get(histogram, name).ToLocalChecked();
Local<Object> tuple;
int64_t ncount = 0;
int64_t nsize = 0;
if (!(tupleval->IsNull() || tupleval->IsUndefined()))
{
tuple = Nan::To<Object>(tupleval).ToLocalChecked();
/* Nothing else can access the tuple or histogram objects,
* if we've found an entry for "name" then it will have these
* fields set. There's no need to check for null/undefined
* from Get.
*/
Local<Value> count = Nan::Get(tuple, countName).ToLocalChecked();
ncount = count->IntegerValue(Nan::GetCurrentContext()).FromJust();
Local<Value> size = Nan::Get(tuple, sizeName).ToLocalChecked();
nsize = size->IntegerValue(Nan::GetCurrentContext()).FromJust();
}
else
{
/* Create a new tuple and add it to the histogram.
* Number objects are immutable so we have to replace
* existing values. There's no need to create initial
* values for count and size.
*/
tuple = Object::New(isolate);
#if NODE_VERSION_AT_LEAST(13, 0, 0) // > v13.0+
v8::Maybe<bool> result = histogram->Set(currentContext, name, tuple);
if (result.IsNothing())
{
// Handle the error
}
}
/* Update the values in the existing (or new) tuple */
Local<Value> newcount = Number::New(isolate, ++ncount);
tuple->Set(currentContext, countName, newcount);
Local<Value> newsize = Number::New(isolate, nsize + node->GetShallowSize());
tuple->Set(currentContext, sizeName, newsize);
#else
v8::Maybe<bool> result = histogram->Set(currentContext, name, tuple);
if (result.IsNothing())
{
// Handle the error
}
}
/* Update the values in the existing (or new) tuple */
Local<Value> newcount = Number::New(isolate, ++ncount);
tuple->Set(currentContext, countName, newcount);
Local<Value> newsize = Number::New(isolate, nsize + node->GetShallowSize());
tuple->Set(currentContext, sizeName, newsize);
#endif
}
// Delete the snapshot as soon as we are done with it.
heapProfiler->DeleteAllHeapSnapshots();
info.GetReturnValue().Set(histogram);
}
#endif