Move HermesSamplingProfiler to OSS

Reviewed By: willholen

Differential Revision: D16069575

fbshipit-source-id: a67d19be8790a27e6b3fbd2da0d5c9fdd1e9d53a
This commit is contained in:
Ram N
2019-08-12 18:19:40 -07:00
committed by Facebook Github Bot
parent 47afa7c945
commit 054d2fc172
7 changed files with 170 additions and 2 deletions
@@ -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)/../../../../../../../..
@@ -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",
],
)
@@ -0,0 +1,37 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#include "HermesSamplingProfiler.h"
#include <hermes/hermes.h>
namespace facebook {
namespace jsi {
namespace jni {
void HermesSamplingProfiler::enable(jni::alias_ref<jclass>) {
hermes::HermesRuntime::enableSamplingProfiler();
}
void HermesSamplingProfiler::disable(jni::alias_ref<jclass>) {
hermes::HermesRuntime::disableSamplingProfiler();
}
void HermesSamplingProfiler::dumpSampledTraceToFile(
jni::alias_ref<jclass>,
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
@@ -0,0 +1,36 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#ifndef HERMESSAMPLINGPROFILER_H_
#define HERMESSAMPLINGPROFILER_H_
#include <fb/fbjni.h>
#include <jni/Registration.h>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
namespace jni {
namespace jni = ::facebook::jni;
class HermesSamplingProfiler : public jni::JavaClass<HermesSamplingProfiler> {
public:
constexpr static auto kJavaDescriptor =
"Lcom/facebook/hermes/instrumentation/HermesSamplingProfiler;";
static void enable(jni::alias_ref<jclass>);
static void disable(jni::alias_ref<jclass>);
static void dumpSampledTraceToFile(
jni::alias_ref<jclass>,
std::string filename);
static void registerNatives();
private:
HermesSamplingProfiler();
};
} // namespace jni
} // namespace jsi
} // namespace facebook
#endif /* HERMESSAMPLINGPROFILER_H_ */
@@ -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() {}
}
@@ -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();
});
}