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