diff options
author | Amith Yamasani <yamasani@google.com> | 2009-06-03 15:16:10 -0700 |
---|---|---|
committer | Amith Yamasani <yamasani@google.com> | 2009-06-03 15:38:24 -0700 |
commit | eaeb663bcd7a82b654954b42663232cbd7bef7e7 (patch) | |
tree | c9b62c0df637d7bd394aaf39e490641dadda9744 /services | |
parent | cede1ed3e1721dc4a697a540388ef0f4b51c60eb (diff) | |
download | frameworks_base-eaeb663bcd7a82b654954b42663232cbd7bef7e7.zip frameworks_base-eaeb663bcd7a82b654954b42663232cbd7bef7e7.tar.gz frameworks_base-eaeb663bcd7a82b654954b42663232cbd7bef7e7.tar.bz2 |
Track activity foreground CPU usage for battery stats.
Track the foreground CPU time of an activity so that we can tell if apps are
spending more time in the background compared to foreground.
Update power profile values for screen backlight and GPS.
Fix some javadoc bugs (milliseconds vs. microseconds).
Diffstat (limited to 'services')
3 files changed, 48 insertions, 2 deletions
diff --git a/services/java/com/android/server/ProcessStats.java b/services/java/com/android/server/ProcessStats.java index 55adabb..58f8980 100644 --- a/services/java/com/android/server/ProcessStats.java +++ b/services/java/com/android/server/ProcessStats.java @@ -54,7 +54,10 @@ public class ProcessStats { PROC_SPACE_TERM|PROC_OUT_LONG // 14: stime }; + /** Stores user time and system time in 100ths of a second. */ private final long[] mProcessStatsData = new long[2]; + /** Stores user time and system time in 100ths of a second. */ + private final long[] mSinglePidStatsData = new long[2]; private static final int[] PROCESS_FULL_STATS_FORMAT = new int[] { PROC_SPACE_TERM, @@ -418,7 +421,18 @@ public class ProcessStats { return pids; } - + + public long getCpuTimeForPid(int pid) { + final String statFile = "/proc/" + pid + "/stat"; + final long[] statsData = mSinglePidStatsData; + if (Process.readProcFile(statFile, PROCESS_STATS_FORMAT, + null, statsData, null)) { + long time = statsData[0] + statsData[1]; + return time; + } + return 0; + } + final public int getLastUserTime() { return mRelUserTime; } diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java index 3b26cb7..9650790 100644 --- a/services/java/com/android/server/am/ActivityManagerService.java +++ b/services/java/com/android/server/am/ActivityManagerService.java @@ -62,6 +62,7 @@ import android.content.pm.ServiceInfo; import android.content.res.Configuration; import android.graphics.Bitmap; import android.net.Uri; +import android.os.BatteryStats; import android.os.Binder; import android.os.Bundle; import android.os.Environment; @@ -1438,7 +1439,7 @@ public final class ActivityManagerService extends ActivityManagerNative implemen synchronized (mProcessStatsThread) { final long now = SystemClock.uptimeMillis(); boolean haveNewCpuStats = false; - + if (MONITOR_CPU_USAGE && mLastCpuTime < (now-MONITOR_CPU_MIN_TIME)) { mLastCpuTime = now; @@ -2063,6 +2064,25 @@ public final class ActivityManagerService extends ActivityManagerNative implemen if (prev != null) { prev.resumeKeyDispatchingLocked(); } + + if (prev.app != null && prev.cpuTimeAtResume > 0 && mBatteryStatsService.isOnBattery()) { + long diff = 0; + synchronized (mProcessStatsThread) { + diff = mProcessStats.getCpuTimeForPid(prev.app.pid) - prev.cpuTimeAtResume; + } + if (diff > 0) { + BatteryStatsImpl bsi = mBatteryStatsService.getActiveStatistics(); + synchronized (bsi) { + BatteryStatsImpl.Uid.Proc ps = + bsi.getProcessStatsLocked(prev.info.applicationInfo.uid, + prev.info.packageName); + if (ps != null) { + ps.addForegroundTimeLocked(diff); + } + } + } + } + prev.cpuTimeAtResume = 0; // reset it } /** @@ -2095,6 +2115,17 @@ public final class ActivityManagerService extends ActivityManagerNative implemen next.resumeKeyDispatchingLocked(); ensureActivitiesVisibleLocked(null, 0); mWindowManager.executeAppTransition(); + + // Mark the point when the activity is resuming + // TODO: To be more accurate, the mark should be before the onCreate, + // not after the onResume. But for subsequent starts, onResume is fine. + if (next.app != null) { + synchronized (mProcessStatsThread) { + next.cpuTimeAtResume = mProcessStats.getCpuTimeForPid(next.app.pid); + } + } else { + next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process + } } /** diff --git a/services/java/com/android/server/am/HistoryRecord.java b/services/java/com/android/server/am/HistoryRecord.java index 1789687..944ea02 100644 --- a/services/java/com/android/server/am/HistoryRecord.java +++ b/services/java/com/android/server/am/HistoryRecord.java @@ -66,6 +66,7 @@ class HistoryRecord extends IApplicationToken.Stub { int theme; // resource identifier of activity's theme. TaskRecord task; // the task this is in. long startTime; // when we starting launching this activity + long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity Configuration configuration; // configuration activity was last running in HistoryRecord resultTo; // who started this entry, so will get our reply final String resultWho; // additional identifier for use by resultTo. |