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