1 |
|
#include "node.h" |
2 |
|
#include "env.h" |
3 |
|
#include "env-inl.h" |
4 |
|
#include "util.h" |
5 |
|
#include "util-inl.h" |
6 |
|
#include "v8.h" |
7 |
|
|
8 |
|
namespace node { |
9 |
|
|
10 |
|
using v8::Array; |
11 |
|
using v8::ArrayBuffer; |
12 |
|
using v8::Context; |
13 |
|
using v8::Function; |
14 |
|
using v8::FunctionCallbackInfo; |
15 |
|
using v8::HeapSpaceStatistics; |
16 |
|
using v8::HeapStatistics; |
17 |
|
using v8::Isolate; |
18 |
|
using v8::Local; |
19 |
|
using v8::NewStringType; |
20 |
|
using v8::Object; |
21 |
|
using v8::String; |
22 |
|
using v8::Uint32; |
23 |
|
using v8::V8; |
24 |
|
using v8::Value; |
25 |
|
|
26 |
|
#define HEAP_STATISTICS_PROPERTIES(V) \ |
27 |
|
V(0, total_heap_size, kTotalHeapSizeIndex) \ |
28 |
|
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \ |
29 |
|
V(2, total_physical_size, kTotalPhysicalSizeIndex) \ |
30 |
|
V(3, total_available_size, kTotalAvailableSize) \ |
31 |
|
V(4, used_heap_size, kUsedHeapSizeIndex) \ |
32 |
|
V(5, heap_size_limit, kHeapSizeLimitIndex) |
33 |
|
|
34 |
|
#define V(a, b, c) +1 |
35 |
|
static const size_t kHeapStatisticsPropertiesCount = |
36 |
|
HEAP_STATISTICS_PROPERTIES(V); |
37 |
|
#undef V |
38 |
|
|
39 |
|
#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \ |
40 |
|
V(0, space_size, kSpaceSizeIndex) \ |
41 |
|
V(1, space_used_size, kSpaceUsedSizeIndex) \ |
42 |
|
V(2, space_available_size, kSpaceAvailableSizeIndex) \ |
43 |
|
V(3, physical_space_size, kPhysicalSpaceSizeIndex) |
44 |
|
|
45 |
|
#define V(a, b, c) +1 |
46 |
|
static const size_t kHeapSpaceStatisticsPropertiesCount = |
47 |
|
HEAP_SPACE_STATISTICS_PROPERTIES(V); |
48 |
|
#undef V |
49 |
|
|
50 |
|
// Will be populated in InitializeV8Bindings. |
51 |
|
static size_t number_of_heap_spaces = 0; |
52 |
|
|
53 |
|
|
54 |
1 |
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) { |
55 |
1 |
Environment* env = Environment::GetCurrent(args); |
56 |
1 |
HeapStatistics s; |
57 |
1 |
env->isolate()->GetHeapStatistics(&s); |
58 |
2 |
uint32_t* const buffer = env->heap_statistics_buffer(); |
59 |
|
#define V(index, name, _) buffer[index] = static_cast<uint32_t>(s.name()); |
60 |
1 |
HEAP_STATISTICS_PROPERTIES(V) |
61 |
|
#undef V |
62 |
1 |
} |
63 |
|
|
64 |
|
|
65 |
1 |
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { |
66 |
1 |
Environment* env = Environment::GetCurrent(args); |
67 |
1 |
HeapSpaceStatistics s; |
68 |
1 |
Isolate* const isolate = env->isolate(); |
69 |
1 |
uint32_t* buffer = env->heap_space_statistics_buffer(); |
70 |
|
|
71 |
11 |
for (size_t i = 0; i < number_of_heap_spaces; i++) { |
72 |
5 |
isolate->GetHeapSpaceStatistics(&s, i); |
73 |
5 |
size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount; |
74 |
|
#define V(index, name, _) buffer[property_offset + index] = \ |
75 |
|
static_cast<uint32_t>(s.name()); |
76 |
5 |
HEAP_SPACE_STATISTICS_PROPERTIES(V) |
77 |
|
#undef V |
78 |
|
} |
79 |
1 |
} |
80 |
|
|
81 |
|
|
82 |
4 |
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) { |
83 |
4 |
Environment* env = Environment::GetCurrent(args); |
84 |
|
|
85 |
4 |
if (args.Length() < 1) |
86 |
2 |
return env->ThrowTypeError("v8 flag is required"); |
87 |
6 |
if (!args[0]->IsString()) |
88 |
|
return env->ThrowTypeError("v8 flag must be a string"); |
89 |
|
|
90 |
4 |
String::Utf8Value flags(args[0]); |
91 |
2 |
V8::SetFlagsFromString(*flags, flags.length()); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
3 |
void InitializeV8Bindings(Local<Object> target, |
96 |
|
Local<Value> unused, |
97 |
|
Local<Context> context) { |
98 |
3 |
Environment* env = Environment::GetCurrent(context); |
99 |
|
|
100 |
|
env->SetMethod(target, |
101 |
|
"updateHeapStatisticsArrayBuffer", |
102 |
3 |
UpdateHeapStatisticsArrayBuffer); |
103 |
|
|
104 |
6 |
env->set_heap_statistics_buffer(new uint32_t[kHeapStatisticsPropertiesCount]); |
105 |
|
|
106 |
|
const size_t heap_statistics_buffer_byte_length = |
107 |
3 |
sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount; |
108 |
|
|
109 |
12 |
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), |
110 |
|
"heapStatisticsArrayBuffer"), |
111 |
|
ArrayBuffer::New(env->isolate(), |
112 |
|
env->heap_statistics_buffer(), |
113 |
3 |
heap_statistics_buffer_byte_length)); |
114 |
|
|
115 |
|
#define V(i, _, name) \ |
116 |
|
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ |
117 |
|
Uint32::NewFromUnsigned(env->isolate(), i)); |
118 |
|
|
119 |
39 |
HEAP_STATISTICS_PROPERTIES(V) |
120 |
|
#undef V |
121 |
|
|
122 |
12 |
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), |
123 |
|
"kHeapSpaceStatisticsPropertiesCount"), |
124 |
|
Uint32::NewFromUnsigned(env->isolate(), |
125 |
3 |
kHeapSpaceStatisticsPropertiesCount)); |
126 |
|
|
127 |
3 |
number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces(); |
128 |
|
|
129 |
|
// Heap space names are extracted once and exposed to JavaScript to |
130 |
|
// avoid excessive creation of heap space name Strings. |
131 |
3 |
HeapSpaceStatistics s; |
132 |
|
const Local<Array> heap_spaces = Array::New(env->isolate(), |
133 |
3 |
number_of_heap_spaces); |
134 |
18 |
for (size_t i = 0; i < number_of_heap_spaces; i++) { |
135 |
15 |
env->isolate()->GetHeapSpaceStatistics(&s, i); |
136 |
30 |
Local<String> heap_space_name = String::NewFromUtf8(env->isolate(), |
137 |
|
s.space_name(), |
138 |
15 |
NewStringType::kNormal) |
139 |
15 |
.ToLocalChecked(); |
140 |
30 |
heap_spaces->Set(i, heap_space_name); |
141 |
|
} |
142 |
9 |
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"), |
143 |
3 |
heap_spaces); |
144 |
|
|
145 |
|
env->SetMethod(target, |
146 |
|
"updateHeapSpaceStatisticsArrayBuffer", |
147 |
3 |
UpdateHeapSpaceStatisticsBuffer); |
148 |
|
|
149 |
|
env->set_heap_space_statistics_buffer( |
150 |
6 |
new uint32_t[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]); |
151 |
|
|
152 |
|
const size_t heap_space_statistics_buffer_byte_length = |
153 |
|
sizeof(*env->heap_space_statistics_buffer()) * |
154 |
3 |
kHeapSpaceStatisticsPropertiesCount * |
155 |
3 |
number_of_heap_spaces; |
156 |
|
|
157 |
12 |
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), |
158 |
|
"heapSpaceStatisticsArrayBuffer"), |
159 |
|
ArrayBuffer::New(env->isolate(), |
160 |
|
env->heap_space_statistics_buffer(), |
161 |
3 |
heap_space_statistics_buffer_byte_length)); |
162 |
|
|
163 |
|
#define V(i, _, name) \ |
164 |
|
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ |
165 |
|
Uint32::NewFromUnsigned(env->isolate(), i)); |
166 |
|
|
167 |
27 |
HEAP_SPACE_STATISTICS_PROPERTIES(V) |
168 |
|
#undef V |
169 |
|
|
170 |
3 |
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString); |
171 |
3 |
} |
172 |
|
|
173 |
|
} // namespace node |
174 |
|
|
175 |
1560 |
NODE_MODULE_CONTEXT_AWARE_BUILTIN(v8, node::InitializeV8Bindings) |