Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 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 | |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 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 | |
David Sehr | 11c22e3 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 57 | // When set, indicates that dexopt is invoked from the background service. |
| 58 | public static final int DEXOPT_IDLE_BACKGROUND_JOB = 1 << 9; |
| 59 | |
Calin Juravle | 29c772c | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 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 | |
Patrick Baumann | 90d4e0d | 2020-04-14 16:57:30 -0700 | [diff] [blame] | 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 | |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 68 | // The name of package to optimize. |
| 69 | private final String mPackageName; |
| 70 | |
| 71 | // The intended target compiler filter. Note that dexopt might adjust the filter before the |
| 72 | // execution based on factors like: vmSafeMode and packageUsedByOtherApps. |
| 73 | private final String mCompilerFilter; |
| 74 | |
| 75 | // The set of flags for the dexopt options. It's a mix of the DEXOPT_* flags. |
| 76 | private final int mFlags; |
| 77 | |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 78 | // When not null, dexopt will optimize only the split identified by this name. |
| 79 | // It only applies for primary apk and it's always null if mOnlySecondaryDex is true. |
| 80 | private final String mSplitName; |
| 81 | |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 82 | // The reason for invoking dexopt (see PackageManagerService.REASON_* constants). |
| 83 | // A -1 value denotes an unknown reason. |
| 84 | private final int mCompilationReason; |
| 85 | |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 86 | public DexoptOptions(String packageName, String compilerFilter, int flags) { |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 87 | this(packageName, /*compilationReason*/ -1, compilerFilter, /*splitName*/ null, flags); |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 90 | public DexoptOptions(String packageName, int compilationReason, int flags) { |
| 91 | this(packageName, compilationReason, getCompilerFilterForReason(compilationReason), |
| 92 | /*splitName*/ null, flags); |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 95 | public DexoptOptions(String packageName, int compilationReason, String compilerFilter, |
| 96 | String splitName, int flags) { |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 97 | int validityMask = |
| 98 | DEXOPT_CHECK_FOR_PROFILES_UPDATES | |
| 99 | DEXOPT_FORCE | |
| 100 | DEXOPT_BOOT_COMPLETE | |
| 101 | DEXOPT_ONLY_SECONDARY_DEX | |
| 102 | DEXOPT_ONLY_SHARED_DEX | |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 103 | DEXOPT_DOWNGRADE | |
David Sehr | 11c22e3 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 104 | DEXOPT_AS_SHARED_LIBRARY | |
Calin Juravle | 29c772c | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 105 | DEXOPT_IDLE_BACKGROUND_JOB | |
Patrick Baumann | 90d4e0d | 2020-04-14 16:57:30 -0700 | [diff] [blame] | 106 | DEXOPT_INSTALL_WITH_DEX_METADATA_FILE | |
| 107 | DEXOPT_FOR_RESTORE; |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 108 | if ((flags & (~validityMask)) != 0) { |
| 109 | throw new IllegalArgumentException("Invalid flags : " + Integer.toHexString(flags)); |
| 110 | } |
| 111 | |
| 112 | mPackageName = packageName; |
| 113 | mCompilerFilter = compilerFilter; |
| 114 | mFlags = flags; |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 115 | mSplitName = splitName; |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 116 | mCompilationReason = compilationReason; |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public String getPackageName() { |
| 120 | return mPackageName; |
| 121 | } |
| 122 | |
| 123 | public boolean isCheckForProfileUpdates() { |
| 124 | return (mFlags & DEXOPT_CHECK_FOR_PROFILES_UPDATES) != 0; |
| 125 | } |
| 126 | |
| 127 | public String getCompilerFilter() { |
| 128 | return mCompilerFilter; |
| 129 | } |
| 130 | |
| 131 | public boolean isForce() { |
| 132 | return (mFlags & DEXOPT_FORCE) != 0; |
| 133 | } |
| 134 | |
| 135 | public boolean isBootComplete() { |
| 136 | return (mFlags & DEXOPT_BOOT_COMPLETE) != 0; |
| 137 | } |
| 138 | |
| 139 | public boolean isDexoptOnlySecondaryDex() { |
| 140 | return (mFlags & DEXOPT_ONLY_SECONDARY_DEX) != 0; |
| 141 | } |
| 142 | |
| 143 | public boolean isDexoptOnlySharedDex() { |
| 144 | return (mFlags & DEXOPT_ONLY_SHARED_DEX) != 0; |
| 145 | } |
| 146 | |
| 147 | public boolean isDowngrade() { |
| 148 | return (mFlags & DEXOPT_DOWNGRADE) != 0; |
| 149 | } |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 150 | |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 151 | public boolean isDexoptAsSharedLibrary() { |
| 152 | return (mFlags & DEXOPT_AS_SHARED_LIBRARY) != 0; |
| 153 | } |
| 154 | |
David Sehr | 11c22e3 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 155 | public boolean isDexoptIdleBackgroundJob() { |
| 156 | return (mFlags & DEXOPT_IDLE_BACKGROUND_JOB) != 0; |
| 157 | } |
| 158 | |
Calin Juravle | 29c772c | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 159 | public boolean isDexoptInstallWithDexMetadata() { |
| 160 | return (mFlags & DEXOPT_INSTALL_WITH_DEX_METADATA_FILE) != 0; |
| 161 | } |
| 162 | |
Patrick Baumann | 90d4e0d | 2020-04-14 16:57:30 -0700 | [diff] [blame] | 163 | public boolean isDexoptInstallForRestore() { |
| 164 | return (mFlags & DEXOPT_FOR_RESTORE) != 0; |
| 165 | } |
| 166 | |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 167 | public String getSplitName() { |
| 168 | return mSplitName; |
| 169 | } |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 170 | |
| 171 | public int getFlags() { |
| 172 | return mFlags; |
| 173 | } |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 174 | |
| 175 | public int getCompilationReason() { |
| 176 | return mCompilationReason; |
| 177 | } |
Calin Juravle | ffcd7a5 | 2020-04-09 20:05:33 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Creates a new set of DexoptOptions which are the same with the exception of the compiler |
| 181 | * filter (set to the given value). |
| 182 | */ |
| 183 | public DexoptOptions overrideCompilerFilter(String newCompilerFilter) { |
| 184 | return new DexoptOptions( |
| 185 | mPackageName, |
| 186 | mCompilationReason, |
| 187 | newCompilerFilter, |
| 188 | mSplitName, |
| 189 | mFlags); |
| 190 | } |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 191 | } |