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; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 23 | import android.os.Environment; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 24 | import android.os.PowerManager; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 25 | import android.os.UserHandle; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 26 | import android.os.WorkSource; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.util.Slog; |
| 29 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 30 | import com.android.internal.os.InstallerConnection.InstallerException; |
Narayan Kamath | 88eea9e | 2016-05-02 14:44:31 +0100 | [diff] [blame] | 31 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 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; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 35 | import java.util.List; |
| 36 | |
| 37 | import dalvik.system.DexFile; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 38 | |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 39 | import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; |
| 40 | import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 41 | import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED; |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 42 | import static com.android.server.pm.Installer.DEXOPT_PUBLIC; |
| 43 | import static com.android.server.pm.Installer.DEXOPT_SAFEMODE; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 44 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 45 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
Andreas Gampe | 47c170a | 2016-03-30 17:21:19 -0700 | [diff] [blame] | 46 | import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Helper class for running dexopt command on packages. |
| 50 | */ |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 51 | 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; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 57 | static final int DEX_OPT_FAILED = -1; |
| 58 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 59 | private final Installer mInstaller; |
| 60 | private final Object mInstallLock; |
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 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 65 | PackageDexOptimizer(Installer installer, Object installLock, Context context, |
| 66 | String wakeLockTag) { |
| 67 | this.mInstaller = installer; |
| 68 | this.mInstallLock = installLock; |
| 69 | |
| 70 | PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 71 | mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); |
| 72 | } |
| 73 | |
| 74 | protected PackageDexOptimizer(PackageDexOptimizer from) { |
| 75 | this.mInstaller = from.mInstaller; |
| 76 | this.mInstallLock = from.mInstallLock; |
| 77 | this.mDexoptWakeLock = from.mDexoptWakeLock; |
| 78 | this.mSystemReady = from.mSystemReady; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 81 | static boolean canOptimizePackage(PackageParser.Package pkg) { |
Calin Juravle | 6dfd83d | 2016-01-29 18:23:57 +0000 | [diff] [blame] | 82 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0; |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 87 | * instruction sets. |
| 88 | * |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 89 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are |
| 90 | * synchronized on {@link #mInstallLock}. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 91 | */ |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 92 | int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries, |
| 93 | String[] instructionSets, boolean checkProfiles, String targetCompilationFilter) { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 94 | synchronized (mInstallLock) { |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 95 | final boolean useLock = mSystemReady; |
| 96 | if (useLock) { |
| 97 | mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid)); |
| 98 | mDexoptWakeLock.acquire(); |
| 99 | } |
| 100 | try { |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 101 | return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles, |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 102 | targetCompilationFilter); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 103 | } finally { |
| 104 | if (useLock) { |
| 105 | mDexoptWakeLock.release(); |
| 106 | } |
| 107 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 111 | /** |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 112 | * Adjust the given dexopt-needed value. Can be overridden to influence the decision to |
| 113 | * optimize or not (and in what way). |
| 114 | */ |
| 115 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 116 | return dexoptNeeded; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Adjust the given dexopt flags that will be passed to the installer. |
| 121 | */ |
| 122 | protected int adjustDexoptFlags(int dexoptFlags) { |
| 123 | return dexoptFlags; |
| 124 | } |
| 125 | |
Narayan Kamath | 88eea9e | 2016-05-02 14:44:31 +0100 | [diff] [blame] | 126 | /** |
| 127 | * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}. |
| 128 | */ |
| 129 | void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) { |
| 130 | final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo); |
| 131 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 132 | |
| 133 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 134 | |
| 135 | for (String instructionSet : dexCodeInstructionSets) { |
| 136 | pw.println("Instruction Set: " + instructionSet); |
| 137 | pw.increaseIndent(); |
| 138 | for (String path : paths) { |
| 139 | String status = null; |
| 140 | try { |
| 141 | status = DexFile.getDexFileStatus(path, instructionSet); |
| 142 | } catch (IOException ioe) { |
| 143 | status = "[Exception]: " + ioe.getMessage(); |
| 144 | } |
| 145 | pw.println("path: " + path); |
| 146 | pw.println("status: " + status); |
| 147 | } |
| 148 | pw.decreaseIndent(); |
| 149 | } |
| 150 | } |
| 151 | |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 152 | private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries, |
| 153 | String[] targetInstructionSets, boolean checkProfiles, String targetCompilerFilter) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 154 | final String[] instructionSets = targetInstructionSets != null ? |
| 155 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 156 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 157 | if (!canOptimizePackage(pkg)) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 158 | return DEX_OPT_SKIPPED; |
| 159 | } |
| 160 | |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 161 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 162 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 163 | |
| 164 | boolean isProfileGuidedFilter = DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter); |
| 165 | // If any part of the app is used by other apps, we cannot use profile-guided |
| 166 | // compilation. |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 167 | if (isProfileGuidedFilter && isUsedByOtherApps(pkg)) { |
| 168 | checkProfiles = false; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 169 | |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 170 | targetCompilerFilter = getNonProfileGuidedCompilerFilter(targetCompilerFilter); |
| 171 | if (DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter)) { |
| 172 | throw new IllegalStateException(targetCompilerFilter); |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 173 | } |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 174 | isProfileGuidedFilter = false; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Mathieu Chartier | 41e4a37 | 2016-09-02 16:36:42 -0700 | [diff] [blame^] | 177 | // Disable profile guided compilation for vmSafeMode. |
| 178 | final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) |
| 179 | != 0; |
| 180 | final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) |
| 181 | != 0; |
| 182 | if (vmSafeMode) { |
| 183 | targetCompilerFilter = getNonProfileGuidedCompilerFilter(targetCompilerFilter); |
| 184 | isProfileGuidedFilter = false; |
| 185 | } |
| 186 | |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 187 | // If we're asked to take profile updates into account, check now. |
| 188 | boolean newProfile = false; |
| 189 | if (checkProfiles && isProfileGuidedFilter) { |
| 190 | // Merge profiles, see if we need to do anything. |
| 191 | try { |
| 192 | newProfile = mInstaller.mergeProfiles(sharedGid, pkg.packageName); |
| 193 | } catch (InstallerException e) { |
| 194 | Slog.w(TAG, "Failed to merge profiles", e); |
| 195 | } |
| 196 | } |
| 197 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 198 | boolean performedDexOpt = false; |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 199 | boolean successfulDexOpt = true; |
| 200 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 201 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 202 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 203 | for (String path : paths) { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 204 | int dexoptNeeded; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 205 | try { |
Calin Juravle | 693f997 | 2016-02-25 15:26:20 +0000 | [diff] [blame] | 206 | dexoptNeeded = DexFile.getDexOptNeeded(path, |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 207 | dexCodeInstructionSet, targetCompilerFilter, newProfile); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 208 | } catch (IOException ioe) { |
| 209 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 210 | return DEX_OPT_FAILED; |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 211 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 212 | dexoptNeeded = adjustDexoptNeeded(dexoptNeeded); |
Andreas Gampe | 47c170a | 2016-03-30 17:21:19 -0700 | [diff] [blame] | 213 | if (PackageManagerService.DEBUG_DEXOPT) { |
| 214 | Log.i(TAG, "DexoptNeeded for " + path + "@" + targetCompilerFilter + " is " + |
| 215 | dexoptNeeded); |
| 216 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 217 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 218 | final String dexoptType; |
| 219 | String oatDir = null; |
Calin Juravle | 693f997 | 2016-02-25 15:26:20 +0000 | [diff] [blame] | 220 | switch (dexoptNeeded) { |
| 221 | case DexFile.NO_DEXOPT_NEEDED: |
| 222 | continue; |
| 223 | case DexFile.DEX2OAT_NEEDED: |
| 224 | dexoptType = "dex2oat"; |
| 225 | oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet); |
| 226 | break; |
| 227 | case DexFile.PATCHOAT_NEEDED: |
| 228 | dexoptType = "patchoat"; |
| 229 | break; |
| 230 | case DexFile.SELF_PATCHOAT_NEEDED: |
| 231 | dexoptType = "self patchoat"; |
| 232 | break; |
| 233 | default: |
| 234 | throw new IllegalStateException("Invalid dexopt:" + dexoptNeeded); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 235 | } |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 236 | |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 237 | String sharedLibrariesPath = null; |
| 238 | if (sharedLibraries != null && sharedLibraries.length != 0) { |
| 239 | StringBuilder sb = new StringBuilder(); |
| 240 | for (String lib : sharedLibraries) { |
| 241 | if (sb.length() != 0) { |
| 242 | sb.append(":"); |
| 243 | } |
| 244 | sb.append(lib); |
| 245 | } |
| 246 | sharedLibrariesPath = sb.toString(); |
| 247 | } |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 248 | Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg=" |
| 249 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
| 250 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 251 | + " target-filter=" + targetCompilerFilter + " oatDir = " + oatDir |
| 252 | + " sharedLibraries=" + sharedLibrariesPath); |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 253 | // Profile guide compiled oat files should not be public. |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 254 | final boolean isPublic = !pkg.isForwardLocked() && !isProfileGuidedFilter; |
| 255 | final int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 256 | final int dexFlags = adjustDexoptFlags( |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 257 | ( isPublic ? DEXOPT_PUBLIC : 0) |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 258 | | (vmSafeMode ? DEXOPT_SAFEMODE : 0) |
| 259 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 260 | | profileFlag |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 261 | | DEXOPT_BOOTCOMPLETE); |
| 262 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 263 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 264 | mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet, |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 265 | dexoptNeeded, oatDir, dexFlags, targetCompilerFilter, pkg.volumeUuid, |
| 266 | sharedLibrariesPath); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 267 | performedDexOpt = true; |
| 268 | } catch (InstallerException e) { |
| 269 | Slog.w(TAG, "Failed to dexopt", e); |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 270 | successfulDexOpt = false; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 273 | } |
| 274 | |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 275 | if (successfulDexOpt) { |
| 276 | // If we've gotten here, we're sure that no error occurred. We've either |
| 277 | // dex-opted one or more paths or instruction sets or we've skipped |
| 278 | // all of them because they are up to date. In both cases this package |
| 279 | // doesn't need dexopt any longer. |
| 280 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 281 | } else { |
| 282 | return DEX_OPT_FAILED; |
| 283 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 286 | /** |
| 287 | * Creates oat dir for the specified package. In certain cases oat directory |
| 288 | * <strong>cannot</strong> be created: |
| 289 | * <ul> |
| 290 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 291 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 292 | * </ul> |
| 293 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 294 | * @return Absolute path to the oat directory or null, if oat directory |
| 295 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 296 | */ |
| 297 | @Nullable |
Todd Kennedy | 27c24fb | 2015-09-17 16:49:25 -0700 | [diff] [blame] | 298 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 299 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 300 | return null; |
| 301 | } |
| 302 | File codePath = new File(pkg.codePath); |
| 303 | if (codePath.isDirectory()) { |
| 304 | File oatDir = getOatDir(codePath); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 305 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 306 | mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 307 | } catch (InstallerException e) { |
| 308 | Slog.w(TAG, "Failed to create oat dir", e); |
| 309 | return null; |
| 310 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 311 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 312 | } |
| 313 | return null; |
| 314 | } |
| 315 | |
| 316 | static File getOatDir(File codePath) { |
| 317 | return new File(codePath, OAT_DIR_NAME); |
| 318 | } |
| 319 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 320 | void systemReady() { |
| 321 | mSystemReady = true; |
| 322 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 323 | |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 324 | /** |
| 325 | * Returns true if the profiling data collected for the given app indicate |
| 326 | * that the apps's APK has been loaded by another app. |
| 327 | * Note that this returns false for all forward-locked apps and apps without |
| 328 | * any collected profiling data. |
| 329 | */ |
| 330 | public static boolean isUsedByOtherApps(PackageParser.Package pkg) { |
| 331 | if (pkg.isForwardLocked()) { |
| 332 | // Skip the check for forward locked packages since they don't share their code. |
| 333 | return false; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 334 | } |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 335 | |
| 336 | for (String apkPath : pkg.getAllCodePathsExcludingResourceOnly()) { |
| 337 | try { |
Narayan Kamath | 6d99f79 | 2016-05-16 17:34:48 +0100 | [diff] [blame] | 338 | apkPath = PackageManagerServiceUtils.realpath(new File(apkPath)); |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 339 | } catch (IOException e) { |
| 340 | // Log an error but continue without it. |
| 341 | Slog.w(TAG, "Failed to get canonical path", e); |
Narayan Kamath | 6d99f79 | 2016-05-16 17:34:48 +0100 | [diff] [blame] | 342 | continue; |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 343 | } |
| 344 | String useMarker = apkPath.replace('/', '@'); |
| 345 | final int[] currentUserIds = UserManagerService.getInstance().getUserIds(); |
| 346 | for (int i = 0; i < currentUserIds.length; i++) { |
| 347 | File profileDir = |
| 348 | Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]); |
| 349 | File foreignUseMark = new File(profileDir, useMarker); |
| 350 | if (foreignUseMark.exists()) { |
| 351 | return true; |
| 352 | } |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | return false; |
| 356 | } |
| 357 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 358 | /** |
| 359 | * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a |
| 360 | * dexopt path. |
| 361 | */ |
| 362 | public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer { |
| 363 | |
| 364 | public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock, |
| 365 | Context context, String wakeLockTag) { |
| 366 | super(installer, installLock, context, wakeLockTag); |
| 367 | } |
| 368 | |
| 369 | public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) { |
| 370 | super(from); |
| 371 | } |
| 372 | |
| 373 | @Override |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 374 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 375 | // Ensure compilation, no matter the current state. |
| 376 | // TODO: The return value is wrong when patchoat is needed. |
| 377 | return DexFile.DEX2OAT_NEEDED; |
| 378 | } |
| 379 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 380 | } |