From 86e18ace940198464b0ff4d809b4df73a463298c Mon Sep 17 00:00:00 2001 From: Marc Horowitz Date: Fri, 13 May 2016 17:14:59 -0700 Subject: [PATCH] make RCTFBSystrace actually work Reviewed By: tadeuzagallo Differential Revision: D3234824 fbshipit-source-id: 9f2f7efd6877fe7b9096dedecb311d226eae7bc8 --- React/Base/RCTBatchedBridge.m | 12 +++--- React/Base/RCTDisplayLink.m | 6 +-- React/Base/RCTModuleData.m | 4 +- React/Base/RCTPerformanceLogger.m | 4 +- React/Base/RCTRootView.m | 4 +- React/Executors/RCTJSCExecutor.mm | 13 ++++--- React/Modules/RCTUIManager.m | 4 +- React/Profiler/RCTProfile.h | 10 ++--- React/Profiler/RCTProfile.m | 61 ++++++++++++++++--------------- 9 files changed, 59 insertions(+), 59 deletions(-) diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 7318999d6..4b210e072 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -664,7 +664,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR * AnyThread */ - RCT_PROFILE_BEGIN_EVENT(0, @"-[RCTBatchedBridge enqueueJSCall:]", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTBatchedBridge enqueueJSCall:]", nil); NSArray *ids = [moduleDotMethod componentsSeparatedByString:@"."]; @@ -692,7 +692,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR } }]; - RCT_PROFILE_END_EVENT(0, @"", nil); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"", nil); } /** @@ -762,10 +762,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR return; } - RCT_PROFILE_BEGIN_EVENT(0, @"FetchApplicationScriptCallbacks", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"FetchApplicationScriptCallbacks", nil); [_javaScriptExecutor flushedQueue:^(id json, NSError *error) { - RCT_PROFILE_END_EVENT(0, @"js_call,init", @{ + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"js_call,init", @{ @"json": RCTNullIfNil(json), @"error": RCTNullIfNil(error), }); @@ -889,7 +889,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR dispatch_block_t block = ^{ RCTProfileEndFlowEvent(); - RCT_PROFILE_BEGIN_EVENT(0, @"-[RCTBatchedBridge handleBuffer:]", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTBatchedBridge handleBuffer:]", nil); NSOrderedSet *calls = [buckets objectForKey:queue]; @autoreleasepool { @@ -909,7 +909,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR } } - RCT_PROFILE_END_EVENT(0, @"objc_call,dispatch_async", @{ + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"objc_call,dispatch_async", @{ @"calls": @(calls.count), }); }; diff --git a/React/Base/RCTDisplayLink.m b/React/Base/RCTDisplayLink.m index 8c7c80820..7ad102dae 100644 --- a/React/Base/RCTDisplayLink.m +++ b/React/Base/RCTDisplayLink.m @@ -97,7 +97,7 @@ { [self assertOnRunLoop]; - RCT_PROFILE_BEGIN_EVENT(0, @"-[RCTDisplayLink _jsThreadUpdate:]", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTDisplayLink _jsThreadUpdate:]", nil); RCTFrameUpdate *frameUpdate = [[RCTFrameUpdate alloc] initWithDisplayLink:displayLink]; for (RCTModuleData *moduleData in _frameUpdateObservers) { @@ -114,9 +114,9 @@ [self updateJSDisplayLinkState]; - RCTProfileImmediateEvent(0, @"JS Thread Tick", displayLink.timestamp, 'g'); + RCTProfileImmediateEvent(RCTProfileTagAlways, @"JS Thread Tick", displayLink.timestamp, 'g'); - RCT_PROFILE_END_EVENT(0, @"objc_call", nil); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"objc_call", nil); } - (void)updateJSDisplayLinkState diff --git a/React/Base/RCTModuleData.m b/React/Base/RCTModuleData.m index f9afb5963..6ef07ae07 100644 --- a/React/Base/RCTModuleData.m +++ b/React/Base/RCTModuleData.m @@ -187,7 +187,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init); - (id)instance { if (!_setupComplete) { - RCT_PROFILE_BEGIN_EVENT(0, [NSString stringWithFormat:@"[RCTModuleData instanceForClass:%@]", _moduleClass], nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, [NSString stringWithFormat:@"[RCTModuleData instanceForClass:%@]", _moduleClass], nil); if (_requiresMainThreadSetup) { // The chances of deadlock here are low, because module init very rarely // calls out to other threads, however we can't control when a module might @@ -199,7 +199,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init); } else { [self setUpInstanceAndBridge]; } - RCT_PROFILE_END_EVENT(0, @"", nil); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"", nil); } return _instance; } diff --git a/React/Base/RCTPerformanceLogger.m b/React/Base/RCTPerformanceLogger.m index 05e6e418e..5223fc823 100644 --- a/React/Base/RCTPerformanceLogger.m +++ b/React/Base/RCTPerformanceLogger.m @@ -21,7 +21,7 @@ void RCTPerformanceLoggerStart(RCTPLTag tag) { if (RCTProfileIsProfiling()) { NSString *label = RCTPerformanceLoggerLabels()[tag]; - RCTPLCookies[tag] = RCTProfileBeginAsyncEvent(0, label, nil); + RCTPLCookies[tag] = RCTProfileBeginAsyncEvent(RCTProfileTagAlways, label, nil); } RCTPLData[tag][0] = CACurrentMediaTime() * 1000; @@ -35,7 +35,7 @@ void RCTPerformanceLoggerEnd(RCTPLTag tag) if (RCTProfileIsProfiling()) { NSString *label = RCTPerformanceLoggerLabels()[tag]; - RCTProfileEndAsyncEvent(0, @"native", RCTPLCookies[tag], label, @"RCTPerformanceLogger", nil); + RCTProfileEndAsyncEvent(RCTProfileTagAlways, @"native", RCTPLCookies[tag], label, @"RCTPerformanceLogger", nil); } } else { RCTLogInfo(@"Unbalanced calls start/end for tag %li", (unsigned long)tag); diff --git a/React/Base/RCTRootView.m b/React/Base/RCTRootView.m index 482ecf92d..30084e4ee 100644 --- a/React/Base/RCTRootView.m +++ b/React/Base/RCTRootView.m @@ -62,7 +62,7 @@ NSString *const RCTContentDidAppearNotification = @"RCTContentDidAppearNotificat RCTAssert(bridge, @"A bridge instance is required to create an RCTRootView"); RCTAssert(moduleName, @"A moduleName is required to create an RCTRootView"); - RCT_PROFILE_BEGIN_EVENT(0, @"-[RCTRootView init]", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTRootView init]", nil); if ((self = [super initWithFrame:CGRectZero])) { @@ -98,7 +98,7 @@ NSString *const RCTContentDidAppearNotification = @"RCTContentDidAppearNotificat [self showLoadingView]; } - RCT_PROFILE_END_EVENT(0, @"", nil); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"", nil); return self; } diff --git a/React/Executors/RCTJSCExecutor.mm b/React/Executors/RCTJSCExecutor.mm index 916858752..792e8b55a 100644 --- a/React/Executors/RCTJSCExecutor.mm +++ b/React/Executors/RCTJSCExecutor.mm @@ -322,10 +322,10 @@ static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context) return nil; } - RCT_PROFILE_BEGIN_EVENT(0, @"nativeRequireModuleConfig", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"nativeRequireModuleConfig", nil); NSArray *config = [strongSelf->_bridge configForModuleName:moduleName]; NSString *result = config ? RCTJSONStringify(config, NULL) : nil; - RCT_PROFILE_END_EVENT(0, @"js_call,config", @{ @"moduleName": moduleName }); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"js_call,config", @{ @"moduleName": moduleName }); return result; }]; @@ -335,9 +335,9 @@ static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context) return; } - RCT_PROFILE_BEGIN_EVENT(0, @"nativeFlushQueueImmediate", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"nativeFlushQueueImmediate", nil); [strongSelf->_bridge handleBuffer:calls batchEnded:NO]; - RCT_PROFILE_END_EVENT(0, @"js_call", nil); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"js_call", nil); }]; [self addSynchronousHookWithName:@"nativePerformanceNow" usingBlock:^{ @@ -736,7 +736,8 @@ static int readBundle(FILE *fd, size_t offset, size_t length, void *ptr) RCTPerformanceLoggerAdd(RCTPLRAMNativeRequiresCount, 1); RCTPerformanceLoggerAppendStart(RCTPLRAMNativeRequires); - RCT_PROFILE_BEGIN_EVENT(0, [@"nativeRequire_" stringByAppendingString:moduleName], nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, + [@"nativeRequire_" stringByAppendingString:moduleName], nil); ModuleData *data = (ModuleData *)CFDictionaryGetValue(strongSelf->_jsModules, moduleName.UTF8String); RCTPerformanceLoggerAdd(RCTPLRAMNativeRequiresSize, data->length); @@ -756,7 +757,7 @@ static int readBundle(FILE *fd, size_t offset, size_t length, void *ptr) JSStringRelease(code); JSStringRelease(sourceURL); - RCT_PROFILE_END_EVENT(0, @"js_call", nil); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"js_call", nil); RCTPerformanceLoggerAppendEnd(RCTPLRAMNativeRequires); if (!result) { diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index 4aef2983a..4b33bb91d 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -1114,7 +1114,7 @@ RCT_EXPORT_METHOD(dispatchViewManagerCommand:(nonnull NSNumber *)reactTag RCTProfileBeginFlowEvent(); dispatch_async(dispatch_get_main_queue(), ^{ RCTProfileEndFlowEvent(); - RCT_PROFILE_BEGIN_EVENT(0, @"-[UIManager flushUIBlocks]", nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[UIManager flushUIBlocks]", nil); @try { for (dispatch_block_t block in previousPendingUIBlocks) { block(); @@ -1123,7 +1123,7 @@ RCT_EXPORT_METHOD(dispatchViewManagerCommand:(nonnull NSNumber *)reactTag @catch (NSException *exception) { RCTLogError(@"Exception thrown while executing UI block: %@", exception); } - RCT_PROFILE_END_EVENT(0, @"objc_call", @{ + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"objc_call", @{ @"count": @(previousPendingUIBlocks.count), }); }); diff --git a/React/Profiler/RCTProfile.h b/React/Profiler/RCTProfile.h index e47728e2b..17a6001a1 100644 --- a/React/Profiler/RCTProfile.h +++ b/React/Profiler/RCTProfile.h @@ -24,6 +24,8 @@ RCT_EXTERN NSString *const RCTProfileDidStartProfiling; RCT_EXTERN NSString *const RCTProfileDidEndProfiling; +RCT_EXTERN const uint64_t RCTProfileTagAlways; + #if RCT_DEV @class RCTBridge; @@ -72,9 +74,7 @@ RCT_EXTERN void _RCTProfileBeginEvent(NSThread *calleeThread, if (RCTProfileIsProfiling()) { \ NSThread *__calleeThread = [NSThread currentThread]; \ NSTimeInterval __time = CACurrentMediaTime(); \ - dispatch_async(RCTProfileGetQueue(), ^{ \ - _RCTProfileBeginEvent(__calleeThread, __time, __VA_ARGS__); \ - }); \ + _RCTProfileBeginEvent(__calleeThread, __time, __VA_ARGS__); \ } \ } while(0) @@ -96,9 +96,7 @@ RCT_EXTERN void _RCTProfileEndEvent(NSThread *calleeThread, NSThread *__calleeThread = [NSThread currentThread]; \ NSString *__threadName = RCTCurrentThreadName(); \ NSTimeInterval __time = CACurrentMediaTime(); \ - dispatch_async(RCTProfileGetQueue(), ^{ \ - _RCTProfileEndEvent(__calleeThread, __threadName, __time, __VA_ARGS__); \ - }); \ + _RCTProfileEndEvent(__calleeThread, __threadName, __time, __VA_ARGS__); \ } \ } while(0) diff --git a/React/Profiler/RCTProfile.m b/React/Profiler/RCTProfile.m index 56ba31dc8..067949b4b 100644 --- a/React/Profiler/RCTProfile.m +++ b/React/Profiler/RCTProfile.m @@ -32,6 +32,8 @@ NSString *const RCTProfileDidStartProfiling = @"RCTProfileDidStartProfiling"; NSString *const RCTProfileDidEndProfiling = @"RCTProfileDidEndProfiling"; +const uint64_t RCTProfileTagAlways = 1L << 0; + #if RCT_DEV #pragma mark - Constants @@ -205,13 +207,13 @@ void RCTProfileTrampolineStart(id self, SEL cmd) * block. */ Class klass = [self class]; - RCT_PROFILE_BEGIN_EVENT(0, [NSString stringWithFormat:@"-[%s %s]", class_getName(klass), sel_getName(cmd)], nil); + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, [NSString stringWithFormat:@"-[%s %s]", class_getName(klass), sel_getName(cmd)], nil); } RCT_EXTERN void RCTProfileTrampolineEnd(void); void RCTProfileTrampolineEnd(void) { - RCT_PROFILE_END_EVENT(0, @"objc_call,modules,auto", nil); + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"objc_call,modules,auto", nil); } static UIView *(*originalCreateView)(RCTComponentData *, SEL, NSNumber *); @@ -370,7 +372,7 @@ void RCTProfileUnhookModules(RCTBridge *bridge) + (void)vsync:(CADisplayLink *)displayLink { - RCTProfileImmediateEvent(0, @"VSYNC", displayLink.timestamp, 'g'); + RCTProfileImmediateEvent(RCTProfileTagAlways, @"VSYNC", displayLink.timestamp, 'g'); } + (void)reload @@ -543,22 +545,21 @@ void _RCTProfileBeginEvent( NSString *name, NSDictionary *args ) { - CHECK(); - RCTAssertThread(RCTProfileGetQueue(), @"Must be called RCTProfile queue");; - if (callbacks != NULL) { callbacks->begin_section(tag, name.UTF8String, args.count, RCTProfileSystraceArgsFromNSDictionary(args)); return; } - NSMutableArray *events = RCTProfileGetThreadEvents(calleeThread); - [events addObject:@[ - RCTProfileTimestamp(time), - name, - RCTNullIfNil(args), - ]]; + dispatch_async(RCTProfileGetQueue(), ^{ + NSMutableArray *events = RCTProfileGetThreadEvents(calleeThread); + [events addObject:@[ + RCTProfileTimestamp(time), + name, + RCTNullIfNil(args), + ]]; + }); } void _RCTProfileEndEvent( @@ -571,32 +572,32 @@ void _RCTProfileEndEvent( ) { CHECK(); - RCTAssertThread(RCTProfileGetQueue(), @"Must be called RCTProfile queue");; - if (callbacks != NULL) { callbacks->end_section(tag, args.count, RCTProfileSystraceArgsFromNSDictionary(args)); return; } - NSMutableArray *events = RCTProfileGetThreadEvents(calleeThread); - NSArray *event = events.lastObject; - [events removeLastObject]; + dispatch_async(RCTProfileGetQueue(), ^{ + NSMutableArray *events = RCTProfileGetThreadEvents(calleeThread); + NSArray *event = events.lastObject; + [events removeLastObject]; - if (!event) { - return; - } + if (!event) { + return; + } - NSNumber *start = event[0]; + NSNumber *start = event[0]; - RCTProfileAddEvent(RCTProfileTraceEvents, - @"tid": threadName, - @"name": event[1], - @"cat": category, - @"ph": @"X", - @"ts": start, - @"dur": @(RCTProfileTimestamp(time).doubleValue - start.doubleValue), - @"args": RCTProfileMergeArgs(event[2], args), - ); + RCTProfileAddEvent(RCTProfileTraceEvents, + @"tid": threadName, + @"name": event[1], + @"cat": category, + @"ph": @"X", + @"ts": start, + @"dur": @(RCTProfileTimestamp(time).doubleValue - start.doubleValue), + @"args": RCTProfileMergeArgs(event[2], args), + ); + }); } NSUInteger RCTProfileBeginAsyncEvent(