• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.pm.dex;
18 
19 import android.util.Slog;
20 
21 import com.android.internal.art.ArtStatsLog;
22 import com.android.internal.os.BackgroundThread;
23 
24 import libcore.io.IoUtils;
25 
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 
30 /**
31  * This class is responsible for reading metrics files generated by odsign and sending them to
32  * statsd. odsign can't send the stats directly to statsd, because statsd can't run until after
33  * odsign has completed. The code here is intended to run once per boot, since odsign runs at boot
34  * time.
35  */
36 public class OdsignStatsLogger {
37     private static final String TAG = "OdsignStatsLogger";
38 
39     // These need to be kept in sync with system/security/ondevice-signing/StatsReporter.{h, cpp}.
40     private static final String METRICS_FILE = "/data/misc/odsign/metrics/odsign-metrics.txt";
41     private static final String COMPOS_METRIC_NAME = "comp_os_artifacts_check_record";
42 
43     /**
44      * Arrange for stats to be uploaded in the background.
45      */
triggerStatsWrite()46     public static void triggerStatsWrite() {
47         BackgroundThread.getExecutor().execute(OdsignStatsLogger::writeStats);
48     }
49 
writeStats()50     private static void writeStats() {
51         try {
52             String lines = IoUtils.readFileAsString(METRICS_FILE);
53 
54             // Delete the file now so we don't upload it more than once, and don't keep trying
55             // to re-parse it if there is a problem.
56             if (!new File(METRICS_FILE).delete()) {
57                 Slog.w(TAG, "Failed to delete metrics file");
58             }
59 
60             // The format is simple - each line is a series of space separated tokens. The first is
61             // the metric name and subsequent ones are the metric values. The logic here must be
62             // kept in sync with system/security/ondevice-signing/StatsReporter.cpp.
63 
64             for (String line : lines.split("\n")) {
65                 String[] metrics = line.split(" ");
66 
67                 if (metrics.length != 4 || !metrics[0].equals(COMPOS_METRIC_NAME)) {
68                     Slog.w(TAG, "Malformed metrics file");
69                     break;
70                 }
71 
72                 boolean currentArtifactsOk = metrics[1].equals("1");
73                 boolean compOsPendingArtifactsExists = metrics[2].equals("1");
74                 boolean useCompOsGeneratedArtifacts = metrics[3].equals("1");
75 
76                 ArtStatsLog.write(ArtStatsLog.EARLY_BOOT_COMP_OS_ARTIFACTS_CHECK_REPORTED,
77                         currentArtifactsOk, compOsPendingArtifactsExists,
78                         useCompOsGeneratedArtifacts);
79             }
80         } catch (FileNotFoundException e) {
81             // This is normal and probably means no new metrics have been generated.
82         } catch (IOException e) {
83             Slog.w(TAG, "Reading metrics file failed", e);
84         }
85     }
86 }
87