Merge commit '9af53ea6ef9a986bc65bcd11deb7994f1f4ba8ec' into eclair-mr2 * commit '9af53ea6ef9a986bc65bcd11deb7994f1f4ba8ec': when logging free space on /data, log /system and /cache as well
This commit is contained in:
@@ -43,8 +43,8 @@ import android.provider.Settings;
|
|||||||
/**
|
/**
|
||||||
* This class implements a service to monitor the amount of disk storage space
|
* This class implements a service to monitor the amount of disk storage space
|
||||||
* on the device. If the free storage on device is less than a tunable threshold value
|
* on the device. If the free storage on device is less than a tunable threshold value
|
||||||
* (default is 10%. this value is a gservices parameter) a low memory notification is
|
* (default is 10%. this value is a gservices parameter) a low memory notification is
|
||||||
* displayed to alert the user. If the user clicks on the low memory notification the
|
* displayed to alert the user. If the user clicks on the low memory notification the
|
||||||
* Application Manager application gets launched to let the user free storage space.
|
* Application Manager application gets launched to let the user free storage space.
|
||||||
* Event log events:
|
* Event log events:
|
||||||
* A low memory event with the free storage on device in bytes is logged to the event log
|
* A low memory event with the free storage on device in bytes is logged to the event log
|
||||||
@@ -68,32 +68,35 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
private static final int EVENT_LOG_FREE_STORAGE_LEFT = 2746;
|
private static final int EVENT_LOG_FREE_STORAGE_LEFT = 2746;
|
||||||
private static final long DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD = 2 * 1024 * 1024; // 2MB
|
private static final long DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD = 2 * 1024 * 1024; // 2MB
|
||||||
private static final long DEFAULT_CHECK_INTERVAL = MONITOR_INTERVAL*60*1000;
|
private static final long DEFAULT_CHECK_INTERVAL = MONITOR_INTERVAL*60*1000;
|
||||||
private long mFreeMem;
|
private long mFreeMem; // on /data
|
||||||
private long mLastReportedFreeMem;
|
private long mLastReportedFreeMem;
|
||||||
private long mLastReportedFreeMemTime;
|
private long mLastReportedFreeMemTime;
|
||||||
private boolean mLowMemFlag=false;
|
private boolean mLowMemFlag=false;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private ContentResolver mContentResolver;
|
private ContentResolver mContentResolver;
|
||||||
long mBlkSize;
|
private long mTotalMemory; // on /data
|
||||||
long mTotalMemory;
|
private StatFs mDataFileStats;
|
||||||
StatFs mFileStats;
|
private StatFs mSystemFileStats;
|
||||||
private static final String DATA_PATH="/data";
|
private StatFs mCacheFileStats;
|
||||||
long mThreadStartTime = -1;
|
private static final String DATA_PATH = "/data";
|
||||||
boolean mClearSucceeded = false;
|
private static final String SYSTEM_PATH = "/system";
|
||||||
boolean mClearingCache;
|
private static final String CACHE_PATH = "/cache";
|
||||||
|
private long mThreadStartTime = -1;
|
||||||
|
private boolean mClearSucceeded = false;
|
||||||
|
private boolean mClearingCache;
|
||||||
private Intent mStorageLowIntent;
|
private Intent mStorageLowIntent;
|
||||||
private Intent mStorageOkIntent;
|
private Intent mStorageOkIntent;
|
||||||
private CachePackageDataObserver mClearCacheObserver;
|
private CachePackageDataObserver mClearCacheObserver;
|
||||||
private static final int _TRUE = 1;
|
private static final int _TRUE = 1;
|
||||||
private static final int _FALSE = 0;
|
private static final int _FALSE = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This string is used for ServiceManager access to this class.
|
* This string is used for ServiceManager access to this class.
|
||||||
*/
|
*/
|
||||||
static final String SERVICE = "devicestoragemonitor";
|
static final String SERVICE = "devicestoragemonitor";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler that checks the amount of disk space on the device and sends a
|
* Handler that checks the amount of disk space on the device and sends a
|
||||||
* notification if the device runs low on disk space
|
* notification if the device runs low on disk space
|
||||||
*/
|
*/
|
||||||
Handler mHandler = new Handler() {
|
Handler mHandler = new Handler() {
|
||||||
@@ -107,7 +110,7 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
checkMemory(msg.arg1 == _TRUE);
|
checkMemory(msg.arg1 == _TRUE);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class CachePackageDataObserver extends IPackageDataObserver.Stub {
|
class CachePackageDataObserver extends IPackageDataObserver.Stub {
|
||||||
public void onRemoveCompleted(String packageName, boolean succeeded) {
|
public void onRemoveCompleted(String packageName, boolean succeeded) {
|
||||||
mClearSucceeded = succeeded;
|
mClearSucceeded = succeeded;
|
||||||
@@ -115,12 +118,17 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
if(localLOGV) Log.i(TAG, " Clear succeeded:"+mClearSucceeded
|
if(localLOGV) Log.i(TAG, " Clear succeeded:"+mClearSucceeded
|
||||||
+", mClearingCache:"+mClearingCache+" Forcing memory check");
|
+", mClearingCache:"+mClearingCache+" Forcing memory check");
|
||||||
postCheckMemoryMsg(false, 0);
|
postCheckMemoryMsg(false, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final void restatDataDir() {
|
private final void restatDataDir() {
|
||||||
mFileStats.restat(DATA_PATH);
|
try {
|
||||||
mFreeMem = mFileStats.getAvailableBlocks()*mBlkSize;
|
mDataFileStats.restat(DATA_PATH);
|
||||||
|
mFreeMem = (long) mDataFileStats.getAvailableBlocks() *
|
||||||
|
mDataFileStats.getBlockSize();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// use the old value of mFreeMem
|
||||||
|
}
|
||||||
// Allow freemem to be overridden by debug.freemem for testing
|
// Allow freemem to be overridden by debug.freemem for testing
|
||||||
String debugFreeMem = SystemProperties.get("debug.freemem");
|
String debugFreeMem = SystemProperties.get("debug.freemem");
|
||||||
if (!"".equals(debugFreeMem)) {
|
if (!"".equals(debugFreeMem)) {
|
||||||
@@ -132,10 +140,27 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES)*60*1000;
|
DEFAULT_FREE_STORAGE_LOG_INTERVAL_IN_MINUTES)*60*1000;
|
||||||
//log the amount of free memory in event log
|
//log the amount of free memory in event log
|
||||||
long currTime = SystemClock.elapsedRealtime();
|
long currTime = SystemClock.elapsedRealtime();
|
||||||
if((mLastReportedFreeMemTime == 0) ||
|
if((mLastReportedFreeMemTime == 0) ||
|
||||||
(currTime-mLastReportedFreeMemTime) >= freeMemLogInterval) {
|
(currTime-mLastReportedFreeMemTime) >= freeMemLogInterval) {
|
||||||
mLastReportedFreeMemTime = currTime;
|
mLastReportedFreeMemTime = currTime;
|
||||||
EventLog.writeEvent(EVENT_LOG_FREE_STORAGE_LEFT, mFreeMem);
|
long mFreeSystem = -1, mFreeCache = -1;
|
||||||
|
try {
|
||||||
|
mSystemFileStats.restat(SYSTEM_PATH);
|
||||||
|
mFreeSystem = (long) mSystemFileStats.getAvailableBlocks() *
|
||||||
|
mSystemFileStats.getBlockSize();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// ignore; report -1
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
mCacheFileStats.restat(CACHE_PATH);
|
||||||
|
mFreeCache = (long) mCacheFileStats.getAvailableBlocks() *
|
||||||
|
mCacheFileStats.getBlockSize();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// ignore; report -1
|
||||||
|
}
|
||||||
|
mCacheFileStats.restat(CACHE_PATH);
|
||||||
|
EventLog.writeEvent(EVENT_LOG_FREE_STORAGE_LEFT,
|
||||||
|
mFreeMem, mFreeSystem, mFreeCache);
|
||||||
}
|
}
|
||||||
// Read the reporting threshold from Gservices
|
// Read the reporting threshold from Gservices
|
||||||
long threshold = Gservices.getLong(mContentResolver,
|
long threshold = Gservices.getLong(mContentResolver,
|
||||||
@@ -148,7 +173,7 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
EventLog.writeEvent(EVENT_LOG_STORAGE_BELOW_THRESHOLD, mFreeMem);
|
EventLog.writeEvent(EVENT_LOG_STORAGE_BELOW_THRESHOLD, mFreeMem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final void clearCache() {
|
private final void clearCache() {
|
||||||
if (mClearCacheObserver == null) {
|
if (mClearCacheObserver == null) {
|
||||||
// Lazy instantiation
|
// Lazy instantiation
|
||||||
@@ -165,10 +190,10 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
mClearSucceeded = false;
|
mClearSucceeded = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final void checkMemory(boolean checkCache) {
|
private final void checkMemory(boolean checkCache) {
|
||||||
//if the thread that was started to clear cache is still running do nothing till its
|
//if the thread that was started to clear cache is still running do nothing till its
|
||||||
//finished clearing cache. Ideally this flag could be modified by clearCache
|
//finished clearing cache. Ideally this flag could be modified by clearCache
|
||||||
// and should be accessed via a lock but even if it does this test will fail now and
|
// and should be accessed via a lock but even if it does this test will fail now and
|
||||||
//hopefully the next time this flag will be set to the correct value.
|
//hopefully the next time this flag will be set to the correct value.
|
||||||
if(mClearingCache) {
|
if(mClearingCache) {
|
||||||
@@ -177,11 +202,11 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
long diffTime = System.currentTimeMillis() - mThreadStartTime;
|
long diffTime = System.currentTimeMillis() - mThreadStartTime;
|
||||||
if(diffTime > (10*60*1000)) {
|
if(diffTime > (10*60*1000)) {
|
||||||
Log.w(TAG, "Thread that clears cache file seems to run for ever");
|
Log.w(TAG, "Thread that clears cache file seems to run for ever");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
restatDataDir();
|
restatDataDir();
|
||||||
if (localLOGV) Log.v(TAG, "freeMemory="+mFreeMem);
|
if (localLOGV) Log.v(TAG, "freeMemory="+mFreeMem);
|
||||||
|
|
||||||
//post intent to NotificationManager to display icon if necessary
|
//post intent to NotificationManager to display icon if necessary
|
||||||
long memThreshold = getMemThreshold();
|
long memThreshold = getMemThreshold();
|
||||||
if (mFreeMem < memThreshold) {
|
if (mFreeMem < memThreshold) {
|
||||||
@@ -214,7 +239,7 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
//keep posting messages to itself periodically
|
//keep posting messages to itself periodically
|
||||||
postCheckMemoryMsg(true, DEFAULT_CHECK_INTERVAL);
|
postCheckMemoryMsg(true, DEFAULT_CHECK_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postCheckMemoryMsg(boolean clearCache, long delay) {
|
private void postCheckMemoryMsg(boolean clearCache, long delay) {
|
||||||
// Remove queued messages
|
// Remove queued messages
|
||||||
mHandler.removeMessages(DEVICE_MEMORY_WHAT);
|
mHandler.removeMessages(DEVICE_MEMORY_WHAT);
|
||||||
@@ -222,16 +247,16 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
clearCache ?_TRUE : _FALSE, 0),
|
clearCache ?_TRUE : _FALSE, 0),
|
||||||
delay);
|
delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* just query settings to retrieve the memory threshold.
|
* just query settings to retrieve the memory threshold.
|
||||||
* Preferred this over using a ContentObserver since Settings.Gservices caches the value
|
* Preferred this over using a ContentObserver since Settings.Gservices caches the value
|
||||||
* any way
|
* any way
|
||||||
*/
|
*/
|
||||||
private long getMemThreshold() {
|
private long getMemThreshold() {
|
||||||
int value = Settings.Gservices.getInt(
|
int value = Settings.Gservices.getInt(
|
||||||
mContentResolver,
|
mContentResolver,
|
||||||
Settings.Gservices.SYS_STORAGE_THRESHOLD_PERCENTAGE,
|
Settings.Gservices.SYS_STORAGE_THRESHOLD_PERCENTAGE,
|
||||||
DEFAULT_THRESHOLD_PERCENTAGE);
|
DEFAULT_THRESHOLD_PERCENTAGE);
|
||||||
if(localLOGV) Log.v(TAG, "Threshold Percentage="+value);
|
if(localLOGV) Log.v(TAG, "Threshold Percentage="+value);
|
||||||
//evaluate threshold value
|
//evaluate threshold value
|
||||||
@@ -247,16 +272,17 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
mContext = context;
|
mContext = context;
|
||||||
mContentResolver = mContext.getContentResolver();
|
mContentResolver = mContext.getContentResolver();
|
||||||
//create StatFs object
|
//create StatFs object
|
||||||
mFileStats = new StatFs(DATA_PATH);
|
mDataFileStats = new StatFs(DATA_PATH);
|
||||||
//initialize block size
|
mSystemFileStats = new StatFs(SYSTEM_PATH);
|
||||||
mBlkSize = mFileStats.getBlockSize();
|
mCacheFileStats = new StatFs(CACHE_PATH);
|
||||||
//initialize total storage on device
|
//initialize total storage on device
|
||||||
mTotalMemory = ((long)mFileStats.getBlockCount()*mBlkSize)/100L;
|
mTotalMemory = ((long)mDataFileStats.getBlockCount() *
|
||||||
|
mDataFileStats.getBlockSize())/100L;
|
||||||
mStorageLowIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_LOW);
|
mStorageLowIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_LOW);
|
||||||
mStorageOkIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_OK);
|
mStorageOkIntent = new Intent(Intent.ACTION_DEVICE_STORAGE_OK);
|
||||||
checkMemory(true);
|
checkMemory(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method sends a notification to NotificationManager to display
|
* This method sends a notification to NotificationManager to display
|
||||||
@@ -271,7 +297,7 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
Intent lowMemIntent = new Intent(Intent.ACTION_MANAGE_PACKAGE_STORAGE);
|
Intent lowMemIntent = new Intent(Intent.ACTION_MANAGE_PACKAGE_STORAGE);
|
||||||
lowMemIntent.putExtra("memory", mFreeMem);
|
lowMemIntent.putExtra("memory", mFreeMem);
|
||||||
lowMemIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
lowMemIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
NotificationManager mNotificationMgr =
|
NotificationManager mNotificationMgr =
|
||||||
(NotificationManager)mContext.getSystemService(
|
(NotificationManager)mContext.getSystemService(
|
||||||
Context.NOTIFICATION_SERVICE);
|
Context.NOTIFICATION_SERVICE);
|
||||||
CharSequence title = mContext.getText(
|
CharSequence title = mContext.getText(
|
||||||
@@ -302,7 +328,7 @@ class DeviceStorageMonitorService extends Binder {
|
|||||||
mContext.removeStickyBroadcast(mStorageLowIntent);
|
mContext.removeStickyBroadcast(mStorageLowIntent);
|
||||||
mContext.sendBroadcast(mStorageOkIntent);
|
mContext.sendBroadcast(mStorageOkIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateMemory() {
|
public void updateMemory() {
|
||||||
int callingUid = getCallingUid();
|
int callingUid = getCallingUid();
|
||||||
if(callingUid != Process.SYSTEM_UID) {
|
if(callingUid != Process.SYSTEM_UID) {
|
||||||
|
|||||||
Reference in New Issue
Block a user