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

Commit d0326490 authored by Alan Stokes's avatar Alan Stokes Committed by Automerger Merge Worker
Browse files

Merge "Log pending odsign metrics on start" am: 59137a22 am: a6976f72 am:...

Merge "Log pending odsign metrics on start" am: 59137a22 am: a6976f72 am: 1ef1c6a8 am: 240171e2

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2058177



Change-Id: Ib78bd88604f1944d46283605e59dda26d67b9709
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 9dfb43e1 240171e2
Loading
Loading
Loading
Loading
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 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.server.pm.dex;

import android.util.Slog;

import com.android.internal.art.ArtStatsLog;
import com.android.internal.os.BackgroundThread;

import libcore.io.IoUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * This class is responsible for reading metrics files generated by odsign and sending them to
 * statsd. odsign can't send the stats directly to statsd, because statsd can't run until after
 * odsign has completed. The code here is intended to run once per boot, since odsign runs at boot
 * time.
 */
public class OdsignStatsLogger {
    private static final String TAG = "OdsignStatsLogger";

    // These need to be kept in sync with system/security/ondevice-signing/StatsReporter.{h, cpp}.
    private static final String METRICS_FILE = "/data/misc/odsign/metrics/odsign-metrics.txt";
    private static final String COMPOS_METRIC_NAME = "comp_os_artifacts_check_record";

    /**
     * Arrange for stats to be uploaded in the background.
     */
    public static void triggerStatsWrite() {
        BackgroundThread.getExecutor().execute(OdsignStatsLogger::writeStats);
    }

    private static void writeStats() {
        try {
            String lines = IoUtils.readFileAsString(METRICS_FILE);

            // Delete the file now so we don't upload it more than once, and don't keep trying
            // to re-parse it if there is a problem.
            if (!new File(METRICS_FILE).delete()) {
                Slog.w(TAG, "Failed to delete metrics file");
            }

            // The format is simple - each line is a series of space separated tokens. The first is
            // the metric name and subsequent ones are the metric values. The logic here must be
            // kept in sync with system/security/ondevice-signing/StatsReporter.cpp.

            for (String line : lines.split("\n")) {
                String[] metrics = line.split(" ");

                if (metrics.length != 4 || !metrics[0].equals(COMPOS_METRIC_NAME)) {
                    Slog.w(TAG, "Malformed metrics file");
                    break;
                }

                boolean currentArtifactsOk = metrics[1].equals("1");
                boolean compOsPendingArtifactsExists = metrics[2].equals("1");
                boolean useCompOsGeneratedArtifacts = metrics[3].equals("1");

                ArtStatsLog.write(ArtStatsLog.EARLY_BOOT_COMP_OS_ARTIFACTS_CHECK_REPORTED,
                        currentArtifactsOk, compOsPendingArtifactsExists,
                        useCompOsGeneratedArtifacts);
            }
        } catch (FileNotFoundException e) {
            // This is normal and probably means no new metrics have been generated.
        } catch (IOException e) {
            Slog.w(TAG, "Reading metrics file failed", e);
        }
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -164,6 +164,7 @@ import com.android.server.pm.OtaDexoptService;
import com.android.server.pm.PackageManagerService;
import com.android.server.pm.ShortcutService;
import com.android.server.pm.UserManagerService;
import com.android.server.pm.dex.OdsignStatsLogger;
import com.android.server.pm.dex.SystemServerDexLoadReporter;
import com.android.server.pm.verify.domain.DomainVerificationService;
import com.android.server.policy.AppOpsPolicy;
@@ -3042,6 +3043,14 @@ public final class SystemServer implements Dumpable {
                setIncrementalServiceSystemReady(mIncrementalServiceHandle);
                t.traceEnd();
            }

            t.traceBegin("OdsignStatsLogger");
            try {
                OdsignStatsLogger.triggerStatsWrite();
            } catch (Throwable e) {
                reportWtf("Triggering OdsignStatsLogger", e);
            }
            t.traceEnd();
        }, t);

        t.traceBegin("StartSystemUI");