Convert systrace module (#43877)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/43877

Changelog: [Internal]

Reviewed By: alanleedev

Differential Revision: D55765258

fbshipit-source-id: 1582500d73239064272f567f183f6f6f406d815d
This commit is contained in:
Thomas Nardone
2024-04-05 07:37:15 -07:00
committed by Facebook GitHub Bot
parent bc832ca3aa
commit 8c0b5df05a
5 changed files with 177 additions and 209 deletions
@@ -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);
}
}
@@ -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')
}
}
@@ -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<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 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;
}
}
}
@@ -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<String> = 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
}
}
@@ -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()
}