Package Manager: Sort list of packages to dexopt
Sort the list by last-use-time, if available. Interleave the dependencies with the packages. Clean up the code a bit for better code reuse and ease of writing filters. This should help with prioritization under space constraints. Bug: 31347757 Change-Id: Ia0ec62faf013a379dc4c80b18fd6b2bfbfa470c4
This commit is contained in:
@@ -19,6 +19,7 @@ package com.android.server.pm;
|
|||||||
import static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT;
|
import static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT;
|
||||||
import static com.android.server.pm.PackageManagerService.TAG;
|
import static com.android.server.pm.PackageManagerService.TAG;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
import android.app.AppGlobals;
|
import android.app.AppGlobals;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageParser;
|
import android.content.pm.PackageParser;
|
||||||
@@ -35,13 +36,9 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class containing helper methods for the PackageManagerService.
|
* Class containing helper methods for the PackageManagerService.
|
||||||
@@ -67,34 +64,51 @@ public class PackageManagerServiceUtils {
|
|||||||
return pkgNames;
|
return pkgNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void filterRecentlyUsedApps(Collection<PackageParser.Package> pkgs,
|
// Sort a list of apps by their last usage, most recently used apps first. The order of
|
||||||
long estimatedPreviousSystemUseTime,
|
// packages without usage data is undefined (but they will be sorted after the packages
|
||||||
long dexOptLRUThresholdInMills) {
|
// that do have usage data).
|
||||||
// Filter out packages that aren't recently used.
|
public static void sortPackagesByUsageDate(List<PackageParser.Package> pkgs,
|
||||||
int total = pkgs.size();
|
PackageManagerService packageManagerService) {
|
||||||
int skipped = 0;
|
if (!packageManagerService.isHistoricalPackageUsageAvailable()) {
|
||||||
for (Iterator<PackageParser.Package> i = pkgs.iterator(); i.hasNext();) {
|
return;
|
||||||
PackageParser.Package pkg = i.next();
|
}
|
||||||
long then = pkg.getLatestForegroundPackageUseTimeInMills();
|
|
||||||
if (then < estimatedPreviousSystemUseTime - dexOptLRUThresholdInMills) {
|
Collections.sort(pkgs, (pkg1, pkg2) ->
|
||||||
if (DEBUG_DEXOPT) {
|
Long.compare(pkg2.getLatestForegroundPackageUseTimeInMills(),
|
||||||
Log.i(TAG, "Skipping dexopt of " + pkg.packageName +
|
pkg1.getLatestForegroundPackageUseTimeInMills()));
|
||||||
" last used in foreground: " +
|
}
|
||||||
((then == 0) ? "never" : new Date(then)));
|
|
||||||
}
|
// Apply the given {@code filter} to all packages in {@code packages}. If tested positive, the
|
||||||
i.remove();
|
// package will be removed from {@code packages} and added to {@code result} with its
|
||||||
skipped++;
|
// dependencies. If usage data is available, the positive packages will be sorted by usage
|
||||||
} else {
|
// data (with {@code sortTemp} as temporary storage).
|
||||||
if (DEBUG_DEXOPT) {
|
private static void applyPackageFilter(Predicate<PackageParser.Package> filter,
|
||||||
Log.i(TAG, "Will dexopt " + pkg.packageName +
|
Collection<PackageParser.Package> result,
|
||||||
" last used in foreground: " +
|
Collection<PackageParser.Package> packages,
|
||||||
((then == 0) ? "never" : new Date(then)));
|
@NonNull List<PackageParser.Package> sortTemp,
|
||||||
}
|
PackageManagerService packageManagerService) {
|
||||||
|
for (PackageParser.Package pkg : packages) {
|
||||||
|
if (filter.test(pkg)) {
|
||||||
|
sortTemp.add(pkg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DEBUG_DEXOPT) {
|
|
||||||
Log.i(TAG, "Skipped dexopt " + skipped + " of " + total);
|
sortPackagesByUsageDate(sortTemp, packageManagerService);
|
||||||
|
packages.removeAll(sortTemp);
|
||||||
|
|
||||||
|
for (PackageParser.Package pkg : sortTemp) {
|
||||||
|
result.add(pkg);
|
||||||
|
|
||||||
|
Collection<PackageParser.Package> deps =
|
||||||
|
packageManagerService.findSharedNonSystemLibraries(pkg);
|
||||||
|
if (!deps.isEmpty()) {
|
||||||
|
deps.removeAll(result);
|
||||||
|
result.addAll(deps);
|
||||||
|
packages.removeAll(deps);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sortTemp.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort apps by importance for dexopt ordering. Important apps are given
|
// Sort apps by importance for dexopt ordering. Important apps are given
|
||||||
@@ -104,46 +118,25 @@ public class PackageManagerServiceUtils {
|
|||||||
PackageManagerService packageManagerService) {
|
PackageManagerService packageManagerService) {
|
||||||
ArrayList<PackageParser.Package> remainingPkgs = new ArrayList<>(packages);
|
ArrayList<PackageParser.Package> remainingPkgs = new ArrayList<>(packages);
|
||||||
LinkedList<PackageParser.Package> result = new LinkedList<>();
|
LinkedList<PackageParser.Package> result = new LinkedList<>();
|
||||||
|
ArrayList<PackageParser.Package> sortTemp = new ArrayList<>(remainingPkgs.size());
|
||||||
|
|
||||||
// Give priority to core apps.
|
// Give priority to core apps.
|
||||||
for (PackageParser.Package pkg : remainingPkgs) {
|
applyPackageFilter((pkg) -> pkg.coreApp, result, remainingPkgs, sortTemp,
|
||||||
if (pkg.coreApp) {
|
packageManagerService);
|
||||||
if (DEBUG_DEXOPT) {
|
|
||||||
Log.i(TAG, "Adding core app " + result.size() + ": " + pkg.packageName);
|
|
||||||
}
|
|
||||||
result.add(pkg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
remainingPkgs.removeAll(result);
|
|
||||||
|
|
||||||
// Give priority to system apps that listen for pre boot complete.
|
// Give priority to system apps that listen for pre boot complete.
|
||||||
Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
|
Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
|
||||||
ArraySet<String> pkgNames = getPackageNamesForIntent(intent, UserHandle.USER_SYSTEM);
|
final ArraySet<String> pkgNames = getPackageNamesForIntent(intent, UserHandle.USER_SYSTEM);
|
||||||
for (PackageParser.Package pkg : remainingPkgs) {
|
applyPackageFilter((pkg) -> pkgNames.contains(pkg.packageName), result, remainingPkgs,
|
||||||
if (pkgNames.contains(pkg.packageName)) {
|
sortTemp, packageManagerService);
|
||||||
if (DEBUG_DEXOPT) {
|
|
||||||
Log.i(TAG, "Adding pre boot system app " + result.size() + ": " +
|
|
||||||
pkg.packageName);
|
|
||||||
}
|
|
||||||
result.add(pkg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
remainingPkgs.removeAll(result);
|
|
||||||
|
|
||||||
// Give priority to apps used by other apps.
|
// Give priority to apps used by other apps.
|
||||||
for (PackageParser.Package pkg : remainingPkgs) {
|
applyPackageFilter((pkg) -> PackageDexOptimizer.isUsedByOtherApps(pkg), result,
|
||||||
if (PackageDexOptimizer.isUsedByOtherApps(pkg)) {
|
remainingPkgs, sortTemp, packageManagerService);
|
||||||
if (DEBUG_DEXOPT) {
|
|
||||||
Log.i(TAG, "Adding app used by other apps " + result.size() + ": " +
|
|
||||||
pkg.packageName);
|
|
||||||
}
|
|
||||||
result.add(pkg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
remainingPkgs.removeAll(result);
|
|
||||||
|
|
||||||
// Filter out packages that aren't recently used, add all remaining apps.
|
// Filter out packages that aren't recently used, add all remaining apps.
|
||||||
// TODO: add a property to control this?
|
// TODO: add a property to control this?
|
||||||
|
Predicate<PackageParser.Package> remainingPredicate;
|
||||||
if (!remainingPkgs.isEmpty() && packageManagerService.isHistoricalPackageUsageAvailable()) {
|
if (!remainingPkgs.isEmpty() && packageManagerService.isHistoricalPackageUsageAvailable()) {
|
||||||
if (DEBUG_DEXOPT) {
|
if (DEBUG_DEXOPT) {
|
||||||
Log.i(TAG, "Looking at historical package use");
|
Log.i(TAG, "Looking at historical package use");
|
||||||
@@ -159,24 +152,20 @@ public class PackageManagerServiceUtils {
|
|||||||
lastUsed.getLatestForegroundPackageUseTimeInMills();
|
lastUsed.getLatestForegroundPackageUseTimeInMills();
|
||||||
// Be defensive if for some reason package usage has bogus data.
|
// Be defensive if for some reason package usage has bogus data.
|
||||||
if (estimatedPreviousSystemUseTime != 0) {
|
if (estimatedPreviousSystemUseTime != 0) {
|
||||||
filterRecentlyUsedApps(remainingPkgs, estimatedPreviousSystemUseTime,
|
final long cutoffTime = estimatedPreviousSystemUseTime - SEVEN_DAYS_IN_MILLISECONDS;
|
||||||
SEVEN_DAYS_IN_MILLISECONDS);
|
remainingPredicate =
|
||||||
|
(pkg) -> pkg.getLatestForegroundPackageUseTimeInMills() >= cutoffTime;
|
||||||
|
} else {
|
||||||
|
// No meaningful historical info. Take all.
|
||||||
|
remainingPredicate = (pkg) -> true;
|
||||||
}
|
}
|
||||||
|
sortPackagesByUsageDate(remainingPkgs, packageManagerService);
|
||||||
|
} else {
|
||||||
|
// No historical info. Take all.
|
||||||
|
remainingPredicate = (pkg) -> true;
|
||||||
}
|
}
|
||||||
result.addAll(remainingPkgs);
|
applyPackageFilter(remainingPredicate, result, remainingPkgs, sortTemp,
|
||||||
|
packageManagerService);
|
||||||
// Now go ahead and also add the libraries required for these packages.
|
|
||||||
// TODO: Think about interleaving things.
|
|
||||||
Set<PackageParser.Package> dependencies = new HashSet<>();
|
|
||||||
for (PackageParser.Package p : result) {
|
|
||||||
dependencies.addAll(packageManagerService.findSharedNonSystemLibraries(p));
|
|
||||||
}
|
|
||||||
if (!dependencies.isEmpty()) {
|
|
||||||
// We might have packages already in `result` that are dependencies
|
|
||||||
// of other packages. Make sure we don't add those to the list twice.
|
|
||||||
dependencies.removeAll(result);
|
|
||||||
}
|
|
||||||
result.addAll(dependencies);
|
|
||||||
|
|
||||||
if (DEBUG_DEXOPT) {
|
if (DEBUG_DEXOPT) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@@ -187,6 +176,15 @@ public class PackageManagerServiceUtils {
|
|||||||
sb.append(pkg.packageName);
|
sb.append(pkg.packageName);
|
||||||
}
|
}
|
||||||
Log.i(TAG, "Packages to be dexopted: " + sb.toString());
|
Log.i(TAG, "Packages to be dexopted: " + sb.toString());
|
||||||
|
|
||||||
|
sb.setLength(0);
|
||||||
|
for (PackageParser.Package pkg : remainingPkgs) {
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
sb.append(", ");
|
||||||
|
}
|
||||||
|
sb.append(pkg.packageName);
|
||||||
|
}
|
||||||
|
Log.i(TAG, "Packages skipped from dexopt: " + sb.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user