diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-23 06:47:28 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-05-26 23:18:00 +0300 |
commit | 4b74dae6b2601ee8be3b16fed48986f1be49f20a (patch) | |
tree | d9dde6a3876ade51a6857cf46b242ea59ce9e5ef /src/inspector_profiler.cc | |
parent | e2c0c0c68074c539ae2ae30debdeafb9f94d989b (diff) |
inspector: implement --heap-prof
In addition implements --heap-prof-name, --heap-prof-dir and
--heap-prof-interval.
These flags are similar to --cpu-prof flags but they are meant
for the V8 sampling heap profiler instead of the CPU profiler.
PR-URL: https://github.com/nodejs/node/pull/27596
Fixes: https://github.com/nodejs/node/issues/27421
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/inspector_profiler.cc')
-rw-r--r-- | src/inspector_profiler.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index 2a1559f8f97..1b20398dba4 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -258,6 +258,44 @@ void V8CpuProfilerConnection::End() { DispatchMessage("Profiler.stop"); } +std::string V8HeapProfilerConnection::GetDirectory() const { + return env()->heap_prof_dir(); +} + +std::string V8HeapProfilerConnection::GetFilename() const { + return env()->heap_prof_name(); +} + +MaybeLocal<Object> V8HeapProfilerConnection::GetProfile(Local<Object> result) { + Local<Value> profile_v; + if (!result + ->Get(env()->context(), + FIXED_ONE_BYTE_STRING(env()->isolate(), "profile")) + .ToLocal(&profile_v)) { + fprintf(stderr, "'profile' from heap profile result is undefined\n"); + return MaybeLocal<Object>(); + } + if (!profile_v->IsObject()) { + fprintf(stderr, "'profile' from heap profile result is not an Object\n"); + return MaybeLocal<Object>(); + } + return profile_v.As<Object>(); +} + +void V8HeapProfilerConnection::Start() { + DispatchMessage("HeapProfiler.enable"); + std::string params = R"({ "samplingInterval": )"; + params += std::to_string(env()->heap_prof_interval()); + params += " }"; + DispatchMessage("HeapProfiler.startSampling", params.c_str()); +} + +void V8HeapProfilerConnection::End() { + CHECK_EQ(ending_, false); + ending_ = true; + DispatchMessage("HeapProfiler.stopSampling"); +} + // For now, we only support coverage profiling, but we may add more // in the future. void EndStartedProfilers(Environment* env) { @@ -268,6 +306,12 @@ void EndStartedProfilers(Environment* env) { connection->End(); } + connection = env->heap_profiler_connection(); + if (connection != nullptr && !connection->ending()) { + Debug(env, DebugCategory::INSPECTOR_PROFILER, "Ending heap profiling\n"); + connection->End(); + } + connection = env->coverage_connection(); if (connection != nullptr && !connection->ending()) { Debug( @@ -313,6 +357,20 @@ void StartProfilers(Environment* env) { std::make_unique<V8CpuProfilerConnection>(env)); env->cpu_profiler_connection()->Start(); } + if (env->options()->heap_prof) { + const std::string& dir = env->options()->heap_prof_dir; + env->set_heap_prof_interval(env->options()->heap_prof_interval); + env->set_heap_prof_dir(dir.empty() ? GetCwd() : dir); + if (env->options()->heap_prof_name.empty()) { + DiagnosticFilename filename(env, "Heap", "heapprofile"); + env->set_heap_prof_name(*filename); + } else { + env->set_heap_prof_name(env->options()->heap_prof_name); + } + env->set_heap_profiler_connection( + std::make_unique<profiler::V8HeapProfilerConnection>(env)); + env->heap_profiler_connection()->Start(); + } } static void SetCoverageDirectory(const FunctionCallbackInfo<Value>& args) { |