use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by ParanoidAndroid.
the class ActivityManagerService method newProcessRecordLocked.
// =========================================================
// GLOBAL MANAGEMENT
// =========================================================
final ProcessRecord newProcessRecordLocked(IApplicationThread thread, ApplicationInfo info, String customProcess, boolean isolated) {
String proc = customProcess != null ? customProcess : info.processName;
BatteryStatsImpl.Uid.Proc ps = null;
BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
int uid = info.uid;
if (isolated) {
int userId = UserHandle.getUserId(uid);
int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1;
uid = 0;
while (true) {
if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) {
mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID;
}
uid = UserHandle.getUid(userId, mNextIsolatedProcessUid);
mNextIsolatedProcessUid++;
if (mIsolatedProcesses.indexOfKey(uid) < 0) {
// No process for this uid, use it.
break;
}
stepsLeft--;
if (stepsLeft <= 0) {
return null;
}
}
}
synchronized (stats) {
ps = stats.getProcessStatsLocked(info.uid, proc);
}
return new ProcessRecord(ps, thread, info, proc, uid);
}
use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by ParanoidAndroid.
the class ActivityManagerService method checkExcessivePowerUsageLocked.
final void checkExcessivePowerUsageLocked(boolean doKills) {
updateCpuStatsNow();
BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
boolean doWakeKills = doKills;
boolean doCpuKills = doKills;
if (mLastPowerCheckRealtime == 0) {
doWakeKills = false;
}
if (mLastPowerCheckUptime == 0) {
doCpuKills = false;
}
if (stats.isScreenOn()) {
doWakeKills = false;
}
final long curRealtime = SystemClock.elapsedRealtime();
final long realtimeSince = curRealtime - mLastPowerCheckRealtime;
final long curUptime = SystemClock.uptimeMillis();
final long uptimeSince = curUptime - mLastPowerCheckUptime;
mLastPowerCheckRealtime = curRealtime;
mLastPowerCheckUptime = curUptime;
if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) {
doWakeKills = false;
}
if (uptimeSince < CPU_MIN_CHECK_DURATION) {
doCpuKills = false;
}
int i = mLruProcesses.size();
while (i > 0) {
i--;
ProcessRecord app = mLruProcesses.get(i);
if (!app.keeping) {
long wtime;
synchronized (stats) {
wtime = stats.getProcessWakeTime(app.info.uid, app.pid, curRealtime);
}
long wtimeUsed = wtime - app.lastWakeTime;
long cputimeUsed = app.curCpuTime - app.lastCpuTime;
if (DEBUG_POWER) {
StringBuilder sb = new StringBuilder(128);
sb.append("Wake for ");
app.toShortString(sb);
sb.append(": over ");
TimeUtils.formatDuration(realtimeSince, sb);
sb.append(" used ");
TimeUtils.formatDuration(wtimeUsed, sb);
sb.append(" (");
sb.append((wtimeUsed * 100) / realtimeSince);
sb.append("%)");
Slog.i(TAG, sb.toString());
sb.setLength(0);
sb.append("CPU for ");
app.toShortString(sb);
sb.append(": over ");
TimeUtils.formatDuration(uptimeSince, sb);
sb.append(" used ");
TimeUtils.formatDuration(cputimeUsed, sb);
sb.append(" (");
sb.append((cputimeUsed * 100) / uptimeSince);
sb.append("%)");
Slog.i(TAG, sb.toString());
}
// that sounds bad. Kill!
if (doWakeKills && realtimeSince > 0 && ((wtimeUsed * 100) / realtimeSince) >= 50) {
synchronized (stats) {
stats.reportExcessiveWakeLocked(app.info.uid, app.processName, realtimeSince, wtimeUsed);
}
Slog.w(TAG, "Excessive wake lock in " + app.processName + " (pid " + app.pid + "): held " + wtimeUsed + " during " + realtimeSince);
EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, app.processName, app.setAdj, "excessive wake lock");
Process.killProcessQuiet(app.pid);
} else if (doCpuKills && uptimeSince > 0 && ((cputimeUsed * 100) / uptimeSince) >= 50) {
synchronized (stats) {
stats.reportExcessiveCpuLocked(app.info.uid, app.processName, uptimeSince, cputimeUsed);
}
Slog.w(TAG, "Excessive CPU in " + app.processName + " (pid " + app.pid + "): used " + cputimeUsed + " during " + uptimeSince);
EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, app.processName, app.setAdj, "excessive cpu");
Process.killProcessQuiet(app.pid);
} else {
app.lastWakeTime = wtime;
app.lastCpuTime = app.curCpuTime;
}
}
}
}
use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by ParanoidAndroid.
the class ActivityStack method completePauseLocked.
private final void completePauseLocked() {
ActivityRecord prev = mPausingActivity;
if (DEBUG_PAUSE)
Slog.v(TAG, "Complete pause: " + prev);
if (prev != null) {
if (prev.finishing) {
if (DEBUG_PAUSE)
Slog.v(TAG, "Executing finish of activity: " + prev);
prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
} else if (prev.app != null) {
if (DEBUG_PAUSE)
Slog.v(TAG, "Enqueueing pending stop: " + prev);
if (prev.waitingVisible) {
prev.waitingVisible = false;
mWaitingVisibleActivities.remove(prev);
if (DEBUG_SWITCH || DEBUG_PAUSE)
Slog.v(TAG, "Complete pause, no longer waiting: " + prev);
}
if (prev.configDestroy) {
// the current instance before starting the new one.
if (DEBUG_PAUSE)
Slog.v(TAG, "Destroying after pause: " + prev);
destroyActivityLocked(prev, true, false, "pause-config");
} else {
mStoppingActivities.add(prev);
if (mStoppingActivities.size() > 3) {
// them out.
if (DEBUG_PAUSE)
Slog.v(TAG, "To many pending stops, forcing idle");
scheduleIdleLocked();
} else {
checkReadyForSleepLocked();
}
}
} else {
if (DEBUG_PAUSE)
Slog.v(TAG, "App died during pause, not stopping: " + prev);
prev = null;
}
mPausingActivity = null;
}
if (!mService.isSleeping()) {
resumeTopActivityLocked(prev);
} else {
checkReadyForSleepLocked();
ActivityRecord top = topRunningActivityLocked(null);
if (top == null || (prev != null && top != prev)) {
// If there are no more activities available to run,
// do resume anyway to start something. Also if the top
// activity on the stack is not the just paused activity,
// we need to go ahead and resume it to ensure we complete
// an in-flight app switch.
resumeTopActivityLocked(null);
}
}
if (prev != null) {
prev.resumeKeyDispatchingLocked();
}
if (prev.app != null && prev.cpuTimeAtResume > 0 && mService.mBatteryStatsService.isOnBattery()) {
long diff = 0;
synchronized (mService.mProcessStatsThread) {
diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid) - prev.cpuTimeAtResume;
}
if (diff > 0) {
BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
synchronized (bsi) {
BatteryStatsImpl.Uid.Proc ps = bsi.getProcessStatsLocked(prev.info.applicationInfo.uid, prev.info.packageName);
if (ps != null) {
ps.addForegroundTimeLocked(diff);
}
}
}
}
// reset it
prev.cpuTimeAtResume = 0;
}
use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by ParanoidAndroid.
the class ActivityManagerService method updateCpuStatsNow.
void updateCpuStatsNow() {
synchronized (mProcessStatsThread) {
mProcessStatsMutexFree.set(false);
final long now = SystemClock.uptimeMillis();
boolean haveNewCpuStats = false;
if (MONITOR_CPU_USAGE && mLastCpuTime.get() < (now - MONITOR_CPU_MIN_TIME)) {
mLastCpuTime.set(now);
haveNewCpuStats = true;
mProcessStats.update();
// Slog the cpu usage if the property is set.
if ("true".equals(SystemProperties.get("events.cpu"))) {
int user = mProcessStats.getLastUserTime();
int system = mProcessStats.getLastSystemTime();
int iowait = mProcessStats.getLastIoWaitTime();
int irq = mProcessStats.getLastIrqTime();
int softIrq = mProcessStats.getLastSoftIrqTime();
int idle = mProcessStats.getLastIdleTime();
int total = user + system + iowait + irq + softIrq + idle;
if (total == 0)
total = 1;
EventLog.writeEvent(EventLogTags.CPU, ((user + system + iowait + irq + softIrq) * 100) / total, (user * 100) / total, (system * 100) / total, (iowait * 100) / total, (irq * 100) / total, (softIrq * 100) / total);
}
}
long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes();
final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
synchronized (bstats) {
synchronized (mPidsSelfLocked) {
if (haveNewCpuStats) {
if (mOnBattery) {
int perc = bstats.startAddingCpuLocked();
int totalUTime = 0;
int totalSTime = 0;
final int N = mProcessStats.countStats();
for (int i = 0; i < N; i++) {
ProcessStats.Stats st = mProcessStats.getStats(i);
if (!st.working) {
continue;
}
ProcessRecord pr = mPidsSelfLocked.get(st.pid);
int otherUTime = (st.rel_utime * perc) / 100;
int otherSTime = (st.rel_stime * perc) / 100;
totalUTime += otherUTime;
totalSTime += otherSTime;
if (pr != null) {
BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
ps.addCpuTimeLocked(st.rel_utime - otherUTime, st.rel_stime - otherSTime);
ps.addSpeedStepTimes(cpuSpeedTimes);
pr.curCpuTime += (st.rel_utime + st.rel_stime) * 10;
} else {
BatteryStatsImpl.Uid.Proc ps = bstats.getProcessStatsLocked(st.name, st.pid);
if (ps != null) {
ps.addCpuTimeLocked(st.rel_utime - otherUTime, st.rel_stime - otherSTime);
ps.addSpeedStepTimes(cpuSpeedTimes);
}
}
}
bstats.finishAddingCpuLocked(perc, totalUTime, totalSTime, cpuSpeedTimes);
}
}
}
if (mLastWriteTime < (now - BATTERY_STATS_TIME)) {
mLastWriteTime = now;
mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
}
}
}
}
use of com.android.internal.os.BatteryStatsImpl in project platform_frameworks_base by android.
the class ActivityStack method completePauseLocked.
private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
ActivityRecord prev = mPausingActivity;
if (DEBUG_PAUSE)
Slog.v(TAG_PAUSE, "Complete pause: " + prev);
if (prev != null) {
final boolean wasStopping = prev.state == ActivityState.STOPPING;
prev.state = ActivityState.PAUSED;
if (prev.finishing) {
if (DEBUG_PAUSE)
Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
} else if (prev.app != null) {
if (DEBUG_PAUSE)
Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev + " wasStopping=" + wasStopping + " visible=" + prev.visible);
if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
if (DEBUG_SWITCH || DEBUG_PAUSE)
Slog.v(TAG_PAUSE, "Complete pause, no longer waiting: " + prev);
}
if (prev.deferRelaunchUntilPaused) {
// Complete the deferred relaunch that was waiting for pause to complete.
if (DEBUG_PAUSE)
Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
relaunchActivityLocked(prev, prev.configChangeFlags, false, prev.preserveWindowOnDeferredRelaunch);
} else if (wasStopping) {
// We are also stopping, the stop request must have gone soon after the pause.
// We can't clobber it, because the stop confirmation will not be handled.
// We don't need to schedule another stop, we only need to let it happen.
prev.state = ActivityState.STOPPING;
} else if ((!prev.visible && !hasVisibleBehindActivity()) || mService.isSleepingOrShuttingDownLocked()) {
// If we were visible then resumeTopActivities will release resources before
// stopping.
addToStopping(prev, true);
}
} else {
if (DEBUG_PAUSE)
Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
prev = null;
}
// since it is no longer visible.
if (prev != null) {
prev.stopFreezingScreenLocked(true);
}
mPausingActivity = null;
}
if (resumeNext) {
final ActivityStack topStack = mStackSupervisor.getFocusedStack();
if (!mService.isSleepingOrShuttingDownLocked()) {
mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
} else {
mStackSupervisor.checkReadyForSleepLocked();
ActivityRecord top = topStack.topRunningActivityLocked();
if (top == null || (prev != null && top != prev)) {
// If there are no more activities available to run, do resume anyway to start
// something. Also if the top activity on the stack is not the just paused
// activity, we need to go ahead and resume it to ensure we complete an
// in-flight app switch.
mStackSupervisor.resumeFocusedStackTopActivityLocked();
}
}
}
if (prev != null) {
prev.resumeKeyDispatchingLocked();
if (prev.app != null && prev.cpuTimeAtResume > 0 && mService.mBatteryStatsService.isOnBattery()) {
long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid) - prev.cpuTimeAtResume;
if (diff > 0) {
BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
synchronized (bsi) {
BatteryStatsImpl.Uid.Proc ps = bsi.getProcessStatsLocked(prev.info.applicationInfo.uid, prev.info.packageName);
if (ps != null) {
ps.addForegroundTimeLocked(diff);
}
}
}
}
// reset it
prev.cpuTimeAtResume = 0;
}
// task stack changes, because its positioning may depend on it.
if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause || mService.mStackSupervisor.getStack(PINNED_STACK_ID) != null) {
mService.notifyTaskStackChangedLocked();
mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
}
mStackSupervisor.ensureActivitiesVisibleLocked(resuming, 0, !PRESERVE_WINDOWS);
}
Aggregations