@@ -258,6 +258,44 @@ void V8CpuProfilerConnection::End() {
258258 DispatchMessage (" Profiler.stop" );
259259}
260260
261+ std::string V8HeapProfilerConnection::GetDirectory () const {
262+ return env ()->heap_prof_dir ();
263+ }
264+
265+ std::string V8HeapProfilerConnection::GetFilename () const {
266+ return env ()->heap_prof_name ();
267+ }
268+
269+ MaybeLocal<Object> V8HeapProfilerConnection::GetProfile (Local<Object> result) {
270+ Local<Value> profile_v;
271+ if (!result
272+ ->Get (env ()->context (),
273+ FIXED_ONE_BYTE_STRING (env ()->isolate (), " profile" ))
274+ .ToLocal (&profile_v)) {
275+ fprintf (stderr, " 'profile' from heap profile result is undefined\n " );
276+ return MaybeLocal<Object>();
277+ }
278+ if (!profile_v->IsObject ()) {
279+ fprintf (stderr, " 'profile' from heap profile result is not an Object\n " );
280+ return MaybeLocal<Object>();
281+ }
282+ return profile_v.As <Object>();
283+ }
284+
285+ void V8HeapProfilerConnection::Start () {
286+ DispatchMessage (" HeapProfiler.enable" );
287+ std::string params = R"( { "samplingInterval": )" ;
288+ params += std::to_string (env ()->heap_prof_interval ());
289+ params += " }" ;
290+ DispatchMessage (" HeapProfiler.startSampling" , params.c_str ());
291+ }
292+
293+ void V8HeapProfilerConnection::End () {
294+ CHECK_EQ (ending_, false );
295+ ending_ = true ;
296+ DispatchMessage (" HeapProfiler.stopSampling" );
297+ }
298+
261299// For now, we only support coverage profiling, but we may add more
262300// in the future.
263301void EndStartedProfilers (Environment* env) {
@@ -268,6 +306,12 @@ void EndStartedProfilers(Environment* env) {
268306 connection->End ();
269307 }
270308
309+ connection = env->heap_profiler_connection ();
310+ if (connection != nullptr && !connection->ending ()) {
311+ Debug (env, DebugCategory::INSPECTOR_PROFILER, " Ending heap profiling\n " );
312+ connection->End ();
313+ }
314+
271315 connection = env->coverage_connection ();
272316 if (connection != nullptr && !connection->ending ()) {
273317 Debug (
@@ -313,6 +357,20 @@ void StartProfilers(Environment* env) {
313357 std::make_unique<V8CpuProfilerConnection>(env));
314358 env->cpu_profiler_connection ()->Start ();
315359 }
360+ if (env->options ()->heap_prof ) {
361+ const std::string& dir = env->options ()->heap_prof_dir ;
362+ env->set_heap_prof_interval (env->options ()->heap_prof_interval );
363+ env->set_heap_prof_dir (dir.empty () ? GetCwd () : dir);
364+ if (env->options ()->heap_prof_name .empty ()) {
365+ DiagnosticFilename filename (env, " Heap" , " heapprofile" );
366+ env->set_heap_prof_name (*filename);
367+ } else {
368+ env->set_heap_prof_name (env->options ()->heap_prof_name );
369+ }
370+ env->set_heap_profiler_connection (
371+ std::make_unique<profiler::V8HeapProfilerConnection>(env));
372+ env->heap_profiler_connection ()->Start ();
373+ }
316374}
317375
318376static void SetCoverageDirectory (const FunctionCallbackInfo<Value>& args) {
0 commit comments