diff --git a/React/Executors/RCTJSCExecutor.h b/React/Executors/RCTJSCExecutor.h index cbcf224fc..b34218f2b 100644 --- a/React/Executors/RCTJSCExecutor.h +++ b/React/Executors/RCTJSCExecutor.h @@ -11,6 +11,11 @@ #import "RCTJavaScriptExecutor.h" +/** + * Default name for the JS thread + */ +RCT_EXTERN NSString *const RCTJSCThreadName; + /** * Uses a JavaScriptCore context as the execution engine. */ diff --git a/React/Executors/RCTJSCExecutor.m b/React/Executors/RCTJSCExecutor.m index f96d2992a..176bffe87 100644 --- a/React/Executors/RCTJSCExecutor.m +++ b/React/Executors/RCTJSCExecutor.m @@ -26,6 +26,8 @@ #import "RCTRedBox.h" #import "RCTSourceCode.h" +NSString *const RCTJSCThreadName = @"com.facebook.React.JavaScript"; + static NSString *const RCTJSCProfilerEnabledDefaultsKey = @"RCTJSCProfilerEnabled"; @interface RCTJavaScriptContext : NSObject @@ -172,7 +174,7 @@ static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context) NSThread *javaScriptThread = [[NSThread alloc] initWithTarget:[self class] selector:@selector(runRunLoopThread) object:nil]; - javaScriptThread.name = @"com.facebook.React.JavaScript"; + javaScriptThread.name = RCTJSCThreadName; if ([javaScriptThread respondsToSelector:@selector(setQualityOfService:)]) { [javaScriptThread setQualityOfService:NSOperationQualityOfServiceUserInteractive]; diff --git a/React/Profiler/RCTProfile.m b/React/Profiler/RCTProfile.m index 32f63e916..6ed1e0f7d 100644 --- a/React/Profiler/RCTProfile.m +++ b/React/Profiler/RCTProfile.m @@ -27,6 +27,7 @@ #import "RCTModuleData.h" #import "RCTUtils.h" #import "RCTUIManager.h" +#import "RCTJSCExecutor.h" NSString *const RCTProfileDidStartProfiling = @"RCTProfileDidStartProfiling"; NSString *const RCTProfileDidEndProfiling = @"RCTProfileDidEndProfiling"; @@ -372,7 +373,6 @@ BOOL RCTProfileIsProfiling(void) void RCTProfileInit(RCTBridge *bridge) { // TODO: enable assert JS thread from any file (and assert here) - if (RCTProfileIsProfiling()) { return; } @@ -395,6 +395,20 @@ void RCTProfileInit(RCTBridge *bridge) }); } + // Set up thread ordering + dispatch_async(RCTProfileGetQueue(), ^{ + NSString *shadowQueue = @(dispatch_queue_get_label([[bridge uiManager] methodQueue])); + NSArray *orderedThreads = @[@"JS async", RCTJSCThreadName, shadowQueue, @"main"]; + [orderedThreads enumerateObjectsUsingBlock:^(NSString *thread, NSUInteger idx, BOOL *stop) { + RCTProfileAddEvent(RCTProfileTraceEvents, + @"ph": @"M", // metadata event + @"name": @"thread_sort_index", + @"tid": thread, + @"args": @{ @"sort_index": @(-1000 + (NSInteger)idx) } + ); + }]; + }); + RCTProfileHookModules(bridge); RCTProfileDisplayLink = [CADisplayLink displayLinkWithTarget:[RCTProfile class]