Introduce JNativeModulePerfLogger

Summary:
## Description
This diff introduces `NativeModulePerfLogger.java`, the Android extension (a `jni::HybridClass`) to `NativeModulePerfLogger`.

### Why is this a Hybrid class?
Because we have C++ and Java markers, and the perf-logger has both a Java and a C++ interface that the application must implement. `jni::Hybrid` classes are a convenient solution for these constraints.

Changelog:
[Android][Added] - Introduce JNativeModulePerfLogger

Reviewed By: ejanzer

Differential Revision: D21318052

fbshipit-source-id: 2f43853b243fa2a629068bb4aced1e3f12f038ba
This commit is contained in:
Ramanpreet Nara
2020-07-11 09:38:45 -07:00
committed by Facebook GitHub Bot
parent 46d6e7f575
commit c0dd11e532
7 changed files with 195 additions and 0 deletions
@@ -0,0 +1,22 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")
rn_android_library(
name = "reactperflogger",
srcs = glob(
[
"*.java",
],
),
labels = [
"supermodule:xplat/default/public.react_native.infra",
],
required_for_source_only_abi = True,
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
react_native_dep("libraries/fbjni:java"),
react_native_target("java/com/facebook/react/reactperflogger/jni:jni"),
],
)
@@ -0,0 +1,56 @@
/*
* 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.
*/
package com.facebook.react.perflogger;
import com.facebook.jni.HybridData;
import com.facebook.soloader.SoLoader;
public abstract class NativeModulePerfLogger {
private final HybridData mHybridData;
private static volatile boolean sIsSoLibraryLoaded;
protected abstract HybridData initHybrid();
protected NativeModulePerfLogger() {
maybeLoadOtherSoLibraries();
maybeLoadSoLibrary();
mHybridData = initHybrid();
}
public abstract void moduleDataCreateStart(String moduleName, int id);
public abstract void moduleDataCreateEnd(String moduleName, int id);
public abstract void moduleCreateStart(String moduleName, int id);
public abstract void moduleCreateCacheHit(String moduleName, int id);
public abstract void moduleCreateConstructStart(String moduleName, int id);
public abstract void moduleCreateConstructEnd(String moduleName, int id);
public abstract void moduleCreateSetUpStart(String moduleName, int id);
public abstract void moduleCreateSetUpEnd(String moduleName, int id);
public abstract void moduleCreateEnd(String moduleName, int id);
public abstract void moduleCreateFail(String moduleName, int id);
// Prevents issues with initializer interruptions. See T38996825 and D13793825 for more context.
private static synchronized void maybeLoadSoLibrary() {
if (!sIsSoLibraryLoaded) {
SoLoader.loadLibrary("reactperfloggerjni");
sIsSoLibraryLoaded = true;
}
}
/** Subclasses will override this method to load their own SO libraries. */
protected synchronized void maybeLoadOtherSoLibraries() {}
}
@@ -0,0 +1,31 @@
# 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)
# Header search path for all source files in this module.
LOCAL_C_INCLUDES := $(LOCAL_PATH)/reactperflogger
# Header search path for modules that depend on this module
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CFLAGS += -fexceptions -frtti -std=c++14 -Wall
LOCAL_LDLIBS += -landroid
LOCAL_STATIC_LIBRARIES = libreactperflogger
LOCAL_SHARED_LIBRARIES = libfb libfbjni
# Name of this module.
LOCAL_MODULE := reactperfloggerjni
# Compile all local c++ files
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/reactperflogger/*.cpp)
# Build the files in this directory as a shared library
include $(BUILD_SHARED_LIBRARY)
@@ -0,0 +1,37 @@
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "FBJNI_TARGET", "react_native_xplat_target", "rn_xplat_cxx_library")
rn_xplat_cxx_library(
name = "jni",
srcs = [
"reactperflogger/OnLoad.cpp",
],
header_namespace = "",
exported_headers = {
"reactperflogger/JNativeModulePerfLogger.h": "reactperflogger/JNativeModulePerfLogger.h",
},
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
fbandroid_allow_jni_merging = True,
fbandroid_labels = [
"supermodule:xplat/default/public.react_native.infra",
],
platforms = ANDROID,
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
soname = "libreactperfloggerjni.$(ext)",
visibility = [
"PUBLIC",
],
deps = [
FBJNI_TARGET,
],
exported_deps = [
react_native_xplat_target("reactperflogger:reactperflogger"),
],
)
@@ -0,0 +1,5 @@
---
Checks: '>
clang-diagnostic-*,
'
...
@@ -0,0 +1,30 @@
/*
* 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.
*/
#pragma once
#include <fbjni/fbjni.h>
#include <reactperflogger/NativeModulePerfLogger.h>
#include <memory>
namespace facebook {
namespace react {
class JNativeModulePerfLogger
: public jni::HybridClass<JNativeModulePerfLogger> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/perflogger/NativeModulePerfLogger;";
virtual std::unique_ptr<facebook::react::NativeModulePerfLogger> get() = 0;
private:
friend HybridBase;
};
} // namespace react
} // namespace facebook
@@ -0,0 +1,14 @@
/*
* 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.
*/
#include <fbjni/fbjni.h>
#include "JNativeModulePerfLogger.h"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return facebook::jni::initialize(vm, [] {});
}