Merge "BGDexopt: Have a single run for primary and secondary dexes"

This commit is contained in:
Andreas Gampe
2019-07-31 21:48:36 +00:00
committed by Gerrit Code Review

View File

@@ -281,20 +281,7 @@ public class BackgroundDexOptService extends JobService {
mAbortIdleOptimization.set(false); mAbortIdleOptimization.set(false);
long lowStorageThreshold = getLowStorageThreshold(context); long lowStorageThreshold = getLowStorageThreshold(context);
// Optimize primary apks. int result = idleOptimizePackages(pm, pkgs, lowStorageThreshold);
int result = optimizePackages(pm, pkgs, lowStorageThreshold,
/*isForPrimaryDex=*/ true);
if (result == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) {
return result;
}
if (supportSecondaryDex()) {
result = reconcileSecondaryDexFiles(pm.getDexManager());
if (result == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) {
return result;
}
result = optimizePackages(pm, pkgs, lowStorageThreshold,
/*isForPrimaryDex=*/ false);
}
return result; return result;
} }
@@ -342,11 +329,20 @@ public class BackgroundDexOptService extends JobService {
return 0; return 0;
} }
private int optimizePackages(PackageManagerService pm, ArraySet<String> pkgs, private int idleOptimizePackages(PackageManagerService pm, ArraySet<String> pkgs,
long lowStorageThreshold, boolean isForPrimaryDex) { long lowStorageThreshold) {
ArraySet<String> updatedPackages = new ArraySet<>(); ArraySet<String> updatedPackages = new ArraySet<>();
try { try {
final boolean supportSecondaryDex = supportSecondaryDex();
if (supportSecondaryDex) {
int result = reconcileSecondaryDexFiles(pm.getDexManager());
if (result == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) {
return result;
}
}
// Only downgrade apps when space is low on device. // Only downgrade apps when space is low on device.
// Threshold is selected above the lowStorageThreshold so that we can pro-actively clean // Threshold is selected above the lowStorageThreshold so that we can pro-actively clean
// up disk before user hits the actual lowStorageThreshold. // up disk before user hits the actual lowStorageThreshold.
@@ -359,43 +355,61 @@ public class BackgroundDexOptService extends JobService {
pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis); pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis);
Log.d(TAG, "Unsused Packages " + String.join(",", unusedPackages)); Log.d(TAG, "Unsused Packages " + String.join(",", unusedPackages));
for (String pkg : unusedPackages) {
int abortCode = abortIdleOptimizations(/*lowStorageThreshold*/ -1);
if (abortCode != OPTIMIZE_CONTINUE) {
// Should be aborted by the scheduler.
return abortCode;
}
if (downgradePackage(pm, pkg, isForPrimaryDex)) {
updatedPackages.add(pkg);
}
}
if (!unusedPackages.isEmpty()) { if (!unusedPackages.isEmpty()) {
for (String pkg : unusedPackages) {
int abortCode = abortIdleOptimizations(/*lowStorageThreshold*/ -1);
if (abortCode != OPTIMIZE_CONTINUE) {
// Should be aborted by the scheduler.
return abortCode;
}
if (downgradePackage(pm, pkg, /*isForPrimaryDex*/ true)) {
updatedPackages.add(pkg);
}
if (supportSecondaryDex) {
downgradePackage(pm, pkg, /*isForPrimaryDex*/ false);
}
}
pkgs = new ArraySet<>(pkgs); pkgs = new ArraySet<>(pkgs);
pkgs.removeAll(unusedPackages); pkgs.removeAll(unusedPackages);
} }
} }
for (String pkg : pkgs) { int primaryResult = optimizePackages(pm, pkgs, lowStorageThreshold,
int abortCode = abortIdleOptimizations(lowStorageThreshold); /*isForPrimaryDex*/ true, updatedPackages);
if (abortCode != OPTIMIZE_CONTINUE) { if (primaryResult != OPTIMIZE_PROCESSED) {
// Either aborted by the scheduler or no space left. return primaryResult;
return abortCode;
}
boolean dexOptPerformed = optimizePackage(pm, pkg, isForPrimaryDex);
if (dexOptPerformed) {
updatedPackages.add(pkg);
}
} }
return OPTIMIZE_PROCESSED; if (!supportSecondaryDex) {
return OPTIMIZE_PROCESSED;
}
int secondaryResult = optimizePackages(pm, pkgs, lowStorageThreshold,
/*isForPrimaryDex*/ false, updatedPackages);
return secondaryResult;
} finally { } finally {
// Always let the pinner service know about changes. // Always let the pinner service know about changes.
notifyPinService(updatedPackages); notifyPinService(updatedPackages);
} }
} }
private int optimizePackages(PackageManagerService pm, ArraySet<String> pkgs,
long lowStorageThreshold, boolean isForPrimaryDex, ArraySet<String> updatedPackages) {
for (String pkg : pkgs) {
int abortCode = abortIdleOptimizations(lowStorageThreshold);
if (abortCode != OPTIMIZE_CONTINUE) {
// Either aborted by the scheduler or no space left.
return abortCode;
}
boolean dexOptPerformed = optimizePackage(pm, pkg, isForPrimaryDex);
if (dexOptPerformed) {
updatedPackages.add(pkg);
}
}
return OPTIMIZE_PROCESSED;
}
/** /**
* Try to downgrade the package to a smaller compilation filter. * Try to downgrade the package to a smaller compilation filter.