From 7f710f9050636a4c884052ec989037329f63ad65 Mon Sep 17 00:00:00 2001 From: Mike Armstrong Date: Thu, 10 Dec 2015 04:36:23 -0800 Subject: [PATCH] Add fbsystrace markers using the legacyprofiler Reviewed By: astreet Differential Revision: D2728033 fb-gh-sync-id: 264d40930b8fec0262cbea36529bd8b11efcc58e --- Libraries/Utilities/BridgeProfiling.js | 10 ++- .../src/main/jni/react/JSCExecutor.cpp | 4 ++ .../src/main/jni/react/JSCLegacyProfiler.cpp | 4 ++ .../src/main/jni/react/JSCTracing.cpp | 65 +++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/Libraries/Utilities/BridgeProfiling.js b/Libraries/Utilities/BridgeProfiling.js index 8e60354f2..36cb86252 100644 --- a/Libraries/Utilities/BridgeProfiling.js +++ b/Libraries/Utilities/BridgeProfiling.js @@ -20,8 +20,9 @@ type RelayProfiler = { var GLOBAL = GLOBAL || this; var TRACE_TAG_REACT_APPS = 1 << 17; +var TRACE_TAG_JSC_CALLS = 1 << 27; -var _enabled; +var _enabled = false; var _asyncCookie = 0; var _ReactPerf = null; function ReactPerf() { @@ -33,6 +34,13 @@ function ReactPerf() { var BridgeProfiling = { setEnabled(enabled: boolean) { + if (_enabled !== enabled) { + if (enabled) { + global.nativeTraceBeginLegacy && global.nativeTraceBeginLegacy(TRACE_TAG_JSC_CALLS); + } else { + global.nativeTraceEndLegacy && global.nativeTraceEndLegacy(TRACE_TAG_JSC_CALLS); + } + } _enabled = enabled; ReactPerf().enableMeasure = enabled; diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp index de9dffc05..6e1f493e8 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp @@ -195,7 +195,11 @@ bool JSCExecutor::supportsProfiling() { void JSCExecutor::startProfiler(const std::string &titleString) { #ifdef WITH_JSC_EXTRA_TRACING JSStringRef title = JSStringCreateWithUTF8CString(titleString.c_str()); + #if WITH_JSC_INTERNAL + JSStartProfiling(m_context, title, false); + #else JSStartProfiling(m_context, title); + #endif JSStringRelease(title); #endif } diff --git a/ReactAndroid/src/main/jni/react/JSCLegacyProfiler.cpp b/ReactAndroid/src/main/jni/react/JSCLegacyProfiler.cpp index 74222c2ef..e1e2d8b4b 100644 --- a/ReactAndroid/src/main/jni/react/JSCLegacyProfiler.cpp +++ b/ReactAndroid/src/main/jni/react/JSCLegacyProfiler.cpp @@ -26,7 +26,11 @@ static JSValueRef nativeProfilerStart( } JSStringRef title = JSValueToStringCopy(ctx, arguments[0], exception); + #if WITH_JSC_INTERNAL + JSStartProfiling(ctx, title, false); + #else JSStartProfiling(ctx, title); + #endif JSStringRelease(title); return JSValueMakeUndefined(ctx); } diff --git a/ReactAndroid/src/main/jni/react/JSCTracing.cpp b/ReactAndroid/src/main/jni/react/JSCTracing.cpp index 945d6fe72..c607cc8c6 100644 --- a/ReactAndroid/src/main/jni/react/JSCTracing.cpp +++ b/ReactAndroid/src/main/jni/react/JSCTracing.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -9,6 +10,8 @@ using std::min; +static const char *ENABLED_FBSYSTRACE_PROFILE_NAME = "__fbsystrace__"; + static uint64_t tagFromJSValue( JSContextRef ctx, JSValueRef value, @@ -409,12 +412,74 @@ static JSValueRef nativeTraceCounter( return JSValueMakeUndefined(ctx); } +static JSValueRef nativeTraceBeginLegacy( + JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) { + if (FBSYSTRACE_UNLIKELY(argumentCount < 1)) { + if (exception) { + *exception = facebook::react::makeJSCException( + ctx, + "nativeTraceBeginLegacy: requires TAG Argument"); + } + return JSValueMakeUndefined(ctx); + } + + uint64_t tag = tagFromJSValue(ctx, arguments[0], exception); + if (!fbsystrace_is_tracing(tag)) { + return JSValueMakeUndefined(ctx); + } + + JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME); + #if WITH_JSC_INTERNAL + JSStartProfiling(ctx, title, true); + #else + JSStartProfiling(ctx, title); + #endif + JSStringRelease(title); + + return JSValueMakeUndefined(ctx); +} + +static JSValueRef nativeTraceEndLegacy( + JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) { + if (FBSYSTRACE_UNLIKELY(argumentCount < 1)) { + if (exception) { + *exception = facebook::react::makeJSCException( + ctx, + "nativeTraceBeginLegacy: requires TAG Argument"); + } + return JSValueMakeUndefined(ctx); + } + + uint64_t tag = tagFromJSValue(ctx, arguments[0], exception); + if (!fbsystrace_is_tracing(tag)) { + return JSValueMakeUndefined(ctx); + } + + JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME); + JSEndProfiling(ctx, title); + JSStringRelease(title); + + return JSValueMakeUndefined(ctx); +} + namespace facebook { namespace react { void addNativeTracingHooks(JSGlobalContextRef ctx) { installGlobalFunction(ctx, "nativeTraceBeginSection", nativeTraceBeginSection); installGlobalFunction(ctx, "nativeTraceEndSection", nativeTraceEndSection); + installGlobalFunction(ctx, "nativeTraceBeginLegacy", nativeTraceBeginLegacy); + installGlobalFunction(ctx, "nativeTraceEndLegacy", nativeTraceEndLegacy); installGlobalFunction(ctx, "nativeTraceBeginAsyncSection", nativeTraceBeginAsyncSection); installGlobalFunction(ctx, "nativeTraceEndAsyncSection", nativeTraceEndAsyncSection); installGlobalFunction(ctx, "nativeTraceAsyncSectionStage", nativeTraceAsyncSectionStage);