PackageDexOptimizer.java revision 52c8620c1945de45f22c1e62dcb69ddadca07675
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.PackageInfo; 23import android.content.pm.PackageParser; 24import android.os.Environment; 25import android.os.FileUtils; 26import android.os.PowerManager; 27import android.os.SystemClock; 28import android.os.UserHandle; 29import android.os.WorkSource; 30import android.util.Log; 31import android.util.Slog; 32import android.util.SparseArray; 33 34import com.android.internal.annotations.GuardedBy; 35import com.android.internal.util.IndentingPrintWriter; 36import com.android.server.pm.Installer.InstallerException; 37 38import java.io.File; 39import java.io.IOException; 40import java.util.ArrayList; 41import java.util.List; 42import java.util.Set; 43 44import dalvik.system.DexFile; 45 46import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; 47import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; 48import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED; 49import static com.android.server.pm.Installer.DEXOPT_PUBLIC; 50import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX; 51import static com.android.server.pm.Installer.DEXOPT_FORCE; 52import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE; 53import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE; 54import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; 55import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; 56 57import static com.android.server.pm.PackageManagerService.WATCHDOG_TIMEOUT; 58 59import static dalvik.system.DexFile.getNonProfileGuidedCompilerFilter; 60import static dalvik.system.DexFile.getSafeModeCompilerFilter; 61import static dalvik.system.DexFile.isProfileGuidedCompilerFilter; 62 63/** 64 * Helper class for running dexopt command on packages. 65 */ 66public class PackageDexOptimizer { 67 private static final String TAG = "PackageManager.DexOptimizer"; 68 static final String OAT_DIR_NAME = "oat"; 69 // TODO b/19550105 Remove error codes and use exceptions 70 public static final int DEX_OPT_SKIPPED = 0; 71 public static final int DEX_OPT_PERFORMED = 1; 72 public static final int DEX_OPT_FAILED = -1; 73 // One minute over PM WATCHDOG_TIMEOUT 74 private static final long WAKELOCK_TIMEOUT_MS = WATCHDOG_TIMEOUT + 1000 * 60; 75 76 /** Special library name that skips shared libraries check during compilation. */ 77 public static final String SKIP_SHARED_LIBRARY_CHECK = "&"; 78 79 @GuardedBy("mInstallLock") 80 private final Installer mInstaller; 81 private final Object mInstallLock; 82 83 @GuardedBy("mInstallLock") 84 private final PowerManager.WakeLock mDexoptWakeLock; 85 private volatile boolean mSystemReady; 86 87 PackageDexOptimizer(Installer installer, Object installLock, Context context, 88 String wakeLockTag) { 89 this.mInstaller = installer; 90 this.mInstallLock = installLock; 91 92 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 93 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); 94 } 95 96 protected PackageDexOptimizer(PackageDexOptimizer from) { 97 this.mInstaller = from.mInstaller; 98 this.mInstallLock = from.mInstallLock; 99 this.mDexoptWakeLock = from.mDexoptWakeLock; 100 this.mSystemReady = from.mSystemReady; 101 } 102 103 static boolean canOptimizePackage(PackageParser.Package pkg) { 104 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0; 105 } 106 107 /** 108 * Performs dexopt on all code paths and libraries of the specified package for specified 109 * instruction sets. 110 * 111 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are 112 * synchronized on {@link #mInstallLock}. 113 */ 114 int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries, 115 String[] instructionSets, boolean checkProfiles, String targetCompilationFilter, 116 CompilerStats.PackageStats packageStats, boolean isUsedByOtherApps, 117 boolean bootComplete) { 118 if (!canOptimizePackage(pkg)) { 119 return DEX_OPT_SKIPPED; 120 } 121 synchronized (mInstallLock) { 122 final long acquireTime = acquireWakeLockLI(pkg.applicationInfo.uid); 123 try { 124 return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles, 125 targetCompilationFilter, packageStats, isUsedByOtherApps, bootComplete); 126 } finally { 127 releaseWakeLockLI(acquireTime); 128 } 129 } 130 } 131 132 /** 133 * Performs dexopt on all code paths of the given package. 134 * It assumes the install lock is held. 135 */ 136 @GuardedBy("mInstallLock") 137 private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries, 138 String[] targetInstructionSets, boolean checkForProfileUpdates, 139 String targetCompilerFilter, CompilerStats.PackageStats packageStats, 140 boolean isUsedByOtherApps, boolean bootComplete) { 141 final String[] instructionSets = targetInstructionSets != null ? 142 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); 143 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); 144 final List<String> paths = pkg.getAllCodePaths(); 145 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); 146 147 final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo, 148 targetCompilerFilter, isUsedByOtherApps); 149 final boolean profileUpdated = checkForProfileUpdates && 150 isProfileUpdated(pkg, sharedGid, compilerFilter); 151 152 final String sharedLibrariesPath = getSharedLibrariesPath(sharedLibraries); 153 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. 154 final int dexoptFlags = getDexFlags(pkg, compilerFilter, bootComplete); 155 // Get the dependencies of each split in the package. For each code path in the package, 156 // this array contains the relative paths of each split it depends on, separated by colons. 157 String[] splitDependencies = getSplitDependencies(pkg); 158 159 int result = DEX_OPT_SKIPPED; 160 for (int i = 0; i < paths.size(); i++) { 161 // Skip paths that have no code. 162 if ((i == 0 && (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) || 163 (i != 0 && (pkg.splitFlags[i - 1] & ApplicationInfo.FLAG_HAS_CODE) == 0)) { 164 continue; 165 } 166 // Append shared libraries with split dependencies for this split. 167 String path = paths.get(i); 168 String sharedLibrariesPathWithSplits; 169 if (sharedLibrariesPath != null && splitDependencies[i] != null) { 170 sharedLibrariesPathWithSplits = sharedLibrariesPath + ":" + splitDependencies[i]; 171 } else { 172 sharedLibrariesPathWithSplits = 173 splitDependencies[i] != null ? splitDependencies[i] : sharedLibrariesPath; 174 } 175 for (String dexCodeIsa : dexCodeInstructionSets) { 176 int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated, 177 sharedLibrariesPathWithSplits, dexoptFlags, sharedGid, packageStats); 178 // The end result is: 179 // - FAILED if any path failed, 180 // - PERFORMED if at least one path needed compilation, 181 // - SKIPPED when all paths are up to date 182 if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) { 183 result = newResult; 184 } 185 } 186 } 187 return result; 188 } 189 190 /** 191 * Performs dexopt on the {@code path} belonging to the package {@code pkg}. 192 * 193 * @return 194 * DEX_OPT_FAILED if there was any exception during dexopt 195 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path. 196 * DEX_OPT_SKIPPED if the path does not need to be deopt-ed. 197 */ 198 @GuardedBy("mInstallLock") 199 private int dexOptPath(PackageParser.Package pkg, String path, String isa, 200 String compilerFilter, boolean profileUpdated, String sharedLibrariesPath, 201 int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) { 202 int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated); 203 if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) { 204 return DEX_OPT_SKIPPED; 205 } 206 207 // TODO(calin): there's no need to try to create the oat dir over and over again, 208 // especially since it involve an extra installd call. We should create 209 // if (if supported) on the fly during the dexopt call. 210 String oatDir = createOatDirIfSupported(pkg, isa); 211 212 Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path 213 + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa 214 + " dexoptFlags=" + printDexoptFlags(dexoptFlags) 215 + " target-filter=" + compilerFilter + " oatDir=" + oatDir 216 + " sharedLibraries=" + sharedLibrariesPath); 217 218 try { 219 long startTime = System.currentTimeMillis(); 220 221 mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags, 222 compilerFilter, pkg.volumeUuid, sharedLibrariesPath, pkg.applicationInfo.seInfo); 223 224 if (packageStats != null) { 225 long endTime = System.currentTimeMillis(); 226 packageStats.setCompileTime(path, (int)(endTime - startTime)); 227 } 228 return DEX_OPT_PERFORMED; 229 } catch (InstallerException e) { 230 Slog.w(TAG, "Failed to dexopt", e); 231 return DEX_OPT_FAILED; 232 } 233 } 234 235 /** 236 * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}. 237 * 238 * @return 239 * DEX_OPT_FAILED if there was any exception during dexopt 240 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path. 241 * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file 242 * didn't need an update. That's because at the moment we don't get more than success/failure 243 * from installd. 244 * 245 * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than 246 * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though 247 * that seems wasteful. 248 */ 249 public int dexOptSecondaryDexPath(ApplicationInfo info, String path, Set<String> isas, 250 String compilerFilter, boolean isUsedByOtherApps) { 251 synchronized (mInstallLock) { 252 final long acquireTime = acquireWakeLockLI(info.uid); 253 try { 254 return dexOptSecondaryDexPathLI(info, path, isas, compilerFilter, 255 isUsedByOtherApps); 256 } finally { 257 releaseWakeLockLI(acquireTime); 258 } 259 } 260 } 261 262 @GuardedBy("mInstallLock") 263 private long acquireWakeLockLI(final int uid) { 264 // During boot the system doesn't need to instantiate and obtain a wake lock. 265 // PowerManager might not be ready, but that doesn't mean that we can't proceed with 266 // dexopt. 267 if (!mSystemReady) { 268 return -1; 269 } 270 mDexoptWakeLock.setWorkSource(new WorkSource(uid)); 271 mDexoptWakeLock.acquire(WAKELOCK_TIMEOUT_MS); 272 return SystemClock.elapsedRealtime(); 273 } 274 275 @GuardedBy("mInstallLock") 276 private void releaseWakeLockLI(final long acquireTime) { 277 if (acquireTime < 0) { 278 return; 279 } 280 try { 281 if (mDexoptWakeLock.isHeld()) { 282 mDexoptWakeLock.release(); 283 } 284 final long duration = SystemClock.elapsedRealtime() - acquireTime; 285 if (duration >= WAKELOCK_TIMEOUT_MS) { 286 Slog.wtf(TAG, "WakeLock " + mDexoptWakeLock.getTag() 287 + " time out. Operation took " + duration + " ms. Thread: " 288 + Thread.currentThread().getName()); 289 } 290 } catch (Exception e) { 291 Slog.wtf(TAG, "Error while releasing " + mDexoptWakeLock.getTag() + " lock", e); 292 } 293 } 294 295 @GuardedBy("mInstallLock") 296 private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, Set<String> isas, 297 String compilerFilter, boolean isUsedByOtherApps) { 298 compilerFilter = getRealCompilerFilter(info, compilerFilter, isUsedByOtherApps); 299 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. 300 // Secondary dex files are currently not compiled at boot. 301 int dexoptFlags = getDexFlags(info, compilerFilter, /* bootComplete */ true) 302 | DEXOPT_SECONDARY_DEX; 303 // Check the app storage and add the appropriate flags. 304 if (info.deviceProtectedDataDir != null && 305 FileUtils.contains(info.deviceProtectedDataDir, path)) { 306 dexoptFlags |= DEXOPT_STORAGE_DE; 307 } else if (info.credentialProtectedDataDir != null && 308 FileUtils.contains(info.credentialProtectedDataDir, path)) { 309 dexoptFlags |= DEXOPT_STORAGE_CE; 310 } else { 311 Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName); 312 return DEX_OPT_FAILED; 313 } 314 Log.d(TAG, "Running dexopt on: " + path 315 + " pkg=" + info.packageName + " isa=" + isas 316 + " dexoptFlags=" + printDexoptFlags(dexoptFlags) 317 + " target-filter=" + compilerFilter); 318 319 try { 320 for (String isa : isas) { 321 // Reuse the same dexopt path as for the primary apks. We don't need all the 322 // arguments as some (dexopNeeded and oatDir) will be computed by installd because 323 // system server cannot read untrusted app content. 324 // TODO(calin): maybe add a separate call. 325 mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0, 326 /*oatDir*/ null, dexoptFlags, 327 compilerFilter, info.volumeUuid, SKIP_SHARED_LIBRARY_CHECK, info.seInfoUser); 328 } 329 330 return DEX_OPT_PERFORMED; 331 } catch (InstallerException e) { 332 Slog.w(TAG, "Failed to dexopt", e); 333 return DEX_OPT_FAILED; 334 } 335 } 336 337 /** 338 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to 339 * optimize or not (and in what way). 340 */ 341 protected int adjustDexoptNeeded(int dexoptNeeded) { 342 return dexoptNeeded; 343 } 344 345 /** 346 * Adjust the given dexopt flags that will be passed to the installer. 347 */ 348 protected int adjustDexoptFlags(int dexoptFlags) { 349 return dexoptFlags; 350 } 351 352 /** 353 * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}. 354 */ 355 void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) { 356 final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo); 357 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); 358 359 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); 360 361 for (String instructionSet : dexCodeInstructionSets) { 362 pw.println("Instruction Set: " + instructionSet); 363 pw.increaseIndent(); 364 for (String path : paths) { 365 String status = null; 366 try { 367 status = DexFile.getDexFileStatus(path, instructionSet); 368 } catch (IOException ioe) { 369 status = "[Exception]: " + ioe.getMessage(); 370 } 371 pw.println("path: " + path); 372 pw.println("status: " + status); 373 } 374 pw.decreaseIndent(); 375 } 376 } 377 378 /** 379 * Returns the compiler filter that should be used to optimize the package code. 380 * The target filter will be updated if the package code is used by other apps 381 * or if it has the safe mode flag set. 382 */ 383 private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter, 384 boolean isUsedByOtherApps) { 385 int flags = info.flags; 386 boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; 387 if (vmSafeMode) { 388 return getSafeModeCompilerFilter(targetCompilerFilter); 389 } 390 391 if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) { 392 // If the dex files is used by other apps, we cannot use profile-guided compilation. 393 return getNonProfileGuidedCompilerFilter(targetCompilerFilter); 394 } 395 396 return targetCompilerFilter; 397 } 398 399 /** 400 * Computes the dex flags that needs to be pass to installd for the given package and compiler 401 * filter. 402 */ 403 private int getDexFlags(PackageParser.Package pkg, String compilerFilter, 404 boolean bootComplete) { 405 return getDexFlags(pkg.applicationInfo, compilerFilter, bootComplete); 406 } 407 408 private int getDexFlags(ApplicationInfo info, String compilerFilter, boolean bootComplete) { 409 int flags = info.flags; 410 boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; 411 // Profile guide compiled oat files should not be public. 412 boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter); 413 boolean isPublic = !info.isForwardLocked() && !isProfileGuidedFilter; 414 int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0; 415 int dexFlags = 416 (isPublic ? DEXOPT_PUBLIC : 0) 417 | (debuggable ? DEXOPT_DEBUGGABLE : 0) 418 | profileFlag 419 | (bootComplete ? DEXOPT_BOOTCOMPLETE : 0); 420 return adjustDexoptFlags(dexFlags); 421 } 422 423 /** 424 * Assesses if there's a need to perform dexopt on {@code path} for the given 425 * configuration (isa, compiler filter, profile). 426 */ 427 private int getDexoptNeeded(String path, String isa, String compilerFilter, 428 boolean newProfile) { 429 int dexoptNeeded; 430 try { 431 dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile, 432 false /* downgrade */); 433 } catch (IOException ioe) { 434 Slog.w(TAG, "IOException reading apk: " + path, ioe); 435 return DEX_OPT_FAILED; 436 } 437 return adjustDexoptNeeded(dexoptNeeded); 438 } 439 440 /** 441 * Computes the shared libraries path that should be passed to dexopt. 442 */ 443 private String getSharedLibrariesPath(String[] sharedLibraries) { 444 if (sharedLibraries == null || sharedLibraries.length == 0) { 445 return null; 446 } 447 StringBuilder sb = new StringBuilder(); 448 for (String lib : sharedLibraries) { 449 if (sb.length() != 0) { 450 sb.append(":"); 451 } 452 sb.append(lib); 453 } 454 return sb.toString(); 455 } 456 457 /** 458 * Walks dependency tree and gathers the dependencies for each split in a split apk. 459 * The split paths are stored as relative paths, separated by colons. 460 */ 461 private String[] getSplitDependencies(PackageParser.Package pkg) { 462 // Convert all the code paths to relative paths. 463 String baseCodePath = new File(pkg.baseCodePath).getParent(); 464 List<String> paths = pkg.getAllCodePaths(); 465 String[] splitDependencies = new String[paths.size()]; 466 for (int i = 0; i < paths.size(); i++) { 467 File pathFile = new File(paths.get(i)); 468 String fileName = pathFile.getName(); 469 paths.set(i, fileName); 470 471 // Sanity check that the base paths of the splits are all the same. 472 String basePath = pathFile.getParent(); 473 if (!basePath.equals(baseCodePath)) { 474 Slog.wtf(TAG, "Split paths have different base paths: " + basePath + " and " + 475 baseCodePath); 476 } 477 } 478 479 // If there are no other dependencies, fill in the implicit dependency on the base apk. 480 SparseArray<int[]> dependencies = pkg.applicationInfo.splitDependencies; 481 if (dependencies == null) { 482 for (int i = 1; i < paths.size(); i++) { 483 splitDependencies[i] = paths.get(0); 484 } 485 return splitDependencies; 486 } 487 488 // Fill in the dependencies, skipping the base apk which has no dependencies. 489 for (int i = 1; i < dependencies.size(); i++) { 490 getParentDependencies(dependencies.keyAt(i), paths, dependencies, splitDependencies); 491 } 492 493 return splitDependencies; 494 } 495 496 /** 497 * Recursive method to generate dependencies for a particular split. 498 * The index is a key from the package's splitDependencies. 499 */ 500 private String getParentDependencies(int index, List<String> paths, 501 SparseArray<int[]> dependencies, String[] splitDependencies) { 502 // The base apk is always first, and has no dependencies. 503 if (index == 0) { 504 return null; 505 } 506 // Return the result if we've computed the dependencies for this index already. 507 if (splitDependencies[index] != null) { 508 return splitDependencies[index]; 509 } 510 // Get the dependencies for the parent of this index and append its path to it. 511 int parent = dependencies.get(index)[0]; 512 String parentDependencies = 513 getParentDependencies(parent, paths, dependencies, splitDependencies); 514 String path = parentDependencies == null ? paths.get(parent) : 515 parentDependencies + ":" + paths.get(parent); 516 splitDependencies[index] = path; 517 return path; 518 } 519 520 /** 521 * Checks if there is an update on the profile information of the {@code pkg}. 522 * If the compiler filter is not profile guided the method returns false. 523 * 524 * Note that this is a "destructive" operation with side effects. Under the hood the 525 * current profile and the reference profile will be merged and subsequent calls 526 * may return a different result. 527 */ 528 private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String compilerFilter) { 529 // Check if we are allowed to merge and if the compiler filter is profile guided. 530 if (!isProfileGuidedCompilerFilter(compilerFilter)) { 531 return false; 532 } 533 // Merge profiles. It returns whether or not there was an updated in the profile info. 534 try { 535 return mInstaller.mergeProfiles(uid, pkg.packageName); 536 } catch (InstallerException e) { 537 Slog.w(TAG, "Failed to merge profiles", e); 538 } 539 return false; 540 } 541 542 /** 543 * Creates oat dir for the specified package if needed and supported. 544 * In certain cases oat directory 545 * <strong>cannot</strong> be created: 546 * <ul> 547 * <li>{@code pkg} is a system app, which is not updated.</li> 548 * <li>Package location is not a directory, i.e. monolithic install.</li> 549 * </ul> 550 * 551 * @return Absolute path to the oat directory or null, if oat directory 552 * cannot be created. 553 */ 554 @Nullable 555 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { 556 if (!pkg.canHaveOatDir()) { 557 return null; 558 } 559 File codePath = new File(pkg.codePath); 560 if (codePath.isDirectory()) { 561 // TODO(calin): why do we create this only if the codePath is a directory? (i.e for 562 // cluster packages). It seems that the logic for the folder creation is 563 // split between installd and here. 564 File oatDir = getOatDir(codePath); 565 try { 566 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet); 567 } catch (InstallerException e) { 568 Slog.w(TAG, "Failed to create oat dir", e); 569 return null; 570 } 571 return oatDir.getAbsolutePath(); 572 } 573 return null; 574 } 575 576 static File getOatDir(File codePath) { 577 return new File(codePath, OAT_DIR_NAME); 578 } 579 580 void systemReady() { 581 mSystemReady = true; 582 } 583 584 private String printDexoptFlags(int flags) { 585 ArrayList<String> flagsList = new ArrayList<>(); 586 587 if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) { 588 flagsList.add("boot_complete"); 589 } 590 if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) { 591 flagsList.add("debuggable"); 592 } 593 if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) { 594 flagsList.add("profile_guided"); 595 } 596 if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) { 597 flagsList.add("public"); 598 } 599 if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) { 600 flagsList.add("secondary"); 601 } 602 if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) { 603 flagsList.add("force"); 604 } 605 if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) { 606 flagsList.add("storage_ce"); 607 } 608 if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) { 609 flagsList.add("storage_de"); 610 } 611 612 return String.join(",", flagsList); 613 } 614 615 /** 616 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a 617 * dexopt path. 618 */ 619 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer { 620 621 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock, 622 Context context, String wakeLockTag) { 623 super(installer, installLock, context, wakeLockTag); 624 } 625 626 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) { 627 super(from); 628 } 629 630 @Override 631 protected int adjustDexoptNeeded(int dexoptNeeded) { 632 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) { 633 // Ensure compilation by pretending a compiler filter change on the 634 // apk/odex location (the reason for the '-'. A positive value means 635 // the 'oat' location). 636 return -DexFile.DEX2OAT_FOR_FILTER; 637 } 638 return dexoptNeeded; 639 } 640 641 @Override 642 protected int adjustDexoptFlags(int flags) { 643 // Add DEXOPT_FORCE flag to signal installd that it should force compilation 644 // and discard dexoptanalyzer result. 645 return flags | DEXOPT_FORCE; 646 } 647 } 648} 649