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