Skip to content

Commit aee74ef

Browse files
committed
[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.
1 parent e15f584 commit aee74ef

5 files changed

Lines changed: 23 additions & 58 deletions

File tree

JSCLegacyProfiler/JSCLegacyProfiler.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,7 @@
66

77
extern "C" {
88

9-
JSValueRef nativeProfilerStart(
10-
JSContextRef ctx,
11-
JSObjectRef function,
12-
JSObjectRef thisObject,
13-
size_t argumentCount,
14-
const JSValueRef arguments[],
15-
JSValueRef *exception);
16-
17-
JSValueRef nativeProfilerEnd(
18-
JSContextRef ctx,
19-
JSObjectRef function,
20-
JSObjectRef thisObject,
21-
size_t argumentCount,
22-
const JSValueRef arguments[],
23-
JSValueRef *exception);
9+
void nativeProfilerStart(JSContextRef ctx, const char *title);
10+
const char *nativeProfilerEnd(JSContextRef ctx, const char *title);
2411

2512
}

JSCLegacyProfiler/JSCLegacyProfiler.mm

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "OpaqueJSString.h"
88
#include "JSProfilerPrivate.h"
99
#include "JSStringRef.h"
10+
#include "String.h"
1011

1112
#include <YAJL/yajl_gen.h>
1213

@@ -114,48 +115,18 @@ static yajl_gen_status append_node_json(yajl_gen gen, const JSC::ProfileNode *no
114115
return json_copy;
115116
}
116117

117-
static char *JSEndProfilingAndRender(JSContextRef ctx, JSStringRef title)
118+
static const char *JSEndProfilingAndRender(JSContextRef ctx, const char *title)
118119
{
119120
JSC::ExecState *exec = toJS(ctx);
120121
JSC::LegacyProfiler *profiler = JSC::LegacyProfiler::profiler();
121-
RefPtr<JSC::Profile> rawProfile = profiler->stopProfiling(exec, title->string());
122+
RefPtr<JSC::Profile> rawProfile = profiler->stopProfiling(exec, WTF::String(title));
122123
return convert_to_json(rawProfile.get());
123124
}
124125

125-
JSValueRef nativeProfilerStart(
126-
JSContextRef ctx,
127-
JSObjectRef function,
128-
JSObjectRef thisObject,
129-
size_t argumentCount,
130-
const JSValueRef arguments[],
131-
JSValueRef *exception) {
132-
if (argumentCount < 1) {
133-
// Could raise an exception here.
134-
return JSValueMakeUndefined(ctx);
135-
}
136-
137-
JSStringRef title = JSValueToStringCopy(ctx, arguments[0], NULL);
138-
JSStartProfiling(ctx, title);
139-
JSStringRelease(title);
140-
return JSValueMakeUndefined(ctx);
126+
void nativeProfilerStart(JSContextRef ctx, const char *title) {
127+
JSStartProfiling(ctx, JSStringCreateWithUTF8CString(title));
141128
}
142129

143-
JSValueRef nativeProfilerEnd(
144-
JSContextRef ctx,
145-
JSObjectRef function,
146-
JSObjectRef thisObject,
147-
size_t argumentCount,
148-
const JSValueRef arguments[],
149-
JSValueRef *exception) {
150-
if (argumentCount < 1) {
151-
// Could raise an exception here.
152-
return JSValueMakeUndefined(ctx);
153-
}
154-
155-
JSStringRef title = JSValueToStringCopy(ctx, arguments[0], NULL);
156-
char *rendered = JSEndProfilingAndRender(ctx, title);
157-
JSStringRelease(title);
158-
JSStringRef profile = JSStringCreateWithUTF8CString(rendered);
159-
free(rendered);
160-
return JSValueMakeString(ctx, profile);
130+
const char *nativeProfilerEnd( JSContextRef ctx, const char *title) {
131+
return JSEndProfilingAndRender(ctx, title);
161132
}

JSCLegacyProfiler/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,3 @@ armv7:
110110
${HEADER_PATHS} \
111111
-undefined dynamic_lookup \
112112
./JSCLegacyProfiler.mm ./tmp/yajl.a
113-
114-
.PHONY: ios8

React/Executors/RCTContextExecutor.m

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import "RCTAssert.h"
1818
#import "RCTDefines.h"
19+
#import "RCTDevMenu.h"
1920
#import "RCTLog.h"
2021
#import "RCTProfile.h"
2122
#import "RCTPerformanceLogger.h"
@@ -89,6 +90,7 @@ @implementation RCTContextExecutor
8990
}
9091

9192
@synthesize valid = _valid;
93+
@synthesize bridge = _bridge;
9294

9395
RCT_EXPORT_MODULE()
9496

@@ -285,11 +287,18 @@ - (void)setUp
285287
#if RCT_JSC_PROFILER
286288
void *JSCProfiler = dlopen(RCT_JSC_PROFILER_DYLIB, RTLD_NOW);
287289
if (JSCProfiler != NULL) {
288-
JSObjectCallAsFunctionCallback nativeProfilerStart = dlsym(JSCProfiler, "nativeProfilerStart");
289-
JSObjectCallAsFunctionCallback nativeProfilerEnd = dlsym(JSCProfiler, "nativeProfilerEnd");
290+
void (*nativeProfilerStart)(JSContextRef, const char *) = (void (*)(JSContextRef, const char *))dlsym(JSCProfiler, "nativeProfilerStart");
291+
const char *(*nativeProfilerEnd)(JSContextRef, const char *) = (const char *(*)(JSContextRef, const char *))dlsym(JSCProfiler, "nativeProfilerEnd");
290292
if (nativeProfilerStart != NULL && nativeProfilerEnd != NULL) {
291-
[strongSelf _addNativeHook:nativeProfilerStart withName:"nativeProfilerStart"];
292-
[strongSelf _addNativeHook:nativeProfilerEnd withName:"nativeProfilerStop"];
293+
__block BOOL isProfiling = NO;
294+
[_bridge.devMenu addItem:@"Profile" handler:^{
295+
if (isProfiling) {
296+
RCTLogInfo(@"%s", nativeProfilerEnd(strongSelf->_context.ctx, "profile"));
297+
} else {
298+
nativeProfilerStart(strongSelf->_context.ctx, "profile");
299+
}
300+
isProfiling = !isProfiling;
301+
}];
293302
}
294303
}
295304
#endif

React/Modules/RCTDevMenu.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ - (NSArray *)menuItems
314314
self.liveReloadEnabled = !_liveReloadEnabled;
315315
}]];
316316

317-
NSString *profilingTitle = RCTProfileIsProfiling() ? @"Stop Profiling" : @"Start Profiling";
317+
NSString *profilingTitle = RCTProfileIsProfiling() ? @"Stop Systrace" : @"Start Systrace";
318318
[items addObject:[[RCTDevMenuItem alloc] initWithTitle:profilingTitle handler:^{
319319
self.profilingEnabled = !_profilingEnabled;
320320
}]];

0 commit comments

Comments
 (0)