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