Search in sources :

Example 16 with BatteryStatsImpl

use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by ResurrectionRemix.

the class BatteryStatsService method dump.

@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
        pw.println("Permission Denial: can't dump BatteryStats from from pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() + " without permission " + android.Manifest.permission.DUMP);
        return;
    }
    int flags = 0;
    boolean useCheckinFormat = false;
    boolean isRealCheckin = false;
    boolean noOutput = false;
    boolean writeData = false;
    long historyStart = -1;
    int reqUid = -1;
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if ("--checkin".equals(arg)) {
                useCheckinFormat = true;
                isRealCheckin = true;
            } else if ("--history".equals(arg)) {
                flags |= BatteryStats.DUMP_HISTORY_ONLY;
            } else if ("--history-start".equals(arg)) {
                flags |= BatteryStats.DUMP_HISTORY_ONLY;
                i++;
                if (i >= args.length) {
                    pw.println("Missing time argument for --history-since");
                    dumpHelp(pw);
                    return;
                }
                historyStart = Long.parseLong(args[i]);
                writeData = true;
            } else if ("-c".equals(arg)) {
                useCheckinFormat = true;
                flags |= BatteryStats.DUMP_INCLUDE_HISTORY;
            } else if ("--charged".equals(arg)) {
                flags |= BatteryStats.DUMP_CHARGED_ONLY;
            } else if ("--daily".equals(arg)) {
                flags |= BatteryStats.DUMP_DAILY_ONLY;
            } else if ("--reset".equals(arg)) {
                synchronized (mStats) {
                    mStats.resetAllStatsCmdLocked();
                    pw.println("Battery stats reset.");
                    noOutput = true;
                }
                updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
            } else if ("--write".equals(arg)) {
                updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
                synchronized (mStats) {
                    mStats.writeSyncLocked();
                    pw.println("Battery stats written.");
                    noOutput = true;
                }
            } else if ("--new-daily".equals(arg)) {
                synchronized (mStats) {
                    mStats.recordDailyStatsLocked();
                    pw.println("New daily stats written.");
                    noOutput = true;
                }
            } else if ("--read-daily".equals(arg)) {
                synchronized (mStats) {
                    mStats.readDailyStatsLocked();
                    pw.println("Last daily stats read.");
                    noOutput = true;
                }
            } else if ("--enable".equals(arg) || "enable".equals(arg)) {
                i = doEnableOrDisable(pw, i, args, true);
                if (i < 0) {
                    return;
                }
                pw.println("Enabled: " + args[i]);
                return;
            } else if ("--disable".equals(arg) || "disable".equals(arg)) {
                i = doEnableOrDisable(pw, i, args, false);
                if (i < 0) {
                    return;
                }
                pw.println("Disabled: " + args[i]);
                return;
            } else if ("-h".equals(arg)) {
                dumpHelp(pw);
                return;
            } else if ("-a".equals(arg)) {
                flags |= BatteryStats.DUMP_VERBOSE;
            } else if (arg.length() > 0 && arg.charAt(0) == '-') {
                pw.println("Unknown option: " + arg);
                dumpHelp(pw);
                return;
            } else {
                // Not an option, last argument must be a package name.
                try {
                    reqUid = mContext.getPackageManager().getPackageUidAsUser(arg, UserHandle.getCallingUserId());
                } catch (PackageManager.NameNotFoundException e) {
                    pw.println("Unknown package: " + arg);
                    dumpHelp(pw);
                    return;
                }
            }
        }
    }
    if (noOutput) {
        return;
    }
    long ident = Binder.clearCallingIdentity();
    try {
        if (BatteryStatsHelper.checkWifiOnly(mContext)) {
            flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY;
        }
        // Fetch data from external sources and update the BatteryStatsImpl object with them.
        updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
    if (reqUid >= 0) {
        // we only dump the aggregated data since charged.
        if ((flags & (BatteryStats.DUMP_HISTORY_ONLY | BatteryStats.DUMP_CHARGED_ONLY)) == 0) {
            flags |= BatteryStats.DUMP_CHARGED_ONLY;
            // Also if they are doing -c, we don't want history.
            flags &= ~BatteryStats.DUMP_INCLUDE_HISTORY;
        }
    }
    if (useCheckinFormat) {
        List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_ALL);
        if (isRealCheckin) {
            // file if there is one.
            synchronized (mStats.mCheckinFile) {
                if (mStats.mCheckinFile.exists()) {
                    try {
                        byte[] raw = mStats.mCheckinFile.readFully();
                        if (raw != null) {
                            Parcel in = Parcel.obtain();
                            in.unmarshall(raw, 0, raw.length);
                            in.setDataPosition(0);
                            BatteryStatsImpl checkinStats = new BatteryStatsImpl(null, mStats.mHandler, null);
                            checkinStats.readSummaryFromParcel(in);
                            in.recycle();
                            checkinStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
                            mStats.mCheckinFile.delete();
                            return;
                        }
                    } catch (IOException | ParcelFormatException e) {
                        Slog.w(TAG, "Failure reading checkin file " + mStats.mCheckinFile.getBaseFile(), e);
                    }
                }
            }
        }
        synchronized (mStats) {
            mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
            if (writeData) {
                mStats.writeAsyncLocked();
            }
        }
    } else {
        synchronized (mStats) {
            mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart);
            if (writeData) {
                mStats.writeAsyncLocked();
            }
        }
    }
}
Also used : ParcelFormatException(android.os.ParcelFormatException) Parcel(android.os.Parcel) ApplicationInfo(android.content.pm.ApplicationInfo) IOException(java.io.IOException) BatteryStatsImpl(com.android.internal.os.BatteryStatsImpl) PackageManager(android.content.pm.PackageManager)

Example 17 with BatteryStatsImpl

use of com.android.internal.os.BatteryStatsImpl in project cornerstone by Onskreen.

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.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.pid, app.processName, app.setAdj, "excessive cpu");
                Process.killProcessQuiet(app.pid);
            } else {
                app.lastWakeTime = wtime;
                app.lastCpuTime = app.curCpuTime;
            }
        }
    }
}
Also used : BatteryStatsImpl(com.android.internal.os.BatteryStatsImpl)

Example 18 with BatteryStatsImpl

use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by DirtyUnicorns.

the class ActivityManagerService method dumpProcessOomList.

private static final boolean dumpProcessOomList(PrintWriter pw, ActivityManagerService service, List<ProcessRecord> origList, String prefix, String normalLabel, String persistentLabel, boolean inclDetails, String dumpPackage) {
    ArrayList<Pair<ProcessRecord, Integer>> list = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size());
    for (int i = 0; i < origList.size(); i++) {
        ProcessRecord r = origList.get(i);
        if (dumpPackage != null && !r.pkgList.containsKey(dumpPackage)) {
            continue;
        }
        list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i));
    }
    if (list.size() <= 0) {
        return false;
    }
    Comparator<Pair<ProcessRecord, Integer>> comparator = new Comparator<Pair<ProcessRecord, Integer>>() {

        @Override
        public int compare(Pair<ProcessRecord, Integer> object1, Pair<ProcessRecord, Integer> object2) {
            if (object1.first.setAdj != object2.first.setAdj) {
                return object1.first.setAdj > object2.first.setAdj ? -1 : 1;
            }
            if (object1.first.setProcState != object2.first.setProcState) {
                return object1.first.setProcState > object2.first.setProcState ? -1 : 1;
            }
            if (object1.second.intValue() != object2.second.intValue()) {
                return object1.second.intValue() > object2.second.intValue() ? -1 : 1;
            }
            return 0;
        }
    };
    Collections.sort(list, comparator);
    final long curRealtime = SystemClock.elapsedRealtime();
    final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
    final long curUptime = SystemClock.uptimeMillis();
    final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
    for (int i = list.size() - 1; i >= 0; i--) {
        ProcessRecord r = list.get(i).first;
        String oomAdj = ProcessList.makeOomAdjString(r.setAdj);
        char schedGroup;
        switch(r.setSchedGroup) {
            case ProcessList.SCHED_GROUP_BACKGROUND:
                schedGroup = 'B';
                break;
            case ProcessList.SCHED_GROUP_DEFAULT:
                schedGroup = 'F';
                break;
            case ProcessList.SCHED_GROUP_TOP_APP:
                schedGroup = 'T';
                break;
            default:
                schedGroup = '?';
                break;
        }
        char foreground;
        if (r.foregroundActivities) {
            foreground = 'A';
        } else if (r.foregroundServices) {
            foreground = 'S';
        } else {
            foreground = ' ';
        }
        String procState = ProcessList.makeProcStateString(r.curProcState);
        pw.print(prefix);
        pw.print(r.persistent ? persistentLabel : normalLabel);
        pw.print(" #");
        int num = (origList.size() - 1) - list.get(i).second;
        if (num < 10)
            pw.print(' ');
        pw.print(num);
        pw.print(": ");
        pw.print(oomAdj);
        pw.print(' ');
        pw.print(schedGroup);
        pw.print('/');
        pw.print(foreground);
        pw.print('/');
        pw.print(procState);
        pw.print(" trm:");
        if (r.trimMemoryLevel < 10)
            pw.print(' ');
        pw.print(r.trimMemoryLevel);
        pw.print(' ');
        pw.print(r.toShortString());
        pw.print(" (");
        pw.print(r.adjType);
        pw.println(')');
        if (r.adjSource != null || r.adjTarget != null) {
            pw.print(prefix);
            pw.print("    ");
            if (r.adjTarget instanceof ComponentName) {
                pw.print(((ComponentName) r.adjTarget).flattenToShortString());
            } else if (r.adjTarget != null) {
                pw.print(r.adjTarget.toString());
            } else {
                pw.print("{null}");
            }
            pw.print("<=");
            if (r.adjSource instanceof ProcessRecord) {
                pw.print("Proc{");
                pw.print(((ProcessRecord) r.adjSource).toShortString());
                pw.println("}");
            } else if (r.adjSource != null) {
                pw.println(r.adjSource.toString());
            } else {
                pw.println("{null}");
            }
        }
        if (inclDetails) {
            pw.print(prefix);
            pw.print("    ");
            pw.print("oom: max=");
            pw.print(r.maxAdj);
            pw.print(" curRaw=");
            pw.print(r.curRawAdj);
            pw.print(" setRaw=");
            pw.print(r.setRawAdj);
            pw.print(" cur=");
            pw.print(r.curAdj);
            pw.print(" set=");
            pw.println(r.setAdj);
            pw.print(prefix);
            pw.print("    ");
            pw.print("state: cur=");
            pw.print(ProcessList.makeProcStateString(r.curProcState));
            pw.print(" set=");
            pw.print(ProcessList.makeProcStateString(r.setProcState));
            pw.print(" lastPss=");
            DebugUtils.printSizeValue(pw, r.lastPss * 1024);
            pw.print(" lastSwapPss=");
            DebugUtils.printSizeValue(pw, r.lastSwapPss * 1024);
            pw.print(" lastCachedPss=");
            DebugUtils.printSizeValue(pw, r.lastCachedPss * 1024);
            pw.println();
            pw.print(prefix);
            pw.print("    ");
            pw.print("cached=");
            pw.print(r.cached);
            pw.print(" empty=");
            pw.print(r.empty);
            pw.print(" hasAboveClient=");
            pw.println(r.hasAboveClient);
            if (r.setProcState >= ActivityManager.PROCESS_STATE_SERVICE) {
                if (r.lastWakeTime != 0) {
                    long wtime;
                    BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics();
                    synchronized (stats) {
                        wtime = stats.getProcessWakeTime(r.info.uid, r.pid, curRealtime);
                    }
                    long timeUsed = wtime - r.lastWakeTime;
                    pw.print(prefix);
                    pw.print("    ");
                    pw.print("keep awake over ");
                    TimeUtils.formatDuration(realtimeSince, pw);
                    pw.print(" used ");
                    TimeUtils.formatDuration(timeUsed, pw);
                    pw.print(" (");
                    pw.print((timeUsed * 100) / realtimeSince);
                    pw.println("%)");
                }
                if (r.lastCpuTime != 0) {
                    long timeUsed = r.curCpuTime - r.lastCpuTime;
                    pw.print(prefix);
                    pw.print("    ");
                    pw.print("run cpu over ");
                    TimeUtils.formatDuration(uptimeSince, pw);
                    pw.print(" used ");
                    TimeUtils.formatDuration(timeUsed, pw);
                    pw.print(" (");
                    pw.print((timeUsed * 100) / uptimeSince);
                    pw.println("%)");
                }
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) BatteryStatsImpl(com.android.internal.os.BatteryStatsImpl) Point(android.graphics.Point) Comparator(java.util.Comparator) ComponentName(android.content.ComponentName) Pair(android.util.Pair)

Example 19 with BatteryStatsImpl

use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by DirtyUnicorns.

the class ActivityManagerService method applyOomAdjLocked.

private final boolean applyOomAdjLocked(ProcessRecord app, boolean doingAll, long now, long nowElapsed) {
    boolean success = true;
    if (app.curRawAdj != app.setRawAdj) {
        app.setRawAdj = app.curRawAdj;
    }
    int changes = 0;
    if (app.curAdj != app.setAdj) {
        ProcessList.setOomAdj(app.pid, app.info.uid, app.curAdj);
        if (DEBUG_SWITCH || DEBUG_OOM_ADJ)
            Slog.v(TAG_OOM_ADJ, "Set " + app.pid + " " + app.processName + " adj " + app.curAdj + ": " + app.adjType);
        app.setAdj = app.curAdj;
        app.verifiedAdj = ProcessList.INVALID_ADJ;
    }
    if (app.setSchedGroup != app.curSchedGroup) {
        int oldSchedGroup = app.setSchedGroup;
        app.setSchedGroup = app.curSchedGroup;
        if (DEBUG_SWITCH || DEBUG_OOM_ADJ)
            Slog.v(TAG_OOM_ADJ, "Setting sched group of " + app.processName + " to " + app.curSchedGroup);
        if (app.waitingToKill != null && app.curReceivers.isEmpty() && app.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND) {
            app.kill(app.waitingToKill, true);
            success = false;
        } else {
            int processGroup;
            switch(app.curSchedGroup) {
                case ProcessList.SCHED_GROUP_BACKGROUND:
                    processGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
                    break;
                case ProcessList.SCHED_GROUP_TOP_APP:
                case ProcessList.SCHED_GROUP_TOP_APP_BOUND:
                    processGroup = Process.THREAD_GROUP_TOP_APP;
                    break;
                default:
                    processGroup = Process.THREAD_GROUP_DEFAULT;
                    break;
            }
            long oldId = Binder.clearCallingIdentity();
            try {
                Process.setProcessGroup(app.pid, processGroup);
                if (app.curSchedGroup == ProcessList.SCHED_GROUP_TOP_APP) {
                    // do nothing if we already switched to RT
                    if (oldSchedGroup != ProcessList.SCHED_GROUP_TOP_APP) {
                        // Switch VR thread for app to SCHED_FIFO
                        if (mInVrMode && app.vrThreadTid != 0) {
                            try {
                                Process.setThreadScheduler(app.vrThreadTid, Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
                            } catch (IllegalArgumentException e) {
                            // thread died, ignore
                            }
                        }
                        if (mUseFifoUiScheduling) {
                            // Switch UI pipeline for app to SCHED_FIFO
                            app.savedPriority = Process.getThreadPriority(app.pid);
                            try {
                                Process.setThreadScheduler(app.pid, Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
                            } catch (IllegalArgumentException e) {
                            // thread died, ignore
                            }
                            if (app.renderThreadTid != 0) {
                                try {
                                    Process.setThreadScheduler(app.renderThreadTid, Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
                                } catch (IllegalArgumentException e) {
                                // thread died, ignore
                                }
                                if (DEBUG_OOM_ADJ) {
                                    Slog.d("UI_FIFO", "Set RenderThread (TID " + app.renderThreadTid + ") to FIFO");
                                }
                            } else {
                                if (DEBUG_OOM_ADJ) {
                                    Slog.d("UI_FIFO", "Not setting RenderThread TID");
                                }
                            }
                        } else {
                            // Boost priority for top app UI and render threads
                            Process.setThreadPriority(app.pid, -10);
                            if (app.renderThreadTid != 0) {
                                try {
                                    Process.setThreadPriority(app.renderThreadTid, -10);
                                } catch (IllegalArgumentException e) {
                                // thread died, ignore
                                }
                            }
                        }
                    }
                } else if (oldSchedGroup == ProcessList.SCHED_GROUP_TOP_APP && app.curSchedGroup != ProcessList.SCHED_GROUP_TOP_APP) {
                    // Safe to do even if we're not in VR mode
                    if (app.vrThreadTid != 0) {
                        Process.setThreadScheduler(app.vrThreadTid, Process.SCHED_OTHER, 0);
                    }
                    if (mUseFifoUiScheduling) {
                        // Reset UI pipeline to SCHED_OTHER
                        Process.setThreadScheduler(app.pid, Process.SCHED_OTHER, 0);
                        Process.setThreadPriority(app.pid, app.savedPriority);
                        if (app.renderThreadTid != 0) {
                            Process.setThreadScheduler(app.renderThreadTid, Process.SCHED_OTHER, 0);
                            Process.setThreadPriority(app.renderThreadTid, -4);
                        }
                    } else {
                        // Reset priority for top app UI and render threads
                        Process.setThreadPriority(app.pid, 0);
                        if (app.renderThreadTid != 0) {
                            Process.setThreadPriority(app.renderThreadTid, 0);
                        }
                    }
                }
            } catch (Exception e) {
                Slog.w(TAG, "Failed setting process group of " + app.pid + " to " + app.curSchedGroup);
                e.printStackTrace();
            } finally {
                Binder.restoreCallingIdentity(oldId);
            }
        }
    }
    if (app.repForegroundActivities != app.foregroundActivities) {
        app.repForegroundActivities = app.foregroundActivities;
        changes |= ProcessChangeItem.CHANGE_ACTIVITIES;
    }
    if (app.repProcState != app.curProcState) {
        app.repProcState = app.curProcState;
        changes |= ProcessChangeItem.CHANGE_PROCESS_STATE;
        if (app.thread != null) {
            try {
                if (false) {
                    //RuntimeException h = new RuntimeException("here");
                    Slog.i(TAG, "Sending new process state " + app.repProcState + " to " + app);
                }
                app.thread.setProcessState(app.repProcState);
            } catch (RemoteException e) {
            }
        }
    }
    if (app.setProcState == ActivityManager.PROCESS_STATE_NONEXISTENT || ProcessList.procStatesDifferForMem(app.curProcState, app.setProcState)) {
        if (false && mTestPssMode && app.setProcState >= 0 && app.lastStateTime <= (now - 200)) {
            // Experimental code to more aggressively collect pss while
            // running test...  the problem is that this tends to collect
            // the data right when a process is transitioning between process
            // states, which well tend to give noisy data.
            long start = SystemClock.uptimeMillis();
            long pss = Debug.getPss(app.pid, mTmpLong, null);
            recordPssSampleLocked(app, app.curProcState, pss, mTmpLong[0], mTmpLong[1], now);
            mPendingPssProcesses.remove(app);
            Slog.i(TAG, "Recorded pss for " + app + " state " + app.setProcState + " to " + app.curProcState + ": " + (SystemClock.uptimeMillis() - start) + "ms");
        }
        app.lastStateTime = now;
        app.nextPssTime = ProcessList.computeNextPssTime(app.curProcState, true, mTestPssMode, isSleepingLocked(), now);
        if (DEBUG_PSS)
            Slog.d(TAG_PSS, "Process state change from " + ProcessList.makeProcStateString(app.setProcState) + " to " + ProcessList.makeProcStateString(app.curProcState) + " next pss in " + (app.nextPssTime - now) + ": " + app);
    } else {
        if (now > app.nextPssTime || (now > (app.lastPssTime + ProcessList.PSS_MAX_INTERVAL) && now > (app.lastStateTime + ProcessList.minTimeFromStateChange(mTestPssMode)))) {
            requestPssLocked(app, app.setProcState);
            app.nextPssTime = ProcessList.computeNextPssTime(app.curProcState, false, mTestPssMode, isSleepingLocked(), now);
        } else if (false && DEBUG_PSS)
            Slog.d(TAG_PSS, "Not requesting PSS of " + app + ": next=" + (app.nextPssTime - now));
    }
    if (app.setProcState != app.curProcState) {
        if (DEBUG_SWITCH || DEBUG_OOM_ADJ)
            Slog.v(TAG_OOM_ADJ, "Proc state change of " + app.processName + " to " + app.curProcState);
        boolean setImportant = app.setProcState < ActivityManager.PROCESS_STATE_SERVICE;
        boolean curImportant = app.curProcState < ActivityManager.PROCESS_STATE_SERVICE;
        if (setImportant && !curImportant) {
            // This app is no longer something we consider important enough to allow to
            // use arbitrary amounts of battery power.  Note
            // its current wake lock time to later know to kill it if
            // it is not behaving well.
            BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
            synchronized (stats) {
                app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, app.pid, nowElapsed);
            }
            app.lastCpuTime = app.curCpuTime;
        }
        // Inform UsageStats of important process state change
        // Must be called before updating setProcState
        maybeUpdateUsageStatsLocked(app, nowElapsed);
        app.setProcState = app.curProcState;
        if (app.setProcState >= ActivityManager.PROCESS_STATE_HOME) {
            app.notCachedSinceIdle = false;
        }
        if (!doingAll) {
            setProcessTrackerStateLocked(app, mProcessStats.getMemFactorLocked(), now);
        } else {
            app.procStateChanged = true;
        }
    } else if (app.reportedInteraction && (nowElapsed - app.interactionEventTime) > USAGE_STATS_INTERACTION_INTERVAL) {
        // For apps that sit around for a long time in the interactive state, we need
        // to report this at least once a day so they don't go idle.
        maybeUpdateUsageStatsLocked(app, nowElapsed);
    }
    if (changes != 0) {
        if (DEBUG_PROCESS_OBSERVERS)
            Slog.i(TAG_PROCESS_OBSERVERS, "Changes in " + app + ": " + changes);
        int i = mPendingProcessChanges.size() - 1;
        ProcessChangeItem item = null;
        while (i >= 0) {
            item = mPendingProcessChanges.get(i);
            if (item.pid == app.pid) {
                if (DEBUG_PROCESS_OBSERVERS)
                    Slog.i(TAG_PROCESS_OBSERVERS, "Re-using existing item: " + item);
                break;
            }
            i--;
        }
        if (i < 0) {
            // No existing item in pending changes; need a new one.
            final int NA = mAvailProcessChanges.size();
            if (NA > 0) {
                item = mAvailProcessChanges.remove(NA - 1);
                if (DEBUG_PROCESS_OBSERVERS)
                    Slog.i(TAG_PROCESS_OBSERVERS, "Retrieving available item: " + item);
            } else {
                item = new ProcessChangeItem();
                if (DEBUG_PROCESS_OBSERVERS)
                    Slog.i(TAG_PROCESS_OBSERVERS, "Allocating new item: " + item);
            }
            item.changes = 0;
            item.pid = app.pid;
            item.uid = app.info.uid;
            if (mPendingProcessChanges.size() == 0) {
                if (DEBUG_PROCESS_OBSERVERS)
                    Slog.i(TAG_PROCESS_OBSERVERS, "*** Enqueueing dispatch processes changed!");
                mUiHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED_UI_MSG).sendToTarget();
            }
            mPendingProcessChanges.add(item);
        }
        item.changes |= changes;
        item.processState = app.repProcState;
        item.foregroundActivities = app.repForegroundActivities;
        if (DEBUG_PROCESS_OBSERVERS)
            Slog.i(TAG_PROCESS_OBSERVERS, "Item " + Integer.toHexString(System.identityHashCode(item)) + " " + app.toShortString() + ": changes=" + item.changes + " procState=" + item.processState + " foreground=" + item.foregroundActivities + " type=" + app.adjType + " source=" + app.adjSource + " target=" + app.adjTarget);
    }
    return success;
}
Also used : RemoteException(android.os.RemoteException) BatteryStatsImpl(com.android.internal.os.BatteryStatsImpl) Point(android.graphics.Point) RemoteException(android.os.RemoteException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ActivityNotFoundException(android.content.ActivityNotFoundException) TransactionTooLargeException(android.os.TransactionTooLargeException) FileNotFoundException(java.io.FileNotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) InstallerException(com.android.internal.os.InstallerConnection.InstallerException)

Example 20 with BatteryStatsImpl

use of com.android.internal.os.BatteryStatsImpl in project android_frameworks_base by DirtyUnicorns.

the class ActivityManagerService method noteAlarmStart.

public void noteAlarmStart(IIntentSender sender, int sourceUid, String tag) {
    if (sender != null && !(sender instanceof PendingIntentRecord)) {
        return;
    }
    final PendingIntentRecord rec = (PendingIntentRecord) sender;
    final BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
    synchronized (stats) {
        mBatteryStatsService.enforceCallingPermission();
        int MY_UID = Binder.getCallingUid();
        final int uid;
        if (sender == null) {
            uid = sourceUid;
        } else {
            uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
        }
        mBatteryStatsService.noteAlarmStart(tag, sourceUid >= 0 ? sourceUid : uid);
    }
}
Also used : BatteryStatsImpl(com.android.internal.os.BatteryStatsImpl) Point(android.graphics.Point)

Aggregations

BatteryStatsImpl (com.android.internal.os.BatteryStatsImpl)62 RemoteException (android.os.RemoteException)19 Point (android.graphics.Point)18 ComponentName (android.content.ComponentName)15 ApplicationInfo (android.content.pm.ApplicationInfo)14 PendingIntent (android.app.PendingIntent)11 Intent (android.content.Intent)11 ResolveInfo (android.content.pm.ResolveInfo)11 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 PackageManager (android.content.pm.PackageManager)7 ServiceInfo (android.content.pm.ServiceInfo)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)6 Parcel (android.os.Parcel)5 ParcelFormatException (android.os.ParcelFormatException)5 ActivityNotFoundException (android.content.ActivityNotFoundException)4 IPackageManager (android.content.pm.IPackageManager)4 Uri (android.net.Uri)4 Bundle (android.os.Bundle)4 RemoteCallbackList (android.os.RemoteCallbackList)4