summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Alan Stokes <alanstokes@google.com> 2022-04-06 15:00:54 +0100
committer Alan Stokes <alanstokes@google.com> 2022-04-07 14:01:07 +0100
commit32ebf109be0ee21b2c18ace7cbdd9a8fe0436560 (patch)
treeaa62032a255acdbbe0fa0886c338139ef7c30ac7
parentde6f930c612601faecb8cd5943bce2a2059f1349 (diff)
Log pending odsign metrics on start
Bug: 202926606 Test: Manual - create file, observe logs, file is deleted Change-Id: Ie7f35cf4ebd9e293b376da6c7a2d13b98315c587
-rw-r--r--services/core/java/com/android/server/pm/dex/OdsignStatsLogger.java86
-rw-r--r--services/java/com/android/server/SystemServer.java9
2 files changed, 95 insertions, 0 deletions
diff --git a/services/core/java/com/android/server/pm/dex/OdsignStatsLogger.java b/services/core/java/com/android/server/pm/dex/OdsignStatsLogger.java
new file mode 100644
index 000000000000..fa08addc9b69
--- /dev/null
+++ b/services/core/java/com/android/server/pm/dex/OdsignStatsLogger.java
@@ -0,0 +1,86 @@
+/*
+ * 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);
+ }
+ }
+}
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 77d40d247622..8e4afe685372 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -159,6 +159,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;
@@ -2887,6 +2888,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");