From 86f2eb18e52a6dc87bf45e63aed79d61bdaec201 Mon Sep 17 00:00:00 2001 From: Marc Horowitz Date: Fri, 13 May 2016 17:15:03 -0700 Subject: [PATCH] Support JS Systrace on all platforms Reviewed By: tadeuzagallo Differential Revision: D3234830 fbshipit-source-id: 94cc870d47d620c8bd8d35f83d0b017e5ddba90d --- ReactCommon/bridge/BUCK | 2 + ReactCommon/bridge/JSCExecutor.cpp | 14 +++- ReactCommon/bridge/JSCLegacyTracing.cpp | 73 ++++++++++++++++++++ ReactCommon/bridge/JSCLegacyTracing.h | 15 +++++ ReactCommon/bridge/JSCTracing.cpp | 89 ++++++------------------- ReactCommon/bridge/JSCTracing.h | 6 +- 6 files changed, 127 insertions(+), 72 deletions(-) create mode 100644 ReactCommon/bridge/JSCLegacyTracing.cpp create mode 100644 ReactCommon/bridge/JSCLegacyTracing.h diff --git a/ReactCommon/bridge/BUCK b/ReactCommon/bridge/BUCK index b306db8fa..9d5a04cb0 100644 --- a/ReactCommon/bridge/BUCK +++ b/ReactCommon/bridge/BUCK @@ -48,6 +48,7 @@ elif THIS_IS_FBOBJC: ], **kwargs_add( kwargs, + preprocessor_flags = DEBUG_PREPROCESSOR_FLAGS, deps = [ '//xplat/folly:molly', ] @@ -57,6 +58,7 @@ elif THIS_IS_FBOBJC: LOCAL_HEADERS = [ 'JSCTracing.h', 'JSCLegacyProfiler.h', + 'JSCLegacyTracing.h', 'JSCMemory.h', 'JSCPerfStats.h', ] diff --git a/ReactCommon/bridge/JSCExecutor.cpp b/ReactCommon/bridge/JSCExecutor.cpp index fdc23c5d6..4eb6f40bc 100644 --- a/ReactCommon/bridge/JSCExecutor.cpp +++ b/ReactCommon/bridge/JSCExecutor.cpp @@ -18,9 +18,13 @@ #include "Platform.h" #include "Value.h" -#ifdef WITH_JSC_EXTRA_TRACING +#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG #include "JSCTracing.h" +#endif + +#ifdef WITH_JSC_EXTRA_TRACING #include "JSCLegacyProfiler.h" +#include "JSCLegacyTracing.h" #include #endif @@ -204,9 +208,13 @@ void JSCExecutor::initOnJSVMThread() { installGlobalFunction(m_context, "nativeLoggingHook", JSNativeHooks::loggingHook); installGlobalFunction(m_context, "nativePerformanceNow", JSNativeHooks::nowHook); - #ifdef WITH_JSC_EXTRA_TRACING + #if defined(WITH_JSC_EXTRA_TRACING) || DEBUG addNativeTracingHooks(m_context); + #endif + + #ifdef WITH_JSC_EXTRA_TRACING addNativeProfilingHooks(m_context); + addNativeTracingLegacyHooks(m_context); PerfLogging::installNativeHooks(m_context); #endif @@ -301,6 +309,8 @@ void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic& } void JSCExecutor::setGlobalVariable(const std::string& propName, const std::string& jsonValue) { + // TODO mhorowitz: systrace this. + auto globalObject = JSContextGetGlobalObject(m_context); String jsPropertyName(propName.c_str()); diff --git a/ReactCommon/bridge/JSCLegacyTracing.cpp b/ReactCommon/bridge/JSCLegacyTracing.cpp new file mode 100644 index 000000000..6def88697 --- /dev/null +++ b/ReactCommon/bridge/JSCLegacyTracing.cpp @@ -0,0 +1,73 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#ifdef WITH_JSC_EXTRA_TRACING + +#include "JSCLegacyTracing.h" + +#include +#include + +#include + +#include "JSCHelpers.h" +#include "JSCTracing.h" + +static const char *ENABLED_FBSYSTRACE_PROFILE_NAME = "__fbsystrace__"; + +static JSValueRef nativeTraceBeginLegacy( + JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) { + if (FBSYSTRACE_LIKELY(argumentCount >= 1)) { + uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception); + if (!fbsystrace_is_tracing(tag)) { + return JSValueMakeUndefined(ctx); + } + } + + JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME); + #if WITH_REACT_INTERNAL_SETTINGS + 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_LIKELY(argumentCount >= 1)) { + uint64_t tag = facebook::react::tracingTagFromJSValue(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 addNativeTracingLegacyHooks(JSGlobalContextRef ctx) { + installGlobalFunction(ctx, "nativeTraceBeginLegacy", nativeTraceBeginLegacy); + installGlobalFunction(ctx, "nativeTraceEndLegacy", nativeTraceEndLegacy); +} + +} } + +#endif diff --git a/ReactCommon/bridge/JSCLegacyTracing.h b/ReactCommon/bridge/JSCLegacyTracing.h new file mode 100644 index 000000000..93908b03d --- /dev/null +++ b/ReactCommon/bridge/JSCLegacyTracing.h @@ -0,0 +1,15 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +#if defined(WITH_JSC_EXTRA_TRACING) + +#include +namespace facebook { +namespace react { + +void addNativeTracingLegacyHooks(JSGlobalContextRef ctx); + +} } + +#endif diff --git a/ReactCommon/bridge/JSCTracing.cpp b/ReactCommon/bridge/JSCTracing.cpp index 6bdf03ff9..d9aca2196 100644 --- a/ReactCommon/bridge/JSCTracing.cpp +++ b/ReactCommon/bridge/JSCTracing.cpp @@ -1,10 +1,11 @@ // Copyright 2004-present Facebook. All Rights Reserved. -#ifdef WITH_JSC_EXTRA_TRACING +#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG + +#include "JSCTracing.h" #include #include -#include #include #include #include @@ -12,20 +13,6 @@ using std::min; -static const char *ENABLED_FBSYSTRACE_PROFILE_NAME = "__fbsystrace__"; - -static uint64_t tagFromJSValue( - JSContextRef ctx, - JSValueRef value, - JSValueRef* exception) { - // XXX validate that this is a lossless conversion. - // XXX should we just have separate functions for bridge, infra, and apps, - // then drop this argument to save time? - (void)exception; - uint64_t tag = (uint64_t) JSValueToNumber(ctx, value, NULL); - return tag; -} - static int64_t int64FromJSValue( JSContextRef ctx, JSValueRef value, @@ -105,7 +92,7 @@ static JSValueRef nativeTraceBeginSection( return JSValueMakeUndefined(ctx); } - uint64_t tag = tagFromJSValue(ctx, arguments[0], exception); + uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception); if (!fbsystrace_is_tracing(tag)) { return JSValueMakeUndefined(ctx); } @@ -145,7 +132,7 @@ static JSValueRef nativeTraceEndSection( return JSValueMakeUndefined(ctx); } - uint64_t tag = tagFromJSValue(ctx, arguments[0], exception); + uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception); if (!fbsystrace_is_tracing(tag)) { return JSValueMakeUndefined(ctx); } @@ -189,7 +176,7 @@ static JSValueRef beginOrEndAsync( return JSValueMakeUndefined(ctx); } - uint64_t tag = tagFromJSValue(ctx, arguments[0], exception); + uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception); if (!fbsystrace_is_tracing(tag)) { return JSValueMakeUndefined(ctx); } @@ -252,7 +239,7 @@ static JSValueRef stageAsync( return JSValueMakeUndefined(ctx); } - uint64_t tag = tagFromJSValue(ctx, arguments[0], exception); + uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception); if (!fbsystrace_is_tracing(tag)) { return JSValueMakeUndefined(ctx); } @@ -398,7 +385,7 @@ static JSValueRef nativeTraceCounter( return JSValueMakeUndefined(ctx); } - uint64_t tag = tagFromJSValue(ctx, arguments[0], exception); + uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception); if (!fbsystrace_is_tracing(tag)) { return JSValueMakeUndefined(ctx); } @@ -414,60 +401,24 @@ 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_LIKELY(argumentCount >= 1)) { - 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_REACT_INTERNAL_SETTINGS - 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_LIKELY(argumentCount >= 1)) { - 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 { +uint64_t tracingTagFromJSValue( + JSContextRef ctx, + JSValueRef value, + JSValueRef* exception) { + // XXX validate that this is a lossless conversion. + // XXX should we just have separate functions for bridge, infra, and apps, + // then drop this argument to save time? + (void)exception; + uint64_t tag = (uint64_t) JSValueToNumber(ctx, value, NULL); + return tag; +} + 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); diff --git a/ReactCommon/bridge/JSCTracing.h b/ReactCommon/bridge/JSCTracing.h index 0cca4092d..b65f633c8 100644 --- a/ReactCommon/bridge/JSCTracing.h +++ b/ReactCommon/bridge/JSCTracing.h @@ -2,12 +2,16 @@ #pragma once -#ifdef WITH_JSC_EXTRA_TRACING +#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG + +#include #include + namespace facebook { namespace react { +uint64_t tracingTagFromJSValue(JSContextRef ctx, JSValueRef value, JSValueRef* exception); void addNativeTracingHooks(JSGlobalContextRef ctx); } }