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.compos;
18 
19 import android.annotation.IntDef;
20 import android.app.job.JobParameters;
21 import android.os.SystemClock;
22 import android.util.Log;
23 
24 import com.android.internal.art.ArtStatsLog;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * A class that handles reporting metrics relating to Isolated Compilation to statsd.
31  *
32  * @hide
33  */
34 class IsolatedCompilationMetrics {
35     private static final String TAG = IsolatedCompilationMetrics.class.getName();
36 
37     // TODO(b/218525257): Move the definition of these enums to atoms.proto
38     @Retention(RetentionPolicy.SOURCE)
39     @IntDef({
40         RESULT_UNKNOWN,
41         RESULT_SUCCESS,
42         RESULT_UNKNOWN_FAILURE,
43         RESULT_FAILED_TO_START,
44         RESULT_JOB_CANCELED,
45         RESULT_COMPILATION_FAILED,
46         RESULT_UNEXPECTED_COMPILATION_RESULT,
47         RESULT_COMPOSD_DIED,
48         RESULT_FAILED_TO_ENABLE_FSVERITY
49     })
50     public @interface CompilationResult {}
51 
52     // Keep this in sync with Result enum in IsolatedCompilationEnded in
53     // frameworks/proto_logging/stats/atoms.proto
54     public static final int RESULT_UNKNOWN =
55             ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_UNKNOWN;
56     public static final int RESULT_SUCCESS =
57             ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_SUCCESS;
58     public static final int RESULT_UNKNOWN_FAILURE =
59             ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_UNKNOWN_FAILURE;
60     public static final int RESULT_FAILED_TO_START =
61             ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_FAILED_TO_START;
62     public static final int RESULT_JOB_CANCELED =
63             ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_JOB_CANCELED;
64     public static final int RESULT_COMPILATION_FAILED = ArtStatsLog
65             .ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_COMPILATION_FAILED;
66     public static final int RESULT_UNEXPECTED_COMPILATION_RESULT = ArtStatsLog
67             .ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_UNEXPECTED_COMPILATION_RESULT;
68     public static final int RESULT_COMPOSD_DIED =
69             ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_COMPOSD_DIED;
70     public static final int RESULT_FAILED_TO_ENABLE_FSVERITY =
71             ArtStatsLog
72                     .ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_FAILED_TO_ENABLE_FSVERITY;
73 
74     @Retention(RetentionPolicy.SOURCE)
75     @IntDef({SCHEDULING_RESULT_UNKNOWN, SCHEDULING_SUCCESS, SCHEDULING_FAILURE})
76     public @interface ScheduleJobResult {}
77 
78     // Keep this in sync with Result enum in IsolatedCompilationScheduled in
79     // frameworks/proto_logging/stats/atoms.proto
80 
81     public static final int SCHEDULING_RESULT_UNKNOWN = ArtStatsLog
82             .ISOLATED_COMPILATION_SCHEDULED__SCHEDULING_RESULT__SCHEDULING_RESULT_UNKNOWN;
83     public static final int SCHEDULING_FAILURE =
84             ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED__SCHEDULING_RESULT__SCHEDULING_FAILURE;
85     public static final int SCHEDULING_SUCCESS =
86             ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED__SCHEDULING_RESULT__SCHEDULING_SUCCESS;
87 
88     private long mCompilationStartTimeMs = 0;
89 
onCompilationScheduled(@cheduleJobResult int result)90     public static void onCompilationScheduled(@ScheduleJobResult int result) {
91         ArtStatsLog.write(ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED, result);
92         Log.i(TAG, "ISOLATED_COMPILATION_SCHEDULED: " + result);
93     }
94 
onCompilationStarted()95     public void onCompilationStarted() {
96         mCompilationStartTimeMs = SystemClock.elapsedRealtime();
97     }
98 
onCompilationJobCanceled(@obParameters.StopReason int jobStopReason)99     public void onCompilationJobCanceled(@JobParameters.StopReason int jobStopReason) {
100         statsLogPostCompilation(RESULT_JOB_CANCELED, jobStopReason);
101     }
102 
onCompilationEnded(@ompilationResult int result)103     public void onCompilationEnded(@CompilationResult int result) {
104         statsLogPostCompilation(result, JobParameters.STOP_REASON_UNDEFINED);
105     }
106 
statsLogPostCompilation(@ompilationResult int result, @JobParameters.StopReason int jobStopReason)107     private void statsLogPostCompilation(@CompilationResult int result,
108                 @JobParameters.StopReason int jobStopReason) {
109 
110         long compilationTime = mCompilationStartTimeMs == 0 ? -1
111                 : SystemClock.elapsedRealtime() - mCompilationStartTimeMs;
112         mCompilationStartTimeMs = 0;
113 
114         ArtStatsLog.write(ArtStatsLog.ISOLATED_COMPILATION_ENDED, compilationTime,
115                 result, jobStopReason);
116         Log.i(TAG, "ISOLATED_COMPILATION_ENDED: " + result + ", " + compilationTime
117                 + ", " + jobStopReason);
118     }
119 }
120