Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3e058164 authored by Pablo Gamito's avatar Pablo Gamito
Browse files

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
parent 27a22415
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -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) {