Add Systrace instrumentation to RuntimeScheduler (#37880)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37880

This adds systrace sections for the most relevant parts of `RuntimeScheduler`. This helps us identify how things are scheduled, which in this case makes it obvious we're not dispatching events the most efficient way (as top-level callbacks in the runtime executor instead of as tasks in the scheduler).

Changelog: [internal]

Reviewed By: rshest

Differential Revision: D46556399

fbshipit-source-id: 93a13a2c5ec18a34712546889de21792671d5d2c
This commit is contained in:
Rubén Norte
2023-09-19 09:03:33 -07:00
committed by Facebook GitHub Bot
parent b38a9dedd3
commit 8fe2da5391
3 changed files with 22 additions and 0 deletions
@@ -25,4 +25,5 @@ target_link_libraries(react_render_runtimescheduler
jsi
react_debug
react_render_core
react_render_debug
runtimeexecutor)
@@ -55,6 +55,7 @@ Pod::Spec.new do |s|
s.dependency "React-runtimeexecutor"
s.dependency "React-callinvoker"
s.dependency "React-debug"
s.dependency "React-rendererdebug"
s.dependency "React-utils"
s.dependency "glog"
s.dependency "RCT-Folly", folly_version
@@ -8,6 +8,7 @@
#include "RuntimeScheduler.h"
#include "SchedulerPriorityUtils.h"
#include <react/renderer/debug/SystraceSection.h>
#include <utility>
#include "ErrorUtils.h"
@@ -21,10 +22,13 @@ RuntimeScheduler::RuntimeScheduler(
: runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {}
void RuntimeScheduler::scheduleWork(RawCallback callback) const {
SystraceSection s("RuntimeScheduler::scheduleWork");
runtimeAccessRequests_ += 1;
runtimeExecutor_(
[this, callback = std::move(callback)](jsi::Runtime& runtime) {
SystraceSection s2("RuntimeScheduler::scheduleWork callback");
runtimeAccessRequests_ -= 1;
callback(runtime);
startWorkLoop(runtime);
@@ -78,10 +82,15 @@ RuntimeSchedulerTimePoint RuntimeScheduler::now() const noexcept {
}
void RuntimeScheduler::executeNowOnTheSameThread(RawCallback callback) {
SystraceSection s("RuntimeScheduler::executeNowOnTheSameThread");
runtimeAccessRequests_ += 1;
executeSynchronouslyOnSameThread_CAN_DEADLOCK(
runtimeExecutor_,
[this, callback = std::move(callback)](jsi::Runtime& runtime) {
SystraceSection s2(
"RuntimeScheduler::executeNowOnTheSameThread callback");
runtimeAccessRequests_ -= 1;
isSynchronous_ = true;
callback(runtime);
@@ -95,6 +104,8 @@ void RuntimeScheduler::executeNowOnTheSameThread(RawCallback callback) {
}
void RuntimeScheduler::callExpiredTasks(jsi::Runtime& runtime) {
SystraceSection s("RuntimeScheduler::callExpiredTasks");
auto previousPriority = currentPriority_;
try {
while (!taskQueue_.empty()) {
@@ -128,6 +139,8 @@ void RuntimeScheduler::scheduleWorkLoopIfNecessary() const {
}
void RuntimeScheduler::startWorkLoop(jsi::Runtime& runtime) const {
SystraceSection s("RuntimeScheduler::startWorkLoop");
auto previousPriority = currentPriority_;
isPerformingWork_ = true;
try {
@@ -155,6 +168,13 @@ void RuntimeScheduler::executeTask(
jsi::Runtime& runtime,
std::shared_ptr<Task> task,
bool didUserCallbackTimeout) const {
SystraceSection s(
"RuntimeScheduler::executeTask",
"priority",
serialize(task->priority),
"didUserCallbackTimeout",
didUserCallbackTimeout);
currentPriority_ = task->priority;
auto result = task->execute(runtime, didUserCallbackTimeout);