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

Commit 0ce7d7ce authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Refactor ProtoLogImpl to support Perfetto tracing with flag better

Flag: ACONFIG android.tracing.Flags.perfettoProtolog DEVELOPMENT
Bug: 276432490
Test: atest FrameworksServicesTests
Change-Id: If95bfeeaff491ce51b61e34d2f8c22d8b635912d
parent 44fe3a9a
Loading
Loading
Loading
Loading
+60 −55
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 * 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.
@@ -37,8 +37,11 @@ import android.util.Slog;
import android.util.proto.ProtoOutputStream;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.protolog.common.ILogger;
import com.android.internal.protolog.common.IProtoLog;
import com.android.internal.protolog.common.IProtoLogGroup;
import com.android.internal.protolog.common.LogDataType;
import com.android.internal.protolog.common.LogLevel;
import com.android.internal.util.TraceBuffer;

import java.io.File;
@@ -48,52 +51,49 @@ import java.util.ArrayList;
import java.util.TreeMap;
import java.util.stream.Collectors;


/**
 * A service for the ProtoLog logging system.
 */
public class BaseProtoLogImpl {
    protected static final TreeMap<String, IProtoLogGroup> LOG_GROUPS = new TreeMap<>();

    /**
     * A runnable to update the cached output of {@link #isEnabled}.
     *
     * Must be invoked after every action that could change the result of {@link #isEnabled}, eg.
     * starting / stopping proto log, or enabling / disabling log groups.
     */
    public static Runnable sCacheUpdater = () -> { };

    protected static void addLogGroupEnum(IProtoLogGroup[] config) {
        for (IProtoLogGroup group : config) {
            LOG_GROUPS.put(group.name(), group);
        }
    }
public class LegacyProtoLogImpl implements IProtoLog {
    private final TreeMap<String, IProtoLogGroup> mLogGroups = new TreeMap<>();

    private static final int BUFFER_CAPACITY = 1024 * 1024;
    private static final int PER_CHUNK_SIZE = 1024;
    private static final String TAG = "ProtoLog";
    private static final long MAGIC_NUMBER_VALUE = ((long) MAGIC_NUMBER_H << 32) | MAGIC_NUMBER_L;
    static final String PROTOLOG_VERSION = "1.0.0";
    private static final int DEFAULT_PER_CHUNK_SIZE = 0;

    private final File mLogFile;
    private final String mViewerConfigFilename;
    private final String mLegacyViewerConfigFilename;
    private final TraceBuffer mBuffer;
    protected final ProtoLogViewerConfigReader mViewerConfig;
    private final LegacyProtoLogViewerConfigReader mViewerConfig;
    private final int mPerChunkSize;

    private boolean mProtoLogEnabled;
    private boolean mProtoLogEnabledLockFree;
    private final Object mProtoLogEnabledLock = new Object();

    @VisibleForTesting
    public enum LogLevel {
        DEBUG, VERBOSE, INFO, WARN, ERROR, WTF
    public LegacyProtoLogImpl(String outputFile, String viewerConfigFilename) {
        this(new File(outputFile), viewerConfigFilename, BUFFER_CAPACITY,
                new LegacyProtoLogViewerConfigReader(), PER_CHUNK_SIZE);
    }

    public LegacyProtoLogImpl(File file, String viewerConfigFilename, int bufferCapacity,
            LegacyProtoLogViewerConfigReader viewerConfig, int perChunkSize) {
        mLogFile = file;
        mBuffer = new TraceBuffer(bufferCapacity);
        mLegacyViewerConfigFilename = viewerConfigFilename;
        mViewerConfig = viewerConfig;
        mPerChunkSize = perChunkSize;
    }

    /**
     * Main log method, do not call directly.
     */
    @VisibleForTesting
    public void log(LogLevel level, IProtoLogGroup group, int messageHash, int paramsMask,
    @Override
    public void log(LogLevel level, IProtoLogGroup group, long messageHash, int paramsMask,
            @Nullable String messageString, Object[] args) {
        if (group.isLogToProto()) {
            logToProto(messageHash, paramsMask, args);
@@ -103,7 +103,7 @@ public class BaseProtoLogImpl {
        }
    }

    private void logToLogcat(String tag, LogLevel level, int messageHash,
    private void logToLogcat(String tag, LogLevel level, long messageHash,
            @Nullable String messageString, Object[] args) {
        String message = null;
        if (messageString == null) {
@@ -157,7 +157,7 @@ public class BaseProtoLogImpl {
        }
    }

    private void logToProto(int messageHash, int paramsMask, Object[] args) {
    private void logToProto(long messageHash, int paramsMask, Object[] args) {
        if (!isProtoEnabled()) {
            return;
        }
@@ -219,20 +219,6 @@ public class BaseProtoLogImpl {
        }
    }

    public BaseProtoLogImpl(File file, String viewerConfigFilename, int bufferCapacity,
            ProtoLogViewerConfigReader viewerConfig) {
        this(file, viewerConfigFilename, bufferCapacity, viewerConfig, DEFAULT_PER_CHUNK_SIZE);
    }

    public BaseProtoLogImpl(File file, String viewerConfigFilename, int bufferCapacity,
            ProtoLogViewerConfigReader viewerConfig, int perChunkSize) {
        mLogFile = file;
        mBuffer = new TraceBuffer(bufferCapacity);
        mViewerConfigFilename = viewerConfigFilename;
        mViewerConfig = viewerConfig;
        mPerChunkSize = perChunkSize;
    }

    /**
     * Starts the logging a circular proto buffer.
     *
@@ -248,7 +234,6 @@ public class BaseProtoLogImpl {
            mProtoLogEnabled = true;
            mProtoLogEnabledLockFree = true;
        }
        sCacheUpdater.run();
    }

    /**
@@ -274,7 +259,6 @@ public class BaseProtoLogImpl {
                throw new IllegalStateException("logging enabled while waiting for flush.");
            }
        }
        sCacheUpdater.run();
    }

    /**
@@ -284,11 +268,11 @@ public class BaseProtoLogImpl {
        return mProtoLogEnabledLockFree;
    }

    protected int setLogging(boolean setTextLogging, boolean value, PrintWriter pw,
    private int setLogging(boolean setTextLogging, boolean value, ILogger logger,
            String... groups) {
        for (int i = 0; i < groups.length; i++) {
            String group = groups[i];
            IProtoLogGroup g = LOG_GROUPS.get(group);
            IProtoLogGroup g = mLogGroups.get(group);
            if (g != null) {
                if (setTextLogging) {
                    g.setLogToLogcat(value);
@@ -296,11 +280,10 @@ public class BaseProtoLogImpl {
                    g.setLogToProto(value);
                }
            } else {
                logAndPrintln(pw, "No IProtoLogGroup named " + group);
                logger.log("No IProtoLogGroup named " + group);
                return -1;
            }
        }
        sCacheUpdater.run();
        return 0;
    }

@@ -330,6 +313,7 @@ public class BaseProtoLogImpl {
        while ((arg = shell.getNextArg()) != null) {
            args.add(arg);
        }
        final ILogger logger = (msg) -> logAndPrintln(pw, msg);
        String[] groups = args.toArray(new String[args.size()]);
        switch (cmd) {
            case "start":
@@ -342,14 +326,14 @@ public class BaseProtoLogImpl {
                logAndPrintln(pw, getStatus());
                return 0;
            case "enable":
                return setLogging(false, true, pw, groups);
                return setLogging(false, true, logger, groups);
            case "enable-text":
                mViewerConfig.loadViewerConfig(pw, mViewerConfigFilename);
                return setLogging(true, true, pw, groups);
                mViewerConfig.loadViewerConfig(logger, mLegacyViewerConfigFilename);
                return setLogging(true, true, logger, groups);
            case "disable":
                return setLogging(false, false, pw, groups);
                return setLogging(false, false, logger, groups);
            case "disable-text":
                return setLogging(true, false, pw, groups);
                return setLogging(true, false, logger, groups);
            default:
                return unknownCommand(pw);
        }
@@ -362,11 +346,11 @@ public class BaseProtoLogImpl {
        return "ProtoLog status: "
                + ((isProtoEnabled()) ? "Enabled" : "Disabled")
                + "\nEnabled log groups: \n  Proto: "
                + LOG_GROUPS.values().stream().filter(
                + mLogGroups.values().stream().filter(
                        it -> it.isEnabled() && it.isLogToProto())
                .map(IProtoLogGroup::name).collect(Collectors.joining(" "))
                + "\n  Logcat: "
                + LOG_GROUPS.values().stream().filter(
                + mLogGroups.values().stream().filter(
                        it -> it.isEnabled() && it.isLogToLogcat())
                .map(IProtoLogGroup::name).collect(Collectors.joining(" "))
                + "\nLogging definitions loaded: " + mViewerConfig.knownViewerStringsNumber();
@@ -393,5 +377,26 @@ public class BaseProtoLogImpl {
            pw.flush();
        }
    }

    /**
     * Start text logging
     * @param groups Groups to start text logging for
     * @param logger A logger to write status updates to
     * @return status code
     */
    public int startLoggingToLogcat(String[] groups, ILogger logger) {
        mViewerConfig.loadViewerConfig(logger, mLegacyViewerConfigFilename);
        return setLogging(true /* setTextLogging */, true, logger, groups);
    }

    /**
     * Stop text logging
     * @param groups Groups to start text logging for
     * @param logger A logger to write status updates to
     * @return status code
     */
    public int stopLoggingToLogcat(String[] groups, ILogger logger) {
        return setLogging(true /* setTextLogging */, false, logger, groups);
    }
}
+117 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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;

import com.android.internal.protolog.common.ILogger;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.GZIPInputStream;

/**
 * Handles loading and parsing of ProtoLog viewer configuration.
 */
public class LegacyProtoLogViewerConfigReader {

    private static final String TAG = "ProtoLogViewerConfigReader";
    private Map<Long, String> mLogMessageMap = null;

    /** Returns message format string for its hash or null if unavailable. */
    public synchronized String getViewerString(long messageHash) {
        if (mLogMessageMap != null) {
            return mLogMessageMap.get(messageHash);
        } else {
            return null;
        }
    }

    /**
     * Reads the specified viewer configuration file. Does nothing if the config is already loaded.
     */
    public synchronized void loadViewerConfig(ILogger logger, String viewerConfigFilename) {
        try {
            loadViewerConfig(new GZIPInputStream(new FileInputStream(viewerConfigFilename)));
            logger.log("Loaded " + mLogMessageMap.size()
                    + " log definitions from " + viewerConfigFilename);
        } catch (FileNotFoundException e) {
            logger.log("Unable to load log definitions: File "
                    + viewerConfigFilename + " not found." + e);
        } catch (IOException e) {
            logger.log("Unable to load log definitions: IOException while reading "
                    + viewerConfigFilename + ". " + e);
        } catch (JSONException e) {
            logger.log("Unable to load log definitions: JSON parsing exception while reading "
                    + viewerConfigFilename + ". " + e);
        }
    }

    /**
     * Reads the specified viewer configuration input stream.
     * Does nothing if the config is already loaded.
     */
    public synchronized void loadViewerConfig(InputStream viewerConfigInputStream)
            throws IOException, JSONException {
        if (mLogMessageMap != null) {
            return;
        }
        InputStreamReader config = new InputStreamReader(viewerConfigInputStream);
        BufferedReader reader = new BufferedReader(config);
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line).append('\n');
        }
        reader.close();
        JSONObject json = new JSONObject(builder.toString());
        JSONObject messages = json.getJSONObject("messages");

        mLogMessageMap = new TreeMap<>();
        Iterator it = messages.keys();
        while (it.hasNext()) {
            String key = (String) it.next();
            try {
                long hash = Long.parseLong(key);
                JSONObject val = messages.getJSONObject(key);
                String msg = val.getString("message");
                mLogMessageMap.put(hash, msg);
            } catch (NumberFormatException expected) {
                // Not a messageHash - skip it
            }
        }
    }

    /**
     * Returns the number of loaded log definitions kept in memory.
     */
    public synchronized int knownViewerStringsNumber() {
        if (mLogMessageMap != null) {
            return mLogMessageMap.size();
        }
        return 0;
    }

}
+33 −32
Original line number Diff line number Diff line
@@ -16,30 +16,35 @@

package com.android.internal.protolog;

import static com.android.internal.protolog.common.ProtoLogToolInjected.Value.LEGACY_OUTPUT_FILE_PATH;
import static com.android.internal.protolog.common.ProtoLogToolInjected.Value.VIEWER_CONFIG_PATH;
import static com.android.internal.protolog.common.ProtoLogToolInjected.Value.LEGACY_VIEWER_CONFIG_PATH;

import android.annotation.Nullable;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.protolog.common.IProtoLog;
import com.android.internal.protolog.common.IProtoLogGroup;

import java.io.File;
import com.android.internal.protolog.common.LogLevel;
import com.android.internal.protolog.common.ProtoLogToolInjected;

/**
 * A service for the ProtoLog logging system.
 */
public class ProtoLogImpl extends BaseProtoLogImpl {
    private static final int BUFFER_CAPACITY = 1024 * 1024;
    private static final String LOG_FILENAME = "/data/misc/wmtrace/wm_log.winscope";
    private static final String VIEWER_CONFIG_FILENAME = "/system/etc/protolog.conf.json.gz";
    private static final int PER_CHUNK_SIZE = 1024;
public class ProtoLogImpl {
    private static IProtoLog sServiceInstance = null;

    private static ProtoLogImpl sServiceInstance = null;
    @ProtoLogToolInjected(VIEWER_CONFIG_PATH)
    private static String sViewerConfigPath;

    static {
        addLogGroupEnum(ProtoLogGroup.values());
    }
    @ProtoLogToolInjected(LEGACY_VIEWER_CONFIG_PATH)
    private static String sLegacyViewerConfigPath;

    @ProtoLogToolInjected(LEGACY_OUTPUT_FILE_PATH)
    private static String sLegacyOutputFilePath;

    /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
    public static void d(IProtoLogGroup group, int messageHash, int paramsMask,
    public static void d(IProtoLogGroup group, long messageHash, int paramsMask,
            @Nullable String messageString,
            Object... args) {
        getSingleInstance()
@@ -47,7 +52,7 @@ public class ProtoLogImpl extends BaseProtoLogImpl {
    }

    /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
    public static void v(IProtoLogGroup group, int messageHash, int paramsMask,
    public static void v(IProtoLogGroup group, long messageHash, int paramsMask,
            @Nullable String messageString,
            Object... args) {
        getSingleInstance().log(LogLevel.VERBOSE, group, messageHash, paramsMask, messageString,
@@ -55,21 +60,21 @@ public class ProtoLogImpl extends BaseProtoLogImpl {
    }

    /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
    public static void i(IProtoLogGroup group, int messageHash, int paramsMask,
    public static void i(IProtoLogGroup group, long messageHash, int paramsMask,
            @Nullable String messageString,
            Object... args) {
        getSingleInstance().log(LogLevel.INFO, group, messageHash, paramsMask, messageString, args);
    }

    /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
    public static void w(IProtoLogGroup group, int messageHash, int paramsMask,
    public static void w(IProtoLogGroup group, long messageHash, int paramsMask,
            @Nullable String messageString,
            Object... args) {
        getSingleInstance().log(LogLevel.WARN, group, messageHash, paramsMask, messageString, args);
    }

    /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
    public static void e(IProtoLogGroup group, int messageHash, int paramsMask,
    public static void e(IProtoLogGroup group, long messageHash, int paramsMask,
            @Nullable String messageString,
            Object... args) {
        getSingleInstance()
@@ -77,40 +82,36 @@ public class ProtoLogImpl extends BaseProtoLogImpl {
    }

    /** Used by the ProtoLogTool, do not call directly - use {@code ProtoLog} class instead. */
    public static void wtf(IProtoLogGroup group, int messageHash, int paramsMask,
    public static void wtf(IProtoLogGroup group, long messageHash, int paramsMask,
            @Nullable String messageString,
            Object... args) {
        getSingleInstance().log(LogLevel.WTF, group, messageHash, paramsMask, messageString, args);
    }

    /** Returns true iff logging is enabled for the given {@code IProtoLogGroup}. */
    public static boolean isEnabled(IProtoLogGroup group) {
        return group.isLogToLogcat()
                || (group.isLogToProto() && getSingleInstance().isProtoEnabled());
        // TODO: Implement for performance reasons, with optional level parameter?
        return true;
    }

    /**
     * Returns the single instance of the ProtoLogImpl singleton class.
     */
    public static synchronized ProtoLogImpl getSingleInstance() {
    public static synchronized IProtoLog getSingleInstance() {
        if (sServiceInstance == null) {
            sServiceInstance = new ProtoLogImpl(
                    new File(LOG_FILENAME)
                    , BUFFER_CAPACITY
                    , new ProtoLogViewerConfigReader()
                    , PER_CHUNK_SIZE);
            if (android.tracing.Flags.perfettoProtolog()) {
                sServiceInstance =
                        new PerfettoProtoLogImpl(sViewerConfigPath, sLegacyViewerConfigPath);
            } else {
                sServiceInstance =
                        new LegacyProtoLogImpl(sLegacyOutputFilePath, sLegacyViewerConfigPath);
            }
        }
        return sServiceInstance;
    }

    @VisibleForTesting
    public static synchronized void setSingleInstance(@Nullable ProtoLogImpl instance) {
    public static synchronized void setSingleInstance(@Nullable IProtoLog instance) {
        sServiceInstance = instance;
    }

    public ProtoLogImpl(File logFile, int bufferCapacity,
            ProtoLogViewerConfigReader viewConfigReader, int perChunkSize) {
        super(logFile, VIEWER_CONFIG_FILENAME, bufferCapacity, viewConfigReader, perChunkSize);
  }
}
+25 −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.common;

public interface ILogger {
    /**
     * Log a message.
     * @param message The log message.
     */
    void log(String message);
}
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.common;

/**
 * Interface for ProtoLog implementations.
 */
public interface IProtoLog {

    /**
     * Log a ProtoLog message
     * @param logLevel Log level of the proto message.
     * @param group The group this message belongs to.
     * @param messageHash The hash of the message.
     * @param paramsMask The parameters mask of the message.
     * @param messageString The message string.
     * @param args The arguments of the message.
     */
    void log(LogLevel logLevel, IProtoLogGroup group, long messageHash, int paramsMask,
             String messageString, Object[] args);

    /**
     * Check if ProtoLog is tracing.
     * @return true iff a ProtoLog tracing session is active.
     */
    boolean isProtoEnabled();

    /**
     * Start logging log groups to logcat
     * @param groups Groups to start text logging for
     * @return status code
     */
    int startLoggingToLogcat(String[] groups, ILogger logger);

    /**
     * Stop logging log groups to logcat
     * @param groups Groups to start text logging for
     * @return status code
     */
    int stopLoggingToLogcat(String[] groups, ILogger logger);
}
Loading