1 /* 2 * Copyright 2025 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 package com.android.server.appsearch.stats; 17 18 import androidx.annotation.IntDef; 19 20 import org.jspecify.annotations.NonNull; 21 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 25 /** 26 * Represents statistics related to a VM payload execution. This class captures information about 27 * the lifecycle of a payload execution, including callback types, exit codes, error codes, and stop 28 * reasons. 29 * 30 * @hide 31 */ 32 public class VMPayloadStats { 33 /** Call types for the VM payload execution. */ 34 @IntDef( 35 value = { 36 CALLBACK_TYPE_UNKNOWN, 37 CALLBACK_TYPE_START, 38 CALLBACK_TYPE_READY, 39 CALLBACK_TYPE_FINISH, 40 CALLBACK_TYPE_ERROR, 41 CALLBACK_TYPE_STOP, 42 }) 43 @Retention(RetentionPolicy.SOURCE) 44 public @interface PayloadCallbackType {} 45 46 /** Represents an unknown callback type. */ 47 public static final int CALLBACK_TYPE_UNKNOWN = 0; 48 49 /** Represents the start of the payload execution. */ 50 public static final int CALLBACK_TYPE_START = 1; 51 52 /** Represents the payload being ready for execution. */ 53 public static final int CALLBACK_TYPE_READY = 2; 54 55 /** Represents the successful completion of the payload execution. */ 56 public static final int CALLBACK_TYPE_FINISH = 3; 57 58 /** Represents an error during the payload execution. */ 59 public static final int CALLBACK_TYPE_ERROR = 4; 60 61 /** Represents the stopping of the payload execution. */ 62 public static final int CALLBACK_TYPE_STOP = 5; 63 64 /** The last callback type represents the number of callback types. */ 65 public static final int PAYLOAD_CALLBACK_TYPE_SIZE = CALLBACK_TYPE_STOP + 1; 66 67 /** The callback type of the payload execution. */ 68 @PayloadCallbackType private final int mCallbackType; 69 70 /** 71 * The exit code of the payload execution. {@code 72 * IsolateStorageService.VmCallback#onPayloadFinished}. 73 */ 74 private final int mExitCode; 75 76 /** The error code of the payload execution {@code IsolateStorageService.VmCallback#onError}. */ 77 private final int mErrorCode; 78 79 /** 80 * The reason for stopping the payload execution in {@code 81 * IsolateStorageService.VmCallback#onStop}. 82 */ 83 private final int mStopReason; 84 85 /** 86 * Constructs a new {@link VMPayloadStats} object using the provided builder. 87 * 88 * @param builder The builder containing the statistics. 89 */ VMPayloadStats(@onNull Builder builder)90 VMPayloadStats(@NonNull Builder builder) { 91 mCallbackType = builder.mCallbackType; 92 mExitCode = builder.mExitCode; 93 mErrorCode = builder.mErrorCode; 94 mStopReason = builder.mStopReason; 95 } 96 97 /** Returns the callback type of the payload execution. */ getCallbackType()98 public int getCallbackType() { 99 return mCallbackType; 100 } 101 102 /** Returns the exit code of the payload execution. */ getExitCode()103 public int getExitCode() { 104 return mExitCode; 105 } 106 107 /** Returns the error code of the payload execution. */ getErrorCode()108 public int getErrorCode() { 109 return mErrorCode; 110 } 111 112 /** Returns the reason for stopping the payload execution. */ getStopReason()113 public int getStopReason() { 114 return mStopReason; 115 } 116 117 /** Builder for {@link VMPayloadStats}. */ 118 public static class Builder { 119 int mCallbackType; 120 int mExitCode; 121 int mErrorCode; 122 int mStopReason; 123 124 /** Constructs a new {@link Builder} with the specified callback type. */ Builder(@ayloadCallbackType int callbackType)125 public Builder(@PayloadCallbackType int callbackType) { 126 mCallbackType = callbackType; 127 } 128 129 /** Sets the exit code of the payload execution. */ setExitCode(int exitCode)130 public @NonNull Builder setExitCode(int exitCode) { 131 mExitCode = exitCode; 132 return this; 133 } 134 135 /** Sets the error code of the payload execution. */ setErrorCode(int errorCode)136 public @NonNull Builder setErrorCode(int errorCode) { 137 mErrorCode = errorCode; 138 return this; 139 } 140 141 /** Sets the reason for stopping the payload execution. */ setStopReason(int stopReason)142 public @NonNull Builder setStopReason(int stopReason) { 143 mStopReason = stopReason; 144 return this; 145 } 146 147 /** Builds a new {@link VMPayloadStats} object. */ build()148 public VMPayloadStats build() { 149 return new VMPayloadStats(/* builder= */ this); 150 } 151 } 152 } 153