Skip to content

Commit 86f2eb1

Browse files
mhorowitzFacebook Github Bot 8
authored andcommitted
Support JS Systrace on all platforms
Reviewed By: tadeuzagallo Differential Revision: D3234830 fbshipit-source-id: 94cc870d47d620c8bd8d35f83d0b017e5ddba90d
1 parent b7fe8e6 commit 86f2eb1

File tree

6 files changed

+125
-70
lines changed

6 files changed

+125
-70
lines changed

ReactCommon/bridge/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ elif THIS_IS_FBOBJC:
4848
],
4949
**kwargs_add(
5050
kwargs,
51+
preprocessor_flags = DEBUG_PREPROCESSOR_FLAGS,
5152
deps = [
5253
'//xplat/folly:molly',
5354
]
@@ -57,6 +58,7 @@ elif THIS_IS_FBOBJC:
5758
LOCAL_HEADERS = [
5859
'JSCTracing.h',
5960
'JSCLegacyProfiler.h',
61+
'JSCLegacyTracing.h',
6062
'JSCMemory.h',
6163
'JSCPerfStats.h',
6264
]

ReactCommon/bridge/JSCExecutor.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
#include "Platform.h"
1919
#include "Value.h"
2020

21-
#ifdef WITH_JSC_EXTRA_TRACING
21+
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
2222
#include "JSCTracing.h"
23+
#endif
24+
25+
#ifdef WITH_JSC_EXTRA_TRACING
2326
#include "JSCLegacyProfiler.h"
27+
#include "JSCLegacyTracing.h"
2428
#include <JavaScriptCore/API/JSProfilerPrivate.h>
2529
#endif
2630

@@ -204,9 +208,13 @@ void JSCExecutor::initOnJSVMThread() {
204208
installGlobalFunction(m_context, "nativeLoggingHook", JSNativeHooks::loggingHook);
205209
installGlobalFunction(m_context, "nativePerformanceNow", JSNativeHooks::nowHook);
206210

207-
#ifdef WITH_JSC_EXTRA_TRACING
211+
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
208212
addNativeTracingHooks(m_context);
213+
#endif
214+
215+
#ifdef WITH_JSC_EXTRA_TRACING
209216
addNativeProfilingHooks(m_context);
217+
addNativeTracingLegacyHooks(m_context);
210218
PerfLogging::installNativeHooks(m_context);
211219
#endif
212220

@@ -301,6 +309,8 @@ void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic&
301309
}
302310

303311
void JSCExecutor::setGlobalVariable(const std::string& propName, const std::string& jsonValue) {
312+
// TODO mhorowitz: systrace this.
313+
304314
auto globalObject = JSContextGetGlobalObject(m_context);
305315
String jsPropertyName(propName.c_str());
306316

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
#ifdef WITH_JSC_EXTRA_TRACING
4+
5+
#include "JSCLegacyTracing.h"
6+
7+
#include <JavaScriptCore/JavaScript.h>
8+
#include <JavaScriptCore/API/JSProfilerPrivate.h>
9+
10+
#include <fbsystrace.h>
11+
12+
#include "JSCHelpers.h"
13+
#include "JSCTracing.h"
14+
15+
static const char *ENABLED_FBSYSTRACE_PROFILE_NAME = "__fbsystrace__";
16+
17+
static JSValueRef nativeTraceBeginLegacy(
18+
JSContextRef ctx,
19+
JSObjectRef function,
20+
JSObjectRef thisObject,
21+
size_t argumentCount,
22+
const JSValueRef arguments[],
23+
JSValueRef* exception) {
24+
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
25+
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
26+
if (!fbsystrace_is_tracing(tag)) {
27+
return JSValueMakeUndefined(ctx);
28+
}
29+
}
30+
31+
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
32+
#if WITH_REACT_INTERNAL_SETTINGS
33+
JSStartProfiling(ctx, title, true);
34+
#else
35+
JSStartProfiling(ctx, title);
36+
#endif
37+
JSStringRelease(title);
38+
39+
return JSValueMakeUndefined(ctx);
40+
}
41+
42+
static JSValueRef nativeTraceEndLegacy(
43+
JSContextRef ctx,
44+
JSObjectRef function,
45+
JSObjectRef thisObject,
46+
size_t argumentCount,
47+
const JSValueRef arguments[],
48+
JSValueRef* exception) {
49+
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
50+
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
51+
if (!fbsystrace_is_tracing(tag)) {
52+
return JSValueMakeUndefined(ctx);
53+
}
54+
}
55+
56+
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
57+
JSEndProfiling(ctx, title);
58+
JSStringRelease(title);
59+
60+
return JSValueMakeUndefined(ctx);
61+
}
62+
63+
namespace facebook {
64+
namespace react {
65+
66+
void addNativeTracingLegacyHooks(JSGlobalContextRef ctx) {
67+
installGlobalFunction(ctx, "nativeTraceBeginLegacy", nativeTraceBeginLegacy);
68+
installGlobalFunction(ctx, "nativeTraceEndLegacy", nativeTraceEndLegacy);
69+
}
70+
71+
} }
72+
73+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
#pragma once
4+
5+
#if defined(WITH_JSC_EXTRA_TRACING)
6+
7+
#include <JavaScriptCore/JSContextRef.h>
8+
namespace facebook {
9+
namespace react {
10+
11+
void addNativeTracingLegacyHooks(JSGlobalContextRef ctx);
12+
13+
} }
14+
15+
#endif

ReactCommon/bridge/JSCTracing.cpp

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
// Copyright 2004-present Facebook. All Rights Reserved.
22

3-
#ifdef WITH_JSC_EXTRA_TRACING
3+
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
4+
5+
#include "JSCTracing.h"
46

57
#include <algorithm>
68
#include <JavaScriptCore/JavaScript.h>
7-
#include <JavaScriptCore/API/JSProfilerPrivate.h>
89
#include <fbsystrace.h>
910
#include <sys/types.h>
1011
#include <unistd.h>
1112
#include "JSCHelpers.h"
1213

1314
using std::min;
1415

15-
static const char *ENABLED_FBSYSTRACE_PROFILE_NAME = "__fbsystrace__";
16-
17-
static uint64_t tagFromJSValue(
18-
JSContextRef ctx,
19-
JSValueRef value,
20-
JSValueRef* exception) {
21-
// XXX validate that this is a lossless conversion.
22-
// XXX should we just have separate functions for bridge, infra, and apps,
23-
// then drop this argument to save time?
24-
(void)exception;
25-
uint64_t tag = (uint64_t) JSValueToNumber(ctx, value, NULL);
26-
return tag;
27-
}
28-
2916
static int64_t int64FromJSValue(
3017
JSContextRef ctx,
3118
JSValueRef value,
@@ -105,7 +92,7 @@ static JSValueRef nativeTraceBeginSection(
10592
return JSValueMakeUndefined(ctx);
10693
}
10794

108-
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
95+
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
10996
if (!fbsystrace_is_tracing(tag)) {
11097
return JSValueMakeUndefined(ctx);
11198
}
@@ -145,7 +132,7 @@ static JSValueRef nativeTraceEndSection(
145132
return JSValueMakeUndefined(ctx);
146133
}
147134

148-
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
135+
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
149136
if (!fbsystrace_is_tracing(tag)) {
150137
return JSValueMakeUndefined(ctx);
151138
}
@@ -189,7 +176,7 @@ static JSValueRef beginOrEndAsync(
189176
return JSValueMakeUndefined(ctx);
190177
}
191178

192-
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
179+
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
193180
if (!fbsystrace_is_tracing(tag)) {
194181
return JSValueMakeUndefined(ctx);
195182
}
@@ -252,7 +239,7 @@ static JSValueRef stageAsync(
252239
return JSValueMakeUndefined(ctx);
253240
}
254241

255-
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
242+
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
256243
if (!fbsystrace_is_tracing(tag)) {
257244
return JSValueMakeUndefined(ctx);
258245
}
@@ -398,7 +385,7 @@ static JSValueRef nativeTraceCounter(
398385
return JSValueMakeUndefined(ctx);
399386
}
400387

401-
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
388+
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
402389
if (!fbsystrace_is_tracing(tag)) {
403390
return JSValueMakeUndefined(ctx);
404391
}
@@ -414,60 +401,24 @@ static JSValueRef nativeTraceCounter(
414401
return JSValueMakeUndefined(ctx);
415402
}
416403

417-
static JSValueRef nativeTraceBeginLegacy(
418-
JSContextRef ctx,
419-
JSObjectRef function,
420-
JSObjectRef thisObject,
421-
size_t argumentCount,
422-
const JSValueRef arguments[],
423-
JSValueRef* exception) {
424-
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
425-
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
426-
if (!fbsystrace_is_tracing(tag)) {
427-
return JSValueMakeUndefined(ctx);
428-
}
429-
}
430-
431-
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
432-
#if WITH_REACT_INTERNAL_SETTINGS
433-
JSStartProfiling(ctx, title, true);
434-
#else
435-
JSStartProfiling(ctx, title);
436-
#endif
437-
JSStringRelease(title);
438-
439-
return JSValueMakeUndefined(ctx);
440-
}
404+
namespace facebook {
405+
namespace react {
441406

442-
static JSValueRef nativeTraceEndLegacy(
407+
uint64_t tracingTagFromJSValue(
443408
JSContextRef ctx,
444-
JSObjectRef function,
445-
JSObjectRef thisObject,
446-
size_t argumentCount,
447-
const JSValueRef arguments[],
409+
JSValueRef value,
448410
JSValueRef* exception) {
449-
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
450-
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
451-
if (!fbsystrace_is_tracing(tag)) {
452-
return JSValueMakeUndefined(ctx);
453-
}
454-
}
455-
456-
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
457-
JSEndProfiling(ctx, title);
458-
JSStringRelease(title);
459-
460-
return JSValueMakeUndefined(ctx);
411+
// XXX validate that this is a lossless conversion.
412+
// XXX should we just have separate functions for bridge, infra, and apps,
413+
// then drop this argument to save time?
414+
(void)exception;
415+
uint64_t tag = (uint64_t) JSValueToNumber(ctx, value, NULL);
416+
return tag;
461417
}
462418

463-
namespace facebook {
464-
namespace react {
465-
466419
void addNativeTracingHooks(JSGlobalContextRef ctx) {
467420
installGlobalFunction(ctx, "nativeTraceBeginSection", nativeTraceBeginSection);
468421
installGlobalFunction(ctx, "nativeTraceEndSection", nativeTraceEndSection);
469-
installGlobalFunction(ctx, "nativeTraceBeginLegacy", nativeTraceBeginLegacy);
470-
installGlobalFunction(ctx, "nativeTraceEndLegacy", nativeTraceEndLegacy);
471422
installGlobalFunction(ctx, "nativeTraceBeginAsyncSection", nativeTraceBeginAsyncSection);
472423
installGlobalFunction(ctx, "nativeTraceEndAsyncSection", nativeTraceEndAsyncSection);
473424
installGlobalFunction(ctx, "nativeTraceAsyncSectionStage", nativeTraceAsyncSectionStage);

ReactCommon/bridge/JSCTracing.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
#pragma once
44

5-
#ifdef WITH_JSC_EXTRA_TRACING
5+
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
6+
7+
#include <inttypes.h>
68

79
#include <JavaScriptCore/JSContextRef.h>
10+
811
namespace facebook {
912
namespace react {
1013

14+
uint64_t tracingTagFromJSValue(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
1115
void addNativeTracingHooks(JSGlobalContextRef ctx);
1216

1317
} }

0 commit comments

Comments
 (0)