Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cda87c54 authored by Andreas Gampe's avatar Andreas Gampe Committed by android-build-merger
Browse files

Package Manager: Sort list of packages to dexopt am: d3e07d46 am: c6340e70

am: ff2cbd1d

Change-Id: I7a58ff9b1961d4b85aed1f10f6d9de3c1681f593
parents d235187b ff2cbd1d
Loading
Loading
Loading
Loading
+74 −76
Original line number Diff line number Diff line
@@ -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.TAG;

import android.annotation.NonNull;
import android.app.AppGlobals;
import android.content.Intent;
import android.content.pm.PackageParser;
@@ -35,13 +36,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
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.List;
import java.util.Set;
import java.util.function.Predicate;

/**
 * Class containing helper methods for the PackageManagerService.
@@ -67,34 +64,51 @@ public class PackageManagerServiceUtils {
        return pkgNames;
    }

    private static void filterRecentlyUsedApps(Collection<PackageParser.Package> pkgs,
            long estimatedPreviousSystemUseTime,
            long dexOptLRUThresholdInMills) {
        // Filter out packages that aren't recently used.
        int total = pkgs.size();
        int skipped = 0;
        for (Iterator<PackageParser.Package> i = pkgs.iterator(); i.hasNext();) {
            PackageParser.Package pkg = i.next();
            long then = pkg.getLatestForegroundPackageUseTimeInMills();
            if (then < estimatedPreviousSystemUseTime - dexOptLRUThresholdInMills) {
                if (DEBUG_DEXOPT) {
                    Log.i(TAG, "Skipping dexopt of " + pkg.packageName +
                            " last used in foreground: " +
                            ((then == 0) ? "never" : new Date(then)));
    // Sort a list of apps by their last usage, most recently used apps first. The order of
    // packages without usage data is undefined (but they will be sorted after the packages
    // that do have usage data).
    public static void sortPackagesByUsageDate(List<PackageParser.Package> pkgs,
            PackageManagerService packageManagerService) {
        if (!packageManagerService.isHistoricalPackageUsageAvailable()) {
            return;
        }
                i.remove();
                skipped++;
            } else {
                if (DEBUG_DEXOPT) {
                    Log.i(TAG, "Will dexopt " + pkg.packageName +
                            " last used in foreground: " +
                            ((then == 0) ? "never" : new Date(then)));

        Collections.sort(pkgs, (pkg1, pkg2) ->
                Long.compare(pkg2.getLatestForegroundPackageUseTimeInMills(),
                        pkg1.getLatestForegroundPackageUseTimeInMills()));
    }

    // Apply the given {@code filter} to all packages in {@code packages}. If tested positive, the
    // package will be removed from {@code packages} and added to {@code result} with its
    // dependencies. If usage data is available, the positive packages will be sorted by usage
    // data (with {@code sortTemp} as temporary storage).
    private static void applyPackageFilter(Predicate<PackageParser.Package> filter,
            Collection<PackageParser.Package> result,
            Collection<PackageParser.Package> packages,
            @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
@@ -104,46 +118,25 @@ public class PackageManagerServiceUtils {
            PackageManagerService packageManagerService) {
        ArrayList<PackageParser.Package> remainingPkgs = new ArrayList<>(packages);
        LinkedList<PackageParser.Package> result = new LinkedList<>();
        ArrayList<PackageParser.Package> sortTemp = new ArrayList<>(remainingPkgs.size());

        // Give priority to core apps.
        for (PackageParser.Package pkg : remainingPkgs) {
            if (pkg.coreApp) {
                if (DEBUG_DEXOPT) {
                    Log.i(TAG, "Adding core app " + result.size() + ": " + pkg.packageName);
                }
                result.add(pkg);
            }
        }
        remainingPkgs.removeAll(result);
        applyPackageFilter((pkg) -> pkg.coreApp, result, remainingPkgs, sortTemp,
                packageManagerService);

        // Give priority to system apps that listen for pre boot complete.
        Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
        ArraySet<String> pkgNames = getPackageNamesForIntent(intent, UserHandle.USER_SYSTEM);
        for (PackageParser.Package pkg : remainingPkgs) {
            if (pkgNames.contains(pkg.packageName)) {
                if (DEBUG_DEXOPT) {
                    Log.i(TAG, "Adding pre boot system app " + result.size() + ": " +
                            pkg.packageName);
                }
                result.add(pkg);
            }
        }
        remainingPkgs.removeAll(result);
        final ArraySet<String> pkgNames = getPackageNamesForIntent(intent, UserHandle.USER_SYSTEM);
        applyPackageFilter((pkg) -> pkgNames.contains(pkg.packageName), result, remainingPkgs,
                sortTemp, packageManagerService);

        // Give priority to apps used by other apps.
        for (PackageParser.Package pkg : remainingPkgs) {
            if (PackageDexOptimizer.isUsedByOtherApps(pkg)) {
                if (DEBUG_DEXOPT) {
                    Log.i(TAG, "Adding app used by other apps " + result.size() + ": " +
                            pkg.packageName);
                }
                result.add(pkg);
            }
        }
        remainingPkgs.removeAll(result);
        applyPackageFilter((pkg) -> PackageDexOptimizer.isUsedByOtherApps(pkg), result,
                remainingPkgs, sortTemp, packageManagerService);

        // Filter out packages that aren't recently used, add all remaining apps.
        // TODO: add a property to control this?
        Predicate<PackageParser.Package> remainingPredicate;
        if (!remainingPkgs.isEmpty() && packageManagerService.isHistoricalPackageUsageAvailable()) {
            if (DEBUG_DEXOPT) {
                Log.i(TAG, "Looking at historical package use");
@@ -159,24 +152,20 @@ public class PackageManagerServiceUtils {
                    lastUsed.getLatestForegroundPackageUseTimeInMills();
            // Be defensive if for some reason package usage has bogus data.
            if (estimatedPreviousSystemUseTime != 0) {
                filterRecentlyUsedApps(remainingPkgs, estimatedPreviousSystemUseTime,
                        SEVEN_DAYS_IN_MILLISECONDS);
            }
        }
        result.addAll(remainingPkgs);

        // 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));
                final long cutoffTime = estimatedPreviousSystemUseTime - SEVEN_DAYS_IN_MILLISECONDS;
                remainingPredicate =
                        (pkg) -> pkg.getLatestForegroundPackageUseTimeInMills() >= cutoffTime;
            } else {
                // No meaningful historical info. Take all.
                remainingPredicate = (pkg) -> true;
            }
        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);
            sortPackagesByUsageDate(remainingPkgs, packageManagerService);
        } else {
            // No historical info. Take all.
            remainingPredicate = (pkg) -> true;
        }
        result.addAll(dependencies);
        applyPackageFilter(remainingPredicate, result, remainingPkgs, sortTemp,
                packageManagerService);

        if (DEBUG_DEXOPT) {
            StringBuilder sb = new StringBuilder();
@@ -187,6 +176,15 @@ public class PackageManagerServiceUtils {
                sb.append(pkg.packageName);
            }
            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;