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, |
Calin Juravle | 9dff854 | 2016-01-21 11:41:42 -0800 | [diff] [blame] | 84 | boolean inclDependencies, boolean useProfiles, boolean extractOnly) { |
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 | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 100 | extractOnly); |
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, |
Calin Juravle | 9dff854 | 2016-01-21 11:41:42 -0800 | [diff] [blame] | 110 | ArraySet<String> done, boolean useProfiles, boolean extractOnly) { |
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 | |
| 131 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 132 | boolean performedDexOpt = false; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 133 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 134 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 135 | if (!useProfiles && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) { |
| 136 | // Skip only if we do not use profiles since they might trigger a recompilation. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 137 | continue; |
| 138 | } |
| 139 | |
| 140 | for (String path : paths) { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 141 | int dexoptNeeded; |
Nicolas Geoffray | 27c0737 | 2015-11-05 16:54:09 +0000 | [diff] [blame] | 142 | try { |
| 143 | dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName, |
| 144 | dexCodeInstructionSet, /* defer */false); |
| 145 | } catch (IOException ioe) { |
| 146 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 147 | return DEX_OPT_FAILED; |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 148 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 149 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 150 | if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) { |
| 151 | if (useProfiles) { |
| 152 | // If we do a profile guided compilation then we might recompile the same |
| 153 | // package if more profile information is available. |
| 154 | dexoptNeeded = DexFile.DEX2OAT_NEEDED; |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 155 | } else { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 156 | // No dexopt needed and we don't use profiles. Nothing to do. |
| 157 | continue; |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 158 | } |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 159 | } |
| 160 | final String dexoptType; |
| 161 | String oatDir = null; |
| 162 | if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) { |
| 163 | dexoptType = "dex2oat"; |
| 164 | oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet); |
| 165 | } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) { |
| 166 | dexoptType = "patchoat"; |
| 167 | } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) { |
| 168 | dexoptType = "self patchoat"; |
| 169 | } else { |
| 170 | throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded); |
| 171 | } |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 172 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 173 | Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg=" |
| 174 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
| 175 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 176 | + " extractOnly=" + extractOnly + " oatDir = " + oatDir); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 177 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 178 | final int dexFlags = |
| 179 | (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0) |
| 180 | | (vmSafeMode ? DEXOPT_SAFEMODE : 0) |
| 181 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 182 | | (extractOnly ? DEXOPT_EXTRACTONLY : 0) |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 183 | | DEXOPT_BOOTCOMPLETE; |
| 184 | try { |
| 185 | mPackageManagerService.mInstaller.dexopt(path, sharedGid, |
| 186 | pkg.packageName, dexCodeInstructionSet, dexoptNeeded, oatDir, |
Calin Juravle | 9dff854 | 2016-01-21 11:41:42 -0800 | [diff] [blame] | 187 | dexFlags, pkg.volumeUuid, useProfiles); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 188 | performedDexOpt = true; |
| 189 | } catch (InstallerException e) { |
| 190 | Slog.w(TAG, "Failed to dexopt", e); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 194 | if (!extractOnly) { |
| 195 | // At this point we haven't failed dexopt and we haven't deferred dexopt. We must |
| 196 | // either have either succeeded dexopt, or have had getDexOptNeeded tell us |
| 197 | // it isn't required. We therefore mark that this package doesn't need dexopt unless |
| 198 | // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped |
| 199 | // it. |
| 200 | pkg.mDexOptPerformed.add(dexCodeInstructionSet); |
| 201 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // If we've gotten here, we're sure that no error occurred and that we haven't |
| 205 | // deferred dex-opt. We've either dex-opted one more paths or instruction sets or |
| 206 | // we've skipped all of them because they are up to date. In both cases this |
| 207 | // package doesn't need dexopt any longer. |
| 208 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 209 | } |
| 210 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 211 | /** |
| 212 | * Creates oat dir for the specified package. In certain cases oat directory |
| 213 | * <strong>cannot</strong> be created: |
| 214 | * <ul> |
| 215 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 216 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 217 | * </ul> |
| 218 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 219 | * @return Absolute path to the oat directory or null, if oat directory |
| 220 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 221 | */ |
| 222 | @Nullable |
Todd Kennedy | 27c24fb | 2015-09-17 16:49:25 -0700 | [diff] [blame] | 223 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 224 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 225 | return null; |
| 226 | } |
| 227 | File codePath = new File(pkg.codePath); |
| 228 | if (codePath.isDirectory()) { |
| 229 | File oatDir = getOatDir(codePath); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 230 | try { |
| 231 | mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(), |
| 232 | dexInstructionSet); |
| 233 | } catch (InstallerException e) { |
| 234 | Slog.w(TAG, "Failed to create oat dir", e); |
| 235 | return null; |
| 236 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 237 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 238 | } |
| 239 | return null; |
| 240 | } |
| 241 | |
| 242 | static File getOatDir(File codePath) { |
| 243 | return new File(codePath, OAT_DIR_NAME); |
| 244 | } |
| 245 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 246 | private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets, |
Todd Kennedy | 8c6e537 | 2015-09-25 14:47:15 -0700 | [diff] [blame] | 247 | ArraySet<String> done) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 248 | for (String libName : libs) { |
| 249 | PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary( |
| 250 | libName); |
| 251 | if (libPkg != null && !done.contains(libName)) { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 252 | // TODO: Analyze and investigate if we (should) profile libraries. |
| 253 | // Currently this will do a full compilation of the library. |
Calin Juravle | 9dff854 | 2016-01-21 11:41:42 -0800 | [diff] [blame] | 254 | performDexOptLI(libPkg, instructionSets, done, /*useProfiles*/ false, |
David Brazdil | a0e1043 | 2016-01-20 14:04:40 +0000 | [diff] [blame] | 255 | /* extractOnly */ false); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 260 | void systemReady() { |
| 261 | mSystemReady = true; |
| 262 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 263 | } |