Refactor Systrace module

Summary:
This refactors the systrace module to:
1) Migrate it to ESM
2) Fix the typing of certain parameters (e.g.: `eventName` and `cookie` shouldn't be optional)
2) Fix TypeScript definitions
3) Add inline documentation for methods
4) Add args (metadata) to all methods where it's supported in fbsystrace (`endEvent` and `endAsyncEvent`).
5) Make `setEnabled` a no-op. The only place where this method is called is from native, when starting a profile session. The problem is that implementation for Systrace in OSS (https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java#L40-L42) doesn't support listeners, so this wouldn't work correctly there. It also doesn't get called with Venice, so instead of fixing it for Venice with the same Meta-internal behavior, I decided to fix it for everyone with the synchronous method.

I'm not considering this a change in the public API of systrace because this module has been broken in OSS for ages and I'm assuming no one's using it at this point.

Changelog: [internal]

Reviewed By: rshest

Differential Revision: D40095842

fbshipit-source-id: fee41b2b7ae23aefe059e390c55d139db75247c5
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2022-10-05 15:17:53 -07:00
1 parent 87acdfb436
commit a0f56adb07
4 files changed
+153 -141

No files matched your search

+50 -60
View File
@@ -7,70 +7,60 @@
* @format
*/
export interface RelayProfiler {
attachProfileHandler(
name: string,
handler: (name: string, state?: any) => () => void,
): void;
type EventName = string | (() => string);
type EventArgs = {[key: string]: string} | void | null;
attachAggregateHandler(
name: string,
handler: (name: string, callback: () => void) => void,
): void;
}
/**
* Indicates if the application is currently being traced.
*
* Calling methods on this module when the application isn't being traced is
* cheap, but this method can be used to avoid computing expensive values for
* those functions.
*
* @example
* if (Systrace.isEnabled()) {
* const expensiveArgs = computeExpensiveArgs();
* Systrace.beginEvent('myEvent', expensiveArgs);
* }
*/
export function isEnabled(): boolean;
export interface SystraceStatic {
setEnabled(enabled: boolean): void;
/**
* @deprecated This function is now a no-op but it's left for backwards
* compatibility. `isEnabled` will now synchronously check if we're actively
* profiling or not. This is necessary because we don't have callbacks to know
* when profiling has started/stopped on Android APIs.
*/
export function setEnabled(_doEnable: boolean): void;
/**
* beginEvent/endEvent for starting and then ending a profile within the same call stack frame
**/
beginEvent(profileName?: any, args?: any): void;
endEvent(): void;
/**
* Marks the start of a synchronous event that should end in the same stack
* frame. The end of this event should be marked using the `endEvent` function.
*/
export function beginEvent(eventName: EventName, args?: EventArgs): void;
/**
* beginAsyncEvent/endAsyncEvent 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 endAsyncEvent call to end the profile
**/
beginAsyncEvent(profileName?: any): any;
endAsyncEvent(profileName?: any, cookie?: any): void;
/**
* Marks the end of a synchronous event started in the same stack frame.
*/
export function endEvent(args?: EventArgs): void;
/**
* counterEvent registers the value to the profileName on the systrace timeline
**/
counterEvent(profileName?: any, value?: any): void;
/**
* Marks the start of a potentially asynchronous event. The end of this event
* should be marked calling the `endAsyncEvent` function with the cookie
* returned by this function.
*/
export function beginAsyncEvent(eventName: EventName, args?: EventArgs): number;
/**
* Relay profiles use await calls, so likely occur out of current stack frame
* therefore async variant of profiling is used
**/
attachToRelayProfiler(relayProfiler: RelayProfiler): void;
/**
* Registers a new value for a counter event.
*/
export function endAsyncEvent(
eventName: EventName,
cookie: number,
args?: EventArgs,
): void;
/* 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(): void;
/**
* Measures multiple methods of a class. For example, you can do:
* Systrace.measureMethods(JSON, 'JSON', ['parse', 'stringify']);
*
* @param methodNames Map from method names to method display names.
*/
measureMethods(
object: any,
objectName: string,
methodNames: Array<string>,
): void;
/**
* Returns an profiled version of the input function. For example, you can:
* JSON.parse = Systrace.measure('JSON', 'parse', JSON.parse);
*
* @return replacement function
*/
measure<T extends Function>(objName: string, fnName: string, func: T): T;
}
export const Systrace: SystraceStatic;
export type Systrace = SystraceStatic;
/**
* counterEvent registers the value to the eventName on the systrace timeline
**/
export function counterEvent(eventName: EventName, value: number): void;
+101 -79
View File
@@ -8,94 +8,118 @@
* @format
*/
'use strict';
import * as Systrace from './Systrace';
const TRACE_TAG_REACT_APPS = 1 << 17; // eslint-disable-line no-bitwise
let _enabled = false;
let _asyncCookie = 0;
const Systrace = {
setEnabled(enabled: boolean) {
_enabled = enabled;
},
type EventName = string | (() => string);
type EventArgs = ?{[string]: string};
isEnabled(): boolean {
return _enabled;
},
/**
* Indicates if the application is currently being traced.
*
* Calling methods on this module when the application isn't being traced is
* cheap, but this method can be used to avoid computing expensive values for
* those functions.
*
* @example
* if (Systrace.isEnabled()) {
* const expensiveArgs = computeExpensiveArgs();
* Systrace.beginEvent('myEvent', expensiveArgs);
* }
*/
export function isEnabled(): boolean {
return _enabled;
}
/**
* beginEvent/endEvent for starting and then ending a profile within the same call stack frame
**/
beginEvent(
profileName?: string | (() => string),
args?: {[string]: string, ...},
) {
if (_enabled) {
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceBeginSection(
TRACE_TAG_REACT_APPS,
profileNameString,
args,
);
}
},
/**
* @deprecated This function is now a no-op but it's left for backwards
* compatibility. `isEnabled` will now synchronously check if we're actively
* profiling or not. This is necessary because we don't have callbacks to know
* when profiling has started/stopped on Android APIs.
*/
export function setEnabled(_doEnable: boolean): void {}
endEvent() {
if (_enabled) {
global.nativeTraceEndSection(TRACE_TAG_REACT_APPS);
}
},
/**
* Marks the start of a synchronous event that should end in the same stack
* frame. The end of this event should be marked using the `endEvent` function.
*/
export function beginEvent(eventName: EventName, args?: EventArgs): void {
if (_enabled) {
const eventNameString =
typeof eventName === 'function' ? eventName() : eventName;
global.nativeTraceBeginSection(TRACE_TAG_REACT_APPS, eventNameString, args);
}
}
/**
* beginAsyncEvent/endAsyncEvent 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 endAsyncEvent call to end the profile
**/
beginAsyncEvent(profileName?: string | (() => string)): number {
const cookie = _asyncCookie;
if (_enabled) {
_asyncCookie++;
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceBeginAsyncSection(
TRACE_TAG_REACT_APPS,
profileNameString,
cookie,
);
}
return cookie;
},
/**
* Marks the end of a synchronous event started in the same stack frame.
*/
export function endEvent(args?: EventArgs): void {
if (_enabled) {
global.nativeTraceEndSection(TRACE_TAG_REACT_APPS, args);
}
}
endAsyncEvent(profileName?: string | (() => string), cookie?: number) {
if (_enabled) {
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceEndAsyncSection(
TRACE_TAG_REACT_APPS,
profileNameString,
cookie,
);
}
},
/**
* Marks the start of a potentially asynchronous event. The end of this event
* should be marked calling the `endAsyncEvent` function with the cookie
* returned by this function.
*/
export function beginAsyncEvent(
eventName: EventName,
args?: EventArgs,
): number {
const cookie = _asyncCookie;
if (_enabled) {
_asyncCookie++;
const eventNameString =
typeof eventName === 'function' ? eventName() : eventName;
global.nativeTraceBeginAsyncSection(
TRACE_TAG_REACT_APPS,
eventNameString,
cookie,
args,
);
}
return cookie;
}
/**
* counterEvent registers the value to the profileName on the systrace timeline
**/
counterEvent(profileName?: string | (() => string), value?: number) {
if (_enabled) {
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceCounter &&
global.nativeTraceCounter(
TRACE_TAG_REACT_APPS,
profileNameString,
value,
);
}
},
};
/**
* Marks the end of a potentially asynchronous event, which was started with
* the given cookie.
*/
export function endAsyncEvent(
eventName: EventName,
cookie: number,
args?: EventArgs,
): void {
if (_enabled) {
const eventNameString =
typeof eventName === 'function' ? eventName() : eventName;
global.nativeTraceEndAsyncSection(
TRACE_TAG_REACT_APPS,
eventNameString,
cookie,
args,
);
}
}
/**
* Registers a new value for a counter event.
*/
export function counterEvent(eventName: EventName, value: number): void {
if (_enabled) {
const eventNameString =
typeof eventName === 'function' ? eventName() : eventName;
global.nativeTraceCounter &&
global.nativeTraceCounter(TRACE_TAG_REACT_APPS, eventNameString, value);
}
}
if (__DEV__) {
// The metro require polyfill can not have dependencies (true for all polyfills).
@@ -104,7 +128,5 @@ if (__DEV__) {
}
if (global.__RCTProfileIsProfiling) {
Systrace.setEnabled(true);
_enabled = true;
}
module.exports = Systrace;
+1 -1
View File
@@ -76,7 +76,7 @@ import typeof PushNotificationIOS from './Libraries/PushNotificationIOS/PushNoti
import typeof Settings from './Libraries/Settings/Settings';
import typeof Share from './Libraries/Share/Share';
import typeof StyleSheet from './Libraries/StyleSheet/StyleSheet';
import typeof Systrace from './Libraries/Performance/Systrace';
import typeof * as Systrace from './Libraries/Performance/Systrace';
import typeof ToastAndroid from './Libraries/Components/ToastAndroid/ToastAndroid';
import typeof * as TurboModuleRegistry from './Libraries/TurboModule/TurboModuleRegistry';
import typeof UIManager from './Libraries/ReactNative/UIManager';
+1 -1
View File
@@ -122,7 +122,7 @@ export * from '../Libraries/Lists/SectionList';
export * from '../Libraries/Lists/VirtualizedList';
export * from '../Libraries/LogBox/LogBox';
export * from '../Libraries/Modal/Modal';
export * from '../Libraries/Performance/Systrace';
export * as Systrace from '../Libraries/Performance/Systrace';
export * from '../Libraries/PermissionsAndroid/PermissionsAndroid';
export * from '../Libraries/PushNotificationIOS/PushNotificationIOS';
export * from '../Libraries/ReactNative/AppRegistry';