BackgroundDexOptService.java revision ace80c56d7c63dadead34539b643f69a1b7336e8
1/* 2 * Copyright (C) 2014 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 static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT; 20 21import android.app.AlarmManager; 22import android.app.job.JobInfo; 23import android.app.job.JobParameters; 24import android.app.job.JobScheduler; 25import android.app.job.JobService; 26import android.content.ComponentName; 27import android.content.Context; 28import android.content.Intent; 29import android.content.IntentFilter; 30import android.os.BatteryManager; 31import android.os.ServiceManager; 32import android.util.ArraySet; 33import android.util.Log; 34 35import java.util.concurrent.atomic.AtomicBoolean; 36import java.util.concurrent.TimeUnit; 37 38/** 39 * {@hide} 40 */ 41public class BackgroundDexOptService extends JobService { 42 static final String TAG = "BackgroundDexOptService"; 43 44 static final long RETRY_LATENCY = 4 * AlarmManager.INTERVAL_HOUR; 45 46 static final int JOB_IDLE_OPTIMIZE = 800; 47 static final int JOB_POST_BOOT_UPDATE = 801; 48 49 private static ComponentName sDexoptServiceName = new ComponentName( 50 "android", 51 BackgroundDexOptService.class.getName()); 52 53 /** 54 * Set of failed packages remembered across job runs. 55 */ 56 static final ArraySet<String> sFailedPackageNames = new ArraySet<String>(); 57 58 /** 59 * Atomics set to true if the JobScheduler requests an abort. 60 */ 61 final AtomicBoolean mAbortPostBootUpdate = new AtomicBoolean(false); 62 final AtomicBoolean mAbortIdleOptimization = new AtomicBoolean(false); 63 64 /** 65 * Atomic set to true if one job should exit early because another job was started. 66 */ 67 final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false); 68 69 public static void schedule(Context context) { 70 JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); 71 72 // Schedule a one-off job which scans installed packages and updates 73 // out-of-date oat files. 74 js.schedule(new JobInfo.Builder(JOB_POST_BOOT_UPDATE, sDexoptServiceName) 75 .setMinimumLatency(TimeUnit.MINUTES.toMillis(1)) 76 .setOverrideDeadline(TimeUnit.MINUTES.toMillis(1)) 77 .build()); 78 79 // Schedule a daily job which scans installed packages and compiles 80 // those with fresh profiling data. 81 js.schedule(new JobInfo.Builder(JOB_IDLE_OPTIMIZE, sDexoptServiceName) 82 .setRequiresDeviceIdle(true) 83 .setRequiresCharging(true) 84 .setPeriodic(TimeUnit.DAYS.toMillis(1)) 85 .build()); 86 87 if (DEBUG_DEXOPT) { 88 Log.i(TAG, "Jobs scheduled"); 89 } 90 } 91 92 public static void notifyPackageChanged(String packageName) { 93 // The idle maintanance job skips packages which previously failed to 94 // compile. The given package has changed and may successfully compile 95 // now. Remove it from the list of known failing packages. 96 synchronized (sFailedPackageNames) { 97 sFailedPackageNames.remove(packageName); 98 } 99 } 100 101 // Returns the current battery level as a 0-100 integer. 102 private int getBatteryLevel() { 103 IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 104 Intent intent = registerReceiver(null, filter); 105 int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 106 int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 107 108 if (level < 0 || scale <= 0) { 109 // Battery data unavailable. This should never happen, so assume the worst. 110 return 0; 111 } 112 113 return (100 * level / scale); 114 } 115 116 private boolean runPostBootUpdate(final JobParameters jobParams, 117 final PackageManagerService pm, final ArraySet<String> pkgs) { 118 if (mExitPostBootUpdate.get()) { 119 // This job has already been superseded. Do not start it. 120 return false; 121 } 122 123 // Load low battery threshold from the system config. This is a 0-100 integer. 124 final int lowBatteryThreshold = getResources().getInteger( 125 com.android.internal.R.integer.config_lowBatteryWarningLevel); 126 127 mAbortPostBootUpdate.set(false); 128 new Thread("BackgroundDexOptService_PostBootUpdate") { 129 @Override 130 public void run() { 131 for (String pkg : pkgs) { 132 if (mAbortPostBootUpdate.get()) { 133 // JobScheduler requested an early abort. 134 return; 135 } 136 if (mExitPostBootUpdate.get()) { 137 // Different job, which supersedes this one, is running. 138 break; 139 } 140 if (getBatteryLevel() < lowBatteryThreshold) { 141 // Rather bail than completely drain the battery. 142 break; 143 } 144 if (DEBUG_DEXOPT) { 145 Log.i(TAG, "Updating package " + pkg); 146 } 147 // Update package if needed. Note that there can be no race between concurrent 148 // jobs because PackageDexOptimizer.performDexOpt is synchronized. 149 pm.performDexOpt(pkg, 150 /* instruction set */ null, 151 /* checkProfiles */ false, 152 PackageManagerService.REASON_BOOT, 153 /* force */ false); 154 } 155 // Ran to completion, so we abandon our timeslice and do not reschedule. 156 jobFinished(jobParams, /* reschedule */ false); 157 } 158 }.start(); 159 return true; 160 } 161 162 private boolean runIdleOptimization(final JobParameters jobParams, 163 final PackageManagerService pm, final ArraySet<String> pkgs) { 164 // If post-boot update is still running, request that it exits early. 165 mExitPostBootUpdate.set(true); 166 167 mAbortIdleOptimization.set(false); 168 new Thread("BackgroundDexOptService_IdleOptimization") { 169 @Override 170 public void run() { 171 for (String pkg : pkgs) { 172 if (mAbortIdleOptimization.get()) { 173 // JobScheduler requested an early abort. 174 return; 175 } 176 if (sFailedPackageNames.contains(pkg)) { 177 // Skip previously failing package 178 continue; 179 } 180 // Conservatively add package to the list of failing ones in case performDexOpt 181 // never returns. 182 synchronized (sFailedPackageNames) { 183 sFailedPackageNames.add(pkg); 184 } 185 // Optimize package if needed. Note that there can be no race between 186 // concurrent jobs because PackageDexOptimizer.performDexOpt is synchronized. 187 if (pm.performDexOpt(pkg, 188 /* instruction set */ null, 189 /* checkProfiles */ true, 190 PackageManagerService.REASON_BACKGROUND_DEXOPT, 191 /* force */ false)) { 192 // Dexopt succeeded, remove package from the list of failing ones. 193 synchronized (sFailedPackageNames) { 194 sFailedPackageNames.remove(pkg); 195 } 196 } 197 } 198 // Ran to completion, so we abandon our timeslice and do not reschedule. 199 jobFinished(jobParams, /* reschedule */ false); 200 } 201 }.start(); 202 return true; 203 } 204 205 @Override 206 public boolean onStartJob(JobParameters params) { 207 if (DEBUG_DEXOPT) { 208 Log.i(TAG, "onStartJob"); 209 } 210 211 PackageManagerService pm = (PackageManagerService)ServiceManager.getService("package"); 212 if (pm.isStorageLow()) { 213 if (DEBUG_DEXOPT) { 214 Log.i(TAG, "Low storage, skipping this run"); 215 } 216 return false; 217 } 218 219 final ArraySet<String> pkgs = pm.getOptimizablePackages(); 220 if (pkgs == null || pkgs.isEmpty()) { 221 if (DEBUG_DEXOPT) { 222 Log.i(TAG, "No packages to optimize"); 223 } 224 return false; 225 } 226 227 if (params.getJobId() == JOB_POST_BOOT_UPDATE) { 228 return runPostBootUpdate(params, pm, pkgs); 229 } else { 230 return runIdleOptimization(params, pm, pkgs); 231 } 232 } 233 234 @Override 235 public boolean onStopJob(JobParameters params) { 236 if (DEBUG_DEXOPT) { 237 Log.i(TAG, "onStopJob"); 238 } 239 240 if (params.getJobId() == JOB_POST_BOOT_UPDATE) { 241 mAbortPostBootUpdate.set(true); 242 } else { 243 mAbortIdleOptimization.set(true); 244 } 245 return false; 246 } 247} 248