Skip to content

Commit

Permalink
Improve OSS systrace (#34252)
Browse files Browse the repository at this point in the history
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: #34252

Test Plan:
Run a systrace in Android Studio for RN Tester and make sure RN specific sections are there.

<img width="1263" alt="image" src="https://user-images.githubusercontent.com/2677334/180593493-fc087b4a-2253-43e1-b246-bed3e7bba7ac.png">

Reviewed By: NickGerleman

Differential Revision: D38116890

Pulled By: dmitryrykun

fbshipit-source-id: 744bedbf9ad4004488340a5b4e93d936d9a1e582
  • Loading branch information
janicduplessis authored and facebook-github-bot committed Jul 27, 2022
1 parent 0ce4ea2 commit ccbfdd7
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 20 deletions.
1 change: 1 addition & 0 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
7 changes: 4 additions & 3 deletions ReactAndroid/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ 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
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
Expand Down
5 changes: 4 additions & 1 deletion ReactAndroid/src/main/java/com/facebook/systrace/BUCK
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -8,4 +8,7 @@ rn_android_library(
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/android/androidx:tracing"),
],
)
30 changes: 22 additions & 8 deletions ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String> 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) {
Expand Down
20 changes: 20 additions & 0 deletions ReactAndroid/src/main/third-party/android/androidx/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ccbfdd7

Please sign in to comment.