From d23ac0ea9cb11c90a385a8235e5ae51b486ffcb7 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 15 Nov 2013 22:19:31 +0100 Subject: [PATCH] src: add v8.getHeapStatistics() function Add a one-to-one binding to v8::GetHeapStatistics(). Returns info on the current state of the JS heap, like total size and amount used. --- lib/tracing.js | 3 +++ src/node_v8.cc | 24 ++++++++++++++++++++++++ test/simple/test-v8-stats.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 test/simple/test-v8-stats.js diff --git a/lib/tracing.js b/lib/tracing.js index 62a56091f7..2609630741 100644 --- a/lib/tracing.js +++ b/lib/tracing.js @@ -42,3 +42,6 @@ v8.on('removeListener', function(name) { binding.stopGarbageCollectionTracking(); } }); + + +v8.getHeapStatistics = binding.getHeapStatistics; diff --git a/src/node_v8.cc b/src/node_v8.cc index 2fd276342d..25f73d6183 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -35,6 +35,7 @@ using v8::GCCallbackFlags; using v8::GCType; using v8::Handle; using v8::HandleScope; +using v8::HeapStatistics; using v8::Isolate; using v8::Local; using v8::Null; @@ -178,6 +179,28 @@ void StartGarbageCollectionTracking(const FunctionCallbackInfo& args) { } +void GetHeapStatistics(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + HandleScope handle_scope(isolate); + Environment* env = Environment::GetCurrent(isolate); + HeapStatistics s; + isolate->GetHeapStatistics(&s); + Local info = Object::New(); + // TODO(trevnorris): Setting many object properties in C++ is a significant + // performance hit. Redo this to pass the results to JS and create/set the + // properties there. +#define V(name) \ + info->Set(env->name ## _string(), Uint32::NewFromUnsigned(s.name(), isolate)) + V(total_heap_size); + V(total_heap_size_executable); + V(total_physical_size); + V(used_heap_size); + V(heap_size_limit); +#undef V + args.GetReturnValue().Set(info); +} + + void StopGarbageCollectionTracking(const FunctionCallbackInfo& args) { HandleScope handle_scope(args.GetIsolate()); Environment::GetCurrent(args.GetIsolate())->StopGarbageCollectionTracking(); @@ -193,6 +216,7 @@ void InitializeV8Bindings(Handle target, NODE_SET_METHOD(target, "stopGarbageCollectionTracking", StopGarbageCollectionTracking); + NODE_SET_METHOD(target, "getHeapStatistics", GetHeapStatistics); } } // namespace node diff --git a/test/simple/test-v8-stats.js b/test/simple/test-v8-stats.js new file mode 100644 index 0000000000..6d70fb9a02 --- /dev/null +++ b/test/simple/test-v8-stats.js @@ -0,0 +1,36 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); +var v8 = require('tracing').v8; + +var s = v8.getHeapStatistics(); +var keys = [ + 'heap_size_limit', + 'total_heap_size', + 'total_heap_size_executable', + 'total_physical_size', + 'used_heap_size']; +assert.deepEqual(Object.keys(s).sort(), keys); +keys.forEach(function(key) { + assert.equal(typeof s[key], 'number'); +});