Skip to content

Commit

Permalink
Move HermesSamplingProfiler to OSS
Browse files Browse the repository at this point in the history
Reviewed By: willholen

Differential Revision: D16069575

fbshipit-source-id: a67d19be8790a27e6b3fbd2da0d5c9fdd1e9d53a
  • Loading branch information
axe-fb authored and facebook-github-bot committed Aug 13, 2019
1 parent 47afa7c commit 054d2fc
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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)/../../../../../../../..
Original file line number Diff line number Diff line change
@@ -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",
],
)
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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_ */
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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();
});
}
1 change: 1 addition & 0 deletions ReactAndroid/src/main/jni/react/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 054d2fc

Please sign in to comment.