From f433ed716c290e38211caf245503f0880506b848 Mon Sep 17 00:00:00 2001 From: Marc Horowitz Date: Fri, 13 May 2016 17:15:06 -0700 Subject: [PATCH] Make the pattern for adding systrace sections more concise Reviewed By: astreet Differential Revision: D3234834 fbshipit-source-id: 73e191deb4dd0e06d4c242e46a582051d4bcc9ba --- ReactCommon/bridge/Bridge.cpp | 7 +++-- ReactCommon/bridge/Instance.cpp | 45 +++++++-------------------- ReactCommon/bridge/JSCExecutor.cpp | 24 ++++---------- ReactCommon/bridge/ModuleRegistry.cpp | 16 +++------- ReactCommon/bridge/SystraceSection.h | 33 ++++++++++++++++++++ 5 files changed, 59 insertions(+), 66 deletions(-) create mode 100644 ReactCommon/bridge/SystraceSection.h diff --git a/ReactCommon/bridge/Bridge.cpp b/ReactCommon/bridge/Bridge.cpp index cd8cf7f8819..378ec629f49 100644 --- a/ReactCommon/bridge/Bridge.cpp +++ b/ReactCommon/bridge/Bridge.cpp @@ -4,14 +4,15 @@ #ifdef WITH_FBSYSTRACE #include -using fbsystrace::FbSystraceSection; using fbsystrace::FbSystraceAsyncFlow; #endif + #include #include #include #include "Platform.h" +#include "SystraceSection.h" namespace facebook { namespace react { @@ -75,7 +76,7 @@ void Bridge::callFunction( TRACE_TAG_REACT_CXX_BRIDGE, tracingName.c_str(), systraceCookie); - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, tracingName.c_str()); + SystraceSection s(tracingName.c_str()); #endif // This is safe because we are running on the executor's thread: it won't @@ -100,7 +101,7 @@ void Bridge::invokeCallback(ExecutorToken executorToken, const double callbackId TRACE_TAG_REACT_CXX_BRIDGE, "", systraceCookie); - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "Bridge.invokeCallback"); + SystraceSection s("Bridge.invokeCallback"); #endif executor->invokeCallback(callbackId, arguments); diff --git a/ReactCommon/bridge/Instance.cpp b/ReactCommon/bridge/Instance.cpp index 8f0c8155313..509979ba0e5 100644 --- a/ReactCommon/bridge/Instance.cpp +++ b/ReactCommon/bridge/Instance.cpp @@ -4,11 +4,7 @@ #include "Executor.h" #include "MethodCall.h" - -#ifdef WITH_FBSYSTRACE -#include -using fbsystrace::FbSystraceSection; -#endif +#include "SystraceSection.h" #include #include @@ -77,17 +73,13 @@ void Instance::initializeBridge( bridge_ = folly::make_unique( jsef.get(), jsQueue_, folly::make_unique(callback_.get()), folly::make_unique(this)); }); -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "setBatchedBridgeConfig"); -#endif + SystraceSection s("setBatchedBridgeConfig"); CHECK(bridge_); folly::dynamic nativeModuleDescriptions = folly::dynamic::array(); { -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "collectNativeModuleDescriptions"); -#endif + SystraceSection s("collectNativeModuleDescriptions"); nativeModuleDescriptions = moduleRegistry_->moduleDescriptions(); } @@ -95,9 +87,7 @@ void Instance::initializeBridge( folly::dynamic::object ("remoteModuleConfig", std::move(nativeModuleDescriptions)); -#ifdef WITH_FBSYSTRACE - FbSystraceSection t(TRACE_TAG_REACT_CXX_BRIDGE, "setGlobalVariable"); -#endif + SystraceSection t("setGlobalVariable"); setGlobalVariable( "__fbBatchedBridgeConfig", folly::toJson(config)); @@ -106,11 +96,8 @@ void Instance::initializeBridge( void Instance::loadScriptFromString(const std::string& string, const std::string& sourceURL) { callback_->incrementPendingJSCalls(); -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, - "reactbridge_xplat_loadScriptFromString", - "sourceURL", sourceURL); -#endif + SystraceSection s("reactbridge_xplat_loadScriptFromString", + "sourceURL", sourceURL); // TODO mhorowitz: ReactMarker around loadApplicationScript bridge_->loadApplicationScript(string, sourceURL); } @@ -120,11 +107,8 @@ void Instance::loadScriptFromFile(const std::string& filename, // TODO mhorowitz: ReactMarker around file read std::string script; { -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, - "reactbridge_xplat_loadScriptFromFile", - "fileName", filename); -#endif + SystraceSection s("reactbridge_xplat_loadScriptFromFile", + "fileName", filename); std::ifstream jsfile(filename); if (!jsfile) { @@ -146,10 +130,7 @@ void Instance::loadUnbundle(std::unique_ptr unbundle, const std::string& startupScript, const std::string& startupScriptSourceURL) { callback_->incrementPendingJSCalls(); -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, - "reactbridge_xplat_setJSModulesUnbundle"); -#endif + SystraceSection s("reactbridge_xplat_setJSModulesUnbundle"); bridge_->loadApplicationUnbundle(std::move(unbundle), startupScript, startupScriptSourceURL); } @@ -172,17 +153,13 @@ void Instance::setGlobalVariable(const std::string& propName, void Instance::callJSFunction(ExecutorToken token, const std::string& module, const std::string& method, folly::dynamic&& params, const std::string& tracingName) { -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, tracingName.c_str()); -#endif + SystraceSection s(tracingName.c_str()); callback_->incrementPendingJSCalls(); bridge_->callFunction(token, module, method, std::move(params), tracingName); } void Instance::callJSCallback(ExecutorToken token, uint64_t callbackId, folly::dynamic&& params) { -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, ""); -#endif + SystraceSection s(""); callback_->incrementPendingJSCalls(); bridge_->invokeCallback(token, (double) callbackId, std::move(params)); } diff --git a/ReactCommon/bridge/JSCExecutor.cpp b/ReactCommon/bridge/JSCExecutor.cpp index 4eb6f40bc8a..d715c7d6ad5 100644 --- a/ReactCommon/bridge/JSCExecutor.cpp +++ b/ReactCommon/bridge/JSCExecutor.cpp @@ -16,6 +16,7 @@ #include "Bridge.h" #include "JSCHelpers.h" #include "Platform.h" +#include "SystraceSection.h" #include "Value.h" #if defined(WITH_JSC_EXTRA_TRACING) || DEBUG @@ -32,11 +33,6 @@ #include #endif -#ifdef WITH_FBSYSTRACE -#include -using fbsystrace::FbSystraceSection; -#endif - #ifdef WITH_FB_MEMORY_PROFILING #include "JSCMemory.h" #endif @@ -97,11 +93,8 @@ static std::string executeJSCallWithJSC( JSGlobalContextRef ctx, const std::string& methodName, const std::vector& arguments) { - #ifdef WITH_FBSYSTRACE - FbSystraceSection s( - TRACE_TAG_REACT_CXX_BRIDGE, "JSCExecutor.executeJSCall", - "method", methodName); - #endif + SystraceSection s("JSCExecutor.executeJSCall", + "method", methodName); // Evaluate script with JSC folly::dynamic jsonArgs(arguments.begin(), arguments.end()); @@ -181,10 +174,7 @@ void JSCExecutor::destroy() { } void JSCExecutor::initOnJSVMThread() { - #ifdef WITH_FBSYSTRACE - FbSystraceSection s( - TRACE_TAG_REACT_CXX_BRIDGE, "JSCExecutor.initOnJSVMThread"); - #endif + SystraceSection s("JSCExecutor.initOnJSVMThread"); #if defined(WITH_FB_JSC_TUNING) configureJSCForAndroid(m_jscConfig); @@ -249,10 +239,8 @@ void JSCExecutor::terminateOnJSVMThread() { void JSCExecutor::loadApplicationScript( const std::string& script, const std::string& sourceURL) { - #ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "JSCExecutor::loadApplicationScript", - "sourceURL", sourceURL); - #endif + SystraceSection s("JSCExecutor::loadApplicationScript", + "sourceURL", sourceURL); #ifdef WITH_FBSYSTRACE fbsystrace_begin_section( diff --git a/ReactCommon/bridge/ModuleRegistry.cpp b/ReactCommon/bridge/ModuleRegistry.cpp index 64360fef8c3..18ee5f17825 100644 --- a/ReactCommon/bridge/ModuleRegistry.cpp +++ b/ReactCommon/bridge/ModuleRegistry.cpp @@ -3,11 +3,7 @@ #include "ModuleRegistry.h" #include "NativeModule.h" - -#ifdef WITH_FBSYSTRACE -#include -using fbsystrace::FbSystraceSection; -#endif +#include "SystraceSection.h" namespace facebook { namespace react { @@ -24,9 +20,8 @@ folly::dynamic ModuleRegistry::moduleDescriptions() { folly::dynamic methodDescs = folly::dynamic::object; std::vector methods; { -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "getMethods", "module", module->getName()); -#endif + SystraceSection s("getMethods", + "module", module->getName()); methods = module->getMethods(); } for (size_t methodId = 0; methodId < methods.size(); ++methodId) { @@ -38,9 +33,8 @@ folly::dynamic ModuleRegistry::moduleDescriptions() { folly::dynamic constants = folly::dynamic::array(); { -#ifdef WITH_FBSYSTRACE - FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "getConstants", "module", module->getName()); -#endif + SystraceSection s("getConstants", + "module", module->getName()); constants = module->getConstants(); } diff --git a/ReactCommon/bridge/SystraceSection.h b/ReactCommon/bridge/SystraceSection.h new file mode 100644 index 00000000000..2db1e51b4b3 --- /dev/null +++ b/ReactCommon/bridge/SystraceSection.h @@ -0,0 +1,33 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +#ifdef WITH_FBSYSTRACE +#include +#endif + +namespace facebook { +namespace react { + +/** + * This is a convenience class to avoid lots of verbose profiling + * #ifdefs. If WITH_FBSYSTRACE is not defined, the optimizer will + * remove this completely. If it is defined, it will behave as + * FbSystraceSection, with the right tag provided. + */ +struct SystraceSection { +public: + template + explicit SystraceSection(const char* name, ConvertsToStringPiece&&... args) +#ifdef WITH_FBSYSTRACE + : m_section(TRACE_TAG_REACT_CXX_BRIDGE, name, args...) +#endif + {} + +private: +#ifdef WITH_FBSYSTRACE + fbsystrace::FbSystraceSection m_section; +#endif +}; + +}}