From a407ff94eeb09629d21ecb1b1204c53d3fc3cead Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Wed, 25 Jan 2017 14:28:02 -0800 Subject: [PATCH] Add option to start sampling profiler on app launch Reviewed By: mhorowitz Differential Revision: D4415989 fbshipit-source-id: 30704c2b618656cb7cc0ccdf87dec315b30b62f3 --- React/Executors/RCTJSCExecutor.mm | 6 ++++-- React/Modules/RCTDevMenu.h | 5 +++++ React/Modules/RCTDevMenu.mm | 11 +++++++++++ ReactCommon/cxxreact/JSCExecutor.cpp | 2 +- ReactCommon/jschelpers/JSCWrapper.h | 3 ++- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/React/Executors/RCTJSCExecutor.mm b/React/Executors/RCTJSCExecutor.mm index 66ad2ef83..0eb8d089e 100644 --- a/React/Executors/RCTJSCExecutor.mm +++ b/React/Executors/RCTJSCExecutor.mm @@ -338,7 +338,9 @@ static NSThread *newJavaScriptThread(void) contextRef = context.JSGlobalContextRef; } else { if (self->_useCustomJSCLibrary) { - JSC_configureJSCForIOS(true); + JSC_configureJSCForIOS(true, RCTJSONStringify(@{ + @"StartSamplingProfilerOnInit": @(self->_bridge.devMenu.startSamplingProfilerOnLaunch) + }, NULL).UTF8String); } contextRef = JSC_JSGlobalContextCreateInGroup(self->_useCustomJSCLibrary, nullptr, nullptr); context = [JSC_JSContext(contextRef) contextWithJSGlobalContextRef:contextRef]; @@ -1028,7 +1030,7 @@ static NSData *loadRAMBundle(NSURL *sourceURL, NSError **error, RandomAccessBund - (void)_createContext { if (_useCustomJSCLibrary) { - JSC_configureJSCForIOS(true); + JSC_configureJSCForIOS(true, "{}"); } JSGlobalContextRef ctx = JSC_JSGlobalContextCreateInGroup(_useCustomJSCLibrary, nullptr, nullptr); _context = [JSC_JSContext(ctx) contextWithJSGlobalContextRef:ctx]; diff --git a/React/Modules/RCTDevMenu.h b/React/Modules/RCTDevMenu.h index 7f16fd43b..8f1ce29ff 100644 --- a/React/Modules/RCTDevMenu.h +++ b/React/Modules/RCTDevMenu.h @@ -30,6 +30,11 @@ */ @property (nonatomic, assign) BOOL profilingEnabled; +/** + * Enables starting of profiling sampler on launch + */ +@property (nonatomic, assign) BOOL startSamplingProfilerOnLaunch; + /** * Enables automatic polling for JS code changes. Only applicable when * running the app from a server. diff --git a/React/Modules/RCTDevMenu.mm b/React/Modules/RCTDevMenu.mm index 95f29bf2c..534a39499 100644 --- a/React/Modules/RCTDevMenu.mm +++ b/React/Modules/RCTDevMenu.mm @@ -212,6 +212,9 @@ RCT_EXPORT_MODULE() self->_executorOverride = [self->_defaults objectForKey:@"executor-override"]; }); + // Same values are read during the bridge starup path + _startSamplingProfilerOnLaunch = [_settings[@"startSamplingProfilerOnLaunch"] boolValue]; + // Delay setup until after Bridge init dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf updateSettings:self->_settings]; @@ -537,6 +540,8 @@ RCT_EXPORT_MODULE() } // Add toggles for JSC's sampling profiler, if the profiler is enabled + // Note: bridge.jsContext is not implemented in the old bridge, so this code is + // duplicated in RCTJSCExecutor if (JSC_JSSamplingProfilerEnabled(self->_bridge.jsContext.JSGlobalContextRef)) { JSContext *context = self->_bridge.jsContext; // Allow to toggle the sampling profiler through RN's dev menu @@ -625,6 +630,12 @@ RCT_EXPORT_METHOD(debugRemotely:(BOOL)enableDebug) [self updateSetting:@"shakeToShow" value:@(_shakeToShow)]; } +- (void)setStartSamplingProfilerOnLaunch:(BOOL)startSamplingProfilerOnLaunch +{ + _startSamplingProfilerOnLaunch = startSamplingProfilerOnLaunch; + [self updateSetting:@"startSamplingProfilerOnLaunch" value:@(_startSamplingProfilerOnLaunch)]; +} + RCT_EXPORT_METHOD(setProfilingEnabled:(BOOL)enabled) { _profilingEnabled = enabled; diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index c23e53e48..a748c63c3 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -226,7 +226,7 @@ void JSCExecutor::initOnJSVMThread() throw(JSException) { #if defined(__APPLE__) const bool useCustomJSC = m_jscConfig.getDefault("UseCustomJSC", false).getBool(); if (useCustomJSC) { - JSC_configureJSCForIOS(true); + JSC_configureJSCForIOS(true, toJson(m_jscConfig)); } #else const bool useCustomJSC = false; diff --git a/ReactCommon/jschelpers/JSCWrapper.h b/ReactCommon/jschelpers/JSCWrapper.h index 5bc0e206e..c1a6d0cc1 100644 --- a/ReactCommon/jschelpers/JSCWrapper.h +++ b/ReactCommon/jschelpers/JSCWrapper.h @@ -18,10 +18,11 @@ #if defined(__APPLE__) #import #import +#import // This is used to substitute an alternate JSC implementation for // testing. These calls must all be ABI compatible with the standard JSC. -extern void configureJSCForIOS(); +extern void configureJSCForIOS(std::string); // TODO: replace with folly::dynamic once supported extern JSValueRef JSEvaluateBytecodeBundle(JSContextRef, JSObjectRef, int, JSStringRef, JSValueRef*); extern bool JSSamplingProfilerEnabled(); extern void JSStartSamplingProfilingOnMainJSCThread(JSGlobalContextRef);