Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OSS systrace #34252

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions ReactAndroid/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PROGUARD_ANNOTATIONS_VERSION=1.19.0
ROBOLECTRIC_VERSION=4.4
SO_LOADER_VERSION=0.10.4
SWIPEREFRESH_LAYOUT_VERSION=1.0.0
ANDROIDX_TRACING_VERSION=1.1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we keep this sorted?


# Native Dependency Versions
BOOST_VERSION=1_76_0
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", "rn_android_library", "react_native_dep")

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding arguments to the section name makes grouping similar events a lot harder so by default I disable it. I don't really see how else args are useful for systrace in Android Studio. How is that used internally at facebook?


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,63 @@ 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