Skip to content

Commit

Permalink
Expose flow events to JS + add JS -> Native flows
Browse files Browse the repository at this point in the history
Summary:
public

Expose JS hooks to create flow events in systrace (the nice arrows to show async work flow) +
add support to the showing all the work enqueued from the JS thread as added in D2743733

Depends on D2743733

Reviewed By: jspahrsummers

Differential Revision: D2773664

fb-gh-sync-id: 4a8854b17b4741b882f5f2cc425e4237a5e4b3eb
  • Loading branch information
tadeuzagallo authored and facebook-github-bot-9 committed Jan 4, 2016
1 parent ed4478a commit 89ae0e0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
29 changes: 25 additions & 4 deletions React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
typedef NS_ENUM(NSUInteger, RCTBridgeFields) {
RCTBridgeFieldRequestModuleIDs = 0,
RCTBridgeFieldMethodIDs,
RCTBridgeFieldParamss,
RCTBridgeFieldParams,
RCTBridgeFieldCallID,
};

RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
Expand Down Expand Up @@ -65,6 +66,9 @@ @implementation RCTBatchedBridge
NSUInteger _asyncInitializedModules;
}

@synthesize flowID = _flowID;
@synthesize flowIDMap = _flowIDMap;

- (instancetype)initWithParentBridge:(RCTBridge *)bridge
{
RCTAssertMainThread();
Expand Down Expand Up @@ -591,6 +595,9 @@ - (void)invalidate
_modulesByName_DEPRECATED = nil;
_frameUpdateObservers = nil;

if (_flowIDMap != NULL) {
CFRelease(_flowIDMap);
}
}];
});
}
Expand Down Expand Up @@ -793,15 +800,22 @@ - (void)handleBuffer:(id)buffer batchEnded:(BOOL)batchEnded
- (void)handleBuffer:(NSArray *)buffer
{
NSArray *requestsArray = [RCTConvert NSArray:buffer];
if (RCT_DEBUG && requestsArray.count <= RCTBridgeFieldParamss) {

if (RCT_DEBUG && requestsArray.count <= RCTBridgeFieldParams) {
RCTLogError(@"Buffer should contain at least %tu sub-arrays. Only found %tu",
RCTBridgeFieldParamss + 1, requestsArray.count);
RCTBridgeFieldParams + 1, requestsArray.count);
return;
}

NSArray<NSNumber *> *moduleIDs = [RCTConvert NSNumberArray:requestsArray[RCTBridgeFieldRequestModuleIDs]];
NSArray<NSNumber *> *methodIDs = [RCTConvert NSNumberArray:requestsArray[RCTBridgeFieldMethodIDs]];
NSArray<NSArray *> *paramsArrays = [RCTConvert NSArrayArray:requestsArray[RCTBridgeFieldParamss]];
NSArray<NSArray *> *paramsArrays = [RCTConvert NSArrayArray:requestsArray[RCTBridgeFieldParams]];

int64_t callID = -1;

if (requestsArray.count > 3) {
callID = [requestsArray[RCTBridgeFieldCallID] longLongValue];
}

if (RCT_DEBUG && (moduleIDs.count != methodIDs.count || moduleIDs.count != paramsArrays.count)) {
RCTLogError(@"Invalid data message - all must be length: %zd", moduleIDs.count);
Expand Down Expand Up @@ -835,6 +849,11 @@ - (void)handleBuffer:(NSArray *)buffer
@autoreleasepool {
for (NSNumber *indexObj in calls) {
NSUInteger index = indexObj.unsignedIntegerValue;
if (callID != -1) {
int64_t newFlowID = (int64_t)CFDictionaryGetValue(_flowIDMap, (const void *)(_flowID + index));
_RCTProfileEndFlowEvent(@(newFlowID));
CFDictionaryRemoveValue(_flowIDMap, (const void *)(_flowID + index));
}
[self _handleRequestNumber:index
moduleID:[moduleIDs[index] integerValue]
methodID:[methodIDs[index] integerValue]
Expand All @@ -853,6 +872,8 @@ - (void)handleBuffer:(NSArray *)buffer
dispatch_async(queue, block);
}
}

_flowID = callID;
}

- (void)partialBatchDidFlush
Expand Down
4 changes: 4 additions & 0 deletions React/Base/RCTBridge+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

@interface RCTBridge ()

// Used for the profiler flow events between JS and native
@property (nonatomic, assign) int64_t flowID;
@property (nonatomic, assign) CFMutableDictionaryRef flowIDMap;

+ (instancetype)currentBridge;
+ (void)setCurrentBridge:(RCTBridge *)bridge;

Expand Down
14 changes: 14 additions & 0 deletions React/Executors/RCTJSCExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,20 @@ - (void)setUp
CFDictionaryRemoveValue(cookieMap, (const void *)cookie);
};

#ifndef __clang_analyzer__
weakBridge.flowIDMap = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
#endif
context[@"nativeTraceBeginAsyncFlow"] = ^(__unused uint64_t tag, __unused NSString *name, int64_t cookie) {
int64_t newCookie = [_RCTProfileBeginFlowEvent() longLongValue];
CFDictionarySetValue(weakBridge.flowIDMap, (const void *)cookie, (const void *)newCookie);
};

context[@"nativeTraceEndAsyncFlow"] = ^(__unused uint64_t tag, __unused NSString *name, int64_t cookie) {
int64_t newCookie = (int64_t)CFDictionaryGetValue(weakBridge.flowIDMap, (const void *)cookie);
_RCTProfileEndFlowEvent(@(newCookie));
CFDictionaryRemoveValue(weakBridge.flowIDMap, (const void *)cookie);
};

context[@"nativeTraceBeginSection"] = ^(NSNumber *tag, NSString *profileName){
static int profileCounter = 1;
if (!profileName) {
Expand Down
2 changes: 1 addition & 1 deletion React/Profiler/RCTProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ RCT_EXTERN void RCTProfileRegisterCallbacks(RCTProfileCallbacks *);
#define _RCTProfileBeginFlowEvent() @0

#define RCTProfileEndFlowEvent()
#define _RCTProfileEndFlowEvent()
#define _RCTProfileEndFlowEvent(...)

#define RCTProfileIsProfiling(...) NO
#define RCTProfileInit(...)
Expand Down

0 comments on commit 89ae0e0

Please sign in to comment.