Skip to content

Commit c0dd11e

Browse files
RSNarafacebook-github-bot
authored andcommitted
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
1 parent 46d6e7f commit c0dd11e

7 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")
2+
3+
rn_android_library(
4+
name = "reactperflogger",
5+
srcs = glob(
6+
[
7+
"*.java",
8+
],
9+
),
10+
labels = [
11+
"supermodule:xplat/default/public.react_native.infra",
12+
],
13+
required_for_source_only_abi = True,
14+
visibility = [
15+
"PUBLIC",
16+
],
17+
deps = [
18+
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
19+
react_native_dep("libraries/fbjni:java"),
20+
react_native_target("java/com/facebook/react/reactperflogger/jni:jni"),
21+
],
22+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.perflogger;
9+
10+
import com.facebook.jni.HybridData;
11+
import com.facebook.soloader.SoLoader;
12+
13+
public abstract class NativeModulePerfLogger {
14+
private final HybridData mHybridData;
15+
16+
private static volatile boolean sIsSoLibraryLoaded;
17+
18+
protected abstract HybridData initHybrid();
19+
20+
protected NativeModulePerfLogger() {
21+
maybeLoadOtherSoLibraries();
22+
maybeLoadSoLibrary();
23+
mHybridData = initHybrid();
24+
}
25+
26+
public abstract void moduleDataCreateStart(String moduleName, int id);
27+
28+
public abstract void moduleDataCreateEnd(String moduleName, int id);
29+
30+
public abstract void moduleCreateStart(String moduleName, int id);
31+
32+
public abstract void moduleCreateCacheHit(String moduleName, int id);
33+
34+
public abstract void moduleCreateConstructStart(String moduleName, int id);
35+
36+
public abstract void moduleCreateConstructEnd(String moduleName, int id);
37+
38+
public abstract void moduleCreateSetUpStart(String moduleName, int id);
39+
40+
public abstract void moduleCreateSetUpEnd(String moduleName, int id);
41+
42+
public abstract void moduleCreateEnd(String moduleName, int id);
43+
44+
public abstract void moduleCreateFail(String moduleName, int id);
45+
46+
// Prevents issues with initializer interruptions. See T38996825 and D13793825 for more context.
47+
private static synchronized void maybeLoadSoLibrary() {
48+
if (!sIsSoLibraryLoaded) {
49+
SoLoader.loadLibrary("reactperfloggerjni");
50+
sIsSoLibraryLoaded = true;
51+
}
52+
}
53+
54+
/** Subclasses will override this method to load their own SO libraries. */
55+
protected synchronized void maybeLoadOtherSoLibraries() {}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
LOCAL_PATH := $(call my-dir)
7+
8+
include $(CLEAR_VARS)
9+
10+
# Header search path for all source files in this module.
11+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/reactperflogger
12+
13+
# Header search path for modules that depend on this module
14+
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
15+
16+
LOCAL_CFLAGS += -fexceptions -frtti -std=c++14 -Wall
17+
18+
LOCAL_LDLIBS += -landroid
19+
20+
LOCAL_STATIC_LIBRARIES = libreactperflogger
21+
22+
LOCAL_SHARED_LIBRARIES = libfb libfbjni
23+
24+
# Name of this module.
25+
LOCAL_MODULE := reactperfloggerjni
26+
27+
# Compile all local c++ files
28+
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/reactperflogger/*.cpp)
29+
30+
# Build the files in this directory as a shared library
31+
include $(BUILD_SHARED_LIBRARY)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "FBJNI_TARGET", "react_native_xplat_target", "rn_xplat_cxx_library")
2+
3+
rn_xplat_cxx_library(
4+
name = "jni",
5+
srcs = [
6+
"reactperflogger/OnLoad.cpp",
7+
],
8+
header_namespace = "",
9+
exported_headers = {
10+
"reactperflogger/JNativeModulePerfLogger.h": "reactperflogger/JNativeModulePerfLogger.h",
11+
},
12+
compiler_flags = [
13+
"-fexceptions",
14+
"-frtti",
15+
"-std=c++14",
16+
"-Wall",
17+
],
18+
fbandroid_allow_jni_merging = True,
19+
fbandroid_labels = [
20+
"supermodule:xplat/default/public.react_native.infra",
21+
],
22+
platforms = ANDROID,
23+
preprocessor_flags = [
24+
"-DLOG_TAG=\"ReactNative\"",
25+
"-DWITH_FBSYSTRACE=1",
26+
],
27+
soname = "libreactperfloggerjni.$(ext)",
28+
visibility = [
29+
"PUBLIC",
30+
],
31+
deps = [
32+
FBJNI_TARGET,
33+
],
34+
exported_deps = [
35+
react_native_xplat_target("reactperflogger:reactperflogger"),
36+
],
37+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
Checks: '>
3+
clang-diagnostic-*,
4+
'
5+
...
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <fbjni/fbjni.h>
11+
#include <reactperflogger/NativeModulePerfLogger.h>
12+
#include <memory>
13+
14+
namespace facebook {
15+
namespace react {
16+
17+
class JNativeModulePerfLogger
18+
: public jni::HybridClass<JNativeModulePerfLogger> {
19+
public:
20+
static auto constexpr kJavaDescriptor =
21+
"Lcom/facebook/react/perflogger/NativeModulePerfLogger;";
22+
23+
virtual std::unique_ptr<facebook::react::NativeModulePerfLogger> get() = 0;
24+
25+
private:
26+
friend HybridBase;
27+
};
28+
29+
} // namespace react
30+
} // namespace facebook
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <fbjni/fbjni.h>
9+
10+
#include "JNativeModulePerfLogger.h"
11+
12+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
13+
return facebook::jni::initialize(vm, [] {});
14+
}

0 commit comments

Comments
 (0)