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