Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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; |
| 18 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 22 | import android.content.pm.PackageInfo; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageParser; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 24 | import android.os.Environment; |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 25 | import android.os.FileUtils; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 26 | import android.os.PowerManager; |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 27 | import android.os.SystemClock; |
Nicolas Geoffray | 20a894e | 2017-09-08 13:01:40 +0100 | [diff] [blame^] | 28 | import android.os.SystemProperties; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 29 | import android.os.UserHandle; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 30 | import android.os.WorkSource; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | import android.util.Slog; |
Jeff Hao | 5def5e5 | 2017-04-19 14:42:16 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 34 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 35 | import com.android.internal.annotations.GuardedBy; |
Narayan Kamath | 88eea9e | 2016-05-02 14:44:31 +0100 | [diff] [blame] | 36 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 37 | import com.android.server.pm.Installer.InstallerException; |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 38 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 39 | import java.io.File; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 40 | import java.io.IOException; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 41 | import java.util.ArrayList; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 42 | import java.util.List; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 43 | import java.util.Set; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 44 | |
| 45 | import dalvik.system.DexFile; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 46 | |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 47 | import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; |
| 48 | import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 49 | import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED; |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 50 | import static com.android.server.pm.Installer.DEXOPT_PUBLIC; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 51 | import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX; |
| 52 | import static com.android.server.pm.Installer.DEXOPT_FORCE; |
| 53 | import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE; |
| 54 | import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 55 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 56 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 57 | |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 58 | import static com.android.server.pm.PackageManagerService.WATCHDOG_TIMEOUT; |
Nicolas Geoffray | b0818e8 | 2017-05-05 10:23:19 +0100 | [diff] [blame] | 59 | |
Nicolas Geoffray | d093b20 | 2017-05-03 13:11:58 +0100 | [diff] [blame] | 60 | import static dalvik.system.DexFile.getNonProfileGuidedCompilerFilter; |
| 61 | import static dalvik.system.DexFile.getSafeModeCompilerFilter; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 62 | import static dalvik.system.DexFile.isProfileGuidedCompilerFilter; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Helper class for running dexopt command on packages. |
| 66 | */ |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 67 | public class PackageDexOptimizer { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 68 | private static final String TAG = "PackageManager.DexOptimizer"; |
| 69 | static final String OAT_DIR_NAME = "oat"; |
| 70 | // TODO b/19550105 Remove error codes and use exceptions |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 71 | public static final int DEX_OPT_SKIPPED = 0; |
| 72 | public static final int DEX_OPT_PERFORMED = 1; |
| 73 | public static final int DEX_OPT_FAILED = -1; |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 74 | // One minute over PM WATCHDOG_TIMEOUT |
| 75 | private static final long WAKELOCK_TIMEOUT_MS = WATCHDOG_TIMEOUT + 1000 * 60; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 76 | |
Calin Juravle | aae3576 | 2017-02-14 17:53:13 -0800 | [diff] [blame] | 77 | /** Special library name that skips shared libraries check during compilation. */ |
| 78 | public static final String SKIP_SHARED_LIBRARY_CHECK = "&"; |
| 79 | |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 80 | @GuardedBy("mInstallLock") |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 81 | private final Installer mInstaller; |
| 82 | private final Object mInstallLock; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 83 | |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 84 | @GuardedBy("mInstallLock") |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 85 | private final PowerManager.WakeLock mDexoptWakeLock; |
| 86 | private volatile boolean mSystemReady; |
| 87 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 88 | PackageDexOptimizer(Installer installer, Object installLock, Context context, |
| 89 | String wakeLockTag) { |
| 90 | this.mInstaller = installer; |
| 91 | this.mInstallLock = installLock; |
| 92 | |
| 93 | PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 94 | mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); |
| 95 | } |
| 96 | |
| 97 | protected PackageDexOptimizer(PackageDexOptimizer from) { |
| 98 | this.mInstaller = from.mInstaller; |
| 99 | this.mInstallLock = from.mInstallLock; |
| 100 | this.mDexoptWakeLock = from.mDexoptWakeLock; |
| 101 | this.mSystemReady = from.mSystemReady; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 104 | static boolean canOptimizePackage(PackageParser.Package pkg) { |
Nicolas Geoffray | 20a894e | 2017-09-08 13:01:40 +0100 | [diff] [blame^] | 105 | // We do not dexopt a package with no code. |
| 106 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | // We do not dexopt a priv-app package when pm.dexopt.priv-apps is false. |
| 111 | if (pkg.isPrivilegedApp()) { |
| 112 | return SystemProperties.getBoolean("pm.dexopt.priv-apps", true); |
| 113 | } |
| 114 | |
| 115 | return true; |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 118 | /** |
| 119 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 120 | * instruction sets. |
| 121 | * |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 122 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are |
| 123 | * synchronized on {@link #mInstallLock}. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 124 | */ |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 125 | int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries, |
Andreas Gampe | 7e619a9 | 2016-07-12 22:42:41 -0700 | [diff] [blame] | 126 | String[] instructionSets, boolean checkProfiles, String targetCompilationFilter, |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 127 | CompilerStats.PackageStats packageStats, boolean isUsedByOtherApps, |
| 128 | boolean bootComplete) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 129 | if (!canOptimizePackage(pkg)) { |
| 130 | return DEX_OPT_SKIPPED; |
| 131 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 132 | synchronized (mInstallLock) { |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 133 | final long acquireTime = acquireWakeLockLI(pkg.applicationInfo.uid); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 134 | try { |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 135 | return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles, |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 136 | targetCompilationFilter, packageStats, isUsedByOtherApps, bootComplete); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 137 | } finally { |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 138 | releaseWakeLockLI(acquireTime); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 139 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 143 | /** |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 144 | * Performs dexopt on all code paths of the given package. |
| 145 | * It assumes the install lock is held. |
| 146 | */ |
| 147 | @GuardedBy("mInstallLock") |
| 148 | private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries, |
| 149 | String[] targetInstructionSets, boolean checkForProfileUpdates, |
Calin Juravle | 07b6eab | 2017-03-01 19:55:35 -0800 | [diff] [blame] | 150 | String targetCompilerFilter, CompilerStats.PackageStats packageStats, |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 151 | boolean isUsedByOtherApps, boolean bootComplete) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 152 | final String[] instructionSets = targetInstructionSets != null ? |
| 153 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 154 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
Jeff Hao | 5def5e5 | 2017-04-19 14:42:16 -0700 | [diff] [blame] | 155 | final List<String> paths = pkg.getAllCodePaths(); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 156 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 157 | |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 158 | final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo, |
Calin Juravle | 07b6eab | 2017-03-01 19:55:35 -0800 | [diff] [blame] | 159 | targetCompilerFilter, isUsedByOtherApps); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 160 | final boolean profileUpdated = checkForProfileUpdates && |
| 161 | isProfileUpdated(pkg, sharedGid, compilerFilter); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 162 | |
Jeff Hao | 5def5e5 | 2017-04-19 14:42:16 -0700 | [diff] [blame] | 163 | final String sharedLibrariesPath = getSharedLibrariesPath(sharedLibraries); |
Calin Juravle | efb1c94 | 2017-04-04 20:31:44 -0700 | [diff] [blame] | 164 | // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 165 | final int dexoptFlags = getDexFlags(pkg, compilerFilter, bootComplete); |
Jeff Hao | 5def5e5 | 2017-04-19 14:42:16 -0700 | [diff] [blame] | 166 | // Get the dependencies of each split in the package. For each code path in the package, |
| 167 | // this array contains the relative paths of each split it depends on, separated by colons. |
| 168 | String[] splitDependencies = getSplitDependencies(pkg); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 169 | |
| 170 | int result = DEX_OPT_SKIPPED; |
Jeff Hao | 5def5e5 | 2017-04-19 14:42:16 -0700 | [diff] [blame] | 171 | for (int i = 0; i < paths.size(); i++) { |
| 172 | // Skip paths that have no code. |
| 173 | if ((i == 0 && (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) || |
| 174 | (i != 0 && (pkg.splitFlags[i - 1] & ApplicationInfo.FLAG_HAS_CODE) == 0)) { |
| 175 | continue; |
| 176 | } |
| 177 | // Append shared libraries with split dependencies for this split. |
| 178 | String path = paths.get(i); |
| 179 | String sharedLibrariesPathWithSplits; |
| 180 | if (sharedLibrariesPath != null && splitDependencies[i] != null) { |
| 181 | sharedLibrariesPathWithSplits = sharedLibrariesPath + ":" + splitDependencies[i]; |
| 182 | } else { |
| 183 | sharedLibrariesPathWithSplits = |
| 184 | splitDependencies[i] != null ? splitDependencies[i] : sharedLibrariesPath; |
| 185 | } |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 186 | for (String dexCodeIsa : dexCodeInstructionSets) { |
| 187 | int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated, |
Jeff Hao | 5def5e5 | 2017-04-19 14:42:16 -0700 | [diff] [blame] | 188 | sharedLibrariesPathWithSplits, dexoptFlags, sharedGid, packageStats); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 189 | // The end result is: |
| 190 | // - FAILED if any path failed, |
| 191 | // - PERFORMED if at least one path needed compilation, |
| 192 | // - SKIPPED when all paths are up to date |
| 193 | if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) { |
| 194 | result = newResult; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return result; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Performs dexopt on the {@code path} belonging to the package {@code pkg}. |
| 203 | * |
| 204 | * @return |
| 205 | * DEX_OPT_FAILED if there was any exception during dexopt |
| 206 | * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path. |
| 207 | * DEX_OPT_SKIPPED if the path does not need to be deopt-ed. |
| 208 | */ |
| 209 | @GuardedBy("mInstallLock") |
| 210 | private int dexOptPath(PackageParser.Package pkg, String path, String isa, |
| 211 | String compilerFilter, boolean profileUpdated, String sharedLibrariesPath, |
| 212 | int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) { |
| 213 | int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated); |
| 214 | if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) { |
| 215 | return DEX_OPT_SKIPPED; |
| 216 | } |
| 217 | |
| 218 | // TODO(calin): there's no need to try to create the oat dir over and over again, |
| 219 | // especially since it involve an extra installd call. We should create |
| 220 | // if (if supported) on the fly during the dexopt call. |
| 221 | String oatDir = createOatDirIfSupported(pkg, isa); |
| 222 | |
| 223 | Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path |
| 224 | + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa |
| 225 | + " dexoptFlags=" + printDexoptFlags(dexoptFlags) |
| 226 | + " target-filter=" + compilerFilter + " oatDir=" + oatDir |
| 227 | + " sharedLibraries=" + sharedLibrariesPath); |
| 228 | |
| 229 | try { |
| 230 | long startTime = System.currentTimeMillis(); |
| 231 | |
| 232 | mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags, |
Calin Juravle | 811a75a | 2017-04-05 22:49:38 -0700 | [diff] [blame] | 233 | compilerFilter, pkg.volumeUuid, sharedLibrariesPath, pkg.applicationInfo.seInfo); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 234 | |
| 235 | if (packageStats != null) { |
| 236 | long endTime = System.currentTimeMillis(); |
| 237 | packageStats.setCompileTime(path, (int)(endTime - startTime)); |
| 238 | } |
| 239 | return DEX_OPT_PERFORMED; |
| 240 | } catch (InstallerException e) { |
| 241 | Slog.w(TAG, "Failed to dexopt", e); |
| 242 | return DEX_OPT_FAILED; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 247 | * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}. |
| 248 | * |
| 249 | * @return |
| 250 | * DEX_OPT_FAILED if there was any exception during dexopt |
| 251 | * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path. |
| 252 | * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file |
| 253 | * didn't need an update. That's because at the moment we don't get more than success/failure |
| 254 | * from installd. |
| 255 | * |
| 256 | * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than |
| 257 | * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though |
| 258 | * that seems wasteful. |
| 259 | */ |
| 260 | public int dexOptSecondaryDexPath(ApplicationInfo info, String path, Set<String> isas, |
| 261 | String compilerFilter, boolean isUsedByOtherApps) { |
| 262 | synchronized (mInstallLock) { |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 263 | final long acquireTime = acquireWakeLockLI(info.uid); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 264 | try { |
| 265 | return dexOptSecondaryDexPathLI(info, path, isas, compilerFilter, |
| 266 | isUsedByOtherApps); |
| 267 | } finally { |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 268 | releaseWakeLockLI(acquireTime); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @GuardedBy("mInstallLock") |
Fyodor Kupolov | 28f2855 | 2017-05-02 12:11:02 -0700 | [diff] [blame] | 274 | private long acquireWakeLockLI(final int uid) { |
| 275 | // During boot the system doesn't need to instantiate and obtain a wake lock. |
| 276 | // PowerManager might not be ready, but that doesn't mean that we can't proceed with |
| 277 | // dexopt. |
| 278 | if (!mSystemReady) { |
| 279 | return -1; |
| 280 | } |
| 281 | mDexoptWakeLock.setWorkSource(new WorkSource(uid)); |
| 282 | mDexoptWakeLock.acquire(WAKELOCK_TIMEOUT_MS); |
| 283 | return SystemClock.elapsedRealtime(); |
| 284 | } |
| 285 | |
| 286 | @GuardedBy("mInstallLock") |
| 287 | private void releaseWakeLockLI(final long acquireTime) { |
| 288 | if (acquireTime < 0) { |
| 289 | return; |
| 290 | } |
| 291 | try { |
| 292 | if (mDexoptWakeLock.isHeld()) { |
| 293 | mDexoptWakeLock.release(); |
| 294 | } |
| 295 | final long duration = SystemClock.elapsedRealtime() - acquireTime; |
| 296 | if (duration >= WAKELOCK_TIMEOUT_MS) { |
| 297 | Slog.wtf(TAG, "WakeLock " + mDexoptWakeLock.getTag() |
| 298 | + " time out. Operation took " + duration + " ms. Thread: " |
| 299 | + Thread.currentThread().getName()); |
| 300 | } |
| 301 | } catch (Exception e) { |
| 302 | Slog.wtf(TAG, "Error while releasing " + mDexoptWakeLock.getTag() + " lock", e); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | @GuardedBy("mInstallLock") |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 307 | private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, Set<String> isas, |
| 308 | String compilerFilter, boolean isUsedByOtherApps) { |
Calin Juravle | efb1c94 | 2017-04-04 20:31:44 -0700 | [diff] [blame] | 309 | compilerFilter = getRealCompilerFilter(info, compilerFilter, isUsedByOtherApps); |
| 310 | // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 311 | // Secondary dex files are currently not compiled at boot. |
| 312 | int dexoptFlags = getDexFlags(info, compilerFilter, /* bootComplete */ true) |
| 313 | | DEXOPT_SECONDARY_DEX; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 314 | // Check the app storage and add the appropriate flags. |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 315 | if (info.deviceProtectedDataDir != null && |
| 316 | FileUtils.contains(info.deviceProtectedDataDir, path)) { |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 317 | dexoptFlags |= DEXOPT_STORAGE_DE; |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 318 | } else if (info.credentialProtectedDataDir != null && |
| 319 | FileUtils.contains(info.credentialProtectedDataDir, path)) { |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 320 | dexoptFlags |= DEXOPT_STORAGE_CE; |
| 321 | } else { |
| 322 | Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName); |
| 323 | return DEX_OPT_FAILED; |
| 324 | } |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 325 | Log.d(TAG, "Running dexopt on: " + path |
| 326 | + " pkg=" + info.packageName + " isa=" + isas |
| 327 | + " dexoptFlags=" + printDexoptFlags(dexoptFlags) |
| 328 | + " target-filter=" + compilerFilter); |
| 329 | |
| 330 | try { |
| 331 | for (String isa : isas) { |
| 332 | // Reuse the same dexopt path as for the primary apks. We don't need all the |
| 333 | // arguments as some (dexopNeeded and oatDir) will be computed by installd because |
| 334 | // system server cannot read untrusted app content. |
| 335 | // TODO(calin): maybe add a separate call. |
| 336 | mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0, |
| 337 | /*oatDir*/ null, dexoptFlags, |
Calin Juravle | 811a75a | 2017-04-05 22:49:38 -0700 | [diff] [blame] | 338 | compilerFilter, info.volumeUuid, SKIP_SHARED_LIBRARY_CHECK, info.seInfoUser); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return DEX_OPT_PERFORMED; |
| 342 | } catch (InstallerException e) { |
| 343 | Slog.w(TAG, "Failed to dexopt", e); |
| 344 | return DEX_OPT_FAILED; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 349 | * Adjust the given dexopt-needed value. Can be overridden to influence the decision to |
| 350 | * optimize or not (and in what way). |
| 351 | */ |
| 352 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 353 | return dexoptNeeded; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Adjust the given dexopt flags that will be passed to the installer. |
| 358 | */ |
| 359 | protected int adjustDexoptFlags(int dexoptFlags) { |
| 360 | return dexoptFlags; |
| 361 | } |
| 362 | |
Narayan Kamath | 88eea9e | 2016-05-02 14:44:31 +0100 | [diff] [blame] | 363 | /** |
| 364 | * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}. |
| 365 | */ |
| 366 | void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) { |
| 367 | final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo); |
| 368 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 369 | |
| 370 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 371 | |
| 372 | for (String instructionSet : dexCodeInstructionSets) { |
| 373 | pw.println("Instruction Set: " + instructionSet); |
| 374 | pw.increaseIndent(); |
| 375 | for (String path : paths) { |
| 376 | String status = null; |
| 377 | try { |
| 378 | status = DexFile.getDexFileStatus(path, instructionSet); |
| 379 | } catch (IOException ioe) { |
| 380 | status = "[Exception]: " + ioe.getMessage(); |
| 381 | } |
| 382 | pw.println("path: " + path); |
| 383 | pw.println("status: " + status); |
| 384 | } |
| 385 | pw.decreaseIndent(); |
| 386 | } |
| 387 | } |
| 388 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 389 | /** |
| 390 | * Returns the compiler filter that should be used to optimize the package code. |
| 391 | * The target filter will be updated if the package code is used by other apps |
| 392 | * or if it has the safe mode flag set. |
| 393 | */ |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 394 | private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter, |
| 395 | boolean isUsedByOtherApps) { |
| 396 | int flags = info.flags; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 397 | boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
Mathieu Chartier | 41e4a37 | 2016-09-02 16:36:42 -0700 | [diff] [blame] | 398 | if (vmSafeMode) { |
Nicolas Geoffray | d093b20 | 2017-05-03 13:11:58 +0100 | [diff] [blame] | 399 | return getSafeModeCompilerFilter(targetCompilerFilter); |
Mathieu Chartier | 41e4a37 | 2016-09-02 16:36:42 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 402 | if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 403 | // If the dex files is used by other apps, we cannot use profile-guided compilation. |
| 404 | return getNonProfileGuidedCompilerFilter(targetCompilerFilter); |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 407 | return targetCompilerFilter; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 410 | /** |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 411 | * Computes the dex flags that needs to be pass to installd for the given package and compiler |
| 412 | * filter. |
| 413 | */ |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 414 | private int getDexFlags(PackageParser.Package pkg, String compilerFilter, |
| 415 | boolean bootComplete) { |
| 416 | return getDexFlags(pkg.applicationInfo, compilerFilter, bootComplete); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 419 | private int getDexFlags(ApplicationInfo info, String compilerFilter, boolean bootComplete) { |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 420 | int flags = info.flags; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 421 | boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 422 | // Profile guide compiled oat files should not be public. |
| 423 | boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 424 | boolean isPublic = !info.isForwardLocked() && !isProfileGuidedFilter; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 425 | int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0; |
| 426 | int dexFlags = |
| 427 | (isPublic ? DEXOPT_PUBLIC : 0) |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 428 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
| 429 | | profileFlag |
Nicolas Geoffray | 3b4359a | 2017-05-25 13:53:22 +0100 | [diff] [blame] | 430 | | (bootComplete ? DEXOPT_BOOTCOMPLETE : 0); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 431 | return adjustDexoptFlags(dexFlags); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Assesses if there's a need to perform dexopt on {@code path} for the given |
| 436 | * configuration (isa, compiler filter, profile). |
| 437 | */ |
| 438 | private int getDexoptNeeded(String path, String isa, String compilerFilter, |
| 439 | boolean newProfile) { |
| 440 | int dexoptNeeded; |
| 441 | try { |
Shubham Ajmera | 52c8620 | 2017-07-24 15:21:04 -0700 | [diff] [blame] | 442 | dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile, |
| 443 | false /* downgrade */); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 444 | } catch (IOException ioe) { |
| 445 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 446 | return DEX_OPT_FAILED; |
| 447 | } |
| 448 | return adjustDexoptNeeded(dexoptNeeded); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Computes the shared libraries path that should be passed to dexopt. |
| 453 | */ |
| 454 | private String getSharedLibrariesPath(String[] sharedLibraries) { |
| 455 | if (sharedLibraries == null || sharedLibraries.length == 0) { |
| 456 | return null; |
| 457 | } |
| 458 | StringBuilder sb = new StringBuilder(); |
| 459 | for (String lib : sharedLibraries) { |
| 460 | if (sb.length() != 0) { |
| 461 | sb.append(":"); |
| 462 | } |
| 463 | sb.append(lib); |
| 464 | } |
| 465 | return sb.toString(); |
| 466 | } |
| 467 | |
| 468 | /** |
Jeff Hao | 5def5e5 | 2017-04-19 14:42:16 -0700 | [diff] [blame] | 469 | * Walks dependency tree and gathers the dependencies for each split in a split apk. |
| 470 | * The split paths are stored as relative paths, separated by colons. |
| 471 | */ |
| 472 | private String[] getSplitDependencies(PackageParser.Package pkg) { |
| 473 | // Convert all the code paths to relative paths. |
| 474 | String baseCodePath = new File(pkg.baseCodePath).getParent(); |
| 475 | List<String> paths = pkg.getAllCodePaths(); |
| 476 | String[] splitDependencies = new String[paths.size()]; |
| 477 | for (int i = 0; i < paths.size(); i++) { |
| 478 | File pathFile = new File(paths.get(i)); |
| 479 | String fileName = pathFile.getName(); |
| 480 | paths.set(i, fileName); |
| 481 | |
| 482 | // Sanity check that the base paths of the splits are all the same. |
| 483 | String basePath = pathFile.getParent(); |
| 484 | if (!basePath.equals(baseCodePath)) { |
| 485 | Slog.wtf(TAG, "Split paths have different base paths: " + basePath + " and " + |
| 486 | baseCodePath); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // If there are no other dependencies, fill in the implicit dependency on the base apk. |
| 491 | SparseArray<int[]> dependencies = pkg.applicationInfo.splitDependencies; |
| 492 | if (dependencies == null) { |
| 493 | for (int i = 1; i < paths.size(); i++) { |
| 494 | splitDependencies[i] = paths.get(0); |
| 495 | } |
| 496 | return splitDependencies; |
| 497 | } |
| 498 | |
| 499 | // Fill in the dependencies, skipping the base apk which has no dependencies. |
| 500 | for (int i = 1; i < dependencies.size(); i++) { |
| 501 | getParentDependencies(dependencies.keyAt(i), paths, dependencies, splitDependencies); |
| 502 | } |
| 503 | |
| 504 | return splitDependencies; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Recursive method to generate dependencies for a particular split. |
| 509 | * The index is a key from the package's splitDependencies. |
| 510 | */ |
| 511 | private String getParentDependencies(int index, List<String> paths, |
| 512 | SparseArray<int[]> dependencies, String[] splitDependencies) { |
| 513 | // The base apk is always first, and has no dependencies. |
| 514 | if (index == 0) { |
| 515 | return null; |
| 516 | } |
| 517 | // Return the result if we've computed the dependencies for this index already. |
| 518 | if (splitDependencies[index] != null) { |
| 519 | return splitDependencies[index]; |
| 520 | } |
| 521 | // Get the dependencies for the parent of this index and append its path to it. |
| 522 | int parent = dependencies.get(index)[0]; |
| 523 | String parentDependencies = |
| 524 | getParentDependencies(parent, paths, dependencies, splitDependencies); |
| 525 | String path = parentDependencies == null ? paths.get(parent) : |
| 526 | parentDependencies + ":" + paths.get(parent); |
| 527 | splitDependencies[index] = path; |
| 528 | return path; |
| 529 | } |
| 530 | |
| 531 | /** |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 532 | * Checks if there is an update on the profile information of the {@code pkg}. |
| 533 | * If the compiler filter is not profile guided the method returns false. |
| 534 | * |
| 535 | * Note that this is a "destructive" operation with side effects. Under the hood the |
| 536 | * current profile and the reference profile will be merged and subsequent calls |
| 537 | * may return a different result. |
| 538 | */ |
| 539 | private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String compilerFilter) { |
| 540 | // Check if we are allowed to merge and if the compiler filter is profile guided. |
| 541 | if (!isProfileGuidedCompilerFilter(compilerFilter)) { |
| 542 | return false; |
| 543 | } |
| 544 | // Merge profiles. It returns whether or not there was an updated in the profile info. |
| 545 | try { |
| 546 | return mInstaller.mergeProfiles(uid, pkg.packageName); |
| 547 | } catch (InstallerException e) { |
| 548 | Slog.w(TAG, "Failed to merge profiles", e); |
| 549 | } |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Creates oat dir for the specified package if needed and supported. |
| 555 | * In certain cases oat directory |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 556 | * <strong>cannot</strong> be created: |
| 557 | * <ul> |
| 558 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 559 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 560 | * </ul> |
| 561 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 562 | * @return Absolute path to the oat directory or null, if oat directory |
| 563 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 564 | */ |
| 565 | @Nullable |
Todd Kennedy | 27c24fb | 2015-09-17 16:49:25 -0700 | [diff] [blame] | 566 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 567 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 568 | return null; |
| 569 | } |
| 570 | File codePath = new File(pkg.codePath); |
| 571 | if (codePath.isDirectory()) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 572 | // TODO(calin): why do we create this only if the codePath is a directory? (i.e for |
| 573 | // cluster packages). It seems that the logic for the folder creation is |
| 574 | // split between installd and here. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 575 | File oatDir = getOatDir(codePath); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 576 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 577 | mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 578 | } catch (InstallerException e) { |
| 579 | Slog.w(TAG, "Failed to create oat dir", e); |
| 580 | return null; |
| 581 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 582 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 583 | } |
| 584 | return null; |
| 585 | } |
| 586 | |
| 587 | static File getOatDir(File codePath) { |
| 588 | return new File(codePath, OAT_DIR_NAME); |
| 589 | } |
| 590 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 591 | void systemReady() { |
| 592 | mSystemReady = true; |
| 593 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 594 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 595 | private String printDexoptFlags(int flags) { |
| 596 | ArrayList<String> flagsList = new ArrayList<>(); |
| 597 | |
| 598 | if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) { |
| 599 | flagsList.add("boot_complete"); |
| 600 | } |
| 601 | if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) { |
| 602 | flagsList.add("debuggable"); |
| 603 | } |
| 604 | if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) { |
| 605 | flagsList.add("profile_guided"); |
| 606 | } |
| 607 | if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) { |
| 608 | flagsList.add("public"); |
| 609 | } |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 610 | if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) { |
| 611 | flagsList.add("secondary"); |
| 612 | } |
| 613 | if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) { |
| 614 | flagsList.add("force"); |
| 615 | } |
| 616 | if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) { |
| 617 | flagsList.add("storage_ce"); |
| 618 | } |
| 619 | if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) { |
| 620 | flagsList.add("storage_de"); |
| 621 | } |
| 622 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 623 | return String.join(",", flagsList); |
| 624 | } |
| 625 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 626 | /** |
| 627 | * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a |
| 628 | * dexopt path. |
| 629 | */ |
| 630 | public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer { |
| 631 | |
| 632 | public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock, |
| 633 | Context context, String wakeLockTag) { |
| 634 | super(installer, installLock, context, wakeLockTag); |
| 635 | } |
| 636 | |
| 637 | public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) { |
| 638 | super(from); |
| 639 | } |
| 640 | |
| 641 | @Override |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 642 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
Nicolas Geoffray | 96d12a9 | 2017-05-03 11:51:53 +0100 | [diff] [blame] | 643 | if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) { |
| 644 | // Ensure compilation by pretending a compiler filter change on the |
| 645 | // apk/odex location (the reason for the '-'. A positive value means |
| 646 | // the 'oat' location). |
| 647 | return -DexFile.DEX2OAT_FOR_FILTER; |
| 648 | } |
| 649 | return dexoptNeeded; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 650 | } |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 651 | |
| 652 | @Override |
| 653 | protected int adjustDexoptFlags(int flags) { |
| 654 | // Add DEXOPT_FORCE flag to signal installd that it should force compilation |
| 655 | // and discard dexoptanalyzer result. |
| 656 | return flags | DEXOPT_FORCE; |
| 657 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 658 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 659 | } |