1 /* 2 * Copyright (C) 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.art.model; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.app.job.JobScheduler; 23 24 import com.android.server.art.ArtManagerLocal; 25 import com.android.server.art.PriorityClass; 26 import com.android.server.art.ReasonMapping; 27 import com.android.server.pm.PackageManagerLocal; 28 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.RetentionPolicy; 31 import java.util.List; 32 33 /** @hide */ 34 @SystemApi(client = SystemApi.Client.SYSTEM_SERVER) 35 public class ArtFlags { 36 // Common flags. 37 38 /** 39 * Whether the operation is applied for primary dex'es (all APKs that are installed as part of 40 * the package, including the base APK and all other split APKs). 41 */ 42 public static final int FLAG_FOR_PRIMARY_DEX = 1 << 0; 43 /** 44 * Whether the operation is applied for secondary dex'es (APKs/JARs that the app puts in its 45 * own data directory at runtime and loads with custom classloaders). 46 */ 47 public static final int FLAG_FOR_SECONDARY_DEX = 1 << 1; 48 49 // Flags specific to `dexoptPackage`. 50 51 /** 52 * Whether to dexopt dependency libraries as well (dependencies that are declared by the app 53 * with <uses-library> tags and transitive dependencies). 54 */ 55 public static final int FLAG_SHOULD_INCLUDE_DEPENDENCIES = 1 << 2; 56 /** 57 * Whether the intention is to downgrade the compiler filter. If true, the dexopt will 58 * be skipped if the target compiler filter is better than or equal to the compiler filter 59 * of the existing dexopt artifacts, or dexopt artifacts do not exist. 60 */ 61 public static final int FLAG_SHOULD_DOWNGRADE = 1 << 3; 62 /** 63 * Whether to force dexopt. If true, the dexopt will be performed regardless of 64 * any existing dexopt artifacts. 65 */ 66 public static final int FLAG_FORCE = 1 << 4; 67 /** 68 * If set, the dexopt will be performed for a single split. Otherwise, the dexopt 69 * will be performed for all splits. {@link DexoptParams.Builder#setSplitName()} can be used 70 * to specify the split to dexopt. 71 * 72 * When this flag is set, {@link #FLAG_FOR_PRIMARY_DEX} must be set, and {@link 73 * #FLAG_FOR_SECONDARY_DEX} and {@link #FLAG_SHOULD_INCLUDE_DEPENDENCIES} must not be set. 74 */ 75 public static final int FLAG_FOR_SINGLE_SPLIT = 1 << 5; 76 /** 77 * If set, skips the dexopt if the remaining storage space is low. The threshold is 78 * controlled by the global settings {@code sys_storage_threshold_percentage} and {@code 79 * sys_storage_threshold_max_bytes}. 80 */ 81 public static final int FLAG_SKIP_IF_STORAGE_LOW = 1 << 6; 82 /** 83 * If set, no profile will be used by dexopt. I.e., if the compiler filter is a profile-guided 84 * one, such as "speed-profile", it will be adjusted to "verify". This option is especially 85 * useful when the compiler filter is not explicitly specified (i.e., is inferred from the 86 * compilation reason). 87 */ 88 public static final int FLAG_IGNORE_PROFILE = 1 << 7; 89 /** 90 * Whether to force merge profiles even if the difference between before and after the merge 91 * is not significant. 92 * 93 * @hide 94 */ 95 public static final int FLAG_FORCE_MERGE_PROFILE = 1 << 8; 96 /** 97 * Whether to force the specified compiler filter. If true, the compiler filter cannot be 98 * overridden through {@link ArtManagerLocal#setAdjustCompilerFilterCallback}. ART Service will 99 * not adjust the compiler filter either, unless dexopt cannot be performed with the specified 100 * compiler filter (e.g., the filter is "speed-profile" while no profile is available). 101 * 102 * @hide 103 */ 104 public static final int FLAG_FORCE_COMPILER_FILTER = 1 << 9; 105 106 /** 107 * Flags for {@link 108 * ArtManagerLocal#getDexoptStatus(PackageManagerLocal.FilteredSnapshot, String, int)}. 109 * 110 * @hide 111 */ 112 // clang-format off 113 @IntDef(flag = true, prefix = "FLAG_", value = { 114 FLAG_FOR_PRIMARY_DEX, 115 FLAG_FOR_SECONDARY_DEX, 116 }) 117 // clang-format on 118 @Retention(RetentionPolicy.SOURCE) 119 public @interface GetStatusFlags {} 120 121 /** 122 * Default flags that are used when {@link 123 * ArtManagerLocal#getDexoptStatus(PackageManagerLocal.FilteredSnapshot, String)} is 124 * called. 125 * Value: {@link #FLAG_FOR_PRIMARY_DEX}, {@link #FLAG_FOR_SECONDARY_DEX}. 126 */ defaultGetStatusFlags()127 public static @GetStatusFlags int defaultGetStatusFlags() { 128 return FLAG_FOR_PRIMARY_DEX | FLAG_FOR_SECONDARY_DEX; 129 } 130 131 /** 132 * Flags for {@link DexoptParams}. 133 * 134 * @hide 135 */ 136 // clang-format off 137 @IntDef(flag = true, prefix = "FLAG_", value = { 138 FLAG_FOR_PRIMARY_DEX, 139 FLAG_FOR_SECONDARY_DEX, 140 FLAG_SHOULD_INCLUDE_DEPENDENCIES, 141 FLAG_SHOULD_DOWNGRADE, 142 FLAG_FORCE, 143 FLAG_FOR_SINGLE_SPLIT, 144 FLAG_SKIP_IF_STORAGE_LOW, 145 FLAG_IGNORE_PROFILE, 146 FLAG_FORCE_MERGE_PROFILE, 147 FLAG_FORCE_COMPILER_FILTER, 148 }) 149 // clang-format on 150 @Retention(RetentionPolicy.SOURCE) 151 public @interface DexoptFlags {} 152 153 /** 154 * Default flags that are used when 155 * {@link DexoptParams.Builder#Builder(String)} is called. 156 * 157 * @hide 158 */ defaultDexoptFlags(@onNull String reason)159 public static @DexoptFlags int defaultDexoptFlags(@NonNull String reason) { 160 switch (reason) { 161 case ReasonMapping.REASON_INSTALL: 162 case ReasonMapping.REASON_INSTALL_FAST: 163 case ReasonMapping.REASON_INSTALL_BULK: 164 case ReasonMapping.REASON_INSTALL_BULK_SECONDARY: 165 case ReasonMapping.REASON_INSTALL_BULK_DOWNGRADED: 166 case ReasonMapping.REASON_INSTALL_BULK_SECONDARY_DOWNGRADED: 167 return FLAG_FOR_PRIMARY_DEX; 168 case ReasonMapping.REASON_INACTIVE: 169 return FLAG_FOR_PRIMARY_DEX | FLAG_FOR_SECONDARY_DEX | FLAG_SHOULD_DOWNGRADE; 170 case ReasonMapping.REASON_FIRST_BOOT: 171 case ReasonMapping.REASON_BOOT_AFTER_OTA: 172 case ReasonMapping.REASON_BOOT_AFTER_MAINLINE_UPDATE: 173 return FLAG_FOR_PRIMARY_DEX | FLAG_SHOULD_INCLUDE_DEPENDENCIES; 174 case ReasonMapping.REASON_BG_DEXOPT: 175 case ReasonMapping.REASON_PRE_REBOOT_DEXOPT: 176 return FLAG_FOR_PRIMARY_DEX | FLAG_FOR_SECONDARY_DEX 177 | FLAG_SHOULD_INCLUDE_DEPENDENCIES | FLAG_SKIP_IF_STORAGE_LOW; 178 case ReasonMapping.REASON_CMDLINE: 179 default: 180 return FLAG_FOR_PRIMARY_DEX | FLAG_FOR_SECONDARY_DEX 181 | FLAG_SHOULD_INCLUDE_DEPENDENCIES; 182 } 183 } 184 185 // Keep in sync with `PriorityClass` except for `PRIORITY_NONE`. 186 // Keep this in sync with `ArtShellCommand.printHelp` except for 'PRIORITY_NONE'. 187 188 /** 189 * Initial value. Not expected. 190 * 191 * @hide 192 */ 193 public static final int PRIORITY_NONE = -1; 194 /** Indicates that the operation blocks boot. */ 195 public static final int PRIORITY_BOOT = PriorityClass.BOOT; 196 /** 197 * Indicates that a human is waiting on the result and the operation is more latency sensitive 198 * than usual. 199 */ 200 public static final int PRIORITY_INTERACTIVE_FAST = PriorityClass.INTERACTIVE_FAST; 201 /** Indicates that a human is waiting on the result. */ 202 public static final int PRIORITY_INTERACTIVE = PriorityClass.INTERACTIVE; 203 /** Indicates that the operation runs in background. */ 204 public static final int PRIORITY_BACKGROUND = PriorityClass.BACKGROUND; 205 206 /** 207 * Indicates the priority of an operation. The value affects the resource usage and the process 208 * priority. A higher value may result in faster execution but may consume more resources and 209 * compete for resources with other processes. 210 * 211 * @hide 212 */ 213 // clang-format off 214 @IntDef(prefix = "PRIORITY_", value = { 215 PRIORITY_NONE, 216 PRIORITY_BOOT, 217 PRIORITY_INTERACTIVE_FAST, 218 PRIORITY_INTERACTIVE, 219 PRIORITY_BACKGROUND, 220 }) 221 // clang-format on 222 @Retention(RetentionPolicy.SOURCE) 223 public @interface PriorityClassApi {} 224 225 /** The job has been successfully scheduled. */ 226 public static final int SCHEDULE_SUCCESS = 0; 227 228 /** @see JobScheduler#RESULT_FAILURE */ 229 public static final int SCHEDULE_JOB_SCHEDULER_FAILURE = 1; 230 231 /** The job is disabled by the system property {@code pm.dexopt.disable_bg_dexopt}. */ 232 public static final int SCHEDULE_DISABLED_BY_SYSPROP = 2; 233 234 /** 235 * Indicates the result of scheduling a background dexopt job. 236 * 237 * @hide 238 */ 239 // clang-format off 240 @IntDef(prefix = "SCHEDULE_", value = { 241 SCHEDULE_SUCCESS, 242 SCHEDULE_JOB_SCHEDULER_FAILURE, 243 SCHEDULE_DISABLED_BY_SYSPROP, 244 }) 245 // clang-format on 246 @Retention(RetentionPolicy.SOURCE) 247 public @interface ScheduleStatus {} 248 249 /** 250 * The downgrade pass, run before the main pass. Only applicable to bg-dexopt. 251 * 252 * @hide 253 */ 254 public static final int PASS_DOWNGRADE = 0; 255 256 /** 257 * The main pass. 258 * 259 * @hide 260 */ 261 public static final int PASS_MAIN = 1; 262 263 /** 264 * The supplementary pass, run after the main pass, to take the opportunity to dexopt more 265 * packages. Compared to the main pass, it uses different criteria to determine whether dexopt 266 * is needed or not, but iterates over the same packages in the same order as the main pass (so 267 * the logic in {@link ArtManagerLocal#getDefaultPackages} and {@link 268 * ArtManagerLocal.BatchDexoptStartCallback} controls the packages here too.) 269 * 270 * Only applicable to bg-dexopt. 271 * 272 * @hide 273 */ 274 public static final int PASS_SUPPLEMENTARY = 2; 275 276 /** 277 * Indicates the pass of a batch dexopt run. 278 * 279 * @hide 280 */ 281 // clang-format off 282 @IntDef(prefix = "PASS_", value = { 283 PASS_DOWNGRADE, 284 PASS_MAIN, 285 PASS_SUPPLEMENTARY, 286 }) 287 // clang-format on 288 @Retention(RetentionPolicy.SOURCE) 289 public @interface BatchDexoptPass {} 290 291 /** @hide */ 292 public static final List<Integer> BATCH_DEXOPT_PASSES = 293 List.of(PASS_DOWNGRADE, PASS_MAIN, PASS_SUPPLEMENTARY); 294 ArtFlags()295 private ArtFlags() {} 296 } 297