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