Merge "BackgroundDexOptService: Check for low storage conditions." into nyc-mr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
712dde29ff
@@ -28,10 +28,13 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
|
import android.os.Environment;
|
||||||
import android.os.ServiceManager;
|
import android.os.ServiceManager;
|
||||||
|
import android.os.storage.StorageManager;
|
||||||
import android.util.ArraySet;
|
import android.util.ArraySet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -66,6 +69,8 @@ public class BackgroundDexOptService extends JobService {
|
|||||||
*/
|
*/
|
||||||
final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false);
|
final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
private final File dataDir = Environment.getDataDirectory();
|
||||||
|
|
||||||
public static void schedule(Context context) {
|
public static void schedule(Context context) {
|
||||||
JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
|
JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
|
||||||
|
|
||||||
@@ -113,6 +118,16 @@ public class BackgroundDexOptService extends JobService {
|
|||||||
return (100 * level / scale);
|
return (100 * level / scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long getLowStorageThreshold() {
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
final long lowThreshold = StorageManager.from(this).getStorageLowBytes(dataDir);
|
||||||
|
if (lowThreshold == 0) {
|
||||||
|
Log.e(TAG, "Invalid low storage threshold");
|
||||||
|
}
|
||||||
|
|
||||||
|
return lowThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean runPostBootUpdate(final JobParameters jobParams,
|
private boolean runPostBootUpdate(final JobParameters jobParams,
|
||||||
final PackageManagerService pm, final ArraySet<String> pkgs) {
|
final PackageManagerService pm, final ArraySet<String> pkgs) {
|
||||||
if (mExitPostBootUpdate.get()) {
|
if (mExitPostBootUpdate.get()) {
|
||||||
@@ -124,6 +139,8 @@ public class BackgroundDexOptService extends JobService {
|
|||||||
final int lowBatteryThreshold = getResources().getInteger(
|
final int lowBatteryThreshold = getResources().getInteger(
|
||||||
com.android.internal.R.integer.config_lowBatteryWarningLevel);
|
com.android.internal.R.integer.config_lowBatteryWarningLevel);
|
||||||
|
|
||||||
|
final long lowThreshold = getLowStorageThreshold();
|
||||||
|
|
||||||
mAbortPostBootUpdate.set(false);
|
mAbortPostBootUpdate.set(false);
|
||||||
new Thread("BackgroundDexOptService_PostBootUpdate") {
|
new Thread("BackgroundDexOptService_PostBootUpdate") {
|
||||||
@Override
|
@Override
|
||||||
@@ -141,6 +158,14 @@ public class BackgroundDexOptService extends JobService {
|
|||||||
// Rather bail than completely drain the battery.
|
// Rather bail than completely drain the battery.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
long usableSpace = dataDir.getUsableSpace();
|
||||||
|
if (usableSpace < lowThreshold) {
|
||||||
|
// Rather bail than completely fill up the disk.
|
||||||
|
Log.w(TAG, "Aborting background dex opt job due to low storage: " +
|
||||||
|
usableSpace);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (DEBUG_DEXOPT) {
|
if (DEBUG_DEXOPT) {
|
||||||
Log.i(TAG, "Updating package " + pkg);
|
Log.i(TAG, "Updating package " + pkg);
|
||||||
}
|
}
|
||||||
@@ -171,6 +196,9 @@ public class BackgroundDexOptService extends JobService {
|
|||||||
mExitPostBootUpdate.set(true);
|
mExitPostBootUpdate.set(true);
|
||||||
|
|
||||||
mAbortIdleOptimization.set(false);
|
mAbortIdleOptimization.set(false);
|
||||||
|
|
||||||
|
final long lowThreshold = getLowStorageThreshold();
|
||||||
|
|
||||||
new Thread("BackgroundDexOptService_IdleOptimization") {
|
new Thread("BackgroundDexOptService_IdleOptimization") {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -183,6 +211,15 @@ public class BackgroundDexOptService extends JobService {
|
|||||||
// Skip previously failing package
|
// Skip previously failing package
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long usableSpace = dataDir.getUsableSpace();
|
||||||
|
if (usableSpace < lowThreshold) {
|
||||||
|
// Rather bail than completely fill up the disk.
|
||||||
|
Log.w(TAG, "Aborting background dex opt job due to low storage: " +
|
||||||
|
usableSpace);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Conservatively add package to the list of failing ones in case performDexOpt
|
// Conservatively add package to the list of failing ones in case performDexOpt
|
||||||
// never returns.
|
// never returns.
|
||||||
synchronized (sFailedPackageNames) {
|
synchronized (sFailedPackageNames) {
|
||||||
@@ -213,6 +250,9 @@ public class BackgroundDexOptService extends JobService {
|
|||||||
Log.i(TAG, "onStartJob");
|
Log.i(TAG, "onStartJob");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: PackageManagerService.isStorageLow uses a different set of criteria from
|
||||||
|
// the checks above. This check is not "live" - the value is determined by a background
|
||||||
|
// restart with a period of ~1 minute.
|
||||||
PackageManagerService pm = (PackageManagerService)ServiceManager.getService("package");
|
PackageManagerService pm = (PackageManagerService)ServiceManager.getService("package");
|
||||||
if (pm.isStorageLow()) {
|
if (pm.isStorageLow()) {
|
||||||
if (DEBUG_DEXOPT) {
|
if (DEBUG_DEXOPT) {
|
||||||
|
|||||||
Reference in New Issue
Block a user