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