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_v8.h" |
23 |
|
|
#include "base_object-inl.h" |
24 |
|
|
#include "env-inl.h" |
25 |
|
|
#include "memory_tracker-inl.h" |
26 |
|
|
#include "node.h" |
27 |
|
|
#include "node_external_reference.h" |
28 |
|
|
#include "util-inl.h" |
29 |
|
|
#include "v8.h" |
30 |
|
|
|
31 |
|
|
namespace node { |
32 |
|
|
namespace v8_utils { |
33 |
|
|
using v8::Array; |
34 |
|
|
using v8::Context; |
35 |
|
|
using v8::FunctionCallbackInfo; |
36 |
|
|
using v8::HandleScope; |
37 |
|
|
using v8::HeapCodeStatistics; |
38 |
|
|
using v8::HeapSpaceStatistics; |
39 |
|
|
using v8::HeapStatistics; |
40 |
|
|
using v8::Integer; |
41 |
|
|
using v8::Isolate; |
42 |
|
|
using v8::Local; |
43 |
|
|
using v8::Object; |
44 |
|
|
using v8::ScriptCompiler; |
45 |
|
|
using v8::String; |
46 |
|
|
using v8::Uint32; |
47 |
|
|
using v8::V8; |
48 |
|
|
using v8::Value; |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
#define HEAP_STATISTICS_PROPERTIES(V) \ |
52 |
|
|
V(0, total_heap_size, kTotalHeapSizeIndex) \ |
53 |
|
|
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \ |
54 |
|
|
V(2, total_physical_size, kTotalPhysicalSizeIndex) \ |
55 |
|
|
V(3, total_available_size, kTotalAvailableSize) \ |
56 |
|
|
V(4, used_heap_size, kUsedHeapSizeIndex) \ |
57 |
|
|
V(5, heap_size_limit, kHeapSizeLimitIndex) \ |
58 |
|
|
V(6, malloced_memory, kMallocedMemoryIndex) \ |
59 |
|
|
V(7, peak_malloced_memory, kPeakMallocedMemoryIndex) \ |
60 |
|
|
V(8, does_zap_garbage, kDoesZapGarbageIndex) \ |
61 |
|
|
V(9, number_of_native_contexts, kNumberOfNativeContextsIndex) \ |
62 |
|
|
V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex) |
63 |
|
|
|
64 |
|
|
#define V(a, b, c) +1 |
65 |
|
|
static constexpr size_t kHeapStatisticsPropertiesCount = |
66 |
|
|
HEAP_STATISTICS_PROPERTIES(V); |
67 |
|
|
#undef V |
68 |
|
|
|
69 |
|
|
#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \ |
70 |
|
|
V(0, space_size, kSpaceSizeIndex) \ |
71 |
|
|
V(1, space_used_size, kSpaceUsedSizeIndex) \ |
72 |
|
|
V(2, space_available_size, kSpaceAvailableSizeIndex) \ |
73 |
|
|
V(3, physical_space_size, kPhysicalSpaceSizeIndex) |
74 |
|
|
|
75 |
|
|
#define V(a, b, c) +1 |
76 |
|
|
static constexpr size_t kHeapSpaceStatisticsPropertiesCount = |
77 |
|
|
HEAP_SPACE_STATISTICS_PROPERTIES(V); |
78 |
|
|
#undef V |
79 |
|
|
|
80 |
|
|
#define HEAP_CODE_STATISTICS_PROPERTIES(V) \ |
81 |
|
|
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \ |
82 |
|
|
V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \ |
83 |
|
|
V(2, external_script_source_size, kExternalScriptSourceSizeIndex) |
84 |
|
|
|
85 |
|
|
#define V(a, b, c) +1 |
86 |
|
|
static const size_t kHeapCodeStatisticsPropertiesCount = |
87 |
|
|
HEAP_CODE_STATISTICS_PROPERTIES(V); |
88 |
|
|
#undef V |
89 |
|
|
|
90 |
|
5573 |
BindingData::BindingData(Environment* env, Local<Object> obj) |
91 |
|
|
: SnapshotableObject(env, obj, type_int), |
92 |
|
|
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount), |
93 |
|
|
heap_space_statistics_buffer(env->isolate(), |
94 |
|
|
kHeapSpaceStatisticsPropertiesCount), |
95 |
|
|
heap_code_statistics_buffer(env->isolate(), |
96 |
|
5573 |
kHeapCodeStatisticsPropertiesCount) { |
97 |
|
5573 |
obj->Set(env->context(), |
98 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"), |
99 |
|
22292 |
heap_statistics_buffer.GetJSArray()) |
100 |
|
|
.Check(); |
101 |
|
5573 |
obj->Set(env->context(), |
102 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"), |
103 |
|
22292 |
heap_code_statistics_buffer.GetJSArray()) |
104 |
|
|
.Check(); |
105 |
|
5573 |
obj->Set(env->context(), |
106 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "heapSpaceStatisticsBuffer"), |
107 |
|
16719 |
heap_space_statistics_buffer.GetJSArray()) |
108 |
|
|
.Check(); |
109 |
|
5573 |
} |
110 |
|
|
|
111 |
|
6 |
void BindingData::PrepareForSerialization(Local<Context> context, |
112 |
|
|
v8::SnapshotCreator* creator) { |
113 |
|
|
// We'll just re-initialize the buffers in the constructor since their |
114 |
|
|
// contents can be thrown away once consumed in the previous call. |
115 |
|
6 |
heap_statistics_buffer.Release(); |
116 |
|
6 |
heap_space_statistics_buffer.Release(); |
117 |
|
6 |
heap_code_statistics_buffer.Release(); |
118 |
|
6 |
} |
119 |
|
|
|
120 |
|
4950 |
void BindingData::Deserialize(Local<Context> context, |
121 |
|
|
Local<Object> holder, |
122 |
|
|
int index, |
123 |
|
|
InternalFieldInfo* info) { |
124 |
|
|
DCHECK_EQ(index, BaseObject::kSlot); |
125 |
|
9900 |
HandleScope scope(context->GetIsolate()); |
126 |
|
4950 |
Environment* env = Environment::GetCurrent(context); |
127 |
|
4950 |
BindingData* binding = env->AddBindingData<BindingData>(context, holder); |
128 |
✗✓ |
4950 |
CHECK_NOT_NULL(binding); |
129 |
|
4950 |
} |
130 |
|
|
|
131 |
|
6 |
InternalFieldInfo* BindingData::Serialize(int index) { |
132 |
|
|
DCHECK_EQ(index, BaseObject::kSlot); |
133 |
|
6 |
InternalFieldInfo* info = InternalFieldInfo::New(type()); |
134 |
|
6 |
return info; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
23 |
void BindingData::MemoryInfo(MemoryTracker* tracker) const { |
138 |
|
23 |
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer); |
139 |
|
23 |
tracker->TrackField("heap_space_statistics_buffer", |
140 |
|
23 |
heap_space_statistics_buffer); |
141 |
|
23 |
tracker->TrackField("heap_code_statistics_buffer", |
142 |
|
23 |
heap_code_statistics_buffer); |
143 |
|
23 |
} |
144 |
|
|
|
145 |
|
4 |
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) { |
146 |
|
4 |
Environment* env = Environment::GetCurrent(args); |
147 |
|
|
Local<Integer> result = |
148 |
|
|
Integer::NewFromUnsigned(env->isolate(), |
149 |
|
4 |
ScriptCompiler::CachedDataVersionTag()); |
150 |
|
4 |
args.GetReturnValue().Set(result); |
151 |
|
4 |
} |
152 |
|
|
|
153 |
|
1821 |
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { |
154 |
|
1821 |
BindingData* data = Environment::GetBindingData<BindingData>(args); |
155 |
|
1821 |
HeapStatistics s; |
156 |
|
1821 |
args.GetIsolate()->GetHeapStatistics(&s); |
157 |
|
1821 |
AliasedFloat64Array& buffer = data->heap_statistics_buffer; |
158 |
|
|
#define V(index, name, _) buffer[index] = static_cast<double>(s.name()); |
159 |
|
1821 |
HEAP_STATISTICS_PROPERTIES(V) |
160 |
|
|
#undef V |
161 |
|
1821 |
} |
162 |
|
|
|
163 |
|
|
|
164 |
|
14568 |
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { |
165 |
|
14568 |
BindingData* data = Environment::GetBindingData<BindingData>(args); |
166 |
|
14568 |
HeapSpaceStatistics s; |
167 |
✓✗ |
14568 |
Isolate* const isolate = args.GetIsolate(); |
168 |
✗✓ |
14568 |
CHECK(args[0]->IsUint32()); |
169 |
|
29136 |
size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value()); |
170 |
|
14568 |
isolate->GetHeapSpaceStatistics(&s, space_index); |
171 |
|
|
|
172 |
|
14568 |
AliasedFloat64Array& buffer = data->heap_space_statistics_buffer; |
173 |
|
|
|
174 |
|
|
#define V(index, name, _) buffer[index] = static_cast<double>(s.name()); |
175 |
|
14568 |
HEAP_SPACE_STATISTICS_PROPERTIES(V) |
176 |
|
|
#undef V |
177 |
|
14568 |
} |
178 |
|
|
|
179 |
|
1 |
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { |
180 |
|
1 |
BindingData* data = Environment::GetBindingData<BindingData>(args); |
181 |
|
1 |
HeapCodeStatistics s; |
182 |
|
1 |
args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s); |
183 |
|
1 |
AliasedFloat64Array& buffer = data->heap_code_statistics_buffer; |
184 |
|
|
|
185 |
|
|
#define V(index, name, _) buffer[index] = static_cast<double>(s.name()); |
186 |
|
1 |
HEAP_CODE_STATISTICS_PROPERTIES(V) |
187 |
|
|
#undef V |
188 |
|
1 |
} |
189 |
|
|
|
190 |
|
|
|
191 |
✓✗ |
8 |
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) { |
192 |
✗✓ |
16 |
CHECK(args[0]->IsString()); |
193 |
|
16 |
String::Utf8Value flags(args.GetIsolate(), args[0]); |
194 |
|
8 |
V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length())); |
195 |
|
8 |
} |
196 |
|
|
|
197 |
|
623 |
void Initialize(Local<Object> target, |
198 |
|
|
Local<Value> unused, |
199 |
|
|
Local<Context> context, |
200 |
|
|
void* priv) { |
201 |
|
623 |
Environment* env = Environment::GetCurrent(context); |
202 |
|
|
BindingData* const binding_data = |
203 |
|
623 |
env->AddBindingData<BindingData>(context, target); |
204 |
✗✓ |
623 |
if (binding_data == nullptr) return; |
205 |
|
|
|
206 |
|
623 |
env->SetMethodNoSideEffect(target, "cachedDataVersionTag", |
207 |
|
|
CachedDataVersionTag); |
208 |
|
623 |
env->SetMethod( |
209 |
|
|
target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer); |
210 |
|
|
|
211 |
|
623 |
env->SetMethod( |
212 |
|
|
target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer); |
213 |
|
|
|
214 |
|
623 |
size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces(); |
215 |
|
|
|
216 |
|
|
// Heap space names are extracted once and exposed to JavaScript to |
217 |
|
|
// avoid excessive creation of heap space name Strings. |
218 |
|
623 |
HeapSpaceStatistics s; |
219 |
|
1246 |
MaybeStackBuffer<Local<Value>, 16> heap_spaces(number_of_heap_spaces); |
220 |
✓✓ |
5607 |
for (size_t i = 0; i < number_of_heap_spaces; i++) { |
221 |
|
4984 |
env->isolate()->GetHeapSpaceStatistics(&s, i); |
222 |
|
9968 |
heap_spaces[i] = String::NewFromUtf8(env->isolate(), s.space_name()) |
223 |
|
|
.ToLocalChecked(); |
224 |
|
|
} |
225 |
|
623 |
target->Set(env->context(), |
226 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"), |
227 |
|
|
Array::New(env->isolate(), |
228 |
|
|
heap_spaces.out(), |
229 |
|
1869 |
number_of_heap_spaces)).Check(); |
230 |
|
|
|
231 |
|
623 |
env->SetMethod(target, |
232 |
|
|
"updateHeapSpaceStatisticsBuffer", |
233 |
|
|
UpdateHeapSpaceStatisticsBuffer); |
234 |
|
|
|
235 |
|
|
#define V(i, _, name) \ |
236 |
|
|
target \ |
237 |
|
|
->Set(env->context(), \ |
238 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ |
239 |
|
|
Uint32::NewFromUnsigned(env->isolate(), i)) \ |
240 |
|
|
.Check(); |
241 |
|
|
|
242 |
|
21182 |
HEAP_STATISTICS_PROPERTIES(V) |
243 |
|
6230 |
HEAP_CODE_STATISTICS_PROPERTIES(V) |
244 |
|
7476 |
HEAP_SPACE_STATISTICS_PROPERTIES(V) |
245 |
|
|
#undef V |
246 |
|
|
|
247 |
|
|
// Export symbols used by v8.setFlagsFromString() |
248 |
|
623 |
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
4956 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
252 |
|
4956 |
registry->Register(CachedDataVersionTag); |
253 |
|
4956 |
registry->Register(UpdateHeapStatisticsBuffer); |
254 |
|
4956 |
registry->Register(UpdateHeapCodeStatisticsBuffer); |
255 |
|
4956 |
registry->Register(UpdateHeapSpaceStatisticsBuffer); |
256 |
|
4956 |
registry->Register(SetFlagsFromString); |
257 |
|
4956 |
} |
258 |
|
|
|
259 |
|
|
} // namespace v8_utils |
260 |
|
|
} // namespace node |
261 |
|
|
|
262 |
|
5017 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::v8_utils::Initialize) |
263 |
|
4956 |
NODE_MODULE_EXTERNAL_REFERENCE(v8, node::v8_utils::RegisterExternalReferences) |