7
7
8
8
namespace node {
9
9
10
+ using v8::Array;
10
11
using v8::ArrayBuffer;
11
12
using v8::Context;
12
13
using v8::Function;
13
14
using v8::FunctionCallbackInfo;
15
+ using v8::HeapSpaceStatistics;
14
16
using v8::HeapStatistics;
15
17
using v8::Isolate;
16
18
using v8::Local;
19
+ using v8::NewStringType;
17
20
using v8::Object;
18
21
using v8::String;
19
22
using v8::Uint32;
@@ -34,6 +37,21 @@ static const size_t kHeapStatisticsPropertiesCount =
34
37
HEAP_STATISTICS_PROPERTIES (V);
35
38
#undef V
36
39
40
+ #define HEAP_SPACE_STATISTICS_PROPERTIES (V ) \
41
+ V (0 , space_size, kSpaceSizeIndex ) \
42
+ V (1 , space_used_size, kSpaceUsedSizeIndex ) \
43
+ V (2 , space_available_size, kSpaceAvailableSizeIndex ) \
44
+ V (3 , physical_space_size, kPhysicalSpaceSizeIndex )
45
+
46
+ #define V (a, b, c ) +1
47
+ static const size_t kHeapSpaceStatisticsPropertiesCount =
48
+ HEAP_SPACE_STATISTICS_PROPERTIES (V);
49
+ #undef V
50
+
51
+ // Will be populated in InitializeV8Bindings.
52
+ static size_t number_of_heap_spaces = 0 ;
53
+
54
+
37
55
void UpdateHeapStatisticsArrayBuffer (const FunctionCallbackInfo<Value>& args) {
38
56
Environment* env = Environment::GetCurrent (args);
39
57
HeapStatistics s;
@@ -45,6 +63,23 @@ void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
45
63
}
46
64
47
65
66
+ void UpdateHeapSpaceStatisticsBuffer (const FunctionCallbackInfo<Value>& args) {
67
+ Environment* env = Environment::GetCurrent (args);
68
+ HeapSpaceStatistics s;
69
+ Isolate* const isolate = env->isolate ();
70
+ uint32_t * buffer = env->heap_space_statistics_buffer ();
71
+
72
+ for (size_t i = 0 ; i < number_of_heap_spaces; i++) {
73
+ isolate->GetHeapSpaceStatistics (&s, i);
74
+ size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount ;
75
+ #define V (index, name, _ ) buffer[property_offset + index] = \
76
+ static_cast <uint32_t >(s.name ());
77
+ HEAP_SPACE_STATISTICS_PROPERTIES (V)
78
+ #undef V
79
+ }
80
+ }
81
+
82
+
48
83
void SetFlagsFromString (const FunctionCallbackInfo<Value>& args) {
49
84
Environment* env = Environment::GetCurrent (args);
50
85
@@ -62,10 +97,10 @@ void InitializeV8Bindings(Local<Object> target,
62
97
Local<Value> unused,
63
98
Local<Context> context) {
64
99
Environment* env = Environment::GetCurrent (context);
100
+
65
101
env->SetMethod (target,
66
102
" updateHeapStatisticsArrayBuffer" ,
67
103
UpdateHeapStatisticsArrayBuffer);
68
- env->SetMethod (target, " setFlagsFromString" , SetFlagsFromString);
69
104
70
105
env->set_heap_statistics_buffer (new uint32_t [kHeapStatisticsPropertiesCount ]);
71
106
@@ -84,6 +119,56 @@ void InitializeV8Bindings(Local<Object> target,
84
119
85
120
HEAP_STATISTICS_PROPERTIES (V)
86
121
#undef V
122
+
123
+ target->Set (FIXED_ONE_BYTE_STRING (env->isolate (),
124
+ " kHeapSpaceStatisticsPropertiesCount" ),
125
+ Uint32::NewFromUnsigned (env->isolate (),
126
+ kHeapSpaceStatisticsPropertiesCount ));
127
+
128
+ number_of_heap_spaces = env->isolate ()->NumberOfHeapSpaces ();
129
+
130
+ // Heap space names are extracted once and exposed to JavaScript to
131
+ // avoid excessive creation of heap space name Strings.
132
+ HeapSpaceStatistics s;
133
+ const Local<Array> heap_spaces = Array::New (env->isolate (),
134
+ number_of_heap_spaces);
135
+ for (size_t i = 0 ; i < number_of_heap_spaces; i++) {
136
+ env->isolate ()->GetHeapSpaceStatistics (&s, i);
137
+ Local<String> heap_space_name = String::NewFromUtf8 (env->isolate (),
138
+ s.space_name (),
139
+ NewStringType::kNormal )
140
+ .ToLocalChecked ();
141
+ heap_spaces->Set (i, heap_space_name);
142
+ }
143
+ target->Set (FIXED_ONE_BYTE_STRING (env->isolate (), " kHeapSpaces" ),
144
+ heap_spaces);
145
+
146
+ env->SetMethod (target,
147
+ " updateHeapSpaceStatisticsArrayBuffer" ,
148
+ UpdateHeapSpaceStatisticsBuffer);
149
+
150
+ env->set_heap_space_statistics_buffer (
151
+ new uint32_t [kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
152
+
153
+ const size_t heap_space_statistics_buffer_byte_length =
154
+ sizeof (*env->heap_space_statistics_buffer ()) *
155
+ kHeapSpaceStatisticsPropertiesCount *
156
+ number_of_heap_spaces;
157
+
158
+ target->Set (FIXED_ONE_BYTE_STRING (env->isolate (),
159
+ " heapSpaceStatisticsArrayBuffer" ),
160
+ ArrayBuffer::New (env->isolate (),
161
+ env->heap_space_statistics_buffer (),
162
+ heap_space_statistics_buffer_byte_length));
163
+
164
+ #define V (i, _, name ) \
165
+ target->Set (FIXED_ONE_BYTE_STRING (env->isolate (), #name), \
166
+ Uint32::NewFromUnsigned (env->isolate (), i));
167
+
168
+ HEAP_SPACE_STATISTICS_PROPERTIES (V)
169
+ #undef V
170
+
171
+ env->SetMethod (target, " setFlagsFromString" , SetFlagsFromString);
87
172
}
88
173
89
174
} // namespace node
0 commit comments