Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/js/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(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) {
Expand All @@ -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());
Expand Down Expand Up @@ -414,6 +427,7 @@ struct Host::Impl {
immediate.pop_front();
set_callback(callback);
guarded([&] { callback.call(*runtime); });
enter_microtasks();
guarded([&] { drain(*runtime); });
}
for (;;) {
Expand All @@ -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) {
Expand All @@ -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); });
}
}
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion src/js/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<name> > 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;

Expand Down
43 changes: 43 additions & 0 deletions tests/js_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> entered;
std::promise<void> 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<void> blocking;
Expand Down Expand Up @@ -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());
}

Expand Down
Loading