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

Commit f98d2cc9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge changes I87740397,I0cc4538b,Iaceb2b7c am: 56ba9447

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1700306

Change-Id: I4bc1321f1e1c687ce6d29376d73531919da6b545
parents 8cdf84dd 56ba9447
Loading
Loading
Loading
Loading
+50 −38
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.server.pm;

import static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT;
import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME;

import android.annotation.Nullable;
@@ -24,8 +23,11 @@ import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.os.BatteryManagerInternal;
import android.os.Environment;
@@ -35,6 +37,7 @@ import android.os.UserHandle;
import android.os.storage.StorageManager;
import android.util.ArraySet;
import android.util.Log;
import android.util.Slog;

import com.android.internal.util.ArrayUtils;
import com.android.internal.util.FrameworkStatsLog;
@@ -63,9 +66,7 @@ public class BackgroundDexOptService extends JobService {
    private static final int JOB_IDLE_OPTIMIZE = 800;
    private static final int JOB_POST_BOOT_UPDATE = 801;

    private static final long IDLE_OPTIMIZATION_PERIOD = DEBUG
            ? TimeUnit.MINUTES.toMillis(1)
            : TimeUnit.DAYS.toMillis(1);
    private static final long IDLE_OPTIMIZATION_PERIOD = TimeUnit.DAYS.toMillis(1);

    private static ComponentName sDexoptServiceName = new ComponentName(
            "android",
@@ -113,14 +114,24 @@ public class BackgroundDexOptService extends JobService {
            return;
        }

        JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        final JobScheduler js = context.getSystemService(JobScheduler.class);

        // Schedule a one-off job which scans installed packages and updates
        // out-of-date oat files.
        // out-of-date oat files. Schedule it 10 minutes after the boot complete event,
        // so that we don't overload the boot with additional dex2oat compilations.
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                js.schedule(new JobInfo.Builder(JOB_POST_BOOT_UPDATE, sDexoptServiceName)
                        .setMinimumLatency(TimeUnit.MINUTES.toMillis(10))
                        .setOverrideDeadline(TimeUnit.MINUTES.toMillis(60))
                        .build());
                context.unregisterReceiver(this);
                if (DEBUG) {
                    Slog.i(TAG, "BootBgDexopt scheduled");
                }
            }
        }, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));

        // Schedule a daily job which scans installed packages and compiles
        // those with fresh profiling data.
@@ -130,8 +141,8 @@ public class BackgroundDexOptService extends JobService {
                    .setPeriodic(IDLE_OPTIMIZATION_PERIOD)
                    .build());

        if (DEBUG_DEXOPT) {
            Log.i(TAG, "Jobs scheduled");
        if (DEBUG) {
            Slog.d(TAG, "BgDexopt scheduled");
        }
    }

@@ -151,7 +162,7 @@ public class BackgroundDexOptService extends JobService {
        @SuppressWarnings("deprecation")
        final long lowThreshold = StorageManager.from(context).getStorageLowBytes(mDataDir);
        if (lowThreshold == 0) {
            Log.e(TAG, "Invalid low storage threshold");
            Slog.e(TAG, "Invalid low storage threshold");
        }

        return lowThreshold;
@@ -198,13 +209,12 @@ public class BackgroundDexOptService extends JobService {
            long usableSpace = mDataDir.getUsableSpace();
            if (usableSpace < lowThreshold) {
                // Rather bail than completely fill up the disk.
                Log.w(TAG, "Aborting background dex opt job due to low storage: " +
                Slog.w(TAG, "Aborting background dex opt job due to low storage: " +
                        usableSpace);
                break;
            }

            if (DEBUG_DEXOPT) {
                Log.i(TAG, "Updating package " + pkg);
            if (DEBUG) {
                Slog.i(TAG, "Updating package " + pkg);
            }

            // Update package if needed. Note that there can be no race between concurrent
@@ -236,13 +246,13 @@ public class BackgroundDexOptService extends JobService {
            public void run() {
                int result = idleOptimization(pm, pkgs, BackgroundDexOptService.this);
                if (result == OPTIMIZE_PROCESSED) {
                    Log.i(TAG, "Idle optimizations completed.");
                    Slog.i(TAG, "Idle optimizations completed.");
                } else if (result == OPTIMIZE_ABORT_NO_SPACE_LEFT) {
                    Log.w(TAG, "Idle optimizations aborted because of space constraints.");
                    Slog.w(TAG, "Idle optimizations aborted because of space constraints.");
                } else if (result == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) {
                    Log.w(TAG, "Idle optimizations aborted by job scheduler.");
                    Slog.w(TAG, "Idle optimizations aborted by job scheduler.");
                } else {
                    Log.w(TAG, "Idle optimizations ended with unexpected code: " + result);
                    Slog.w(TAG, "Idle optimizations ended with unexpected code: " + result);
                }
                if (result != OPTIMIZE_ABORT_BY_JOB_SCHEDULER) {
                    // Abandon our timeslice and do not reschedule.
@@ -256,7 +266,7 @@ public class BackgroundDexOptService extends JobService {
    // Optimize the given packages and return the optimization result (one of the OPTIMIZE_* codes).
    private int idleOptimization(PackageManagerService pm, ArraySet<String> pkgs,
            Context context) {
        Log.i(TAG, "Performing idle optimizations");
        Slog.i(TAG, "Performing idle optimizations");
        // If post-boot update is still running, request that it exits early.
        mExitPostBootUpdate.set(true);
        mAbortIdleOptimization.set(false);
@@ -331,11 +341,15 @@ public class BackgroundDexOptService extends JobService {
            final long lowStorageThresholdForDowngrade = LOW_THRESHOLD_MULTIPLIER_FOR_DOWNGRADE
                    * lowStorageThreshold;
            boolean shouldDowngrade = shouldDowngrade(lowStorageThresholdForDowngrade);
            Log.d(TAG, "Should Downgrade " + shouldDowngrade);
            if (DEBUG) {
                Slog.d(TAG, "Should Downgrade " + shouldDowngrade);
            }
            if (shouldDowngrade) {
                Set<String> unusedPackages =
                        pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis);
                Log.d(TAG, "Unsused Packages " +  String.join(",", unusedPackages));
                if (DEBUG) {
                    Slog.d(TAG, "Unsused Packages " +  String.join(",", unusedPackages));
                }

                if (!unusedPackages.isEmpty()) {
                    for (String pkg : unusedPackages) {
@@ -407,7 +421,9 @@ public class BackgroundDexOptService extends JobService {
     */
    private boolean downgradePackage(PackageManagerService pm, String pkg,
            boolean isForPrimaryDex) {
        Log.d(TAG, "Downgrading " + pkg);
        if (DEBUG) {
            Slog.d(TAG, "Downgrading " + pkg);
        }
        boolean dex_opt_performed = false;
        int reason = PackageManagerService.REASON_INACTIVE_PACKAGE_DOWNGRADE;
        int dexoptFlags = DexoptOptions.DEXOPT_BOOT_COMPLETE
@@ -529,7 +545,7 @@ public class BackgroundDexOptService extends JobService {
        long usableSpace = mDataDir.getUsableSpace();
        if (usableSpace < lowStorageThreshold) {
            // Rather bail than completely fill up the disk.
            Log.w(TAG, "Aborting background dex opt job due to low storage: " + usableSpace);
            Slog.w(TAG, "Aborting background dex opt job due to low storage: " + usableSpace);
            return OPTIMIZE_ABORT_NO_SPACE_LEFT;
        }

@@ -568,8 +584,8 @@ public class BackgroundDexOptService extends JobService {

    @Override
    public boolean onStartJob(JobParameters params) {
        if (DEBUG_DEXOPT) {
            Log.i(TAG, "onStartJob");
        if (DEBUG) {
            Slog.i(TAG, "onStartJob");
        }

        // NOTE: PackageManagerService.isStorageLow uses a different set of criteria from
@@ -577,17 +593,13 @@ public class BackgroundDexOptService extends JobService {
        // restart with a period of ~1 minute.
        PackageManagerService pm = (PackageManagerService)ServiceManager.getService("package");
        if (pm.isStorageLow()) {
            if (DEBUG_DEXOPT) {
                Log.i(TAG, "Low storage, skipping this run");
            }
            Slog.i(TAG, "Low storage, skipping this run");
            return false;
        }

        final ArraySet<String> pkgs = pm.getOptimizablePackages();
        if (pkgs.isEmpty()) {
            if (DEBUG_DEXOPT) {
                Log.i(TAG, "No packages to optimize");
            }
            Slog.i(TAG, "No packages to optimize");
            return false;
        }

@@ -603,8 +615,8 @@ public class BackgroundDexOptService extends JobService {

    @Override
    public boolean onStopJob(JobParameters params) {
        if (DEBUG_DEXOPT) {
            Log.i(TAG, "onStopJob");
        if (DEBUG) {
            Slog.d(TAG, "onStopJob");
        }

        if (params.getJobId() == JOB_POST_BOOT_UPDATE) {
@@ -625,7 +637,7 @@ public class BackgroundDexOptService extends JobService {
    private void notifyPinService(ArraySet<String> updatedPackages) {
        PinnerService pinnerService = LocalServices.getService(PinnerService.class);
        if (pinnerService != null) {
            Log.i(TAG, "Pinning optimized code " + updatedPackages);
            Slog.i(TAG, "Pinning optimized code " + updatedPackages);
            pinnerService.update(updatedPackages, false /* force */);
        }
    }
@@ -660,7 +672,7 @@ public class BackgroundDexOptService extends JobService {
        final String sysPropKey = "pm.dexopt.downgrade_after_inactive_days";
        String sysPropValue = SystemProperties.get(sysPropKey);
        if (sysPropValue == null || sysPropValue.isEmpty()) {
            Log.w(TAG, "SysProp " + sysPropKey + " not set");
            Slog.w(TAG, "SysProp " + sysPropKey + " not set");
            return Long.MAX_VALUE;
        }
        return TimeUnit.DAYS.toMillis(Long.parseLong(sysPropValue));