From 054d2fc172dd9a8018303969ef1ce08e697e38d4 Mon Sep 17 00:00:00 2001 From: Ram N Date: Mon, 12 Aug 2019 18:19:40 -0700 Subject: [PATCH] Move HermesSamplingProfiler to OSS Reviewed By: willholen Differential Revision: D16069575 fbshipit-source-id: a67d19be8790a27e6b3fbd2da0d5c9fdd1e9d53a --- .../hermes/instrumentation/Android.mk | 27 ++++++++++++++ .../com/facebook/hermes/instrumentation/BUCK | 35 +++++++++++++++++- .../HermesSamplingProfiler.cpp | 37 +++++++++++++++++++ .../instrumentation/HermesSamplingProfiler.h | 36 ++++++++++++++++++ .../HermesSamplingProfiler.java | 27 ++++++++++++++ .../hermes/instrumentation/OnLoad.cpp | 9 +++++ .../src/main/jni/react/jni/Android.mk | 1 + 7 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/Android.mk create mode 100644 ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.cpp create mode 100644 ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.h create mode 100644 ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.java create mode 100644 ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/OnLoad.cpp diff --git a/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/Android.mk b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/Android.mk new file mode 100644 index 00000000000000..126879958859a2 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/Android.mk @@ -0,0 +1,27 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +REACT_NATIVE := $(LOCAL_PATH)/../../../../../../../.. + +LOCAL_MODULE := jsijniprofiler + +LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp) + +LOCAL_C_INCLUDES := $(LOCAL_PATH) $(REACT_NATIVE)/ReactCommon/jsi $(REACT_NATIVE)/node_modules/hermes-engine/android/include $(REACT_NATIVE)/../hermes-engine/android/include $(REACT_NATIVE)/../node_modules/hermes-engine/include + +LOCAL_CPP_FEATURES := exceptions + +LOCAL_STATIC_LIBRARIES := libjsireact libjsi +LOCAL_SHARED_LIBRARIES := libfolly_json libfb libreactnativejni libhermes + +include $(BUILD_SHARED_LIBRARY) + + +include $(CLEAR_VARS) +REACT_NATIVE := $(LOCAL_PATH)/../../../../../../../.. diff --git a/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/BUCK b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/BUCK index b27cc8023216af..ecdbb7985906c6 100644 --- a/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/BUCK @@ -1,9 +1,40 @@ -load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library") +load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "FBJNI_TARGET", "react_native_dep", "react_native_target", "rn_android_library", "rn_xplat_cxx_library") rn_android_library( name = "instrumentation", - srcs = glob(["**/*.java"]), + srcs = ["HermesMemoryDumper.java"], visibility = [ "PUBLIC", ], ) + +rn_android_library( + name = "hermes_samplingprofiler", + srcs = ["HermesSamplingProfiler.java"], + visibility = ["PUBLIC"], + deps = [ + react_native_dep("java/com/facebook/proguard/annotations:annotations"), + react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"), + react_native_dep("java/com/facebook/jni:jni"), + ":jni_hermes_samplingprofiler", + ], +) + +rn_xplat_cxx_library( + name = "jni_hermes_samplingprofiler", + srcs = ["HermesSamplingProfiler.cpp"], + headers = ["HermesSamplingProfiler.h"], + header_namespace = "", + #allow_jni_merging = True, + compiler_flags = ["-fexceptions"], + platforms = ANDROID, + soname = "libjsijniprofiler.$(ext)", + visibility = [ + "fbandroid//java/com/facebook/jsi:jsi", + ], + deps = [ + react_native_target("jni/react/jni:jni"), + FBJNI_TARGET, + "fbsource//xplat/hermes/API:HermesAPI", + ], +) diff --git a/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.cpp b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.cpp new file mode 100644 index 00000000000000..6fb26eea35f9ed --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.cpp @@ -0,0 +1,37 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#include "HermesSamplingProfiler.h" + +#include + +namespace facebook { +namespace jsi { +namespace jni { + +void HermesSamplingProfiler::enable(jni::alias_ref) { + hermes::HermesRuntime::enableSamplingProfiler(); +} + +void HermesSamplingProfiler::disable(jni::alias_ref) { + hermes::HermesRuntime::disableSamplingProfiler(); +} + +void HermesSamplingProfiler::dumpSampledTraceToFile( + jni::alias_ref, + std::string filename) { + hermes::HermesRuntime::dumpSampledTraceToFile(filename); +} + +void HermesSamplingProfiler::registerNatives() { + javaClassLocal()->registerNatives({ + makeNativeMethod("enable", HermesSamplingProfiler::enable), + makeNativeMethod("disable", HermesSamplingProfiler::enable), + makeNativeMethod( + "dumpSampledTraceToFile", + HermesSamplingProfiler::dumpSampledTraceToFile), + }); +} + +} // namespace jni +} // namespace jsi +} // namespace facebook diff --git a/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.h b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.h new file mode 100644 index 00000000000000..40856294e0d2e9 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.h @@ -0,0 +1,36 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#ifndef HERMESSAMPLINGPROFILER_H_ +#define HERMESSAMPLINGPROFILER_H_ + +#include +#include +#include + +namespace facebook { +namespace jsi { +namespace jni { + +namespace jni = ::facebook::jni; + +class HermesSamplingProfiler : public jni::JavaClass { + public: + constexpr static auto kJavaDescriptor = + "Lcom/facebook/hermes/instrumentation/HermesSamplingProfiler;"; + static void enable(jni::alias_ref); + static void disable(jni::alias_ref); + static void dumpSampledTraceToFile( + jni::alias_ref, + std::string filename); + + static void registerNatives(); + + private: + HermesSamplingProfiler(); +}; + +} // namespace jni +} // namespace jsi +} // namespace facebook + +#endif /* HERMESSAMPLINGPROFILER_H_ */ diff --git a/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.java b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.java new file mode 100644 index 00000000000000..4069c4b26788d2 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/HermesSamplingProfiler.java @@ -0,0 +1,27 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +package com.facebook.hermes.instrumentation; + +import com.facebook.soloader.SoLoader; + +/** Hermes sampling profiler static JSI API. */ +public class HermesSamplingProfiler { + static { + SoLoader.loadLibrary("jsijniprofiler"); + } + + /** Start sample profiling. */ + public static native void enable(); + + /** Stop sample profiling. */ + public static native void disable(); + + /** + * Dump sampled stack traces to file. + * + * @param filename the file to dump sampling trace to. + */ + public static native void dumpSampledTraceToFile(String filename); + + private HermesSamplingProfiler() {} +} diff --git a/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/OnLoad.cpp b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/OnLoad.cpp new file mode 100644 index 00000000000000..7e0626dbb1f8d1 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/hermes/instrumentation/OnLoad.cpp @@ -0,0 +1,9 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#include "HermesSamplingProfiler.h" + +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) { + return facebook::jni::initialize(vm, [] { + facebook::jsi::jni::HermesSamplingProfiler::registerNatives(); + }); +} diff --git a/ReactAndroid/src/main/jni/react/jni/Android.mk b/ReactAndroid/src/main/jni/react/jni/Android.mk index 7446f296d752a3..20b21712d8e519 100644 --- a/ReactAndroid/src/main/jni/react/jni/Android.mk +++ b/ReactAndroid/src/main/jni/react/jni/Android.mk @@ -78,3 +78,4 @@ include $(REACT_SRC_DIR)/turbomodule/core/jni/Android.mk include $(REACT_SRC_DIR)/jscexecutor/Android.mk include $(REACT_SRC_DIR)/../hermes/reactexecutor/Android.mk +include $(REACT_SRC_DIR)/../hermes/instrumentation/Android.mk