BackgroundDexOptService.java revision 09dd1ec4b2dedb862b6276763268380ec037a58e
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.annotation.Nullable; 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.Environment; 32import android.os.ServiceManager; 33import android.os.SystemProperties; 34import android.os.storage.StorageManager; 35import android.util.ArraySet; 36import android.util.Log; 37 38import com.android.server.pm.dex.DexManager; 39import com.android.server.LocalServices; 40import com.android.server.PinnerService; 41import com.android.server.pm.dex.DexoptOptions; 42 43import java.io.File; 44import java.util.List; 45import java.util.Set; 46import java.util.concurrent.atomic.AtomicBoolean; 47import java.util.concurrent.TimeUnit; 48 49/** 50 * {@hide} 51 */ 52public class BackgroundDexOptService extends JobService { 53 private static final String TAG = "BackgroundDexOptService"; 54 55 private static final boolean DEBUG = false; 56 57 private static final int JOB_IDLE_OPTIMIZE = 800; 58 private static final int JOB_POST_BOOT_UPDATE = 801; 59 60 private static final long IDLE_OPTIMIZATION_PERIOD = DEBUG 61 ? TimeUnit.MINUTES.toMillis(1) 62 : TimeUnit.DAYS.toMillis(1); 63 64 private static ComponentName sDexoptServiceName = new ComponentName( 65 "android", 66 BackgroundDexOptService.class.getName()); 67 68 // Possible return codes of individual optimization steps. 69 70 // Optimizations finished. All packages were processed. 71 private static final int OPTIMIZE_PROCESSED = 0; 72 // Optimizations should continue. Issued after checking the scheduler, disk space or battery. 73 private static final int OPTIMIZE_CONTINUE = 1; 74 // Optimizations should be aborted. Job scheduler requested it. 75 private static final int OPTIMIZE_ABORT_BY_JOB_SCHEDULER = 2; 76 // Optimizations should be aborted. No space left on device. 77 private static final int OPTIMIZE_ABORT_NO_SPACE_LEFT = 3; 78 79 // Used for calculating space threshold for downgrading unused apps. 80 private static final int LOW_THRESHOLD_MULTIPLIER_FOR_DOWNGRADE = 2; 81 82 /** 83 * Set of failed packages remembered across job runs. 84 */ 85 static final ArraySet<String> sFailedPackageNamesPrimary = new ArraySet<String>(); 86 static final ArraySet<String> sFailedPackageNamesSecondary = new ArraySet<String>(); 87 88 /** 89 * Atomics set to true if the JobScheduler requests an abort. 90 */ 91 private final AtomicBoolean mAbortPostBootUpdate = new AtomicBoolean(false); 92 private final AtomicBoolean mAbortIdleOptimization = new AtomicBoolean(false); 93 94 /** 95 * Atomic set to true if one job should exit early because another job was started. 96 */ 97 private final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false); 98 99 private final File mDataDir = Environment.getDataDirectory(); 100 101 private static final long mDowngradeUnusedAppsThresholdInMillis = 102 getDowngradeUnusedAppsThresholdInMillis(); 103 104 public static void schedule(Context context) { 105 if (isBackgroundDexoptDisabled()) { 106 return; 107 } 108 109 JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); 110 111 // Schedule a one-off job which scans installed packages and updates 112 // out-of-date oat files. 113 js.schedule(new JobInfo.Builder(JOB_POST_BOOT_UPDATE, sDexoptServiceName) 114 .setMinimumLatency(TimeUnit.MINUTES.toMillis(1)) 115 .setOverrideDeadline(TimeUnit.MINUTES.toMillis(1)) 116 .build()); 117 118 // Schedule a daily job which scans installed packages and compiles 119 // those with fresh profiling data. 120 js.schedule(new JobInfo.Builder(JOB_IDLE_OPTIMIZE, sDexoptServiceName) 121 .setRequiresDeviceIdle(true) 122 .setRequiresCharging(true) 123 .setPeriodic(IDLE_OPTIMIZATION_PERIOD) 124 .build()); 125 126 if (DEBUG_DEXOPT) { 127 Log.i(TAG, "Jobs scheduled"); 128 } 129 } 130 131 public static void notifyPackageChanged(String packageName) { 132 // The idle maintanance job skips packages which previously failed to 133 // compile. The given package has changed and may successfully compile 134 // now. Remove it from the list of known failing packages. 135 synchronized (sFailedPackageNamesPrimary) { 136 sFailedPackageNamesPrimary.remove(packageName); 137 } 138 synchronized (sFailedPackageNamesSecondary) { 139 sFailedPackageNamesSecondary.remove(packageName); 140 } 141 } 142 143 // Returns the current battery level as a 0-100 integer. 144 private int getBatteryLevel() { 145 IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 146 Intent intent = registerReceiver(null, filter); 147 int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 148 int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 149 150 if (level < 0 || scale <= 0) { 151 // Battery data unavailable. This should never happen, so assume the worst. 152 return 0; 153 } 154 155 return (100 * level / scale); 156 } 157 158 private long getLowStorageThreshold(Context context) { 159 @SuppressWarnings("deprecation") 160 final long lowThreshold = StorageManager.from(context).getStorageLowBytes(mDataDir); 161 if (lowThreshold == 0) { 162 Log.e(TAG, "Invalid low storage threshold"); 163 } 164 165 return lowThreshold; 166 } 167 168 private boolean runPostBootUpdate(final JobParameters jobParams, 169 final PackageManagerService pm, final ArraySet<String> pkgs) { 170 if (mExitPostBootUpdate.get()) { 171 // This job has already been superseded. Do not start it. 172 return false; 173 } 174 new Thread("BackgroundDexOptService_PostBootUpdate") { 175 @Override 176 public void run() { 177 postBootUpdate(jobParams, pm, pkgs); 178 } 179 180 }.start(); 181 return true; 182 } 183 184 private void postBootUpdate(JobParameters jobParams, PackageManagerService pm, 185 ArraySet<String> pkgs) { 186 // Load low battery threshold from the system config. This is a 0-100 integer. 187 final int lowBatteryThreshold = getResources().getInteger( 188 com.android.internal.R.integer.config_lowBatteryWarningLevel); 189 final long lowThreshold = getLowStorageThreshold(this); 190 191 mAbortPostBootUpdate.set(false); 192 193 ArraySet<String> updatedPackages = new ArraySet<>(); 194 for (String pkg : pkgs) { 195 if (mAbortPostBootUpdate.get()) { 196 // JobScheduler requested an early abort. 197 return; 198 } 199 if (mExitPostBootUpdate.get()) { 200 // Different job, which supersedes this one, is running. 201 break; 202 } 203 if (getBatteryLevel() < lowBatteryThreshold) { 204 // Rather bail than completely drain the battery. 205 break; 206 } 207 long usableSpace = mDataDir.getUsableSpace(); 208 if (usableSpace < lowThreshold) { 209 // Rather bail than completely fill up the disk. 210 Log.w(TAG, "Aborting background dex opt job due to low storage: " + 211 usableSpace); 212 break; 213 } 214 215 if (DEBUG_DEXOPT) { 216 Log.i(TAG, "Updating package " + pkg); 217 } 218 219 // Update package if needed. Note that there can be no race between concurrent 220 // jobs because PackageDexOptimizer.performDexOpt is synchronized. 221 222 // checkProfiles is false to avoid merging profiles during boot which 223 // might interfere with background compilation (b/28612421). 224 // Unfortunately this will also means that "pm.dexopt.boot=speed-profile" will 225 // behave differently than "pm.dexopt.bg-dexopt=speed-profile" but that's a 226 // trade-off worth doing to save boot time work. 227 int result = pm.performDexOptWithStatus(new DexoptOptions( 228 pkg, 229 PackageManagerService.REASON_BOOT, 230 DexoptOptions.DEXOPT_BOOT_COMPLETE)); 231 if (result == PackageDexOptimizer.DEX_OPT_PERFORMED) { 232 updatedPackages.add(pkg); 233 } 234 } 235 notifyPinService(updatedPackages); 236 // Ran to completion, so we abandon our timeslice and do not reschedule. 237 jobFinished(jobParams, /* reschedule */ false); 238 } 239 240 private boolean runIdleOptimization(final JobParameters jobParams, 241 final PackageManagerService pm, final ArraySet<String> pkgs) { 242 new Thread("BackgroundDexOptService_IdleOptimization") { 243 @Override 244 public void run() { 245 int result = idleOptimization(pm, pkgs, BackgroundDexOptService.this); 246 if (result != OPTIMIZE_ABORT_BY_JOB_SCHEDULER) { 247 Log.w(TAG, "Idle optimizations aborted because of space constraints."); 248 // If we didn't abort we ran to completion (or stopped because of space). 249 // Abandon our timeslice and do not reschedule. 250 jobFinished(jobParams, /* reschedule */ false); 251 } 252 } 253 }.start(); 254 return true; 255 } 256 257 // Optimize the given packages and return the optimization result (one of the OPTIMIZE_* codes). 258 private int idleOptimization(PackageManagerService pm, ArraySet<String> pkgs, 259 Context context) { 260 Log.i(TAG, "Performing idle optimizations"); 261 // If post-boot update is still running, request that it exits early. 262 mExitPostBootUpdate.set(true); 263 mAbortIdleOptimization.set(false); 264 265 long lowStorageThreshold = getLowStorageThreshold(context); 266 // Optimize primary apks. 267 int result = optimizePackages(pm, pkgs, lowStorageThreshold, /*is_for_primary_dex*/ true, 268 sFailedPackageNamesPrimary); 269 270 if (result == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) { 271 return result; 272 } 273 274 if (SystemProperties.getBoolean("dalvik.vm.dexopt.secondary", false)) { 275 result = reconcileSecondaryDexFiles(pm.getDexManager()); 276 if (result == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) { 277 return result; 278 } 279 280 result = optimizePackages(pm, pkgs, lowStorageThreshold, /*is_for_primary_dex*/ false, 281 sFailedPackageNamesSecondary); 282 } 283 return result; 284 } 285 286 private int optimizePackages(PackageManagerService pm, ArraySet<String> pkgs, 287 long lowStorageThreshold, boolean is_for_primary_dex, 288 ArraySet<String> failedPackageNames) { 289 ArraySet<String> updatedPackages = new ArraySet<>(); 290 Set<String> unusedPackages = pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis); 291 // Only downgrade apps when space is low on device. 292 // Threshold is selected above the lowStorageThreshold so that we can pro-actively clean 293 // up disk before user hits the actual lowStorageThreshold. 294 final long lowStorageThresholdForDowngrade = LOW_THRESHOLD_MULTIPLIER_FOR_DOWNGRADE * 295 lowStorageThreshold; 296 boolean shouldDowngrade = shouldDowngrade(lowStorageThresholdForDowngrade); 297 for (String pkg : pkgs) { 298 int abort_code = abortIdleOptimizations(lowStorageThreshold); 299 if (abort_code == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) { 300 return abort_code; 301 } 302 303 synchronized (failedPackageNames) { 304 if (failedPackageNames.contains(pkg)) { 305 // Skip previously failing package 306 continue; 307 } 308 } 309 310 int reason; 311 boolean downgrade; 312 // Downgrade unused packages. 313 if (unusedPackages.contains(pkg) && shouldDowngrade) { 314 // This applies for system apps or if packages location is not a directory, i.e. 315 // monolithic install. 316 if (is_for_primary_dex && !pm.canHaveOatDir(pkg)) { 317 // For apps that don't have the oat directory, instead of downgrading, 318 // remove their compiler artifacts from dalvik cache. 319 pm.deleteOatArtifactsOfPackage(pkg); 320 continue; 321 } else { 322 reason = PackageManagerService.REASON_INACTIVE_PACKAGE_DOWNGRADE; 323 downgrade = true; 324 } 325 } else if (abort_code != OPTIMIZE_ABORT_NO_SPACE_LEFT) { 326 reason = PackageManagerService.REASON_BACKGROUND_DEXOPT; 327 downgrade = false; 328 } else { 329 // can't dexopt because of low space. 330 continue; 331 } 332 333 synchronized (failedPackageNames) { 334 // Conservatively add package to the list of failing ones in case 335 // performDexOpt never returns. 336 failedPackageNames.add(pkg); 337 } 338 339 // Optimize package if needed. Note that there can be no race between 340 // concurrent jobs because PackageDexOptimizer.performDexOpt is synchronized. 341 boolean success; 342 int dexoptFlags = 343 DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES | 344 DexoptOptions.DEXOPT_BOOT_COMPLETE | 345 (downgrade ? DexoptOptions.DEXOPT_DOWNGRADE : 0) | 346 DexoptOptions.DEXOPT_IDLE_BACKGROUND_JOB; 347 if (is_for_primary_dex) { 348 int result = pm.performDexOptWithStatus(new DexoptOptions(pkg, reason, 349 dexoptFlags)); 350 success = result != PackageDexOptimizer.DEX_OPT_FAILED; 351 if (result == PackageDexOptimizer.DEX_OPT_PERFORMED) { 352 updatedPackages.add(pkg); 353 } 354 } else { 355 success = pm.performDexOpt(new DexoptOptions(pkg, 356 reason, dexoptFlags | DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX)); 357 } 358 if (success) { 359 // Dexopt succeeded, remove package from the list of failing ones. 360 synchronized (failedPackageNames) { 361 failedPackageNames.remove(pkg); 362 } 363 } 364 } 365 notifyPinService(updatedPackages); 366 return OPTIMIZE_PROCESSED; 367 } 368 369 private int reconcileSecondaryDexFiles(DexManager dm) { 370 // TODO(calin): should we blacklist packages for which we fail to reconcile? 371 for (String p : dm.getAllPackagesWithSecondaryDexFiles()) { 372 if (mAbortIdleOptimization.get()) { 373 return OPTIMIZE_ABORT_BY_JOB_SCHEDULER; 374 } 375 dm.reconcileSecondaryDexFiles(p); 376 } 377 return OPTIMIZE_PROCESSED; 378 } 379 380 // Evaluate whether or not idle optimizations should continue. 381 private int abortIdleOptimizations(long lowStorageThreshold) { 382 if (mAbortIdleOptimization.get()) { 383 // JobScheduler requested an early abort. 384 return OPTIMIZE_ABORT_BY_JOB_SCHEDULER; 385 } 386 long usableSpace = mDataDir.getUsableSpace(); 387 if (usableSpace < lowStorageThreshold) { 388 // Rather bail than completely fill up the disk. 389 Log.w(TAG, "Aborting background dex opt job due to low storage: " + usableSpace); 390 return OPTIMIZE_ABORT_NO_SPACE_LEFT; 391 } 392 393 return OPTIMIZE_CONTINUE; 394 } 395 396 // Evaluate whether apps should be downgraded. 397 private boolean shouldDowngrade(long lowStorageThresholdForDowngrade) { 398 long usableSpace = mDataDir.getUsableSpace(); 399 if (usableSpace < lowStorageThresholdForDowngrade) { 400 return true; 401 } 402 403 return false; 404 } 405 406 /** 407 * Execute idle optimizations immediately on packages in packageNames. If packageNames is null, 408 * then execute on all packages. 409 */ 410 public static boolean runIdleOptimizationsNow(PackageManagerService pm, Context context, 411 @Nullable List<String> packageNames) { 412 // Create a new object to make sure we don't interfere with the scheduled jobs. 413 // Note that this may still run at the same time with the job scheduled by the 414 // JobScheduler but the scheduler will not be able to cancel it. 415 BackgroundDexOptService bdos = new BackgroundDexOptService(); 416 ArraySet<String> packagesToOptimize; 417 if (packageNames == null) { 418 packagesToOptimize = pm.getOptimizablePackages(); 419 } else { 420 packagesToOptimize = new ArraySet<>(packageNames); 421 } 422 int result = bdos.idleOptimization(pm, packagesToOptimize, context); 423 return result == OPTIMIZE_PROCESSED; 424 } 425 426 @Override 427 public boolean onStartJob(JobParameters params) { 428 if (DEBUG_DEXOPT) { 429 Log.i(TAG, "onStartJob"); 430 } 431 432 // NOTE: PackageManagerService.isStorageLow uses a different set of criteria from 433 // the checks above. This check is not "live" - the value is determined by a background 434 // restart with a period of ~1 minute. 435 PackageManagerService pm = (PackageManagerService)ServiceManager.getService("package"); 436 if (pm.isStorageLow()) { 437 if (DEBUG_DEXOPT) { 438 Log.i(TAG, "Low storage, skipping this run"); 439 } 440 return false; 441 } 442 443 final ArraySet<String> pkgs = pm.getOptimizablePackages(); 444 if (pkgs.isEmpty()) { 445 if (DEBUG_DEXOPT) { 446 Log.i(TAG, "No packages to optimize"); 447 } 448 return false; 449 } 450 451 boolean result; 452 if (params.getJobId() == JOB_POST_BOOT_UPDATE) { 453 result = runPostBootUpdate(params, pm, pkgs); 454 } else { 455 result = runIdleOptimization(params, pm, pkgs); 456 } 457 458 return result; 459 } 460 461 @Override 462 public boolean onStopJob(JobParameters params) { 463 if (DEBUG_DEXOPT) { 464 Log.i(TAG, "onStopJob"); 465 } 466 467 if (params.getJobId() == JOB_POST_BOOT_UPDATE) { 468 mAbortPostBootUpdate.set(true); 469 } else { 470 mAbortIdleOptimization.set(true); 471 } 472 return false; 473 } 474 475 private void notifyPinService(ArraySet<String> updatedPackages) { 476 PinnerService pinnerService = LocalServices.getService(PinnerService.class); 477 if (pinnerService != null) { 478 Log.i(TAG, "Pinning optimized code " + updatedPackages); 479 pinnerService.update(updatedPackages); 480 } 481 } 482 483 private static long getDowngradeUnusedAppsThresholdInMillis() { 484 final String sysPropKey = "pm.dexopt.downgrade_after_inactive_days"; 485 String sysPropValue = SystemProperties.get(sysPropKey); 486 if (sysPropValue == null || sysPropValue.isEmpty()) { 487 Log.w(TAG, "SysProp " + sysPropKey + " not set"); 488 return Long.MAX_VALUE; 489 } 490 return TimeUnit.DAYS.toMillis(Long.parseLong(sysPropValue)); 491 } 492 493 private static boolean isBackgroundDexoptDisabled() { 494 return SystemProperties.getBoolean("pm.dexopt.disable_bg_dexopt" /* key */, 495 false /* default */); 496 } 497} 498