diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2020-02-07 20:49:43 +0300 |
---|---|---|
committer | Shelley Vohr <shelley.vohr@gmail.com> | 2020-02-27 21:07:46 +0300 |
commit | ae3929e958dfa72bcf9f9efeb53c89f825c228ce (patch) | |
tree | 7d5be13212332804a16f3a0e755e2538afd680e7 /src/node_contextify.cc | |
parent | c5acf0a13bd1debf2d8779ab6f2baf2639cc5b0f (diff) |
vm: implement vm.measureMemory() for per-context memory measurement
This patch implements `vm.measureMemory()` with the new
`v8::Isolate::MeasureMemory()` API to measure per-context memory
usage. This should be experimental, since detailed memory
measurement requires further integration with the V8 API
that should be available in a future V8 update.
PR-URL: https://github.com/nodejs/node/pull/31824
Refs: https://github.com/ulan/performance-measure-memory
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'src/node_contextify.cc')
-rw-r--r-- | src/node_contextify.cc | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 5f289aecb34..6de27188be6 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -47,17 +47,20 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; using v8::IndexedPropertyHandlerConfiguration; +using v8::Int32; using v8::Integer; using v8::Isolate; using v8::Local; using v8::Maybe; using v8::MaybeLocal; +using v8::MeasureMemoryMode; using v8::Name; using v8::NamedPropertyHandlerConfiguration; using v8::Number; using v8::Object; using v8::ObjectTemplate; using v8::PrimitiveArray; +using v8::Promise; using v8::PropertyAttribute; using v8::PropertyCallbackInfo; using v8::PropertyDescriptor; @@ -1203,11 +1206,39 @@ static void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) { args.GetReturnValue().Set(ret); } +static void MeasureMemory(const FunctionCallbackInfo<Value>& args) { + CHECK(args[0]->IsInt32()); + int32_t mode = args[0].As<v8::Int32>()->Value(); + Isolate* isolate = args.GetIsolate(); + Environment* env = Environment::GetCurrent(args); + Local<Context> context; + if (args[1]->IsUndefined()) { + context = isolate->GetCurrentContext(); + } else { + CHECK(args[1]->IsObject()); + ContextifyContext* sandbox = + ContextifyContext::ContextFromContextifiedSandbox(env, + args[1].As<Object>()); + CHECK_NOT_NULL(sandbox); + context = sandbox->context(); + if (context.IsEmpty()) { // Not yet fully initilaized + return; + } + } + v8::Local<v8::Promise> promise; + if (!isolate->MeasureMemory(context, static_cast<v8::MeasureMemoryMode>(mode)) + .ToLocal(&promise)) { + return; + } + args.GetReturnValue().Set(promise); +} + void Initialize(Local<Object> target, Local<Value> unused, Local<Context> context, void* priv) { Environment* env = Environment::GetCurrent(context); + Isolate* isolate = env->isolate(); ContextifyContext::Init(env, target); ContextifyScript::Init(env, target); @@ -1224,6 +1255,19 @@ void Initialize(Local<Object> target, env->set_compiled_fn_entry_template(tpl->InstanceTemplate()); } + + Local<Object> constants = Object::New(env->isolate()); + Local<Object> measure_memory = Object::New(env->isolate()); + Local<Object> memory_mode = Object::New(env->isolate()); + MeasureMemoryMode SUMMARY = MeasureMemoryMode::kSummary; + MeasureMemoryMode DETAILED = MeasureMemoryMode::kDetailed; + NODE_DEFINE_CONSTANT(memory_mode, SUMMARY); + NODE_DEFINE_CONSTANT(memory_mode, DETAILED); + READONLY_PROPERTY(measure_memory, "mode", memory_mode); + READONLY_PROPERTY(constants, "measureMemory", measure_memory); + target->Set(context, env->constants_string(), constants).Check(); + + env->SetMethod(target, "measureMemory", MeasureMemory); } } // namespace contextify |