mirror of
https://github.com/nodejs/node.git
synced 2025-05-02 17:01:08 +00:00
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.
This commit is contained in:
parent
010222d39f
commit
d23ac0ea9c
@ -42,3 +42,6 @@ v8.on('removeListener', function(name) {
|
|||||||
binding.stopGarbageCollectionTracking();
|
binding.stopGarbageCollectionTracking();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
v8.getHeapStatistics = binding.getHeapStatistics;
|
||||||
|
@ -35,6 +35,7 @@ using v8::GCCallbackFlags;
|
|||||||
using v8::GCType;
|
using v8::GCType;
|
||||||
using v8::Handle;
|
using v8::Handle;
|
||||||
using v8::HandleScope;
|
using v8::HandleScope;
|
||||||
|
using v8::HeapStatistics;
|
||||||
using v8::Isolate;
|
using v8::Isolate;
|
||||||
using v8::Local;
|
using v8::Local;
|
||||||
using v8::Null;
|
using v8::Null;
|
||||||
@ -178,6 +179,28 @@ void StartGarbageCollectionTracking(const FunctionCallbackInfo<Value>& args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
|
||||||
|
Isolate* isolate = args.GetIsolate();
|
||||||
|
HandleScope handle_scope(isolate);
|
||||||
|
Environment* env = Environment::GetCurrent(isolate);
|
||||||
|
HeapStatistics s;
|
||||||
|
isolate->GetHeapStatistics(&s);
|
||||||
|
Local<Object> 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<Value>& args) {
|
void StopGarbageCollectionTracking(const FunctionCallbackInfo<Value>& args) {
|
||||||
HandleScope handle_scope(args.GetIsolate());
|
HandleScope handle_scope(args.GetIsolate());
|
||||||
Environment::GetCurrent(args.GetIsolate())->StopGarbageCollectionTracking();
|
Environment::GetCurrent(args.GetIsolate())->StopGarbageCollectionTracking();
|
||||||
@ -193,6 +216,7 @@ void InitializeV8Bindings(Handle<Object> target,
|
|||||||
NODE_SET_METHOD(target,
|
NODE_SET_METHOD(target,
|
||||||
"stopGarbageCollectionTracking",
|
"stopGarbageCollectionTracking",
|
||||||
StopGarbageCollectionTracking);
|
StopGarbageCollectionTracking);
|
||||||
|
NODE_SET_METHOD(target, "getHeapStatistics", GetHeapStatistics);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
36
test/simple/test-v8-stats.js
Normal file
36
test/simple/test-v8-stats.js
Normal file
@ -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');
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user