77
88namespace node {
99
10+ using v8::Array;
1011using v8::ArrayBuffer;
1112using v8::Context;
1213using v8::Function;
1314using v8::FunctionCallbackInfo;
15+ using v8::HeapSpaceStatistics;
1416using v8::HeapStatistics;
1517using v8::Isolate;
1618using v8::Local;
19+ using v8::NewStringType;
1720using v8::Object;
1821using v8::String;
1922using v8::Uint32;
@@ -34,6 +37,21 @@ static const size_t kHeapStatisticsPropertiesCount =
3437 HEAP_STATISTICS_PROPERTIES (V);
3538#undef V
3639
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+
3755void UpdateHeapStatisticsArrayBuffer (const FunctionCallbackInfo<Value>& args) {
3856 Environment* env = Environment::GetCurrent (args);
3957 HeapStatistics s;
@@ -45,6 +63,23 @@ void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
4563}
4664
4765
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+
4883void SetFlagsFromString (const FunctionCallbackInfo<Value>& args) {
4984 Environment* env = Environment::GetCurrent (args);
5085
@@ -62,10 +97,10 @@ void InitializeV8Bindings(Local<Object> target,
6297 Local<Value> unused,
6398 Local<Context> context) {
6499 Environment* env = Environment::GetCurrent (context);
100+
65101 env->SetMethod (target,
66102 " updateHeapStatisticsArrayBuffer" ,
67103 UpdateHeapStatisticsArrayBuffer);
68- env->SetMethod (target, " setFlagsFromString" , SetFlagsFromString);
69104
70105 env->set_heap_statistics_buffer (new uint32_t [kHeapStatisticsPropertiesCount ]);
71106
@@ -84,6 +119,56 @@ void InitializeV8Bindings(Local<Object> target,
84119
85120 HEAP_STATISTICS_PROPERTIES (V)
86121#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);
87172}
88173
89174} // namespace node
0 commit comments