2015-06-02 13:15:53 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-12-11 11:49:15 +00:00
|
|
|
* @providesModule Systrace
|
2015-06-02 13:15:53 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-08 21:30:46 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
|
|
|
|
2015-11-24 03:22:34 +00:00
|
|
|
type RelayProfiler = {
|
|
|
|
attachProfileHandler(
|
|
|
|
name: string,
|
|
|
|
handler: (name: string, state?: any) => () => void
|
2015-12-11 19:38:19 +00:00
|
|
|
): void,
|
|
|
|
|
|
|
|
attachAggregateHandler(
|
|
|
|
name: string,
|
|
|
|
handler: (name: string, callback: () => void) => void
|
|
|
|
): void,
|
2015-11-24 03:22:34 +00:00
|
|
|
};
|
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
/* eslint no-bitwise: 0 */
|
|
|
|
const TRACE_TAG_REACT_APPS = 1 << 17;
|
|
|
|
const TRACE_TAG_JSC_CALLS = 1 << 27;
|
2016-05-10 22:57:19 +00:00
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
let _enabled = false;
|
|
|
|
let _asyncCookie = 0;
|
2017-06-08 21:30:46 +00:00
|
|
|
const _markStack = [];
|
|
|
|
let _markStackIndex = -1;
|
2017-08-10 11:09:52 +00:00
|
|
|
let _canInstallReactHook = false;
|
|
|
|
let _useFiber = false;
|
2016-05-23 11:51:25 +00:00
|
|
|
|
2017-06-08 21:30:46 +00:00
|
|
|
// Implements a subset of User Timing API necessary for React measurements.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API
|
|
|
|
const REACT_MARKER = '\u269B';
|
2017-08-14 14:10:27 +00:00
|
|
|
const userTimingPolyfill = __DEV__ ? {
|
2017-06-08 21:30:46 +00:00
|
|
|
mark(markName: string) {
|
2017-08-14 14:10:27 +00:00
|
|
|
if (_enabled) {
|
|
|
|
_markStackIndex++;
|
|
|
|
_markStack[_markStackIndex] = markName;
|
|
|
|
let systraceLabel = markName;
|
|
|
|
// Since perf measurements are a shared namespace in User Timing API,
|
|
|
|
// we prefix all React results with a React emoji.
|
|
|
|
if (markName[0] === REACT_MARKER) {
|
|
|
|
// This is coming from React.
|
|
|
|
// Removing component IDs keeps trace colors stable.
|
|
|
|
const indexOfId = markName.lastIndexOf(' (#');
|
|
|
|
const cutoffIndex = indexOfId !== -1 ? indexOfId : markName.length;
|
|
|
|
// Also cut off the emoji because it breaks Systrace
|
|
|
|
systraceLabel = markName.slice(2, cutoffIndex);
|
2017-06-08 21:30:46 +00:00
|
|
|
}
|
2017-08-14 14:10:27 +00:00
|
|
|
Systrace.beginEvent(systraceLabel);
|
2017-06-08 21:30:46 +00:00
|
|
|
}
|
2016-08-18 22:46:02 +00:00
|
|
|
},
|
2017-06-08 21:30:46 +00:00
|
|
|
measure(measureName: string, startMark: ?string, endMark: ?string) {
|
2017-08-14 14:10:27 +00:00
|
|
|
if (_enabled) {
|
|
|
|
invariant(
|
|
|
|
typeof measureName === 'string' &&
|
|
|
|
typeof startMark === 'string' &&
|
|
|
|
typeof endMark === 'undefined',
|
|
|
|
'Only performance.measure(string, string) overload is supported.'
|
|
|
|
);
|
|
|
|
const topMark = _markStack[_markStackIndex];
|
|
|
|
invariant(
|
|
|
|
startMark === topMark,
|
|
|
|
'There was a mismatching performance.measure() call. ' +
|
|
|
|
'Expected "%s" but got "%s."',
|
|
|
|
topMark,
|
|
|
|
startMark,
|
|
|
|
);
|
|
|
|
_markStackIndex--;
|
|
|
|
// We can't use more descriptive measureName because Systrace doesn't
|
|
|
|
// let us edit labels post factum.
|
|
|
|
Systrace.endEvent();
|
2017-06-08 21:30:46 +00:00
|
|
|
}
|
2016-05-10 22:57:19 +00:00
|
|
|
},
|
2017-06-08 21:30:46 +00:00
|
|
|
clearMarks(markName: string) {
|
2017-08-14 14:10:27 +00:00
|
|
|
if (_enabled) {
|
|
|
|
if (_markStackIndex === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (markName === _markStack[_markStackIndex]) {
|
|
|
|
// React uses this for "cancelling" started measurements.
|
|
|
|
// Systrace doesn't support deleting measurements, so we just stop them.
|
|
|
|
if (userTimingPolyfill != null) {
|
2017-06-08 21:30:46 +00:00
|
|
|
userTimingPolyfill.measure(markName, markName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 22:57:19 +00:00
|
|
|
},
|
2017-06-08 21:30:46 +00:00
|
|
|
clearMeasures() {
|
|
|
|
// React calls this to avoid memory leaks in browsers, but we don't keep
|
|
|
|
// measurements anyway.
|
2016-05-10 22:57:19 +00:00
|
|
|
},
|
2017-08-14 14:10:27 +00:00
|
|
|
} : null;
|
2015-10-01 21:08:13 +00:00
|
|
|
|
2017-08-10 11:09:52 +00:00
|
|
|
// A hook to get React Stack markers in Systrace.
|
2017-08-14 14:10:27 +00:00
|
|
|
const reactDebugToolHook = __DEV__ ? {
|
2017-08-10 11:09:52 +00:00
|
|
|
onBeforeMountComponent(debugID) {
|
|
|
|
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
|
|
|
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
|
|
|
Systrace.beginEvent(`ReactReconciler.mountComponent(${displayName})`);
|
|
|
|
},
|
|
|
|
onMountComponent(debugID) {
|
|
|
|
Systrace.endEvent();
|
|
|
|
},
|
|
|
|
onBeforeUpdateComponent(debugID) {
|
|
|
|
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
|
|
|
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
|
|
|
Systrace.beginEvent(`ReactReconciler.updateComponent(${displayName})`);
|
|
|
|
},
|
|
|
|
onUpdateComponent(debugID) {
|
|
|
|
Systrace.endEvent();
|
|
|
|
},
|
|
|
|
onBeforeUnmountComponent(debugID) {
|
|
|
|
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
|
|
|
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
|
|
|
Systrace.beginEvent(`ReactReconciler.unmountComponent(${displayName})`);
|
|
|
|
},
|
|
|
|
onUnmountComponent(debugID) {
|
|
|
|
Systrace.endEvent();
|
|
|
|
},
|
|
|
|
onBeginLifeCycleTimer(debugID, timerType) {
|
|
|
|
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
|
|
|
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
|
|
|
Systrace.beginEvent(`${displayName}.${timerType}()`);
|
|
|
|
},
|
|
|
|
onEndLifeCycleTimer(debugID, timerType) {
|
|
|
|
Systrace.endEvent();
|
|
|
|
},
|
2017-08-14 14:10:27 +00:00
|
|
|
} : null;
|
2017-08-10 11:09:52 +00:00
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
const Systrace = {
|
2017-08-10 11:09:52 +00:00
|
|
|
installReactHook(useFiber: boolean) {
|
|
|
|
if (_enabled) {
|
2017-08-14 14:10:27 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
if (useFiber) {
|
|
|
|
global.performance = userTimingPolyfill;
|
|
|
|
} else {
|
|
|
|
require('ReactDebugTool').addHook(reactDebugToolHook);
|
|
|
|
}
|
2017-08-10 11:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_useFiber = useFiber;
|
|
|
|
_canInstallReactHook = true;
|
2017-06-08 21:30:46 +00:00
|
|
|
},
|
|
|
|
|
2015-10-01 21:08:13 +00:00
|
|
|
setEnabled(enabled: boolean) {
|
2015-12-10 12:36:23 +00:00
|
|
|
if (_enabled !== enabled) {
|
2016-05-23 11:51:25 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
if (enabled) {
|
|
|
|
global.nativeTraceBeginLegacy && global.nativeTraceBeginLegacy(TRACE_TAG_JSC_CALLS);
|
|
|
|
} else {
|
|
|
|
global.nativeTraceEndLegacy && global.nativeTraceEndLegacy(TRACE_TAG_JSC_CALLS);
|
|
|
|
}
|
2017-08-10 11:09:52 +00:00
|
|
|
if (_canInstallReactHook) {
|
|
|
|
if (_useFiber) {
|
|
|
|
global.performance = enabled ? userTimingPolyfill : undefined;
|
|
|
|
} else {
|
|
|
|
const ReactDebugTool = require('ReactDebugTool');
|
|
|
|
if (enabled) {
|
|
|
|
ReactDebugTool.addHook(reactDebugToolHook);
|
|
|
|
} else {
|
|
|
|
ReactDebugTool.removeHook(reactDebugToolHook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 12:36:23 +00:00
|
|
|
}
|
2016-05-23 11:51:25 +00:00
|
|
|
_enabled = enabled;
|
2015-12-10 12:36:23 +00:00
|
|
|
}
|
2015-10-01 21:08:13 +00:00
|
|
|
},
|
|
|
|
|
2017-05-09 20:19:15 +00:00
|
|
|
isEnabled(): boolean {
|
|
|
|
return _enabled;
|
|
|
|
},
|
|
|
|
|
2015-12-01 15:37:52 +00:00
|
|
|
/**
|
2015-12-11 11:49:15 +00:00
|
|
|
* beginEvent/endEvent for starting and then ending a profile within the same call stack frame
|
2015-12-01 15:37:52 +00:00
|
|
|
**/
|
2016-06-13 11:18:33 +00:00
|
|
|
beginEvent(profileName?: any, args?: any) {
|
2015-10-01 21:08:13 +00:00
|
|
|
if (_enabled) {
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
2016-06-13 11:18:33 +00:00
|
|
|
global.nativeTraceBeginSection(TRACE_TAG_REACT_APPS, profileName, args);
|
2015-06-02 13:15:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
endEvent() {
|
2015-10-01 21:08:13 +00:00
|
|
|
if (_enabled) {
|
2015-10-31 23:41:27 +00:00
|
|
|
global.nativeTraceEndSection(TRACE_TAG_REACT_APPS);
|
2015-06-02 13:15:53 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-15 20:05:05 +00:00
|
|
|
|
2015-12-01 15:37:52 +00:00
|
|
|
/**
|
2015-12-11 11:49:15 +00:00
|
|
|
* beginAsyncEvent/endAsyncEvent for starting and then ending a profile where the end can either
|
2015-12-01 15:37:52 +00:00
|
|
|
* occur on another thread or out of the current stack frame, eg await
|
2015-12-11 11:49:15 +00:00
|
|
|
* the returned cookie variable should be used as input into the endAsyncEvent call to end the profile
|
2015-12-01 15:37:52 +00:00
|
|
|
**/
|
2015-12-11 11:49:15 +00:00
|
|
|
beginAsyncEvent(profileName?: any): any {
|
2016-05-23 11:51:25 +00:00
|
|
|
const cookie = _asyncCookie;
|
2015-12-01 15:37:52 +00:00
|
|
|
if (_enabled) {
|
|
|
|
_asyncCookie++;
|
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
|
|
|
global.nativeTraceBeginAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0);
|
|
|
|
}
|
|
|
|
return cookie;
|
|
|
|
},
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
endAsyncEvent(profileName?: any, cookie?: any) {
|
2015-12-01 15:37:52 +00:00
|
|
|
if (_enabled) {
|
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
|
|
|
global.nativeTraceEndAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-30 07:36:34 +00:00
|
|
|
/**
|
|
|
|
* counterEvent registers the value to the profileName on the systrace timeline
|
|
|
|
**/
|
|
|
|
counterEvent(profileName?: any, value?: any) {
|
|
|
|
if (_enabled) {
|
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
|
|
|
global.nativeTraceCounter &&
|
|
|
|
global.nativeTraceCounter(TRACE_TAG_REACT_APPS, profileName, value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-01 15:37:52 +00:00
|
|
|
/**
|
|
|
|
* Relay profiles use await calls, so likely occur out of current stack frame
|
|
|
|
* therefore async variant of profiling is used
|
|
|
|
**/
|
2015-11-24 03:22:34 +00:00
|
|
|
attachToRelayProfiler(relayProfiler: RelayProfiler) {
|
|
|
|
relayProfiler.attachProfileHandler('*', (name) => {
|
2016-05-23 11:51:25 +00:00
|
|
|
const cookie = Systrace.beginAsyncEvent(name);
|
2015-11-24 03:22:34 +00:00
|
|
|
return () => {
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.endAsyncEvent(name, cookie);
|
2015-11-24 03:22:34 +00:00
|
|
|
};
|
|
|
|
});
|
2015-12-11 19:38:19 +00:00
|
|
|
|
|
|
|
relayProfiler.attachAggregateHandler('*', (name, callback) => {
|
|
|
|
Systrace.beginEvent(name);
|
|
|
|
callback();
|
|
|
|
Systrace.endEvent();
|
|
|
|
});
|
2015-11-23 15:01:06 +00:00
|
|
|
},
|
|
|
|
|
2015-11-23 15:01:11 +00:00
|
|
|
/* This is not called by default due to perf overhead but it's useful
|
|
|
|
if you want to find traces which spend too much time in JSON. */
|
|
|
|
swizzleJSON() {
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.measureMethods(JSON, 'JSON', [
|
2015-11-23 15:01:11 +00:00
|
|
|
'parse',
|
|
|
|
'stringify'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
|
2015-11-23 15:01:06 +00:00
|
|
|
/**
|
|
|
|
* Measures multiple methods of a class. For example, you can do:
|
2015-12-11 11:49:15 +00:00
|
|
|
* Systrace.measureMethods(JSON, 'JSON', ['parse', 'stringify']);
|
2015-11-23 15:01:06 +00:00
|
|
|
*
|
|
|
|
* @param object
|
|
|
|
* @param objectName
|
|
|
|
* @param methodNames Map from method names to method display names.
|
|
|
|
*/
|
|
|
|
measureMethods(object: any, objectName: string, methodNames: Array<string>): void {
|
|
|
|
if (!__DEV__) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
methodNames.forEach(methodName => {
|
2015-12-11 11:49:15 +00:00
|
|
|
object[methodName] = Systrace.measure(
|
2015-11-23 15:01:06 +00:00
|
|
|
objectName,
|
|
|
|
methodName,
|
|
|
|
object[methodName]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an profiled version of the input function. For example, you can:
|
2015-12-11 11:49:15 +00:00
|
|
|
* JSON.parse = Systrace.measure('JSON', 'parse', JSON.parse);
|
2015-11-23 15:01:06 +00:00
|
|
|
*
|
|
|
|
* @param objName
|
|
|
|
* @param fnName
|
|
|
|
* @param {function} func
|
|
|
|
* @return {function} replacement function
|
|
|
|
*/
|
|
|
|
measure(objName: string, fnName: string, func: any): any {
|
|
|
|
if (!__DEV__) {
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
const profileName = `${objName}.${fnName}`;
|
2015-11-23 15:01:06 +00:00
|
|
|
return function() {
|
|
|
|
if (!_enabled) {
|
|
|
|
return func.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.beginEvent(profileName);
|
2016-05-23 11:51:25 +00:00
|
|
|
const ret = func.apply(this, arguments);
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.endEvent();
|
2015-11-23 15:01:06 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
},
|
2015-06-02 13:15:53 +00:00
|
|
|
};
|
|
|
|
|
2016-03-14 23:16:22 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
// This is needed, because require callis in polyfills are not processed as
|
|
|
|
// other files. Therefore, calls to `require('moduleId')` are not replaced
|
|
|
|
// with numeric IDs
|
|
|
|
// TODO(davidaurelio) Scan polyfills for dependencies, too (t9759686)
|
2016-12-14 02:57:42 +00:00
|
|
|
(require: any).Systrace = Systrace;
|
2016-03-14 23:16:22 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
module.exports = Systrace;
|