Skip to content

Commit 89ae0e0

Browse files
tadeuzagallofacebook-github-bot-9
authored andcommitted
Expose flow events to JS + add JS -> Native flows
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
1 parent ed4478a commit 89ae0e0

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

React/Base/RCTBatchedBridge.m

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
typedef NS_ENUM(NSUInteger, RCTBridgeFields) {
3636
RCTBridgeFieldRequestModuleIDs = 0,
3737
RCTBridgeFieldMethodIDs,
38-
RCTBridgeFieldParamss,
38+
RCTBridgeFieldParams,
39+
RCTBridgeFieldCallID,
3940
};
4041

4142
RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
@@ -65,6 +66,9 @@ @implementation RCTBatchedBridge
6566
NSUInteger _asyncInitializedModules;
6667
}
6768

69+
@synthesize flowID = _flowID;
70+
@synthesize flowIDMap = _flowIDMap;
71+
6872
- (instancetype)initWithParentBridge:(RCTBridge *)bridge
6973
{
7074
RCTAssertMainThread();
@@ -591,6 +595,9 @@ - (void)invalidate
591595
_modulesByName_DEPRECATED = nil;
592596
_frameUpdateObservers = nil;
593597

598+
if (_flowIDMap != NULL) {
599+
CFRelease(_flowIDMap);
600+
}
594601
}];
595602
});
596603
}
@@ -793,15 +800,22 @@ - (void)handleBuffer:(id)buffer batchEnded:(BOOL)batchEnded
793800
- (void)handleBuffer:(NSArray *)buffer
794801
{
795802
NSArray *requestsArray = [RCTConvert NSArray:buffer];
796-
if (RCT_DEBUG && requestsArray.count <= RCTBridgeFieldParamss) {
803+
804+
if (RCT_DEBUG && requestsArray.count <= RCTBridgeFieldParams) {
797805
RCTLogError(@"Buffer should contain at least %tu sub-arrays. Only found %tu",
798-
RCTBridgeFieldParamss + 1, requestsArray.count);
806+
RCTBridgeFieldParams + 1, requestsArray.count);
799807
return;
800808
}
801809

802810
NSArray<NSNumber *> *moduleIDs = [RCTConvert NSNumberArray:requestsArray[RCTBridgeFieldRequestModuleIDs]];
803811
NSArray<NSNumber *> *methodIDs = [RCTConvert NSNumberArray:requestsArray[RCTBridgeFieldMethodIDs]];
804-
NSArray<NSArray *> *paramsArrays = [RCTConvert NSArrayArray:requestsArray[RCTBridgeFieldParamss]];
812+
NSArray<NSArray *> *paramsArrays = [RCTConvert NSArrayArray:requestsArray[RCTBridgeFieldParams]];
813+
814+
int64_t callID = -1;
815+
816+
if (requestsArray.count > 3) {
817+
callID = [requestsArray[RCTBridgeFieldCallID] longLongValue];
818+
}
805819

806820
if (RCT_DEBUG && (moduleIDs.count != methodIDs.count || moduleIDs.count != paramsArrays.count)) {
807821
RCTLogError(@"Invalid data message - all must be length: %zd", moduleIDs.count);
@@ -835,6 +849,11 @@ - (void)handleBuffer:(NSArray *)buffer
835849
@autoreleasepool {
836850
for (NSNumber *indexObj in calls) {
837851
NSUInteger index = indexObj.unsignedIntegerValue;
852+
if (callID != -1) {
853+
int64_t newFlowID = (int64_t)CFDictionaryGetValue(_flowIDMap, (const void *)(_flowID + index));
854+
_RCTProfileEndFlowEvent(@(newFlowID));
855+
CFDictionaryRemoveValue(_flowIDMap, (const void *)(_flowID + index));
856+
}
838857
[self _handleRequestNumber:index
839858
moduleID:[moduleIDs[index] integerValue]
840859
methodID:[methodIDs[index] integerValue]
@@ -853,6 +872,8 @@ - (void)handleBuffer:(NSArray *)buffer
853872
dispatch_async(queue, block);
854873
}
855874
}
875+
876+
_flowID = callID;
856877
}
857878

858879
- (void)partialBatchDidFlush

React/Base/RCTBridge+Private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
@interface RCTBridge ()
1515

16+
// Used for the profiler flow events between JS and native
17+
@property (nonatomic, assign) int64_t flowID;
18+
@property (nonatomic, assign) CFMutableDictionaryRef flowIDMap;
19+
1620
+ (instancetype)currentBridge;
1721
+ (void)setCurrentBridge:(RCTBridge *)bridge;
1822

React/Executors/RCTJSCExecutor.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@ - (void)setUp
283283
CFDictionaryRemoveValue(cookieMap, (const void *)cookie);
284284
};
285285

286+
#ifndef __clang_analyzer__
287+
weakBridge.flowIDMap = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
288+
#endif
289+
context[@"nativeTraceBeginAsyncFlow"] = ^(__unused uint64_t tag, __unused NSString *name, int64_t cookie) {
290+
int64_t newCookie = [_RCTProfileBeginFlowEvent() longLongValue];
291+
CFDictionarySetValue(weakBridge.flowIDMap, (const void *)cookie, (const void *)newCookie);
292+
};
293+
294+
context[@"nativeTraceEndAsyncFlow"] = ^(__unused uint64_t tag, __unused NSString *name, int64_t cookie) {
295+
int64_t newCookie = (int64_t)CFDictionaryGetValue(weakBridge.flowIDMap, (const void *)cookie);
296+
_RCTProfileEndFlowEvent(@(newCookie));
297+
CFDictionaryRemoveValue(weakBridge.flowIDMap, (const void *)cookie);
298+
};
299+
286300
context[@"nativeTraceBeginSection"] = ^(NSNumber *tag, NSString *profileName){
287301
static int profileCounter = 1;
288302
if (!profileName) {

React/Profiler/RCTProfile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ RCT_EXTERN void RCTProfileRegisterCallbacks(RCTProfileCallbacks *);
190190
#define _RCTProfileBeginFlowEvent() @0
191191

192192
#define RCTProfileEndFlowEvent()
193-
#define _RCTProfileEndFlowEvent()
193+
#define _RCTProfileEndFlowEvent(...)
194194

195195
#define RCTProfileIsProfiling(...) NO
196196
#define RCTProfileInit(...)

0 commit comments

Comments
 (0)