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; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 23 | import android.os.PowerManager; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 24 | import android.os.UserHandle; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 25 | import android.os.WorkSource; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 26 | import android.util.ArraySet; |
| 27 | import android.util.Log; |
| 28 | import android.util.Slog; |
| 29 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 30 | import java.io.File; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 31 | import java.io.FileNotFoundException; |
| 32 | import java.io.IOException; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
| 36 | import dalvik.system.DexFile; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 37 | |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 38 | import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; |
| 39 | import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; |
| 40 | import static com.android.server.pm.Installer.DEXOPT_PUBLIC; |
| 41 | import static com.android.server.pm.Installer.DEXOPT_SAFEMODE; |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 42 | import static com.android.server.pm.Installer.DEXOPT_USEJIT; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 43 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 44 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
| 45 | |
| 46 | /** |
| 47 | * Helper class for running dexopt command on packages. |
| 48 | */ |
| 49 | final class PackageDexOptimizer { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 50 | private static final String TAG = "PackageManager.DexOptimizer"; |
| 51 | static final String OAT_DIR_NAME = "oat"; |
| 52 | // TODO b/19550105 Remove error codes and use exceptions |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 53 | static final int DEX_OPT_SKIPPED = 0; |
| 54 | static final int DEX_OPT_PERFORMED = 1; |
| 55 | static final int DEX_OPT_DEFERRED = 2; |
| 56 | static final int DEX_OPT_FAILED = -1; |
| 57 | |
| 58 | private final PackageManagerService mPackageManagerService; |
| 59 | private ArraySet<PackageParser.Package> mDeferredDexOpt; |
| 60 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 61 | private final PowerManager.WakeLock mDexoptWakeLock; |
| 62 | private volatile boolean mSystemReady; |
| 63 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 64 | PackageDexOptimizer(PackageManagerService packageManagerService) { |
| 65 | this.mPackageManagerService = packageManagerService; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 66 | PowerManager powerManager = (PowerManager)packageManagerService.mContext.getSystemService( |
| 67 | Context.POWER_SERVICE); |
| 68 | mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*dexopt*"); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 73 | * instruction sets. |
| 74 | * |
| 75 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} are synchronized on |
| 76 | * {@link PackageManagerService#mInstallLock}. |
| 77 | */ |
| 78 | int performDexOpt(PackageParser.Package pkg, String[] instructionSets, |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 79 | boolean forceDex, boolean defer, boolean inclDependencies, |
| 80 | boolean bootComplete, boolean useJit) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 81 | ArraySet<String> done; |
| 82 | if (inclDependencies && (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null)) { |
| 83 | done = new ArraySet<String>(); |
| 84 | done.add(pkg.packageName); |
| 85 | } else { |
| 86 | done = null; |
| 87 | } |
| 88 | synchronized (mPackageManagerService.mInstallLock) { |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 89 | final boolean useLock = mSystemReady; |
| 90 | if (useLock) { |
| 91 | mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid)); |
| 92 | mDexoptWakeLock.acquire(); |
| 93 | } |
| 94 | try { |
Todd Kennedy | 92b28f3 | 2015-09-28 11:20:38 -0700 | [diff] [blame] | 95 | return performDexOptLI(pkg, instructionSets, forceDex, defer, bootComplete, |
| 96 | useJit, done); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 97 | } finally { |
| 98 | if (useLock) { |
| 99 | mDexoptWakeLock.release(); |
| 100 | } |
| 101 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets, |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 106 | boolean forceDex, boolean defer, boolean bootComplete, boolean useJit, |
| 107 | ArraySet<String> done) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 108 | final String[] instructionSets = targetInstructionSets != null ? |
| 109 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 110 | |
| 111 | if (done != null) { |
| 112 | done.add(pkg.packageName); |
| 113 | if (pkg.usesLibraries != null) { |
Andreas Gampe | 06bb908 | 2015-09-21 13:20:07 -0700 | [diff] [blame] | 114 | performDexOptLibsLI(pkg.usesLibraries, instructionSets, forceDex, defer, |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 115 | bootComplete, useJit, done); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 116 | } |
| 117 | if (pkg.usesOptionalLibraries != null) { |
| 118 | performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, forceDex, defer, |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 119 | bootComplete, useJit, done); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) { |
| 124 | return DEX_OPT_SKIPPED; |
| 125 | } |
| 126 | |
| 127 | final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
| 128 | final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 129 | |
| 130 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 131 | boolean performedDexOpt = false; |
| 132 | // There are three basic cases here: |
| 133 | // 1.) we need to dexopt, either because we are forced or it is needed |
| 134 | // 2.) we are deferring a needed dexopt |
| 135 | // 3.) we are skipping an unneeded dexopt |
| 136 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 137 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
| 138 | if (!forceDex && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | for (String path : paths) { |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 143 | final int dexoptNeeded; |
| 144 | if (forceDex) { |
| 145 | dexoptNeeded = DexFile.DEX2OAT_NEEDED; |
| 146 | } else { |
| 147 | try { |
| 148 | dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName, |
| 149 | dexCodeInstructionSet, defer); |
| 150 | } catch (IOException ioe) { |
| 151 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 152 | return DEX_OPT_FAILED; |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 153 | } |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 154 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 155 | |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 156 | if (!forceDex && defer && dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) { |
| 157 | // We're deciding to defer a needed dexopt. Don't bother dexopting for other |
| 158 | // paths and instruction sets. We'll deal with them all together when we process |
| 159 | // our list of deferred dexopts. |
| 160 | addPackageForDeferredDexopt(pkg); |
| 161 | return DEX_OPT_DEFERRED; |
| 162 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 163 | |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 164 | if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) { |
| 165 | final String dexoptType; |
| 166 | String oatDir = null; |
| 167 | if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) { |
| 168 | dexoptType = "dex2oat"; |
| 169 | try { |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 170 | oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet); |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 171 | } catch (IOException ioe) { |
| 172 | Slog.w(TAG, "Unable to create oatDir for package: " + pkg.packageName); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 173 | return DEX_OPT_FAILED; |
| 174 | } |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 175 | } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) { |
| 176 | dexoptType = "patchoat"; |
| 177 | } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) { |
| 178 | dexoptType = "self patchoat"; |
| 179 | } else { |
| 180 | throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded); |
| 181 | } |
| 182 | |
| 183 | Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg=" |
| 184 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
| 185 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable |
Todd Kennedy | 92b28f3 | 2015-09-28 11:20:38 -0700 | [diff] [blame] | 186 | + " oatDir = " + oatDir + " bootComplete=" + bootComplete |
| 187 | + " useJit=" + useJit); |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 188 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
Todd Kennedy | 9f86026 | 2015-09-25 15:06:49 -0700 | [diff] [blame] | 189 | final int dexFlags = |
| 190 | (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0) |
| 191 | | (vmSafeMode ? DEXOPT_SAFEMODE : 0) |
| 192 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
Todd Kennedy | 92b28f3 | 2015-09-28 11:20:38 -0700 | [diff] [blame] | 193 | | (bootComplete ? DEXOPT_BOOTCOMPLETE : 0) |
| 194 | | (useJit ? DEXOPT_USEJIT : 0); |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 195 | final int ret = mPackageManagerService.mInstaller.dexopt(path, sharedGid, |
Todd Kennedy | 9f86026 | 2015-09-25 15:06:49 -0700 | [diff] [blame] | 196 | pkg.packageName, dexCodeInstructionSet, dexoptNeeded, oatDir, dexFlags); |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 197 | |
| 198 | // Dex2oat might fail due to compiler / verifier errors. We soldier on |
| 199 | // regardless, and attempt to interpret the app as a safety net. |
| 200 | if (ret == 0) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 201 | performedDexOpt = true; |
| 202 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | // At this point we haven't failed dexopt and we haven't deferred dexopt. We must |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 207 | // either have either succeeded dexopt, or have had getDexOptNeeded tell us |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 208 | // it isn't required. We therefore mark that this package doesn't need dexopt unless |
| 209 | // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped |
| 210 | // it. |
| 211 | pkg.mDexOptPerformed.add(dexCodeInstructionSet); |
| 212 | } |
| 213 | |
| 214 | // If we've gotten here, we're sure that no error occurred and that we haven't |
| 215 | // deferred dex-opt. We've either dex-opted one more paths or instruction sets or |
| 216 | // we've skipped all of them because they are up to date. In both cases this |
| 217 | // package doesn't need dexopt any longer. |
| 218 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 219 | } |
| 220 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Creates oat dir for the specified package. In certain cases oat directory |
| 223 | * <strong>cannot</strong> be created: |
| 224 | * <ul> |
| 225 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 226 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 227 | * </ul> |
| 228 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 229 | * @return Absolute path to the oat directory or null, if oat directory |
| 230 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 231 | */ |
| 232 | @Nullable |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 233 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 234 | throws IOException { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 235 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 236 | return null; |
| 237 | } |
| 238 | File codePath = new File(pkg.codePath); |
| 239 | if (codePath.isDirectory()) { |
| 240 | File oatDir = getOatDir(codePath); |
| 241 | mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(), |
| 242 | dexInstructionSet); |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 243 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 244 | } |
| 245 | return null; |
| 246 | } |
| 247 | |
| 248 | static File getOatDir(File codePath) { |
| 249 | return new File(codePath, OAT_DIR_NAME); |
| 250 | } |
| 251 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 252 | private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets, |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 253 | boolean forceDex, boolean defer, boolean bootComplete, boolean useJit, |
| 254 | ArraySet<String> done) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 255 | for (String libName : libs) { |
| 256 | PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary( |
| 257 | libName); |
| 258 | if (libPkg != null && !done.contains(libName)) { |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 259 | performDexOptLI(libPkg, instructionSets, forceDex, defer, bootComplete, useJit, done); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Clears set of deferred dexopt packages. |
| 266 | * @return content of dexopt set if it was not empty |
| 267 | */ |
| 268 | public ArraySet<PackageParser.Package> clearDeferredDexOptPackages() { |
| 269 | ArraySet<PackageParser.Package> result = mDeferredDexOpt; |
| 270 | mDeferredDexOpt = null; |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | public void addPackageForDeferredDexopt(PackageParser.Package pkg) { |
| 275 | if (mDeferredDexOpt == null) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 276 | mDeferredDexOpt = new ArraySet<>(); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 277 | } |
| 278 | mDeferredDexOpt.add(pkg); |
| 279 | } |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 280 | |
| 281 | void systemReady() { |
| 282 | mSystemReady = true; |
| 283 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 284 | } |