2015-06-02 13:15:53 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-06-02 13:15:53 +00:00
|
|
|
*
|
2015-12-11 11:49:15 +00:00
|
|
|
* @providesModule Systrace
|
2015-06-02 13:15:53 +00:00
|
|
|
* @flow
|
2018-03-09 07:33:06 +00:00
|
|
|
* @format
|
2015-06-02 13:15:53 +00:00
|
|
|
*/
|
2018-03-09 07:33:06 +00:00
|
|
|
|
2015-06-02 13:15:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-08 21:30:46 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
|
|
|
|
2018-03-09 07:33:06 +00:00
|
|
|
const TRACE_TAG_REACT_APPS = 1 << 17; // eslint-disable-line no-bitwise
|
|
|
|
const TRACE_TAG_JS_VM_CALLS = 1 << 27; // eslint-disable-line no-bitwise
|
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;
|
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';
|
2018-03-09 07:33:06 +00:00
|
|
|
const userTimingPolyfill = __DEV__
|
|
|
|
? {
|
|
|
|
mark(markName: string) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
Systrace.beginEvent(systraceLabel);
|
2017-06-08 21:30:46 +00:00
|
|
|
}
|
2018-03-09 07:33:06 +00:00
|
|
|
},
|
|
|
|
measure(measureName: string, startMark: ?string, endMark: ?string) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
clearMarks(markName: string) {
|
|
|
|
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) {
|
|
|
|
userTimingPolyfill.measure(markName, markName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
clearMeasures() {
|
|
|
|
// React calls this to avoid memory leaks in browsers, but we don't keep
|
|
|
|
// measurements anyway.
|
|
|
|
},
|
2017-06-08 21:30:46 +00:00
|
|
|
}
|
2018-03-09 07:33:06 +00:00
|
|
|
: null;
|
2015-10-01 21:08:13 +00:00
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
const Systrace = {
|
2018-02-20 06:30:36 +00:00
|
|
|
installReactHook() {
|
2017-08-10 11:09:52 +00:00
|
|
|
if (_enabled) {
|
2017-08-14 14:10:27 +00:00
|
|
|
if (__DEV__) {
|
2018-02-20 06:30:36 +00:00
|
|
|
global.performance = userTimingPolyfill;
|
2017-08-10 11:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_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) {
|
2018-03-09 07:33:06 +00:00
|
|
|
global.nativeTraceBeginLegacy &&
|
|
|
|
global.nativeTraceBeginLegacy(TRACE_TAG_JS_VM_CALLS);
|
2016-05-23 11:51:25 +00:00
|
|
|
} else {
|
2018-03-09 07:33:06 +00:00
|
|
|
global.nativeTraceEndLegacy &&
|
|
|
|
global.nativeTraceEndLegacy(TRACE_TAG_JS_VM_CALLS);
|
2016-05-23 11:51:25 +00:00
|
|
|
}
|
2017-08-10 11:09:52 +00:00
|
|
|
if (_canInstallReactHook) {
|
2018-02-20 06:30:36 +00:00
|
|
|
if (enabled && global.performance === undefined) {
|
|
|
|
global.performance = userTimingPolyfill;
|
2017-08-10 11:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
2018-03-09 07:33:06 +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) {
|
2018-03-09 07:33:06 +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
|
2018-03-09 07:33:06 +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++;
|
2018-03-09 07:33:06 +00:00
|
|
|
profileName =
|
|
|
|
typeof profileName === 'function' ? profileName() : profileName;
|
|
|
|
global.nativeTraceBeginAsyncSection(
|
|
|
|
TRACE_TAG_REACT_APPS,
|
|
|
|
profileName,
|
|
|
|
cookie,
|
|
|
|
);
|
2015-12-01 15:37:52 +00:00
|
|
|
}
|
|
|
|
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) {
|
2018-03-09 07:33:06 +00:00
|
|
|
profileName =
|
|
|
|
typeof profileName === 'function' ? profileName() : profileName;
|
|
|
|
global.nativeTraceEndAsyncSection(
|
|
|
|
TRACE_TAG_REACT_APPS,
|
|
|
|
profileName,
|
|
|
|
cookie,
|
|
|
|
);
|
2015-12-01 15:37:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-30 07:36:34 +00:00
|
|
|
/**
|
|
|
|
* counterEvent registers the value to the profileName on the systrace timeline
|
2018-03-09 07:33:06 +00:00
|
|
|
**/
|
2015-12-30 07:36:34 +00:00
|
|
|
counterEvent(profileName?: any, value?: any) {
|
|
|
|
if (_enabled) {
|
2018-03-09 07:33:06 +00:00
|
|
|
profileName =
|
|
|
|
typeof profileName === 'function' ? profileName() : profileName;
|
2015-12-30 07:36:34 +00:00
|
|
|
global.nativeTraceCounter &&
|
|
|
|
global.nativeTraceCounter(TRACE_TAG_REACT_APPS, profileName, value);
|
|
|
|
}
|
|
|
|
},
|
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;
|