diff options
author | Ben Ripkens <bripkens.dev@gmail.com> | 2015-12-29 13:54:35 +0300 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2016-01-18 19:44:00 +0300 |
commit | 5f57005ec9eacbfc7855ec7c9e246f61da5a75ae (patch) | |
tree | 2daff450b026b0d18faa494888319db7d9048393 /src/node_v8.cc | |
parent | e65f1f7954fb364ab70063cdb374603c82f58385 (diff) |
v8,src: expose statistics about heap spaces
Provide means to inspect information about the separate heap spaces
via a callable API. This is helpful to analyze memory issues.
Fixes: https://github.com/nodejs/node/issues/2079
PR-URL: https://github.com/nodejs/node/pull/4463
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_v8.cc')
-rw-r--r-- | src/node_v8.cc | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/src/node_v8.cc b/src/node_v8.cc index 9f456daec46..a1122e57f13 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -7,13 +7,16 @@ namespace node { +using v8::Array; using v8::ArrayBuffer; using v8::Context; using v8::Function; using v8::FunctionCallbackInfo; +using v8::HeapSpaceStatistics; using v8::HeapStatistics; using v8::Isolate; using v8::Local; +using v8::NewStringType; using v8::Object; using v8::String; using v8::Uint32; @@ -34,6 +37,21 @@ static const size_t kHeapStatisticsPropertiesCount = HEAP_STATISTICS_PROPERTIES(V); #undef V +#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \ + V(0, space_size, kSpaceSizeIndex) \ + V(1, space_used_size, kSpaceUsedSizeIndex) \ + V(2, space_available_size, kSpaceAvailableSizeIndex) \ + V(3, physical_space_size, kPhysicalSpaceSizeIndex) + +#define V(a, b, c) +1 +static const size_t kHeapSpaceStatisticsPropertiesCount = + HEAP_SPACE_STATISTICS_PROPERTIES(V); +#undef V + +// Will be populated in InitializeV8Bindings. +static size_t number_of_heap_spaces = 0; + + void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); HeapStatistics s; @@ -45,6 +63,23 @@ void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) { } +void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args); + HeapSpaceStatistics s; + Isolate* const isolate = env->isolate(); + uint32_t* buffer = env->heap_space_statistics_buffer(); + + for (size_t i = 0; i < number_of_heap_spaces; i++) { + isolate->GetHeapSpaceStatistics(&s, i); + size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount; +#define V(index, name, _) buffer[property_offset + index] = \ + static_cast<uint32_t>(s.name()); + HEAP_SPACE_STATISTICS_PROPERTIES(V) +#undef V + } +} + + void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); @@ -62,10 +97,10 @@ void InitializeV8Bindings(Local<Object> target, Local<Value> unused, Local<Context> context) { Environment* env = Environment::GetCurrent(context); + env->SetMethod(target, "updateHeapStatisticsArrayBuffer", UpdateHeapStatisticsArrayBuffer); - env->SetMethod(target, "setFlagsFromString", SetFlagsFromString); env->set_heap_statistics_buffer(new uint32_t[kHeapStatisticsPropertiesCount]); @@ -84,6 +119,56 @@ void InitializeV8Bindings(Local<Object> target, HEAP_STATISTICS_PROPERTIES(V) #undef V + + target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), + "kHeapSpaceStatisticsPropertiesCount"), + Uint32::NewFromUnsigned(env->isolate(), + kHeapSpaceStatisticsPropertiesCount)); + + number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces(); + + // Heap space names are extracted once and exposed to JavaScript to + // avoid excessive creation of heap space name Strings. + HeapSpaceStatistics s; + const Local<Array> heap_spaces = Array::New(env->isolate(), + number_of_heap_spaces); + for (size_t i = 0; i < number_of_heap_spaces; i++) { + env->isolate()->GetHeapSpaceStatistics(&s, i); + Local<String> heap_space_name = String::NewFromUtf8(env->isolate(), + s.space_name(), + NewStringType::kNormal) + .ToLocalChecked(); + heap_spaces->Set(i, heap_space_name); + } + target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"), + heap_spaces); + + env->SetMethod(target, + "updateHeapSpaceStatisticsArrayBuffer", + UpdateHeapSpaceStatisticsBuffer); + + env->set_heap_space_statistics_buffer( + new uint32_t[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]); + + const size_t heap_space_statistics_buffer_byte_length = + sizeof(*env->heap_space_statistics_buffer()) * + kHeapSpaceStatisticsPropertiesCount * + number_of_heap_spaces; + + target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), + "heapSpaceStatisticsArrayBuffer"), + ArrayBuffer::New(env->isolate(), + env->heap_space_statistics_buffer(), + heap_space_statistics_buffer_byte_length)); + +#define V(i, _, name) \ + target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ + Uint32::NewFromUnsigned(env->isolate(), i)); + + HEAP_SPACE_STATISTICS_PROPERTIES(V) +#undef V + + env->SetMethod(target, "setFlagsFromString", SetFlagsFromString); } } // namespace node |