From 531906eb8790c11e68f068e6df6816fcc5bb2517 Mon Sep 17 00:00:00 2001 From: Martok Date: Sat, 21 Jan 2023 22:48:18 +0100 Subject: [PATCH] No issue - add API to tell Profile Timeline Recording state to JS engine Based-on: m-c 1342070/5 --- docshell/base/timeline/TimelineConsumers.cpp | 7 +++++++ js/src/jsapi.cpp | 14 ++++++++++++++ js/src/jsapi.h | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/docshell/base/timeline/TimelineConsumers.cpp b/docshell/base/timeline/TimelineConsumers.cpp index 92d589c5ba..3abe152881 100644 --- a/docshell/base/timeline/TimelineConsumers.cpp +++ b/docshell/base/timeline/TimelineConsumers.cpp @@ -6,6 +6,7 @@ #include "TimelineConsumers.h" #include "mozilla/ClearOnShutdown.h" +#include "jsapi.h" #include "nsAppRunner.h" // for XRE_IsContentProcess, XRE_IsParentProcess #include "nsDocShell.h" @@ -125,6 +126,9 @@ TimelineConsumers::AddConsumer(nsDocShell* aDocShell) UniquePtr& observed = aDocShell->mObserved; MOZ_ASSERT(!observed); + if (mActiveConsumers == 0) { + JS::SetProfileTimelineRecordingEnabled(true); + } mActiveConsumers++; ObservedDocShell* obsDocShell = new ObservedDocShell(aDocShell); @@ -144,6 +148,9 @@ TimelineConsumers::RemoveConsumer(nsDocShell* aDocShell) MOZ_ASSERT(observed); mActiveConsumers--; + if (mActiveConsumers == 0) { + JS::SetProfileTimelineRecordingEnabled(false); + } // Clear all markers from the `mTimelineMarkers` store. observed.get()->ClearMarkers(); diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index ca6289344b..7d32948a27 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -1276,6 +1276,20 @@ JS::detail::ComputeThis(JSContext* cx, Value* vp) return thisv; } +static bool gProfileTimelineRecordingEnabled = false; + +JS_PUBLIC_API(void) +JS::SetProfileTimelineRecordingEnabled(bool enabled) +{ + gProfileTimelineRecordingEnabled = enabled; +} + +JS_PUBLIC_API(bool) +JS::IsProfileTimelineRecordingEnabled() +{ + return gProfileTimelineRecordingEnabled; +} + JS_PUBLIC_API(void*) JS_malloc(JSContext* cx, size_t nbytes) { diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 0b865ff523..938fcb2a33 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -1545,6 +1545,22 @@ JS_DefineProfilingFunctions(JSContext* cx, JS::HandleObject obj); extern JS_PUBLIC_API(bool) JS_DefineDebuggerObject(JSContext* cx, JS::HandleObject obj); +namespace JS { + +/** + * Tell JS engine whether Profile Timeline Recording is enabled or not. + * If Profile Timeline Recording is enabled, data shown there like stack won't + * be optimized out. + * This is global state and not associated with specific runtime or context. + */ +extern JS_PUBLIC_API(void) +SetProfileTimelineRecordingEnabled(bool enabled); + +extern JS_PUBLIC_API(bool) +IsProfileTimelineRecordingEnabled(); + +} // namespace JS + #ifdef JS_HAS_CTYPES /** * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'