diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java deleted file mode 100644 index 497caf8a26b..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and 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.systrace; - -import androidx.tracing.Trace; - -/** - * Systrace stub that mostly does nothing but delegates to Trace for beginning/ending sections. The - * internal version of this file has not been opensourced yet. - */ -public class Systrace { - - public static final long TRACE_TAG_REACT_JAVA_BRIDGE = 0L; - public static final long TRACE_TAG_REACT_APPS = 0L; - public static final long TRACE_TAG_REACT_FRESCO = 0L; - public static final long TRACE_TAG_REACT_VIEW = 0L; - public static final long TRACE_TAG_REACT_JS_VM_CALLS = 0L; - - public enum EventScope { - THREAD('t'), - PROCESS('p'), - GLOBAL('g'); - - private final char mCode; - - private EventScope(char code) { - mCode = code; - } - - public char getCode() { - return mCode; - } - } - - public static void registerListener(TraceListener listener) {} - - public static void unregisterListener(TraceListener listener) {} - - public static boolean isTracing(long tag) { - return false; - } - - public static void traceInstant(long tag, final String title, EventScope scope) {} - - public static void beginSection(long tag, final String sectionName) { - Trace.beginSection(sectionName); - } - - public static void endSection(long tag) { - Trace.endSection(); - } - - 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) { - beginAsyncSection(tag, sectionName, 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) { - endAsyncSection(tag, sectionName, cookie); - } - - 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) { - 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) { - endAsyncSection(tag, sectionName, cookie); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.kt new file mode 100644 index 00000000000..d6feda993f1 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/Systrace.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) Meta Platforms, Inc. and 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.systrace + +import androidx.tracing.Trace + +/** + * Systrace stub that mostly does nothing but delegates to Trace for beginning/ending sections. The + * internal version of this file has not been opensourced yet. + */ +@Suppress("UNUSED_PARAMETER") +public object Systrace { + + public const val TRACE_TAG_REACT_JAVA_BRIDGE: Long = 0L + public const val TRACE_TAG_REACT_APPS: Long = 0L + public const val TRACE_TAG_REACT_FRESCO: Long = 0L + public const val TRACE_TAG_REACT_VIEW: Long = 0L + public const val TRACE_TAG_REACT_JS_VM_CALLS: Long = 0L + + @JvmStatic public fun registerListener(listener: TraceListener?): Unit = Unit + + @JvmStatic public fun unregisterListener(listener: TraceListener?): Unit = Unit + + @JvmStatic public fun isTracing(tag: Long): Boolean = false + + @JvmStatic public fun traceInstant(tag: Long, title: String?, scope: EventScope?): Unit = Unit + + @JvmStatic + public fun beginSection(tag: Long, sectionName: String) { + Trace.beginSection(sectionName) + } + + @JvmStatic + public fun endSection(tag: Long) { + Trace.endSection() + } + + @JvmStatic + public fun beginAsyncSection(tag: Long, sectionName: String, cookie: Int) { + Trace.beginAsyncSection(sectionName, cookie) + } + + @JvmStatic + public fun beginAsyncSection(tag: Long, sectionName: String, cookie: Int, startNanos: Long) { + beginAsyncSection(tag, sectionName, cookie) + } + + @JvmStatic + public fun endAsyncSection(tag: Long, sectionName: String, cookie: Int) { + Trace.endAsyncSection(sectionName, cookie) + } + + @JvmStatic + public fun endAsyncSection(tag: Long, sectionName: String, cookie: Int, endNanos: Long) { + endAsyncSection(tag, sectionName, cookie) + } + + @JvmStatic + public fun traceCounter(tag: Long, counterName: String, counterValue: Int) { + Trace.setCounter(counterName, counterValue) + } + + @JvmStatic + public fun startAsyncFlow(tag: Long, sectionName: String, cookie: Int) { + beginAsyncSection(tag, sectionName, cookie) + } + + @JvmStatic public fun stepAsyncFlow(tag: Long, sectionName: String, cookie: Int): Unit = Unit + + @JvmStatic + public fun endAsyncFlow(tag: Long, sectionName: String, cookie: Int) { + endAsyncSection(tag, sectionName, cookie) + } + + public enum class EventScope(public val code: Char) { + THREAD('t'), + PROCESS('p'), + GLOBAL('g') + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.java deleted file mode 100644 index 9ad0a396f81..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and 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.systrace; - -import java.util.ArrayList; -import java.util.List; - -public final class SystraceMessage { - - public static Boolean INCLUDE_ARGS = false; - - public static Builder beginSection(long tag, String sectionName) { - return new StartSectionBuilder(tag, sectionName); - } - - public static Builder endSection(long tag) { - return new EndSectionBuilder(tag); - } - - public abstract static class Builder { - - public abstract void flush(); - - public abstract Builder arg(String key, Object value); - - public abstract Builder arg(String key, int value); - - public abstract Builder arg(String key, long value); - - public abstract Builder arg(String key, double value); - } - - 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 EndSectionBuilder extends Builder { - private long mTag; - - public EndSectionBuilder(long tag) { - mTag = tag; - } - - @Override - public void flush() { - Systrace.endSection(mTag); - } - - @Override - public Builder arg(String key, Object value) { - return this; - } - - @Override - public Builder arg(String key, int value) { - return this; - } - - @Override - public Builder arg(String key, long value) { - return this; - } - - @Override - public Builder arg(String key, double value) { - return this; - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.kt new file mode 100644 index 00000000000..f95648fcbdb --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/SystraceMessage.kt @@ -0,0 +1,88 @@ +/* + * Copyright (c) Meta Platforms, Inc. and 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.systrace + +import java.util.ArrayList +import kotlin.jvm.JvmField + +public object SystraceMessage { + + public @JvmField var INCLUDE_ARGS: Boolean = false + + @JvmStatic + public fun beginSection(tag: Long, sectionName: String): Builder = + StartSectionBuilder(tag, sectionName) + + @JvmStatic public fun endSection(tag: Long): Builder = EndSectionBuilder(tag) + + public abstract class Builder { + public abstract fun flush() + + public abstract fun arg(key: String, value: Any): Builder + + public abstract fun arg(key: String, value: Int): Builder + + public abstract fun arg(key: String, value: Long): Builder + + public abstract fun arg(key: String, value: Double): Builder + } + + private class StartSectionBuilder(private val tag: Long, private val sectionName: String) : + Builder() { + private val args: MutableList = ArrayList() + + override fun flush() { + Systrace.beginSection( + tag, + sectionName + + if (INCLUDE_ARGS && args.isNotEmpty()) { + " (${java.lang.String.join(", ", args)})" + } else { + "" + }) + } + + override fun arg(key: String, value: Any): Builder { + addArg(key, value.toString()) + return this + } + + override fun arg(key: String, value: Int): Builder { + addArg(key, value.toString()) + return this + } + + override fun arg(key: String, value: Long): Builder { + addArg(key, value.toString()) + return this + } + + override fun arg(key: String, value: Double): Builder { + addArg(key, value.toString()) + return this + } + + private fun addArg(key: String, value: String) { + args.add("$key: $value") + } + } + + private class EndSectionBuilder(private val tag: Long) : Builder() { + override fun flush() { + Systrace.endSection(tag) + } + + override fun arg(key: String, value: Any): Builder = this + + override fun arg(key: String, value: Int): Builder = this + + override fun arg(key: String, value: Long): Builder = this + + override fun arg(key: String, value: Double): Builder = this + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/TraceListener.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/TraceListener.kt similarity index 71% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/TraceListener.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/TraceListener.kt index f9b736fd4e0..80ba0e187b3 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/TraceListener.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/systrace/TraceListener.kt @@ -5,10 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -package com.facebook.systrace; +package com.facebook.systrace public interface TraceListener { - void onTraceStarted(); - void onTraceStopped(); + public fun onTraceStarted() + + public fun onTraceStopped() }