diff --git a/ReactAndroid/src/main/jni/react/BUCK b/ReactAndroid/src/main/jni/react/BUCK index 292463593..809905b50 100644 --- a/ReactAndroid/src/main/jni/react/BUCK +++ b/ReactAndroid/src/main/jni/react/BUCK @@ -54,6 +54,7 @@ react_library( 'MethodCall.cpp', 'JSCHelpers.cpp', 'JSCExecutor.cpp', + 'JSCPerfStats.cpp', 'JSCTracing.cpp', 'JSCMemory.cpp', 'JSCLegacyProfiler.cpp', @@ -63,6 +64,7 @@ react_library( 'JSCTracing.h', 'JSCLegacyProfiler.h', 'JSCMemory.h', + 'JSCPerfStats.h', ], exported_headers = [ 'Bridge.h', diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp index 048758ab6..feb9c63f9 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp @@ -40,6 +40,10 @@ using fbsystrace::FbSystraceSection; #include #endif +#ifdef JSC_HAS_PERF_STATS_API +#include "JSCPerfStats.h" +#endif + static const int64_t NANOSECONDS_IN_SECOND = 1000000000LL; static const int64_t NANOSECONDS_IN_MILLISECOND = 1000000LL; @@ -179,6 +183,10 @@ void JSCExecutor::initOnJSVMThread() { #ifdef WITH_FB_MEMORY_PROFILING addNativeMemoryHooks(m_context); #endif + + #ifdef JSC_HAS_PERF_STATS_API + addJSCPerfStatsHooks(m_context); + #endif } void JSCExecutor::terminateOnJSVMThread() { diff --git a/ReactAndroid/src/main/jni/react/JSCPerfStats.cpp b/ReactAndroid/src/main/jni/react/JSCPerfStats.cpp new file mode 100644 index 000000000..9ef341c9e --- /dev/null +++ b/ReactAndroid/src/main/jni/react/JSCPerfStats.cpp @@ -0,0 +1,65 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#include "JSCPerfStats.h" + +#ifdef JSC_HAS_PERF_STATS_API + +#include +#include + +#include "JSCHelpers.h" +#include "Value.h" + +static JSValueRef nativeGetHeapStats( + JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) { + JSHeapStats heapStats = {0}; + JSGetHeapStats(ctx, &heapStats); + + auto result = facebook::react::Object::create(ctx); + result.setProperty("size", {ctx, JSValueMakeNumber(ctx, heapStats.size)}); + result.setProperty("extra_size", {ctx, JSValueMakeNumber(ctx, heapStats.extraSize)}); + result.setProperty("capacity", {ctx, JSValueMakeNumber(ctx, heapStats.capacity)}); + result.setProperty("object_count", {ctx, JSValueMakeNumber(ctx, heapStats.objectCount)}); + + return (JSObjectRef) result; +} + +static JSValueRef nativeGetGCStats( + JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) { + JSGCStats gcStats = {0}; + JSGetGCStats(ctx, &gcStats); + + auto result = facebook::react::Object::create(ctx); + result.setProperty( + "last_full_gc_length", + {ctx, JSValueMakeNumber(ctx, gcStats.lastFullGCLength)}); + result.setProperty( + "last_eden_gc_length", + {ctx, JSValueMakeNumber(ctx, gcStats.lastEdenGCLength)}); + + return (JSObjectRef) result; +} + +#endif + +namespace facebook { +namespace react { + +void addJSCPerfStatsHooks(JSGlobalContextRef ctx) { +#ifdef JSC_HAS_PERF_STATS_API + installGlobalFunction(ctx, "nativeGetHeapStats", nativeGetHeapStats); + installGlobalFunction(ctx, "nativeGetGCStats", nativeGetGCStats); +#endif +} + +} } diff --git a/ReactAndroid/src/main/jni/react/JSCPerfStats.h b/ReactAndroid/src/main/jni/react/JSCPerfStats.h new file mode 100644 index 000000000..73ca29e8b --- /dev/null +++ b/ReactAndroid/src/main/jni/react/JSCPerfStats.h @@ -0,0 +1,12 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +#include + +namespace facebook { +namespace react { + +void addJSCPerfStatsHooks(JSGlobalContextRef ctx); + +} }