summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Pablo Gamito <pablogamito@google.com> 2024-11-08 13:11:48 +0000
committer Pablo Gamito <pablogamito@google.com> 2024-11-20 15:37:51 +0000
commit3e058164df649a5dfd248e10d92d8d97346424c0 (patch)
treea85bb4d686b78694855fbd0f1f7246d60a0a3b05
parent27a224154483a1f14b00a1aa2ed0002566dd9a27 (diff)
Create shared static ProtoLogDataSource
To be used for any tracing in the process, so that we avoid having duplicate registered datasources since we cannot unregister a datasource. Flag: android.tracing.perfetto_protolog_tracing Bug: 369560789 Test: atest TracingTests Change-Id: I6905c4d717ca8ec38ee1a50bbfcb4eb179149485
-rw-r--r--core/java/com/android/internal/protolog/ProtoLog.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/core/java/com/android/internal/protolog/ProtoLog.java b/core/java/com/android/internal/protolog/ProtoLog.java
index d117e93d7de7..33fdb48e9a9c 100644
--- a/core/java/com/android/internal/protolog/ProtoLog.java
+++ b/core/java/com/android/internal/protolog/ProtoLog.java
@@ -17,6 +17,9 @@
package com.android.internal.protolog;
import android.os.ServiceManager;
+import android.tracing.perfetto.DataSourceParams;
+import android.tracing.perfetto.InitArguments;
+import android.tracing.perfetto.Producer;
import com.android.internal.protolog.common.IProtoLog;
import com.android.internal.protolog.common.IProtoLogGroup;
@@ -54,6 +57,8 @@ public class ProtoLog {
private static IProtoLog sProtoLogInstance;
+ private static ProtoLogDataSource sDataSource;
+
private static final Object sInitLock = new Object();
/**
@@ -190,6 +195,32 @@ public class ProtoLog {
return sProtoLogInstance;
}
+ /**
+ * Gets or creates if it doesn't exist yet the protolog datasource to use in this process.
+ * We should re-use the same datasource to avoid registering the datasource multiple times in
+ * the same process, since there is no way to unregister the datasource after registration.
+ *
+ * @return The single ProtoLog datasource instance to be shared across all ProtoLog tracing
+ * objects.
+ */
+ public static synchronized ProtoLogDataSource getSharedSingleInstanceDataSource() {
+ if (sDataSource == null) {
+ Producer.init(InitArguments.DEFAULTS);
+ sDataSource = new ProtoLogDataSource();
+ DataSourceParams params =
+ new DataSourceParams.Builder()
+ .setBufferExhaustedPolicy(
+ DataSourceParams
+ .PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_DROP)
+ .build();
+ // NOTE: Registering that datasource is an async operation, so there may be no data
+ // traced for some messages logged right after the construction of this class.
+ sDataSource.register(params);
+ }
+
+ return sDataSource;
+ }
+
private static void logStringMessage(LogLevel logLevel, IProtoLogGroup group,
String stringMessage, Object... args) {
if (sProtoLogInstance == null) {