From aee74efde786566fc137e1364f02537ae1f98b39 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Fri, 28 Aug 2015 10:11:02 -0700 Subject: [PATCH] [ReactNative] Add JSC profiler to Dev Menu Summary: Add JSC profiler to the dev menu and rename the pre-existent one to systrace. For now it just outputs to the console, but a better workflow is on the way. --- JSCLegacyProfiler/JSCLegacyProfiler.h | 17 ++-------- JSCLegacyProfiler/JSCLegacyProfiler.mm | 43 +++++--------------------- JSCLegacyProfiler/Makefile | 2 -- React/Executors/RCTContextExecutor.m | 17 +++++++--- React/Modules/RCTDevMenu.m | 2 +- 5 files changed, 23 insertions(+), 58 deletions(-) diff --git a/JSCLegacyProfiler/JSCLegacyProfiler.h b/JSCLegacyProfiler/JSCLegacyProfiler.h index 826e39f21..6705a1de4 100644 --- a/JSCLegacyProfiler/JSCLegacyProfiler.h +++ b/JSCLegacyProfiler/JSCLegacyProfiler.h @@ -6,20 +6,7 @@ extern "C" { -JSValueRef nativeProfilerStart( - JSContextRef ctx, - JSObjectRef function, - JSObjectRef thisObject, - size_t argumentCount, - const JSValueRef arguments[], - JSValueRef *exception); - -JSValueRef nativeProfilerEnd( - JSContextRef ctx, - JSObjectRef function, - JSObjectRef thisObject, - size_t argumentCount, - const JSValueRef arguments[], - JSValueRef *exception); +void nativeProfilerStart(JSContextRef ctx, const char *title); +const char *nativeProfilerEnd(JSContextRef ctx, const char *title); } diff --git a/JSCLegacyProfiler/JSCLegacyProfiler.mm b/JSCLegacyProfiler/JSCLegacyProfiler.mm index b946c8fd1..dd2f01f8a 100644 --- a/JSCLegacyProfiler/JSCLegacyProfiler.mm +++ b/JSCLegacyProfiler/JSCLegacyProfiler.mm @@ -7,6 +7,7 @@ #include "OpaqueJSString.h" #include "JSProfilerPrivate.h" #include "JSStringRef.h" +#include "String.h" #include @@ -114,48 +115,18 @@ static char *convert_to_json(const JSC::Profile *profile) { return json_copy; } -static char *JSEndProfilingAndRender(JSContextRef ctx, JSStringRef title) +static const char *JSEndProfilingAndRender(JSContextRef ctx, const char *title) { JSC::ExecState *exec = toJS(ctx); JSC::LegacyProfiler *profiler = JSC::LegacyProfiler::profiler(); - RefPtr rawProfile = profiler->stopProfiling(exec, title->string()); + RefPtr rawProfile = profiler->stopProfiling(exec, WTF::String(title)); return convert_to_json(rawProfile.get()); } -JSValueRef nativeProfilerStart( - JSContextRef ctx, - JSObjectRef function, - JSObjectRef thisObject, - size_t argumentCount, - const JSValueRef arguments[], - JSValueRef *exception) { - if (argumentCount < 1) { - // Could raise an exception here. - return JSValueMakeUndefined(ctx); - } - - JSStringRef title = JSValueToStringCopy(ctx, arguments[0], NULL); - JSStartProfiling(ctx, title); - JSStringRelease(title); - return JSValueMakeUndefined(ctx); +void nativeProfilerStart(JSContextRef ctx, const char *title) { + JSStartProfiling(ctx, JSStringCreateWithUTF8CString(title)); } -JSValueRef nativeProfilerEnd( - JSContextRef ctx, - JSObjectRef function, - JSObjectRef thisObject, - size_t argumentCount, - const JSValueRef arguments[], - JSValueRef *exception) { - if (argumentCount < 1) { - // Could raise an exception here. - return JSValueMakeUndefined(ctx); - } - - JSStringRef title = JSValueToStringCopy(ctx, arguments[0], NULL); - char *rendered = JSEndProfilingAndRender(ctx, title); - JSStringRelease(title); - JSStringRef profile = JSStringCreateWithUTF8CString(rendered); - free(rendered); - return JSValueMakeString(ctx, profile); +const char *nativeProfilerEnd( JSContextRef ctx, const char *title) { + return JSEndProfilingAndRender(ctx, title); } diff --git a/JSCLegacyProfiler/Makefile b/JSCLegacyProfiler/Makefile index 0cf8f6afe..a1f704d98 100644 --- a/JSCLegacyProfiler/Makefile +++ b/JSCLegacyProfiler/Makefile @@ -110,5 +110,3 @@ armv7: ${HEADER_PATHS} \ -undefined dynamic_lookup \ ./JSCLegacyProfiler.mm ./tmp/yajl.a - -.PHONY: ios8 diff --git a/React/Executors/RCTContextExecutor.m b/React/Executors/RCTContextExecutor.m index 8b051fa67..be12934f0 100644 --- a/React/Executors/RCTContextExecutor.m +++ b/React/Executors/RCTContextExecutor.m @@ -16,6 +16,7 @@ #import "RCTAssert.h" #import "RCTDefines.h" +#import "RCTDevMenu.h" #import "RCTLog.h" #import "RCTProfile.h" #import "RCTPerformanceLogger.h" @@ -89,6 +90,7 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init) } @synthesize valid = _valid; +@synthesize bridge = _bridge; RCT_EXPORT_MODULE() @@ -285,11 +287,18 @@ static JSValueRef RCTNativeTraceEndSection(JSContextRef context, __unused JSObje #if RCT_JSC_PROFILER void *JSCProfiler = dlopen(RCT_JSC_PROFILER_DYLIB, RTLD_NOW); if (JSCProfiler != NULL) { - JSObjectCallAsFunctionCallback nativeProfilerStart = dlsym(JSCProfiler, "nativeProfilerStart"); - JSObjectCallAsFunctionCallback nativeProfilerEnd = dlsym(JSCProfiler, "nativeProfilerEnd"); + void (*nativeProfilerStart)(JSContextRef, const char *) = (void (*)(JSContextRef, const char *))dlsym(JSCProfiler, "nativeProfilerStart"); + const char *(*nativeProfilerEnd)(JSContextRef, const char *) = (const char *(*)(JSContextRef, const char *))dlsym(JSCProfiler, "nativeProfilerEnd"); if (nativeProfilerStart != NULL && nativeProfilerEnd != NULL) { - [strongSelf _addNativeHook:nativeProfilerStart withName:"nativeProfilerStart"]; - [strongSelf _addNativeHook:nativeProfilerEnd withName:"nativeProfilerStop"]; + __block BOOL isProfiling = NO; + [_bridge.devMenu addItem:@"Profile" handler:^{ + if (isProfiling) { + RCTLogInfo(@"%s", nativeProfilerEnd(strongSelf->_context.ctx, "profile")); + } else { + nativeProfilerStart(strongSelf->_context.ctx, "profile"); + } + isProfiling = !isProfiling; + }]; } } #endif diff --git a/React/Modules/RCTDevMenu.m b/React/Modules/RCTDevMenu.m index 56ef7dfd4..b99bd5554 100644 --- a/React/Modules/RCTDevMenu.m +++ b/React/Modules/RCTDevMenu.m @@ -314,7 +314,7 @@ RCT_EXPORT_MODULE() self.liveReloadEnabled = !_liveReloadEnabled; }]]; - NSString *profilingTitle = RCTProfileIsProfiling() ? @"Stop Profiling" : @"Start Profiling"; + NSString *profilingTitle = RCTProfileIsProfiling() ? @"Stop Systrace" : @"Start Systrace"; [items addObject:[[RCTDevMenuItem alloc] initWithTitle:profilingTitle handler:^{ self.profilingEnabled = !_profilingEnabled; }]];