1 /* 2 * Copyright (C) 2017 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 static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason; 20 21 /** 22 * Options used for dexopt invocations. 23 */ 24 public final class DexoptOptions { 25 // When set, the profiles will be checked for updates before calling dexopt. If 26 // the apps profiles didn't update in a meaningful way (decided by the compiler), dexopt 27 // will be skipped. 28 // Currently this only affects the optimization of primary apks. Secondary dex files 29 // will always check the profiles for updates. 30 public static final int DEXOPT_CHECK_FOR_PROFILES_UPDATES = 1 << 0; 31 32 // When set, dexopt will execute unconditionally (even if not needed). 33 public static final int DEXOPT_FORCE = 1 << 1; 34 35 // Whether or not the invocation of dexopt is done after the boot is completed. This is used 36 // in order to adjust the priority of the compilation thread. 37 public static final int DEXOPT_BOOT_COMPLETE = 1 << 2; 38 39 // When set, the dexopt invocation will optimize only the secondary dex files. If false, dexopt 40 // will only consider the primary apk. 41 public static final int DEXOPT_ONLY_SECONDARY_DEX = 1 << 3; 42 43 // When set, dexopt will optimize only dex files that are used by other apps. 44 // Currently, this flag is ignored for primary apks. 45 public static final int DEXOPT_ONLY_SHARED_DEX = 1 << 4; 46 47 // When set, dexopt will attempt to scale down the optimizations previously applied in order 48 // save disk space. 49 public static final int DEXOPT_DOWNGRADE = 1 << 5; 50 51 // When set, dexopt will compile the dex file as a shared library even if it is not actually 52 // used by other apps. This is used to force the compilation or shared libraries declared 53 // with in the manifest with ''uses-library' before we have a chance to detect they are 54 // actually shared at runtime. 55 public static final int DEXOPT_AS_SHARED_LIBRARY = 1 << 6; 56 57 // When set, indicates that dexopt is invoked from the background service. 58 public static final int DEXOPT_IDLE_BACKGROUND_JOB = 1 << 9; 59 60 // When set, indicates that dexopt is invoked from the install time flow and 61 // should get the dex metdata file if present. 62 public static final int DEXOPT_INSTALL_WITH_DEX_METADATA_FILE = 1 << 10; 63 64 // When set, indicates that dexopt is being invoked from the install flow during device restore 65 // or device setup and should be scheduled appropriately. 66 public static final int DEXOPT_FOR_RESTORE = 1 << 11; // TODO(b/135202722): remove 67 68 /** 69 * A value indicating that dexopt shouldn't be run. This string is only used when loading 70 * filters from the `pm.dexopt.install*` properties and is not propagated to dex2oat. 71 */ 72 public static final String COMPILER_FILTER_NOOP = "skip"; 73 74 // The name of package to optimize. 75 private final String mPackageName; 76 77 // The intended target compiler filter. Note that dexopt might adjust the filter before the 78 // execution based on factors like: vmSafeMode and packageUsedByOtherApps. 79 private final String mCompilerFilter; 80 81 // The set of flags for the dexopt options. It's a mix of the DEXOPT_* flags. 82 private final int mFlags; 83 84 // When not null, dexopt will optimize only the split identified by this name. 85 // It only applies for primary apk and it's always null if mOnlySecondaryDex is true. 86 private final String mSplitName; 87 88 // The reason for invoking dexopt (see PackageManagerService.REASON_* constants). 89 // A -1 value denotes an unknown reason. 90 private final int mCompilationReason; 91 DexoptOptions(String packageName, String compilerFilter, int flags)92 public DexoptOptions(String packageName, String compilerFilter, int flags) { 93 this(packageName, /*compilationReason*/ -1, compilerFilter, /*splitName*/ null, flags); 94 } 95 DexoptOptions(String packageName, int compilationReason, int flags)96 public DexoptOptions(String packageName, int compilationReason, int flags) { 97 this(packageName, compilationReason, getCompilerFilterForReason(compilationReason), 98 /*splitName*/ null, flags); 99 } 100 DexoptOptions(String packageName, int compilationReason, String compilerFilter, String splitName, int flags)101 public DexoptOptions(String packageName, int compilationReason, String compilerFilter, 102 String splitName, int flags) { 103 int validityMask = 104 DEXOPT_CHECK_FOR_PROFILES_UPDATES | 105 DEXOPT_FORCE | 106 DEXOPT_BOOT_COMPLETE | 107 DEXOPT_ONLY_SECONDARY_DEX | 108 DEXOPT_ONLY_SHARED_DEX | 109 DEXOPT_DOWNGRADE | 110 DEXOPT_AS_SHARED_LIBRARY | 111 DEXOPT_IDLE_BACKGROUND_JOB | 112 DEXOPT_INSTALL_WITH_DEX_METADATA_FILE | 113 DEXOPT_FOR_RESTORE; 114 if ((flags & (~validityMask)) != 0) { 115 throw new IllegalArgumentException("Invalid flags : " + Integer.toHexString(flags)); 116 } 117 118 mPackageName = packageName; 119 mCompilerFilter = compilerFilter; 120 mFlags = flags; 121 mSplitName = splitName; 122 mCompilationReason = compilationReason; 123 } 124 getPackageName()125 public String getPackageName() { 126 return mPackageName; 127 } 128 isCheckForProfileUpdates()129 public boolean isCheckForProfileUpdates() { 130 return (mFlags & DEXOPT_CHECK_FOR_PROFILES_UPDATES) != 0; 131 } 132 getCompilerFilter()133 public String getCompilerFilter() { 134 return mCompilerFilter; 135 } 136 isForce()137 public boolean isForce() { 138 return (mFlags & DEXOPT_FORCE) != 0; 139 } 140 isBootComplete()141 public boolean isBootComplete() { 142 return (mFlags & DEXOPT_BOOT_COMPLETE) != 0; 143 } 144 isDexoptOnlySecondaryDex()145 public boolean isDexoptOnlySecondaryDex() { 146 return (mFlags & DEXOPT_ONLY_SECONDARY_DEX) != 0; 147 } 148 isDexoptOnlySharedDex()149 public boolean isDexoptOnlySharedDex() { 150 return (mFlags & DEXOPT_ONLY_SHARED_DEX) != 0; 151 } 152 isDowngrade()153 public boolean isDowngrade() { 154 return (mFlags & DEXOPT_DOWNGRADE) != 0; 155 } 156 isDexoptAsSharedLibrary()157 public boolean isDexoptAsSharedLibrary() { 158 return (mFlags & DEXOPT_AS_SHARED_LIBRARY) != 0; 159 } 160 isDexoptIdleBackgroundJob()161 public boolean isDexoptIdleBackgroundJob() { 162 return (mFlags & DEXOPT_IDLE_BACKGROUND_JOB) != 0; 163 } 164 isDexoptInstallWithDexMetadata()165 public boolean isDexoptInstallWithDexMetadata() { 166 return (mFlags & DEXOPT_INSTALL_WITH_DEX_METADATA_FILE) != 0; 167 } 168 isDexoptInstallForRestore()169 public boolean isDexoptInstallForRestore() { 170 return (mFlags & DEXOPT_FOR_RESTORE) != 0; 171 } 172 getSplitName()173 public String getSplitName() { 174 return mSplitName; 175 } 176 getFlags()177 public int getFlags() { 178 return mFlags; 179 } 180 getCompilationReason()181 public int getCompilationReason() { 182 return mCompilationReason; 183 } 184 isCompilationEnabled()185 public boolean isCompilationEnabled() { 186 return !mCompilerFilter.equals(COMPILER_FILTER_NOOP); 187 } 188 189 /** 190 * Creates a new set of DexoptOptions which are the same with the exception of the compiler 191 * filter (set to the given value). 192 */ overrideCompilerFilter(String newCompilerFilter)193 public DexoptOptions overrideCompilerFilter(String newCompilerFilter) { 194 return new DexoptOptions( 195 mPackageName, 196 mCompilationReason, 197 newCompilerFilter, 198 mSplitName, 199 mFlags); 200 } 201 } 202