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 = 65 ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_COMPILATION_FAILED; 66 public static final int RESULT_UNEXPECTED_COMPILATION_RESULT = 67 ArtStatsLog 68 .ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_UNEXPECTED_COMPILATION_RESULT; 69 public static final int RESULT_COMPOSD_DIED = 70 ArtStatsLog.ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_COMPOSD_DIED; 71 public static final int RESULT_FAILED_TO_ENABLE_FSVERITY = 72 ArtStatsLog 73 .ISOLATED_COMPILATION_ENDED__COMPILATION_RESULT__RESULT_FAILED_TO_ENABLE_FSVERITY; 74 75 @Retention(RetentionPolicy.SOURCE) 76 @IntDef({SCHEDULING_RESULT_UNKNOWN, SCHEDULING_SUCCESS, SCHEDULING_FAILURE}) 77 public @interface ScheduleJobResult {} 78 79 // Keep this in sync with Result enum in IsolatedCompilationScheduled in 80 // frameworks/proto_logging/stats/atoms.proto 81 82 public static final int SCHEDULING_RESULT_UNKNOWN = 83 ArtStatsLog 84 .ISOLATED_COMPILATION_SCHEDULED__SCHEDULING_RESULT__SCHEDULING_RESULT_UNKNOWN; 85 public static final int SCHEDULING_FAILURE = 86 ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED__SCHEDULING_RESULT__SCHEDULING_FAILURE; 87 public static final int SCHEDULING_SUCCESS = 88 ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED__SCHEDULING_RESULT__SCHEDULING_SUCCESS; 89 90 private long mCompilationStartTimeMs = 0; 91 onCompilationScheduled(@cheduleJobResult int result)92 public static void onCompilationScheduled(@ScheduleJobResult int result) { 93 ArtStatsLog.write(ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED, result); 94 Log.i(TAG, "ISOLATED_COMPILATION_SCHEDULED: " + result); 95 } 96 onCompilationStarted()97 public void onCompilationStarted() { 98 mCompilationStartTimeMs = SystemClock.elapsedRealtime(); 99 } 100 onCompilationJobCanceled(@obParameters.StopReason int jobStopReason)101 public void onCompilationJobCanceled(@JobParameters.StopReason int jobStopReason) { 102 statsLogPostCompilation(RESULT_JOB_CANCELED, jobStopReason); 103 } 104 onCompilationEnded(@ompilationResult int result)105 public void onCompilationEnded(@CompilationResult int result) { 106 statsLogPostCompilation(result, JobParameters.STOP_REASON_UNDEFINED); 107 } 108 statsLogPostCompilation( @ompilationResult int result, @JobParameters.StopReason int jobStopReason)109 private void statsLogPostCompilation( 110 @CompilationResult int result, @JobParameters.StopReason int jobStopReason) { 111 112 long compilationTime = 113 mCompilationStartTimeMs == 0 114 ? -1 115 : SystemClock.elapsedRealtime() - mCompilationStartTimeMs; 116 mCompilationStartTimeMs = 0; 117 118 ArtStatsLog.write( 119 ArtStatsLog.ISOLATED_COMPILATION_ENDED, compilationTime, result, jobStopReason); 120 Log.i( 121 TAG, 122 "ISOLATED_COMPILATION_ENDED: " 123 + result 124 + ", " 125 + compilationTime 126 + ", " 127 + jobStopReason); 128 } 129 } 130