From 18ec1cbba096881311e0024b99d296f63583cfaa Mon Sep 17 00:00:00 2001 From: phaser Date: Thu, 12 Nov 2026 19:31:02 +0100 Subject: [PATCH] A turn names a callback's microtasks and its slowest finished step Once a timer, immediate or frame callback returns, the microtasks it queued run as " > microtasks", so a watcher can tell the callback from the renders it set off. Each turn also records its longest finished step and how long it ran (Turn::slowestStep, slowestStepMs). --- src/js/host.cpp | 27 +++++++++++++++++++++++--- src/js/host.h | 8 +++++++- tests/js_host_test.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/js/host.cpp b/src/js/host.cpp index a3d53b1..bce59f1 100644 --- a/src/js/host.cpp +++ b/src/js/host.cpp @@ -213,10 +213,17 @@ struct Host::Impl { } void set_step(std::string step) { + const std::int64_t now = steady_ns(); std::lock_guard lock(turnMutex); + const double finishedMs = static_cast(now - stepStartedNs) / 1e6; + const bool slowest = !turn.step.empty() && finishedMs > turn.slowestStepMs; + if (slowest) { + turn.slowestStep = std::move(turn.step); + turn.slowestStepMs = finishedMs; + } turn.step = std::move(step); turn.callback.clear(); - stepStartedNs = steady_ns(); + stepStartedNs = now; } void set_callback(const jsi::Function& callback) { @@ -225,6 +232,12 @@ struct Host::Impl { turn.callback = std::move(name); } + /** The running callback has returned; the microtasks it queued run next. */ + void enter_microtasks() { + std::lock_guard lock(turnMutex); + turn.callback += " > microtasks"; + } + void report(const std::string& message) { error = message; if (options.realClock) std::fprintf(stderr, "[js] %s\n", message.c_str()); @@ -414,6 +427,7 @@ struct Host::Impl { immediate.pop_front(); set_callback(callback); guarded([&] { callback.call(*runtime); }); + enter_microtasks(); guarded([&] { drain(*runtime); }); } for (;;) { @@ -434,6 +448,7 @@ struct Host::Impl { timer.callback.call(*runtime); } }); + enter_microtasks(); guarded([&] { drain(*runtime); }); running_timer_id = 0; if (timer.interval > 0 && !cancel_running_timer) { @@ -450,6 +465,7 @@ struct Host::Impl { for (auto& [id, callback] : due) { set_callback(callback); guarded([&] { callback.call(*runtime, time); }); + enter_microtasks(); guarded([&] { drain(*runtime); }); } } @@ -504,8 +520,13 @@ struct Host::Impl { tickTagged = false; { std::lock_guard lock(turnMutex); - turn = Host::Turn{ - .step = "", .callback = "", .tasks = batch.size(), .tasksRun = 0}; + turn = Host::Turn{.step = "", + .callback = "", + .tasks = batch.size(), + .tasksRun = 0, + .stepMs = 0, + .slowestStep = "", + .slowestStepMs = 0}; stepStartedNs = turnStartedNs; } for (auto& task : batch) { diff --git a/src/js/host.h b/src/js/host.h index 7a90bfa..932e4e2 100644 --- a/src/js/host.h +++ b/src/js/host.h @@ -95,13 +95,19 @@ class Host { std::string step; /** The timer, immediate or frame callback the step is in, by its * function's name ("anonymous" without one); empty outside them. React's - * scheduler is "performWorkUntilDeadline". */ + * scheduler is "performWorkUntilDeadline". The microtasks a callback + * queued run after it returns, as " > microtasks": React's + * synchronous renders are among them. */ std::string callback; std::size_t tasks = 0; std::size_t tasksRun = 0; /** How long the step has run; the rest of the turn went to the steps * before it. */ double stepMs = 0; + /** The longest of those earlier steps and how long it ran; empty while + * the turn is in its first. */ + std::string slowestStep; + double slowestStepMs = 0; }; Turn turn() const; diff --git a/tests/js_host_test.cpp b/tests/js_host_test.cpp index bfd7be7..fe093c2 100644 --- a/tests/js_host_test.cpp +++ b/tests/js_host_test.cpp @@ -240,6 +240,47 @@ TEST_CASE("a held timer names its callback to another thread") { CHECK(host.take_error().empty()); } +TEST_CASE("a held frame names its callback's microtasks apart from it") { + bureau::js::Host::Options options; + options.realClock = true; + bureau::js::Host host(read_bytes(BUREAU_CLOSURE_BUNDLE), options); + std::promise entered; + std::promise release; + auto ready = entered.get_future(); + auto held = release.get_future(); + host.invoke([&](facebook::jsi::Runtime& runtime) { + runtime.global().setProperty( + runtime, "render", + facebook::jsi::Function::createFromHostFunction( + runtime, facebook::jsi::PropNameID::forAscii(runtime, "render"), + 0, + [&](facebook::jsi::Runtime&, const facebook::jsi::Value&, + const facebook::jsi::Value*, size_t) { + entered.set_value(); + held.wait(); + return facebook::jsi::Value::undefined(); + })); + auto drawCity = facebook::jsi::Function::createFromHostFunction( + runtime, facebook::jsi::PropNameID::forAscii(runtime, "drawCity"), 1, + [](facebook::jsi::Runtime& runtime, const facebook::jsi::Value&, + const facebook::jsi::Value*, size_t) { + runtime.queueMicrotask( + runtime.global().getPropertyAsFunction(runtime, "render")); + return facebook::jsi::Value::undefined(); + }); + runtime.global() + .getPropertyAsFunction(runtime, "requestAnimationFrame") + .call(runtime, drawCity); + }); + host.frame(0); + ready.wait(); + const auto turn = host.turn(); + release.set_value(); + CHECK(turn.step == "animation frames"); + CHECK(turn.callback == "drawCity > microtasks"); + CHECK(host.take_error().empty()); +} + TEST_CASE("a held turn times its step apart from the steps before it") { bureau::js::Host host(read_bytes(BUREAU_CLOSURE_BUNDLE)); std::promise blocking; @@ -272,6 +313,8 @@ TEST_CASE("a held turn times its step apart from the steps before it") { CHECK(turn.step == "tickEvents"); CHECK(turn.callback.empty()); CHECK(busyMs - turn.stepMs >= 50); + CHECK(turn.slowestStep == "worldLoaded"); + CHECK(turn.slowestStepMs >= 50); CHECK(host.take_error().empty()); }