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; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageParser.Package; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 24 | import android.os.PowerManager; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 25 | import android.os.UserHandle; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 26 | import android.os.WorkSource; |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 27 | import android.os.storage.StorageManager; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 28 | import android.util.ArraySet; |
| 29 | import android.util.Log; |
| 30 | import android.util.Slog; |
| 31 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 32 | import com.android.internal.os.InstallerConnection.InstallerException; |
| 33 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 34 | import java.io.File; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 35 | import java.io.IOException; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | |
| 39 | import dalvik.system.DexFile; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 40 | |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 41 | import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; |
| 42 | import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; |
| 43 | import static com.android.server.pm.Installer.DEXOPT_PUBLIC; |
| 44 | import static com.android.server.pm.Installer.DEXOPT_SAFEMODE; |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 45 | import static com.android.server.pm.Installer.DEXOPT_EXTRACTONLY; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 46 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 47 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
| 48 | |
| 49 | /** |
| 50 | * Helper class for running dexopt command on packages. |
| 51 | */ |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 52 | class PackageDexOptimizer { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 53 | private static final String TAG = "PackageManager.DexOptimizer"; |
| 54 | static final String OAT_DIR_NAME = "oat"; |
| 55 | // TODO b/19550105 Remove error codes and use exceptions |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 56 | static final int DEX_OPT_SKIPPED = 0; |
| 57 | static final int DEX_OPT_PERFORMED = 1; |
| 58 | static final int DEX_OPT_DEFERRED = 2; |
| 59 | static final int DEX_OPT_FAILED = -1; |
| 60 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 61 | private static final boolean DEBUG_DEXOPT = PackageManagerService.DEBUG_DEXOPT; |
| 62 | |
| 63 | private final Installer mInstaller; |
| 64 | private final Object mInstallLock; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 65 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 66 | private final PowerManager.WakeLock mDexoptWakeLock; |
| 67 | private volatile boolean mSystemReady; |
| 68 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 69 | PackageDexOptimizer(Installer installer, Object installLock, Context context, |
| 70 | String wakeLockTag) { |
| 71 | this.mInstaller = installer; |
| 72 | this.mInstallLock = installLock; |
| 73 | |
| 74 | PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 75 | mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); |
| 76 | } |
| 77 | |
| 78 | protected PackageDexOptimizer(PackageDexOptimizer from) { |
| 79 | this.mInstaller = from.mInstaller; |
| 80 | this.mInstallLock = from.mInstallLock; |
| 81 | this.mDexoptWakeLock = from.mDexoptWakeLock; |
| 82 | this.mSystemReady = from.mSystemReady; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 85 | static boolean canOptimizePackage(PackageParser.Package pkg) { |
Calin Juravle | 6dfd83d | 2016-01-29 18:23:57 +0000 | [diff] [blame] | 86 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0; |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 91 | * instruction sets. |
| 92 | * |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 93 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are |
| 94 | * synchronized on {@link #mInstallLock}. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 95 | */ |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 96 | int performDexOpt(PackageParser.Package pkg, String[] instructionSets, boolean useProfiles, |
| 97 | boolean extractOnly) { |
| 98 | synchronized (mInstallLock) { |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 99 | final boolean useLock = mSystemReady; |
| 100 | if (useLock) { |
| 101 | mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid)); |
| 102 | mDexoptWakeLock.acquire(); |
| 103 | } |
| 104 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 105 | return performDexOptLI(pkg, instructionSets, useProfiles, extractOnly); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 106 | } finally { |
| 107 | if (useLock) { |
| 108 | mDexoptWakeLock.release(); |
| 109 | } |
| 110 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Determine whether the package should be skipped for the given instruction set. A return |
| 116 | * value of true means the package will be skipped. A return value of false means that the |
| 117 | * package will be further investigated, and potentially compiled. |
| 118 | */ |
| 119 | protected boolean shouldSkipBasedOnISA(PackageParser.Package pkg, String instructionSet) { |
| 120 | return pkg.mDexOptPerformed.contains(instructionSet); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Adjust the given dexopt-needed value. Can be overridden to influence the decision to |
| 125 | * optimize or not (and in what way). |
| 126 | */ |
| 127 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 128 | return dexoptNeeded; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Adjust the given dexopt flags that will be passed to the installer. |
| 133 | */ |
| 134 | protected int adjustDexoptFlags(int dexoptFlags) { |
| 135 | return dexoptFlags; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Update the package status after a successful compilation. |
| 140 | */ |
| 141 | protected void recordSuccessfulDexopt(PackageParser.Package pkg, String instructionSet) { |
| 142 | pkg.mDexOptPerformed.add(instructionSet); |
| 143 | } |
| 144 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 145 | private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets, |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 146 | boolean useProfiles, boolean extractOnly) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 147 | final String[] instructionSets = targetInstructionSets != null ? |
| 148 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 149 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 150 | if (!canOptimizePackage(pkg)) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 151 | return DEX_OPT_SKIPPED; |
| 152 | } |
| 153 | |
| 154 | final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
| 155 | final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 156 | |
| 157 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 158 | boolean performedDexOpt = false; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 159 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 160 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 161 | if (!useProfiles && shouldSkipBasedOnISA(pkg, dexCodeInstructionSet)) { |
| 162 | // Skip only if we do not use profiles since they might trigger a recompilation. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | |
| 166 | for (String path : paths) { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 167 | int dexoptNeeded; |
David Brazdil | 493411a | 2016-02-01 13:48:46 +0000 | [diff] [blame] | 168 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 169 | try { |
| 170 | dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName, |
| 171 | dexCodeInstructionSet, /* defer */false); |
| 172 | } catch (IOException ioe) { |
| 173 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 174 | return DEX_OPT_FAILED; |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 175 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 176 | dexoptNeeded = adjustDexoptNeeded(dexoptNeeded); |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 177 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 178 | if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) { |
Calin Juravle | 0cf8cc6 | 2016-02-03 19:05:18 +0000 | [diff] [blame] | 179 | if (useProfiles) { |
| 180 | // Profiles may trigger re-compilation. The final decision is taken in |
| 181 | // installd. |
| 182 | dexoptNeeded = DexFile.DEX2OAT_NEEDED; |
| 183 | } else { |
| 184 | // No dexopt needed and we don't use profiles. Nothing to do. |
| 185 | continue; |
| 186 | } |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 187 | } |
| 188 | final String dexoptType; |
| 189 | String oatDir = null; |
| 190 | if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) { |
| 191 | dexoptType = "dex2oat"; |
| 192 | oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet); |
| 193 | } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) { |
| 194 | dexoptType = "patchoat"; |
| 195 | } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) { |
| 196 | dexoptType = "self patchoat"; |
| 197 | } else { |
| 198 | throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded); |
| 199 | } |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 200 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 201 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 202 | Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg=" |
| 203 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
| 204 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 205 | + " extractOnly=" + extractOnly + " oatDir = " + oatDir); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 206 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 207 | final int dexFlags = adjustDexoptFlags( |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 208 | (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0) |
| 209 | | (vmSafeMode ? DEXOPT_SAFEMODE : 0) |
| 210 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 211 | | (extractOnly ? DEXOPT_EXTRACTONLY : 0) |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 212 | | DEXOPT_BOOTCOMPLETE); |
| 213 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 214 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 215 | mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet, |
| 216 | dexoptNeeded, oatDir, dexFlags, pkg.volumeUuid, useProfiles); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 217 | performedDexOpt = true; |
| 218 | } catch (InstallerException e) { |
| 219 | Slog.w(TAG, "Failed to dexopt", e); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 223 | if (!extractOnly) { |
| 224 | // At this point we haven't failed dexopt and we haven't deferred dexopt. We must |
| 225 | // either have either succeeded dexopt, or have had getDexOptNeeded tell us |
| 226 | // it isn't required. We therefore mark that this package doesn't need dexopt unless |
| 227 | // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped |
| 228 | // it. |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 229 | recordSuccessfulDexopt(pkg, dexCodeInstructionSet); |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 230 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // If we've gotten here, we're sure that no error occurred and that we haven't |
| 234 | // deferred dex-opt. We've either dex-opted one more paths or instruction sets or |
| 235 | // we've skipped all of them because they are up to date. In both cases this |
| 236 | // package doesn't need dexopt any longer. |
| 237 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 238 | } |
| 239 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 240 | /** |
| 241 | * Creates oat dir for the specified package. In certain cases oat directory |
| 242 | * <strong>cannot</strong> be created: |
| 243 | * <ul> |
| 244 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 245 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 246 | * </ul> |
| 247 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 248 | * @return Absolute path to the oat directory or null, if oat directory |
| 249 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 250 | */ |
| 251 | @Nullable |
Todd Kennedy | 27c24fb | 2015-09-17 16:49:25 -0700 | [diff] [blame] | 252 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 253 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 254 | return null; |
| 255 | } |
| 256 | File codePath = new File(pkg.codePath); |
| 257 | if (codePath.isDirectory()) { |
| 258 | File oatDir = getOatDir(codePath); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 259 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 260 | mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 261 | } catch (InstallerException e) { |
| 262 | Slog.w(TAG, "Failed to create oat dir", e); |
| 263 | return null; |
| 264 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 265 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 266 | } |
| 267 | return null; |
| 268 | } |
| 269 | |
| 270 | static File getOatDir(File codePath) { |
| 271 | return new File(codePath, OAT_DIR_NAME); |
| 272 | } |
| 273 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 274 | void systemReady() { |
| 275 | mSystemReady = true; |
| 276 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 277 | |
| 278 | /** |
| 279 | * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a |
| 280 | * dexopt path. |
| 281 | */ |
| 282 | public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer { |
| 283 | |
| 284 | public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock, |
| 285 | Context context, String wakeLockTag) { |
| 286 | super(installer, installLock, context, wakeLockTag); |
| 287 | } |
| 288 | |
| 289 | public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) { |
| 290 | super(from); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | protected boolean shouldSkipBasedOnISA(Package pkg, String instructionSet) { |
| 295 | // Forced compilation, never skip. |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 301 | // Ensure compilation, no matter the current state. |
| 302 | // TODO: The return value is wrong when patchoat is needed. |
| 303 | return DexFile.DEX2OAT_NEEDED; |
| 304 | } |
| 305 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 306 | } |