1 |
|
|
// Copyright Joyent, Inc. and other Node contributors. |
2 |
|
|
// |
3 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a |
4 |
|
|
// copy of this software and associated documentation files (the |
5 |
|
|
// "Software"), to deal in the Software without restriction, including |
6 |
|
|
// without limitation the rights to use, copy, modify, merge, publish, |
7 |
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit |
8 |
|
|
// persons to whom the Software is furnished to do so, subject to the |
9 |
|
|
// following conditions: |
10 |
|
|
// |
11 |
|
|
// The above copyright notice and this permission notice shall be included |
12 |
|
|
// in all copies or substantial portions of the Software. |
13 |
|
|
// |
14 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
15 |
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
16 |
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
17 |
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
18 |
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
19 |
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
20 |
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 |
|
|
|
22 |
|
|
#include "node.h" |
23 |
|
|
#include "base_object-inl.h" |
24 |
|
|
#include "env-inl.h" |
25 |
|
|
#include "memory_tracker-inl.h" |
26 |
|
|
#include "util-inl.h" |
27 |
|
|
#include "v8.h" |
28 |
|
|
|
29 |
|
|
namespace node { |
30 |
|
|
|
31 |
|
|
using v8::Array; |
32 |
|
|
using v8::Context; |
33 |
|
|
using v8::FunctionCallbackInfo; |
34 |
|
|
using v8::HeapCodeStatistics; |
35 |
|
|
using v8::HeapSpaceStatistics; |
36 |
|
|
using v8::HeapStatistics; |
37 |
|
|
using v8::Integer; |
38 |
|
|
using v8::Isolate; |
39 |
|
|
using v8::Local; |
40 |
|
|
using v8::Object; |
41 |
|
|
using v8::ScriptCompiler; |
42 |
|
|
using v8::String; |
43 |
|
|
using v8::Uint32; |
44 |
|
|
using v8::V8; |
45 |
|
|
using v8::Value; |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
#define HEAP_STATISTICS_PROPERTIES(V) \ |
49 |
|
|
V(0, total_heap_size, kTotalHeapSizeIndex) \ |
50 |
|
|
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \ |
51 |
|
|
V(2, total_physical_size, kTotalPhysicalSizeIndex) \ |
52 |
|
|
V(3, total_available_size, kTotalAvailableSize) \ |
53 |
|
|
V(4, used_heap_size, kUsedHeapSizeIndex) \ |
54 |
|
|
V(5, heap_size_limit, kHeapSizeLimitIndex) \ |
55 |
|
|
V(6, malloced_memory, kMallocedMemoryIndex) \ |
56 |
|
|
V(7, peak_malloced_memory, kPeakMallocedMemoryIndex) \ |
57 |
|
|
V(8, does_zap_garbage, kDoesZapGarbageIndex) \ |
58 |
|
|
V(9, number_of_native_contexts, kNumberOfNativeContextsIndex) \ |
59 |
|
|
V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex) |
60 |
|
|
|
61 |
|
|
#define V(a, b, c) +1 |
62 |
|
|
static constexpr size_t kHeapStatisticsPropertiesCount = |
63 |
|
|
HEAP_STATISTICS_PROPERTIES(V); |
64 |
|
|
#undef V |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \ |
68 |
|
|
V(0, space_size, kSpaceSizeIndex) \ |
69 |
|
|
V(1, space_used_size, kSpaceUsedSizeIndex) \ |
70 |
|
|
V(2, space_available_size, kSpaceAvailableSizeIndex) \ |
71 |
|
|
V(3, physical_space_size, kPhysicalSpaceSizeIndex) |
72 |
|
|
|
73 |
|
|
#define V(a, b, c) +1 |
74 |
|
|
static constexpr size_t kHeapSpaceStatisticsPropertiesCount = |
75 |
|
|
HEAP_SPACE_STATISTICS_PROPERTIES(V); |
76 |
|
|
#undef V |
77 |
|
|
|
78 |
|
|
#define HEAP_CODE_STATISTICS_PROPERTIES(V) \ |
79 |
|
|
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \ |
80 |
|
|
V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \ |
81 |
|
|
V(2, external_script_source_size, kExternalScriptSourceSizeIndex) |
82 |
|
|
|
83 |
|
|
#define V(a, b, c) +1 |
84 |
|
|
static const size_t kHeapCodeStatisticsPropertiesCount = |
85 |
|
|
HEAP_CODE_STATISTICS_PROPERTIES(V); |
86 |
|
|
#undef V |
87 |
|
|
|
88 |
|
896 |
class BindingData : public BaseObject { |
89 |
|
|
public: |
90 |
|
633 |
BindingData(Environment* env, Local<Object> obj) |
91 |
|
633 |
: BaseObject(env, obj), |
92 |
|
|
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount), |
93 |
|
|
heap_space_statistics_buffer(env->isolate(), |
94 |
|
|
kHeapSpaceStatisticsPropertiesCount), |
95 |
|
|
heap_code_statistics_buffer(env->isolate(), |
96 |
|
633 |
kHeapCodeStatisticsPropertiesCount) {} |
97 |
|
|
|
98 |
|
|
static constexpr FastStringKey binding_data_name { "v8" }; |
99 |
|
|
|
100 |
|
|
AliasedFloat64Array heap_statistics_buffer; |
101 |
|
|
AliasedFloat64Array heap_space_statistics_buffer; |
102 |
|
|
AliasedFloat64Array heap_code_statistics_buffer; |
103 |
|
|
|
104 |
|
18 |
void MemoryInfo(MemoryTracker* tracker) const override { |
105 |
|
18 |
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer); |
106 |
|
18 |
tracker->TrackField("heap_space_statistics_buffer", |
107 |
|
18 |
heap_space_statistics_buffer); |
108 |
|
18 |
tracker->TrackField("heap_code_statistics_buffer", |
109 |
|
18 |
heap_code_statistics_buffer); |
110 |
|
18 |
} |
111 |
|
18 |
SET_SELF_SIZE(BindingData) |
112 |
|
18 |
SET_MEMORY_INFO_NAME(BindingData) |
113 |
|
|
}; |
114 |
|
|
|
115 |
|
|
// TODO(addaleax): Remove once we're on C++17. |
116 |
|
|
constexpr FastStringKey BindingData::binding_data_name; |
117 |
|
|
|
118 |
|
4 |
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) { |
119 |
|
4 |
Environment* env = Environment::GetCurrent(args); |
120 |
|
|
Local<Integer> result = |
121 |
|
|
Integer::NewFromUnsigned(env->isolate(), |
122 |
|
4 |
ScriptCompiler::CachedDataVersionTag()); |
123 |
|
8 |
args.GetReturnValue().Set(result); |
124 |
|
4 |
} |
125 |
|
|
|
126 |
|
1643 |
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { |
127 |
|
1643 |
BindingData* data = Environment::GetBindingData<BindingData>(args); |
128 |
|
1643 |
HeapStatistics s; |
129 |
|
1643 |
args.GetIsolate()->GetHeapStatistics(&s); |
130 |
|
1643 |
AliasedFloat64Array& buffer = data->heap_statistics_buffer; |
131 |
|
|
#define V(index, name, _) buffer[index] = static_cast<double>(s.name()); |
132 |
|
1643 |
HEAP_STATISTICS_PROPERTIES(V) |
133 |
|
4929 |
#undef V |
134 |
|
9858 |
} |
135 |
|
6572 |
|
136 |
|
6572 |
|
137 |
|
16430 |
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { |
138 |
|
13144 |
BindingData* data = Environment::GetBindingData<BindingData>(args); |
139 |
|
13144 |
HeapSpaceStatistics s; |
140 |
|
13144 |
Isolate* const isolate = args.GetIsolate(); |
141 |
✗✓ |
26288 |
CHECK(args[0]->IsUint32()); |
142 |
|
39432 |
size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value()); |
143 |
|
13144 |
isolate->GetHeapSpaceStatistics(&s, space_index); |
144 |
|
|
|
145 |
|
13144 |
AliasedFloat64Array& buffer = data->heap_space_statistics_buffer; |
146 |
|
|
|
147 |
|
|
#define V(index, name, _) buffer[index] = static_cast<double>(s.name()); |
148 |
|
13144 |
HEAP_SPACE_STATISTICS_PROPERTIES(V) |
149 |
|
|
#undef V |
150 |
|
13144 |
} |
151 |
|
|
|
152 |
|
1 |
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { |
153 |
|
1 |
BindingData* data = Environment::GetBindingData<BindingData>(args); |
154 |
|
1 |
HeapCodeStatistics s; |
155 |
|
1 |
args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s); |
156 |
|
1 |
AliasedFloat64Array& buffer = data->heap_code_statistics_buffer; |
157 |
|
|
|
158 |
|
|
#define V(index, name, _) buffer[index] = static_cast<double>(s.name()); |
159 |
|
1 |
HEAP_CODE_STATISTICS_PROPERTIES(V) |
160 |
|
|
#undef V |
161 |
|
1 |
} |
162 |
|
|
|
163 |
|
|
|
164 |
|
8 |
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) { |
165 |
✗✓ |
24 |
CHECK(args[0]->IsString()); |
166 |
|
16 |
String::Utf8Value flags(args.GetIsolate(), args[0]); |
167 |
|
8 |
V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length())); |
168 |
|
8 |
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
633 |
void Initialize(Local<Object> target, |
172 |
|
|
Local<Value> unused, |
173 |
|
|
Local<Context> context, |
174 |
|
|
void* priv) { |
175 |
|
633 |
Environment* env = Environment::GetCurrent(context); |
176 |
|
|
BindingData* const binding_data = |
177 |
|
633 |
env->AddBindingData<BindingData>(context, target); |
178 |
✗✓ |
633 |
if (binding_data == nullptr) return; |
179 |
|
|
|
180 |
|
|
env->SetMethodNoSideEffect(target, "cachedDataVersionTag", |
181 |
|
633 |
CachedDataVersionTag); |
182 |
|
|
|
183 |
|
|
// Export symbols used by v8.getHeapStatistics() |
184 |
|
|
env->SetMethod( |
185 |
|
633 |
target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer); |
186 |
|
|
|
187 |
|
|
target |
188 |
|
1266 |
->Set(env->context(), |
189 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"), |
190 |
|
3165 |
binding_data->heap_statistics_buffer.GetJSArray()) |
191 |
|
|
.Check(); |
192 |
|
|
|
193 |
|
|
#define V(i, _, name) \ |
194 |
|
|
target->Set(env->context(), \ |
195 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ |
196 |
|
|
Uint32::NewFromUnsigned(env->isolate(), i)).Check(); |
197 |
|
|
|
198 |
|
4431 |
HEAP_STATISTICS_PROPERTIES(V) |
199 |
|
|
|
200 |
|
3165 |
// Export symbols used by v8.getHeapCodeStatistics() |
201 |
|
4431 |
env->SetMethod( |
202 |
|
5064 |
target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer); |
203 |
|
4431 |
|
204 |
|
4431 |
target |
205 |
|
4431 |
->Set(env->context(), |
206 |
|
1266 |
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"), |
207 |
|
7596 |
binding_data->heap_code_statistics_buffer.GetJSArray()) |
208 |
|
4431 |
.Check(); |
209 |
|
4431 |
|
210 |
|
11394 |
HEAP_CODE_STATISTICS_PROPERTIES(V) |
211 |
|
|
|
212 |
|
633 |
size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces(); |
213 |
|
|
|
214 |
|
|
// Heap space names are extracted once and exposed to JavaScript to |
215 |
|
|
// avoid excessive creation of heap space name Strings. |
216 |
|
633 |
HeapSpaceStatistics s; |
217 |
|
1266 |
MaybeStackBuffer<Local<Value>, 16> heap_spaces(number_of_heap_spaces); |
218 |
✓✓ |
5697 |
for (size_t i = 0; i < number_of_heap_spaces; i++) { |
219 |
|
5064 |
env->isolate()->GetHeapSpaceStatistics(&s, i); |
220 |
|
15192 |
heap_spaces[i] = String::NewFromUtf8(env->isolate(), s.space_name()) |
221 |
|
5064 |
.ToLocalChecked(); |
222 |
|
|
} |
223 |
|
1266 |
target->Set(env->context(), |
224 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"), |
225 |
|
|
Array::New(env->isolate(), |
226 |
|
|
heap_spaces.out(), |
227 |
|
3165 |
number_of_heap_spaces)).Check(); |
228 |
|
|
|
229 |
|
|
env->SetMethod(target, |
230 |
|
|
"updateHeapSpaceStatisticsBuffer", |
231 |
|
633 |
UpdateHeapSpaceStatisticsBuffer); |
232 |
|
|
|
233 |
|
|
target |
234 |
|
1266 |
->Set(env->context(), |
235 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), |
236 |
|
|
"heapSpaceStatisticsBuffer"), |
237 |
|
3165 |
binding_data->heap_space_statistics_buffer.GetJSArray()) |
238 |
|
|
.Check(); |
239 |
|
|
|
240 |
|
9495 |
HEAP_SPACE_STATISTICS_PROPERTIES(V) |
241 |
|
|
#undef V |
242 |
|
|
|
243 |
|
|
// Export symbols used by v8.setFlagsFromString() |
244 |
|
3165 |
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
} // namespace node |
248 |
|
|
|
249 |
✓✗✓✗
|
18652 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize) |