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

Commit 80f422d3 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "inject_protolog_impl_test_deps" into main

* changes:
  Update tests to use custom testing ProtoLog perfetto datasource
  Add support for using custom datasource in ProtoLogConfigurationService for testing purposes
  Extract ProtoLogDataSourceBuilder interface
  Support passing a custom DataSource builder and ProtoLogConfigurationService for testing
parents 14d92548 01d869e3
Loading
Loading
Loading
Loading
+28 −10
Original line number Diff line number Diff line
@@ -99,11 +99,8 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
    public static final String NULL_STRING = "null";
    private final AtomicInteger mTracingInstances = new AtomicInteger();

    private final ProtoLogDataSource mDataSource = new ProtoLogDataSource(
            this::onTracingInstanceStart,
            this::onTracingFlush,
            this::onTracingInstanceStop
    );
    @NonNull
    private final ProtoLogDataSource mDataSource;
    @Nullable
    private final ProtoLogViewerConfigReader mViewerConfigReader;
    @Nullable
@@ -156,8 +153,11 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
            @Nullable ViewerConfigInputStreamProvider viewerConfigInputStreamProvider,
            @Nullable ProtoLogViewerConfigReader viewerConfigReader,
            @NonNull Runnable cacheUpdater,
            @NonNull IProtoLogGroup[] groups) {
        this(null, viewerConfigInputStreamProvider, viewerConfigReader, cacheUpdater, groups);
            @NonNull IProtoLogGroup[] groups,
            @NonNull ProtoLogDataSourceBuilder dataSourceBuilder,
            @NonNull ProtoLogConfigurationService configurationService) {
        this(null, viewerConfigInputStreamProvider, viewerConfigReader, cacheUpdater,
                groups, dataSourceBuilder, configurationService);
    }

    private PerfettoProtoLogImpl(
@@ -166,11 +166,31 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
            @Nullable ProtoLogViewerConfigReader viewerConfigReader,
            @NonNull Runnable cacheUpdater,
            @NonNull IProtoLogGroup[] groups) {
        this(viewerConfigFilePath, viewerConfigInputStreamProvider, viewerConfigReader,
                cacheUpdater, groups,
                ProtoLogDataSource::new,
                IProtoLogConfigurationService.Stub
                        .asInterface(ServiceManager.getService(PROTOLOG_CONFIGURATION_SERVICE))
        );
    }

    private PerfettoProtoLogImpl(
            @Nullable String viewerConfigFilePath,
            @Nullable ViewerConfigInputStreamProvider viewerConfigInputStreamProvider,
            @Nullable ProtoLogViewerConfigReader viewerConfigReader,
            @NonNull Runnable cacheUpdater,
            @NonNull IProtoLogGroup[] groups,
            @NonNull ProtoLogDataSourceBuilder dataSourceBuilder,
            @Nullable IProtoLogConfigurationService configurationService) {
        if (viewerConfigFilePath != null && viewerConfigInputStreamProvider != null) {
            throw new RuntimeException("Only one of viewerConfigFilePath and "
                    + "viewerConfigInputStreamProvider can be set");
        }

        mDataSource = dataSourceBuilder.build(
                this::onTracingInstanceStart,
                this::onTracingFlush,
                this::onTracingInstanceStop);
        Producer.init(InitArguments.DEFAULTS);
        DataSourceParams params =
                new DataSourceParams.Builder()
@@ -186,9 +206,7 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
        registerGroupsLocally(groups);

        if (android.tracing.Flags.clientSideProtoLogging()) {
            mProtoLogConfigurationService =
                    IProtoLogConfigurationService.Stub.asInterface(ServiceManager.getService(
                            PROTOLOG_CONFIGURATION_SERVICE));
            mProtoLogConfigurationService = configurationService;
            Objects.requireNonNull(mProtoLogConfigurationService,
                    "ServiceManager returned a null ProtoLog Configuration Service");

+19 −6
Original line number Diff line number Diff line
@@ -75,11 +75,7 @@ import java.util.TreeMap;
public final class ProtoLogConfigurationService extends IProtoLogConfigurationService.Stub {
    private static final String LOG_TAG = "ProtoLogConfigurationService";

    private final ProtoLogDataSource mDataSource = new ProtoLogDataSource(
            this::onTracingInstanceStart,
            this::onTracingInstanceFlush,
            this::onTracingInstanceStop
    );
    private final ProtoLogDataSource mDataSource;

    /**
     * Keeps track of how many of each viewer config file is currently registered.
@@ -116,11 +112,28 @@ public final class ProtoLogConfigurationService extends IProtoLogConfigurationSe
    private final ViewerConfigFileTracer mViewerConfigFileTracer;

    public ProtoLogConfigurationService() {
        this(ProtoLogConfigurationService::dumpTransitionTraceConfig);
        this(ProtoLogDataSource::new, ProtoLogConfigurationService::dumpTransitionTraceConfig);
    }

    @VisibleForTesting
    public ProtoLogConfigurationService(@NonNull ProtoLogDataSourceBuilder dataSourceBuilder) {
        this(dataSourceBuilder, ProtoLogConfigurationService::dumpTransitionTraceConfig);
    }

    @VisibleForTesting
    public ProtoLogConfigurationService(@NonNull ViewerConfigFileTracer tracer) {
        this(ProtoLogDataSource::new, tracer);
    }

    private ProtoLogConfigurationService(
            @NonNull ProtoLogDataSourceBuilder dataSourceBuilder,
            @NonNull ViewerConfigFileTracer tracer) {
        mDataSource = dataSourceBuilder.build(
            this::onTracingInstanceStart,
            this::onTracingInstanceFlush,
            this::onTracingInstanceStop
        );

        // Initialize the Perfetto producer and register the Perfetto ProtoLog datasource to be
        // receive the lifecycle callbacks of the datasource and write the viewer configs if and
        // when required to the datasource.
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.protolog;

public interface ProtoLogDataSourceBuilder {
    /**
     * Builder method for the DataSource the PerfettoProtoLogImpl is going to us.
     * @param onStart The onStart callback that should be used by the created datasource.
     * @param onFlush The onFlush callback that should be used by the created datasource.
     * @param onStop The onStop callback that should be used by the created datasource.
     * @return A new DataSource that uses the provided callbacks.
     */
    ProtoLogDataSource build(ProtoLogDataSource.Instance.TracingInstanceStartCallback onStart,
            Runnable onFlush, ProtoLogDataSource.Instance.TracingInstanceStopCallback onStop);
}
+78 −55
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ import java.util.concurrent.atomic.AtomicInteger;
@Presubmit
@RunWith(JUnit4.class)
public class PerfettoProtoLogImplTest {
    private static final String TEST_PROTOLOG_DATASOURCE_NAME = "test.android.protolog";
    private final File mTracingDirectory = InstrumentationRegistry.getInstrumentation()
            .getTargetContext().getFilesDir();

@@ -91,6 +92,7 @@ public class PerfettoProtoLogImplTest {
            new TraceConfig(false, true, false)
    );

    private ProtoLogConfigurationService mProtoLogConfigurationService;
    private PerfettoProtoLogImpl mProtoLog;
    private Protolog.ProtoLogViewerConfig.Builder mViewerConfigBuilder;
    private File mFile;
@@ -162,9 +164,15 @@ public class PerfettoProtoLogImplTest {

        mCacheUpdater = () -> {};
        mReader = Mockito.spy(new ProtoLogViewerConfigReader(viewerConfigInputStreamProvider));

        final ProtoLogDataSourceBuilder dataSourceBuilder =
                (onStart, onFlush, onStop) -> new ProtoLogDataSource(
                        onStart, onFlush, onStop, TEST_PROTOLOG_DATASOURCE_NAME);
        mProtoLogConfigurationService =
                new ProtoLogConfigurationService(dataSourceBuilder);
        mProtoLog = new PerfettoProtoLogImpl(
                viewerConfigInputStreamProvider, mReader,
                () -> mCacheUpdater.run(), TestProtoLogGroup.values());
                viewerConfigInputStreamProvider, mReader, () -> mCacheUpdater.run(),
                TestProtoLogGroup.values(), dataSourceBuilder, mProtoLogConfigurationService);
    }

    @After
@@ -183,8 +191,9 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void isEnabled_returnsTrueAfterStart() {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog().build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        try {
            traceMonitor.start();
            assertTrue(mProtoLog.isProtoEnabled());
@@ -195,8 +204,9 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void isEnabled_returnsFalseAfterStop() {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog().build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        try {
            traceMonitor.start();
            assertTrue(mProtoLog.isProtoEnabled());
@@ -209,8 +219,9 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void defaultMode() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(false).build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(false, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        try {
            traceMonitor.start();
            // Shouldn't be logging anything except WTF unless explicitly requested in the group
@@ -238,11 +249,13 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void respectsOverrideConfigs_defaultMode() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true,
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(
                        true,
                        List.of(new PerfettoTraceMonitor.Builder.ProtoLogGroupOverride(
                                TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.DEBUG, true)))
                        .build();
                                TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.DEBUG, true)),
                        TEST_PROTOLOG_DATASOURCE_NAME
                ).build();
        try {
            traceMonitor.start();
            mProtoLog.log(LogLevel.DEBUG, TestProtoLogGroup.TEST_GROUP, 1,
@@ -273,10 +286,12 @@ public class PerfettoProtoLogImplTest {
    @Test
    public void respectsOverrideConfigs_allEnabledMode() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true,
                PerfettoTraceMonitor.newBuilder().enableProtoLog(
                        true,
                        List.of(new PerfettoTraceMonitor.Builder.ProtoLogGroupOverride(
                                TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.WARN, false)))
                        .build();
                                TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.WARN, false)),
                        TEST_PROTOLOG_DATASOURCE_NAME
                    ).build();
        try {
            traceMonitor.start();
            mProtoLog.log(LogLevel.DEBUG, TestProtoLogGroup.TEST_GROUP, 1,
@@ -304,8 +319,8 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void respectsAllEnabledMode() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true, List.of())
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(true, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        try {
            traceMonitor.start();
@@ -405,8 +420,9 @@ public class PerfettoProtoLogImplTest {
                ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_INFO,
                "My test message :: %s, %d, %o, %x, %f, %e, %g, %b");

        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog().build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        long before;
        long after;
        try {
@@ -437,8 +453,9 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void log_noProcessing() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog().build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        long before;
        long after;
        try {
@@ -469,8 +486,9 @@ public class PerfettoProtoLogImplTest {

    @Test
    public  void supportsLocationInformation() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true).build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(true, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        try {
            traceMonitor.start();
            mProtoLog.log(LogLevel.DEBUG, TestProtoLogGroup.TEST_GROUP, 1,
@@ -504,8 +522,9 @@ public class PerfettoProtoLogImplTest {
        final long messageHash = addMessageToConfig(
                ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_INFO,
                "My test message :: %s, %d, %f, %b");
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog().build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        long before;
        long after;
        try {
@@ -526,8 +545,9 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void log_protoDisabled() throws Exception {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(false).build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(false, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        try {
            traceMonitor.start();
            mProtoLog.log(LogLevel.DEBUG, TestProtoLogGroup.TEST_GROUP, 1,
@@ -544,12 +564,14 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void stackTraceTrimmed() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true,
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(
                        true,
                        List.of(new PerfettoTraceMonitor.Builder.ProtoLogGroupOverride(
                                TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.DEBUG,
                                true)))
                        .build();
                                true)),
                        TEST_PROTOLOG_DATASOURCE_NAME
                ).build();
        try {
            traceMonitor.start();

@@ -579,18 +601,18 @@ public class PerfettoProtoLogImplTest {
        final AtomicInteger cacheUpdateCallCount = new AtomicInteger(0);
        mCacheUpdater = cacheUpdateCallCount::incrementAndGet;

        PerfettoTraceMonitor traceMonitor1 =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true,
        PerfettoTraceMonitor traceMonitor1 = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(true,
                        List.of(new PerfettoTraceMonitor.Builder.ProtoLogGroupOverride(
                                TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.WARN,
                                        false)))
                        .build();
                                false)), TEST_PROTOLOG_DATASOURCE_NAME
                ).build();

        PerfettoTraceMonitor traceMonitor2 =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true,
                                List.of(new PerfettoTraceMonitor.Builder.ProtoLogGroupOverride(
                                        TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.DEBUG,
                                        false)))
                                        false)), TEST_PROTOLOG_DATASOURCE_NAME)
                        .build();

        Truth.assertThat(cacheUpdateCallCount.get()).isEqualTo(0);
@@ -635,14 +657,14 @@ public class PerfettoProtoLogImplTest {
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true,
                                List.of(new PerfettoTraceMonitor.Builder.ProtoLogGroupOverride(
                                        TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.WARN,
                                        false)))
                                        false)), TEST_PROTOLOG_DATASOURCE_NAME)
                        .build();

        PerfettoTraceMonitor traceMonitor2 =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true,
                                List.of(new PerfettoTraceMonitor.Builder.ProtoLogGroupOverride(
                                        TestProtoLogGroup.TEST_GROUP.toString(), LogLevel.DEBUG,
                                        false)))
                                        false)), TEST_PROTOLOG_DATASOURCE_NAME)
                        .build();

        try {
@@ -712,8 +734,8 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void supportsNullString() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true)
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(true, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();

        try {
@@ -735,8 +757,8 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void supportNullParams() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true)
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(true, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();

        try {
@@ -758,12 +780,12 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void handlesConcurrentTracingSessions() throws IOException {
        PerfettoTraceMonitor traceMonitor1 =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true)
        PerfettoTraceMonitor traceMonitor1 = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(true, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();

        PerfettoTraceMonitor traceMonitor2 =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(true)
        PerfettoTraceMonitor traceMonitor2 = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(true, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();

        final ResultWriter writer2 = new ResultWriter()
@@ -800,8 +822,9 @@ public class PerfettoProtoLogImplTest {

    @Test
    public void usesDefaultLogFromLevel() throws IOException {
        PerfettoTraceMonitor traceMonitor =
                PerfettoTraceMonitor.newBuilder().enableProtoLog(LogLevel.WARN).build();
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(LogLevel.WARN, List.of(), TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        try {
            traceMonitor.start();
            mProtoLog.log(LogLevel.DEBUG, TestProtoLogGroup.TEST_GROUP,