Support JS Systrace on all platforms

Reviewed By: tadeuzagallo

Differential Revision: D3234830

fbshipit-source-id: 94cc870d47d620c8bd8d35f83d0b017e5ddba90d
This commit is contained in:
Marc Horowitz 2016-05-13 17:15:03 -07:00 committed by Facebook Github Bot 8
parent b7fe8e68be
commit 86f2eb18e5
6 changed files with 127 additions and 72 deletions

View File

@ -48,6 +48,7 @@ elif THIS_IS_FBOBJC:
],
**kwargs_add(
kwargs,
preprocessor_flags = DEBUG_PREPROCESSOR_FLAGS,
deps = [
'//xplat/folly:molly',
]
@ -57,6 +58,7 @@ elif THIS_IS_FBOBJC:
LOCAL_HEADERS = [
'JSCTracing.h',
'JSCLegacyProfiler.h',
'JSCLegacyTracing.h',
'JSCMemory.h',
'JSCPerfStats.h',
]

View File

@ -18,9 +18,13 @@
#include "Platform.h"
#include "Value.h"
#ifdef WITH_JSC_EXTRA_TRACING
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
#include "JSCTracing.h"
#endif
#ifdef WITH_JSC_EXTRA_TRACING
#include "JSCLegacyProfiler.h"
#include "JSCLegacyTracing.h"
#include <JavaScriptCore/API/JSProfilerPrivate.h>
#endif
@ -204,9 +208,13 @@ void JSCExecutor::initOnJSVMThread() {
installGlobalFunction(m_context, "nativeLoggingHook", JSNativeHooks::loggingHook);
installGlobalFunction(m_context, "nativePerformanceNow", JSNativeHooks::nowHook);
#ifdef WITH_JSC_EXTRA_TRACING
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
addNativeTracingHooks(m_context);
#endif
#ifdef WITH_JSC_EXTRA_TRACING
addNativeProfilingHooks(m_context);
addNativeTracingLegacyHooks(m_context);
PerfLogging::installNativeHooks(m_context);
#endif
@ -301,6 +309,8 @@ void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic&
}
void JSCExecutor::setGlobalVariable(const std::string& propName, const std::string& jsonValue) {
// TODO mhorowitz: systrace this.
auto globalObject = JSContextGetGlobalObject(m_context);
String jsPropertyName(propName.c_str());

View File

@ -0,0 +1,73 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#ifdef WITH_JSC_EXTRA_TRACING
#include "JSCLegacyTracing.h"
#include <JavaScriptCore/JavaScript.h>
#include <JavaScriptCore/API/JSProfilerPrivate.h>
#include <fbsystrace.h>
#include "JSCHelpers.h"
#include "JSCTracing.h"
static const char *ENABLED_FBSYSTRACE_PROFILE_NAME = "__fbsystrace__";
static JSValueRef nativeTraceBeginLegacy(
JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef* exception) {
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
}
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
#if WITH_REACT_INTERNAL_SETTINGS
JSStartProfiling(ctx, title, true);
#else
JSStartProfiling(ctx, title);
#endif
JSStringRelease(title);
return JSValueMakeUndefined(ctx);
}
static JSValueRef nativeTraceEndLegacy(
JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef* exception) {
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
}
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
JSEndProfiling(ctx, title);
JSStringRelease(title);
return JSValueMakeUndefined(ctx);
}
namespace facebook {
namespace react {
void addNativeTracingLegacyHooks(JSGlobalContextRef ctx) {
installGlobalFunction(ctx, "nativeTraceBeginLegacy", nativeTraceBeginLegacy);
installGlobalFunction(ctx, "nativeTraceEndLegacy", nativeTraceEndLegacy);
}
} }
#endif

View File

@ -0,0 +1,15 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#if defined(WITH_JSC_EXTRA_TRACING)
#include <JavaScriptCore/JSContextRef.h>
namespace facebook {
namespace react {
void addNativeTracingLegacyHooks(JSGlobalContextRef ctx);
} }
#endif

View File

@ -1,10 +1,11 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#ifdef WITH_JSC_EXTRA_TRACING
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
#include "JSCTracing.h"
#include <algorithm>
#include <JavaScriptCore/JavaScript.h>
#include <JavaScriptCore/API/JSProfilerPrivate.h>
#include <fbsystrace.h>
#include <sys/types.h>
#include <unistd.h>
@ -12,20 +13,6 @@
using std::min;
static const char *ENABLED_FBSYSTRACE_PROFILE_NAME = "__fbsystrace__";
static uint64_t tagFromJSValue(
JSContextRef ctx,
JSValueRef value,
JSValueRef* exception) {
// XXX validate that this is a lossless conversion.
// XXX should we just have separate functions for bridge, infra, and apps,
// then drop this argument to save time?
(void)exception;
uint64_t tag = (uint64_t) JSValueToNumber(ctx, value, NULL);
return tag;
}
static int64_t int64FromJSValue(
JSContextRef ctx,
JSValueRef value,
@ -105,7 +92,7 @@ static JSValueRef nativeTraceBeginSection(
return JSValueMakeUndefined(ctx);
}
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
@ -145,7 +132,7 @@ static JSValueRef nativeTraceEndSection(
return JSValueMakeUndefined(ctx);
}
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
@ -189,7 +176,7 @@ static JSValueRef beginOrEndAsync(
return JSValueMakeUndefined(ctx);
}
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
@ -252,7 +239,7 @@ static JSValueRef stageAsync(
return JSValueMakeUndefined(ctx);
}
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
@ -398,7 +385,7 @@ static JSValueRef nativeTraceCounter(
return JSValueMakeUndefined(ctx);
}
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
uint64_t tag = facebook::react::tracingTagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
@ -414,60 +401,24 @@ static JSValueRef nativeTraceCounter(
return JSValueMakeUndefined(ctx);
}
static JSValueRef nativeTraceBeginLegacy(
JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef* exception) {
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
}
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
#if WITH_REACT_INTERNAL_SETTINGS
JSStartProfiling(ctx, title, true);
#else
JSStartProfiling(ctx, title);
#endif
JSStringRelease(title);
return JSValueMakeUndefined(ctx);
}
static JSValueRef nativeTraceEndLegacy(
JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef* exception) {
if (FBSYSTRACE_LIKELY(argumentCount >= 1)) {
uint64_t tag = tagFromJSValue(ctx, arguments[0], exception);
if (!fbsystrace_is_tracing(tag)) {
return JSValueMakeUndefined(ctx);
}
}
JSStringRef title = JSStringCreateWithUTF8CString(ENABLED_FBSYSTRACE_PROFILE_NAME);
JSEndProfiling(ctx, title);
JSStringRelease(title);
return JSValueMakeUndefined(ctx);
}
namespace facebook {
namespace react {
uint64_t tracingTagFromJSValue(
JSContextRef ctx,
JSValueRef value,
JSValueRef* exception) {
// XXX validate that this is a lossless conversion.
// XXX should we just have separate functions for bridge, infra, and apps,
// then drop this argument to save time?
(void)exception;
uint64_t tag = (uint64_t) JSValueToNumber(ctx, value, NULL);
return tag;
}
void addNativeTracingHooks(JSGlobalContextRef ctx) {
installGlobalFunction(ctx, "nativeTraceBeginSection", nativeTraceBeginSection);
installGlobalFunction(ctx, "nativeTraceEndSection", nativeTraceEndSection);
installGlobalFunction(ctx, "nativeTraceBeginLegacy", nativeTraceBeginLegacy);
installGlobalFunction(ctx, "nativeTraceEndLegacy", nativeTraceEndLegacy);
installGlobalFunction(ctx, "nativeTraceBeginAsyncSection", nativeTraceBeginAsyncSection);
installGlobalFunction(ctx, "nativeTraceEndAsyncSection", nativeTraceEndAsyncSection);
installGlobalFunction(ctx, "nativeTraceAsyncSectionStage", nativeTraceAsyncSectionStage);

View File

@ -2,12 +2,16 @@
#pragma once
#ifdef WITH_JSC_EXTRA_TRACING
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
#include <inttypes.h>
#include <JavaScriptCore/JSContextRef.h>
namespace facebook {
namespace react {
uint64_t tracingTagFromJSValue(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
void addNativeTracingHooks(JSGlobalContextRef ctx);
} }