From b6f5c7fa041fb5e13b07876beed117d8a4de0d17 Mon Sep 17 00:00:00 2001 From: Mike Armstrong Date: Tue, 1 Dec 2015 07:37:52 -0800 Subject: [PATCH] Fix systrace profile handling for relay async calls Reviewed By: astreet Differential Revision: D2700239 fb-gh-sync-id: eaa29d63ee4f7688dd70b0cdc12564a9d479f9ef --- Libraries/Utilities/BridgeProfiling.js | 36 ++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Libraries/Utilities/BridgeProfiling.js b/Libraries/Utilities/BridgeProfiling.js index 526775b20..8e60354f2 100644 --- a/Libraries/Utilities/BridgeProfiling.js +++ b/Libraries/Utilities/BridgeProfiling.js @@ -22,6 +22,7 @@ var GLOBAL = GLOBAL || this; var TRACE_TAG_REACT_APPS = 1 << 17; var _enabled; +var _asyncCookie = 0; var _ReactPerf = null; function ReactPerf() { if (!_ReactPerf) { @@ -37,6 +38,9 @@ var BridgeProfiling = { ReactPerf().enableMeasure = enabled; }, + /** + * profile/profileEnd for starting and then ending a profile within the same call stack frame + **/ profile(profileName?: any) { if (_enabled) { profileName = typeof profileName === 'function' ? @@ -51,6 +55,30 @@ var BridgeProfiling = { } }, + /** + * profileAsync/profileAsyncEnd for starting and then ending a profile where the end can either + * occur on another thread or out of the current stack frame, eg await + * the returned cookie variable should be used as input into the asyncEnd call to end the profile + **/ + profileAsync(profileName?: any): any { + var cookie = _asyncCookie; + if (_enabled) { + _asyncCookie++; + profileName = typeof profileName === 'function' ? + profileName() : profileName; + global.nativeTraceBeginAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0); + } + return cookie; + }, + + profileAsyncEnd(profileName?: any, cookie?: any) { + if (_enabled) { + profileName = typeof profileName === 'function' ? + profileName() : profileName; + global.nativeTraceEndAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0); + } + }, + reactPerfMeasure(objName: string, fnName: string, func: any): any { return function (component) { if (!_enabled) { @@ -69,11 +97,15 @@ var BridgeProfiling = { ReactPerf().injection.injectMeasure(BridgeProfiling.reactPerfMeasure); }, + /** + * Relay profiles use await calls, so likely occur out of current stack frame + * therefore async variant of profiling is used + **/ attachToRelayProfiler(relayProfiler: RelayProfiler) { relayProfiler.attachProfileHandler('*', (name) => { - BridgeProfiling.profile(name); + var cookie = BridgeProfiling.profileAsync(name); return () => { - BridgeProfiling.profileEnd(); + BridgeProfiling.profileAsyncEnd(name, cookie); }; }); },