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 | |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
| 22 | |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Options used for dexopt invocations. |
| 25 | */ |
| 26 | public final class DexoptOptions { |
| 27 | // When set, the profiles will be checked for updates before calling dexopt. If |
| 28 | // the apps profiles didn't update in a meaningful way (decided by the compiler), dexopt |
| 29 | // will be skipped. |
| 30 | // Currently this only affects the optimization of primary apks. Secondary dex files |
| 31 | // will always check the profiles for updates. |
| 32 | public static final int DEXOPT_CHECK_FOR_PROFILES_UPDATES = 1 << 0; |
| 33 | |
| 34 | // When set, dexopt will execute unconditionally (even if not needed). |
| 35 | public static final int DEXOPT_FORCE = 1 << 1; |
| 36 | |
| 37 | // Whether or not the invocation of dexopt is done after the boot is completed. This is used |
| 38 | // in order to adjust the priority of the compilation thread. |
| 39 | public static final int DEXOPT_BOOT_COMPLETE = 1 << 2; |
| 40 | |
| 41 | // When set, the dexopt invocation will optimize only the secondary dex files. If false, dexopt |
| 42 | // will only consider the primary apk. |
| 43 | public static final int DEXOPT_ONLY_SECONDARY_DEX = 1 << 3; |
| 44 | |
| 45 | // When set, dexopt will optimize only dex files that are used by other apps. |
| 46 | // Currently, this flag is ignored for primary apks. |
| 47 | public static final int DEXOPT_ONLY_SHARED_DEX = 1 << 4; |
| 48 | |
| 49 | // When set, dexopt will attempt to scale down the optimizations previously applied in order |
| 50 | // save disk space. |
| 51 | public static final int DEXOPT_DOWNGRADE = 1 << 5; |
| 52 | |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 53 | // When set, dexopt will compile the dex file as a shared library even if it is not actually |
| 54 | // used by other apps. This is used to force the compilation or shared libraries declared |
| 55 | // with in the manifest with ''uses-library' before we have a chance to detect they are |
| 56 | // actually shared at runtime. |
| 57 | public static final int DEXOPT_AS_SHARED_LIBRARY = 1 << 6; |
| 58 | |
David Sehr | 11c22e3 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 59 | // When set, indicates that dexopt is invoked from the background service. |
| 60 | public static final int DEXOPT_IDLE_BACKGROUND_JOB = 1 << 9; |
| 61 | |
Calin Juravle | 29c772c | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 62 | // When set, indicates that dexopt is invoked from the install time flow and |
| 63 | // should get the dex metdata file if present. |
| 64 | public static final int DEXOPT_INSTALL_WITH_DEX_METADATA_FILE = 1 << 10; |
| 65 | |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 66 | // The name of package to optimize. |
| 67 | private final String mPackageName; |
| 68 | |
| 69 | // The intended target compiler filter. Note that dexopt might adjust the filter before the |
| 70 | // execution based on factors like: vmSafeMode and packageUsedByOtherApps. |
| 71 | private final String mCompilerFilter; |
| 72 | |
| 73 | // The set of flags for the dexopt options. It's a mix of the DEXOPT_* flags. |
| 74 | private final int mFlags; |
| 75 | |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 76 | // When not null, dexopt will optimize only the split identified by this name. |
| 77 | // It only applies for primary apk and it's always null if mOnlySecondaryDex is true. |
| 78 | private final String mSplitName; |
| 79 | |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 80 | // The reason for invoking dexopt (see PackageManagerService.REASON_* constants). |
| 81 | // A -1 value denotes an unknown reason. |
| 82 | private final int mCompilationReason; |
| 83 | |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 84 | public DexoptOptions(String packageName, String compilerFilter, int flags) { |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 85 | this(packageName, /*compilationReason*/ -1, compilerFilter, /*splitName*/ null, flags); |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 88 | public DexoptOptions(String packageName, int compilationReason, int flags) { |
| 89 | this(packageName, compilationReason, getCompilerFilterForReason(compilationReason), |
| 90 | /*splitName*/ null, flags); |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 93 | public DexoptOptions(String packageName, int compilationReason, String compilerFilter, |
| 94 | String splitName, int flags) { |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 95 | int validityMask = |
| 96 | DEXOPT_CHECK_FOR_PROFILES_UPDATES | |
| 97 | DEXOPT_FORCE | |
| 98 | DEXOPT_BOOT_COMPLETE | |
| 99 | DEXOPT_ONLY_SECONDARY_DEX | |
| 100 | DEXOPT_ONLY_SHARED_DEX | |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 101 | DEXOPT_DOWNGRADE | |
David Sehr | 11c22e3 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 102 | DEXOPT_AS_SHARED_LIBRARY | |
Calin Juravle | 29c772c | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 103 | DEXOPT_IDLE_BACKGROUND_JOB | |
| 104 | DEXOPT_INSTALL_WITH_DEX_METADATA_FILE; |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 105 | if ((flags & (~validityMask)) != 0) { |
| 106 | throw new IllegalArgumentException("Invalid flags : " + Integer.toHexString(flags)); |
| 107 | } |
| 108 | |
| 109 | mPackageName = packageName; |
| 110 | mCompilerFilter = compilerFilter; |
| 111 | mFlags = flags; |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 112 | mSplitName = splitName; |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 113 | mCompilationReason = compilationReason; |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public String getPackageName() { |
| 117 | return mPackageName; |
| 118 | } |
| 119 | |
| 120 | public boolean isCheckForProfileUpdates() { |
| 121 | return (mFlags & DEXOPT_CHECK_FOR_PROFILES_UPDATES) != 0; |
| 122 | } |
| 123 | |
| 124 | public String getCompilerFilter() { |
| 125 | return mCompilerFilter; |
| 126 | } |
| 127 | |
| 128 | public boolean isForce() { |
| 129 | return (mFlags & DEXOPT_FORCE) != 0; |
| 130 | } |
| 131 | |
| 132 | public boolean isBootComplete() { |
| 133 | return (mFlags & DEXOPT_BOOT_COMPLETE) != 0; |
| 134 | } |
| 135 | |
| 136 | public boolean isDexoptOnlySecondaryDex() { |
| 137 | return (mFlags & DEXOPT_ONLY_SECONDARY_DEX) != 0; |
| 138 | } |
| 139 | |
| 140 | public boolean isDexoptOnlySharedDex() { |
| 141 | return (mFlags & DEXOPT_ONLY_SHARED_DEX) != 0; |
| 142 | } |
| 143 | |
| 144 | public boolean isDowngrade() { |
| 145 | return (mFlags & DEXOPT_DOWNGRADE) != 0; |
| 146 | } |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 147 | |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 148 | public boolean isDexoptAsSharedLibrary() { |
| 149 | return (mFlags & DEXOPT_AS_SHARED_LIBRARY) != 0; |
| 150 | } |
| 151 | |
David Sehr | 11c22e3 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 152 | public boolean isDexoptIdleBackgroundJob() { |
| 153 | return (mFlags & DEXOPT_IDLE_BACKGROUND_JOB) != 0; |
| 154 | } |
| 155 | |
Calin Juravle | 29c772c | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 156 | public boolean isDexoptInstallWithDexMetadata() { |
| 157 | return (mFlags & DEXOPT_INSTALL_WITH_DEX_METADATA_FILE) != 0; |
| 158 | } |
| 159 | |
Calin Juravle | caed600 | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 160 | public String getSplitName() { |
| 161 | return mSplitName; |
| 162 | } |
Calin Juravle | fad6dc1 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 163 | |
| 164 | public int getFlags() { |
| 165 | return mFlags; |
| 166 | } |
Calin Juravle | b981617 | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 167 | |
| 168 | public int getCompilationReason() { |
| 169 | return mCompilationReason; |
| 170 | } |
Calin Juravle | dea3fd8 | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 171 | } |