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