From ccbfdd71670a958916c15e8e8e3c5fb94becbe22 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 27 Jul 2022 10:54:35 -0700 Subject: [PATCH] Improve OSS systrace (#34252) Summary: Implements most of systrace using androidx.tracing, this makes it usable using Android Studio profiler systrace. ## Changelog [Android] [Added] - Improve OSS systrace Pull Request resolved: https://github.com/facebook/react-native/pull/34252 Test Plan: Run a systrace in Android Studio for RN Tester and make sure RN specific sections are there. image Reviewed By: NickGerleman Differential Revision: D38116890 Pulled By: dmitryrykun fbshipit-source-id: 744bedbf9ad4004488340a5b4e93d936d9a1e582 --- ReactAndroid/build.gradle | 1 + ReactAndroid/gradle.properties | 7 +- .../src/main/java/com/facebook/systrace/BUCK | 5 +- .../java/com/facebook/systrace/Systrace.java | 30 +++++--- .../facebook/systrace/SystraceMessage.java | 69 ++++++++++++++++--- .../main/third-party/android/androidx/BUCK | 20 ++++++ 6 files changed, 112 insertions(+), 20 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 8945101d657ccf..ff12b4ceb18c22 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -376,6 +376,7 @@ dependencies { api("androidx.appcompat:appcompat:${ANDROIDX_APPCOMPAT_VERSION}") api("androidx.autofill:autofill:${ANDROIDX_AUTOFILL_VERSION}") api("androidx.swiperefreshlayout:swiperefreshlayout:${SWIPEREFRESH_LAYOUT_VERSION}") + api("androidx.tracing:tracing:${ANDROIDX_TRACING_VERSION}") api("com.facebook.fbjni:fbjni-java-only:${FBJNI_VERSION}") api("com.facebook.fresco:fresco:${FRESCO_VERSION}") diff --git a/ReactAndroid/gradle.properties b/ReactAndroid/gradle.properties index 3027dd26292ea3..afff6e699fc7a6 100644 --- a/ReactAndroid/gradle.properties +++ b/ReactAndroid/gradle.properties @@ -6,10 +6,11 @@ POM_ARTIFACT_ID=react-native POM_PACKAGING=aar # JVM Versions -ASSERTJ_VERSION=3.21.0 -ANDROIDX_TEST_VERSION=1.1.0 ANDROIDX_APPCOMPAT_VERSION=1.4.1 ANDROIDX_AUTOFILL_VERSION=1.1.0 +ANDROIDX_TEST_VERSION=1.1.0 +ANDROIDX_TRACING_VERSION=1.1.0 +ASSERTJ_VERSION=3.21.0 FBJNI_VERSION=0.2.2 FRESCO_VERSION=2.5.0 INFER_ANNOTATIONS_VERSION=0.18.0 @@ -17,8 +18,8 @@ JAVAX_INJECT_VERSION=1 JSR305_VERSION=3.0.2 JUNIT_VERSION=4.12 MOCKITO_CORE_VERSION=2.26.0 -OKIO_VERSION=2.9.0 OKHTTP_VERSION=4.9.2 +OKIO_VERSION=2.9.0 POWERMOCK_VERSION=2.0.2 PROGUARD_ANNOTATIONS_VERSION=1.19.0 ROBOLECTRIC_VERSION=4.4 diff --git a/ReactAndroid/src/main/java/com/facebook/systrace/BUCK b/ReactAndroid/src/main/java/com/facebook/systrace/BUCK index f5de217fa454f8..a3c16292a2c99c 100644 --- a/ReactAndroid/src/main/java/com/facebook/systrace/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/systrace/BUCK @@ -1,4 +1,4 @@ -load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library") +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_library") rn_android_library( name = "systrace", @@ -8,4 +8,7 @@ rn_android_library( visibility = [ "PUBLIC", ], + deps = [ + react_native_dep("third-party/android/androidx:tracing"), + ], ) diff --git a/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java b/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java index 0754f9558418e1..497caf8a26b7dc 100644 --- a/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java +++ b/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java @@ -7,7 +7,7 @@ package com.facebook.systrace; -import android.os.Trace; +import androidx.tracing.Trace; /** * Systrace stub that mostly does nothing but delegates to Trace for beginning/ending sections. The @@ -55,21 +55,35 @@ public static void endSection(long tag) { Trace.endSection(); } - public static void beginAsyncSection(long tag, final String sectionName, final int cookie) {} + public static void beginAsyncSection(long tag, final String sectionName, final int cookie) { + Trace.beginAsyncSection(sectionName, cookie); + } public static void beginAsyncSection( - long tag, final String sectionName, final int cookie, final long startNanos) {} + long tag, final String sectionName, final int cookie, final long startNanos) { + beginAsyncSection(tag, sectionName, cookie); + } - public static void endAsyncSection(long tag, final String sectionName, final int cookie) {} + public static void endAsyncSection(long tag, final String sectionName, final int cookie) { + Trace.endAsyncSection(sectionName, cookie); + } public static void endAsyncSection( - long tag, final String sectionName, final int cookie, final long endNanos) {} + long tag, final String sectionName, final int cookie, final long endNanos) { + endAsyncSection(tag, sectionName, cookie); + } - public static void traceCounter(long tag, final String counterName, final int counterValue) {} + public static void traceCounter(long tag, final String counterName, final int counterValue) { + Trace.setCounter(counterName, counterValue); + } - public static void startAsyncFlow(long tag, final String sectionName, final int cookie) {} + public static void startAsyncFlow(long tag, final String sectionName, final int cookie) { + beginAsyncSection(tag, sectionName, cookie); + } public static void stepAsyncFlow(long tag, final String sectionName, final int cookie) {} - public static void endAsyncFlow(long tag, final String sectionName, final int cookie) {} + public static void endAsyncFlow(long tag, final String sectionName, final int cookie) { + endAsyncSection(tag, sectionName, cookie); + } } diff --git a/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.java b/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.java index 0690f3ecea1222..9ad0a396f8174b 100644 --- a/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.java +++ b/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.java @@ -7,17 +7,19 @@ package com.facebook.systrace; -/** Systrace stub. */ +import java.util.ArrayList; +import java.util.List; + public final class SystraceMessage { - private static final Builder NOOP_BUILDER = new NoopBuilder(); + public static Boolean INCLUDE_ARGS = false; public static Builder beginSection(long tag, String sectionName) { - return NOOP_BUILDER; + return new StartSectionBuilder(tag, sectionName); } public static Builder endSection(long tag) { - return NOOP_BUILDER; + return new EndSectionBuilder(tag); } public abstract static class Builder { @@ -33,13 +35,64 @@ public abstract static class Builder { public abstract Builder arg(String key, double value); } - private interface Flusher { - void flush(StringBuilder builder); + private static class StartSectionBuilder extends Builder { + private String mSectionName; + private long mTag; + private List mArgs = new ArrayList<>(); + + public StartSectionBuilder(long tag, String sectionName) { + mTag = tag; + mSectionName = sectionName; + } + + @Override + public void flush() { + Systrace.beginSection( + mTag, + mSectionName + + (INCLUDE_ARGS && mArgs.size() > 0 ? (" (" + String.join(", ", mArgs) + ")") : "")); + } + + @Override + public Builder arg(String key, Object value) { + addArg(key, String.valueOf(value)); + return this; + } + + @Override + public Builder arg(String key, int value) { + addArg(key, String.valueOf(value)); + return this; + } + + @Override + public Builder arg(String key, long value) { + addArg(key, String.valueOf(value)); + return this; + } + + @Override + public Builder arg(String key, double value) { + addArg(key, String.valueOf(value)); + return this; + } + + private void addArg(String key, String value) { + mArgs.add(key + ": " + value); + } } - private static class NoopBuilder extends Builder { + private static class EndSectionBuilder extends Builder { + private long mTag; + + public EndSectionBuilder(long tag) { + mTag = tag; + } + @Override - public void flush() {} + public void flush() { + Systrace.endSection(mTag); + } @Override public Builder arg(String key, Object value) { diff --git a/ReactAndroid/src/main/third-party/android/androidx/BUCK b/ReactAndroid/src/main/third-party/android/androidx/BUCK index 8bea0a99bf3a6c..2035a88fc99d17 100644 --- a/ReactAndroid/src/main/third-party/android/androidx/BUCK +++ b/ReactAndroid/src/main/third-party/android/androidx/BUCK @@ -291,6 +291,15 @@ fb_native.android_library( ], ) +fb_native.android_library( + name = "tracing", + visibility = ["PUBLIC"], + exported_deps = [ + ":annotation", + ":tracing-binary", + ], +) + fb_native.android_library( name = "test-monitor", visibility = ["PUBLIC"], @@ -496,6 +505,11 @@ fb_native.android_prebuilt_aar( aar = ":swiperefreshlayout-binary-aar", ) +fb_native.android_prebuilt_aar( + name = "tracing-binary", + aar = ":tracing-binary-aar", +) + fb_native.android_prebuilt_aar( name = "test-monitor-binary", aar = ":test-monitor-binary-aar", @@ -700,6 +714,12 @@ fb_native.remote_file( url = "mvn:androidx.swiperefreshlayout:swiperefreshlayout:aar:1.0.0", ) +fb_native.remote_file( + name = "tracing-binary-aar", + sha1 = "cdc41b1335c3857e4136d399f6a7962663d05e5e", + url = "mvn:androidx.tracing:tracing:aar:1.1.0", +) + fb_native.remote_file( name = "test-monitor-binary-aar", sha1 = "4f3c15d0a1e0c943b233b0dc5d8f1e690cfe7c0a",