Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 023b6812 authored by Dianne Hackborn's avatar Dianne Hackborn Committed by Android (Google) Code Review
Browse files

Merge "Fix issue #17414533: update batterystats occasionally takes a couple..." into lmp-dev

parents 547f6e18 652973fc
Loading
Loading
Loading
Loading
+14 −10
Original line number Diff line number Diff line
@@ -64,7 +64,9 @@ public class ProcessCpuTracker {

    /** Stores user time and system time in 100ths of a second. */
    private final long[] mProcessStatsData = new long[4];
    /** Stores user time and system time in 100ths of a second. */

    /** Stores user time and system time in 100ths of a second.  Used for
     * public API to retrieve CPU use for a process.  Must lock while in use. */
    private final long[] mSinglePidStatsData = new long[4];

    private static final int[] PROCESS_FULL_STATS_FORMAT = new int[] {
@@ -533,9 +535,10 @@ public class ProcessCpuTracker {

    /**
     * Returns the total time (in clock ticks, or 1/100 sec) spent executing in
     * both user and system code.
     * both user and system code.  Safe to call without lock held.
     */
    public long getCpuTimeForPid(int pid) {
        synchronized (mSinglePidStatsData) {
            final String statFile = "/proc/" + pid + "/stat";
            final long[] statsData = mSinglePidStatsData;
            if (Process.readProcFile(statFile, PROCESS_STATS_FORMAT,
@@ -546,6 +549,7 @@ public class ProcessCpuTracker {
            }
            return 0;
        }
    }

    /**
     * Returns the delta time (in clock ticks, or 1/100 sec) spent at each CPU
+14 −11
Original line number Diff line number Diff line
@@ -1077,13 +1077,16 @@ public final class ActivityManagerService extends ActivityManagerNative
    /**
     * Runtime CPU use collection thread.  This object's lock is used to
     * protect all related state.
     * perform synchronization with the thread (notifying it to run).
     */
    final Thread mProcessCpuThread;
    /**
     * Used to collect process stats when showing not responding dialog.
     * Protected by mProcessCpuThread.
     * Used to collect per-process CPU use for ANRs, battery stats, etc.
     * Must acquire this object's lock when accessing it.
     * NOTE: this lock will be held while doing long operations (trawling
     * through all processes in /proc), so it should never be acquired by
     * any critical paths such as when holding the main activity manager lock.
     */
    final ProcessCpuTracker mProcessCpuTracker = new ProcessCpuTracker(
            MONITOR_THREAD_CPU_USAGE);
@@ -1597,7 +1600,7 @@ public final class ActivityManagerService extends ActivityManagerNative
                            infoMap.put(mi.pid, mi);
                        }
                        updateCpuStatsNow();
                        synchronized (mProcessCpuThread) {
                        synchronized (mProcessCpuTracker) {
                            final int N = mProcessCpuTracker.countStats();
                            for (int i=0; i<N; i++) {
                                ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
@@ -1896,7 +1899,7 @@ public final class ActivityManagerService extends ActivityManagerNative
                if (memInfo != null) {
                    updateCpuStatsNow();
                    long nativeTotalPss = 0;
                    synchronized (mProcessCpuThread) {
                    synchronized (mProcessCpuTracker) {
                        final int N = mProcessCpuTracker.countStats();
                        for (int j=0; j<N; j++) {
                            ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(j);
@@ -1914,7 +1917,7 @@ public final class ActivityManagerService extends ActivityManagerNative
                        }
                    }
                    memInfo.readMemInfo();
                    synchronized (this) {
                    synchronized (ActivityManagerService.this) {
                        if (DEBUG_PSS) Slog.d(TAG, "Collected native and kernel memory in "
                                + (SystemClock.uptimeMillis()-start) + "ms");
                        mProcessStats.addSysMemUsageLocked(memInfo.getCachedSizeKb(),
@@ -2185,7 +2188,7 @@ public final class ActivityManagerService extends ActivityManagerNative
                return;
            }
            synchronized (mActivityManagerService.mProcessCpuThread) {
            synchronized (mActivityManagerService.mProcessCpuTracker) {
                pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentLoad());
                pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentState(
                        SystemClock.uptimeMillis()));
@@ -2381,7 +2384,7 @@ public final class ActivityManagerService extends ActivityManagerNative
    }
    void updateCpuStatsNow() {
        synchronized (mProcessCpuThread) {
        synchronized (mProcessCpuTracker) {
            mProcessCpuMutexFree.set(false);
            final long now = SystemClock.uptimeMillis();
            boolean haveNewCpuStats = false;
@@ -5076,7 +5079,7 @@ public final class ActivityManagerService extends ActivityManagerNative
        String cpuInfo = null;
        if (MONITOR_CPU_USAGE) {
            updateCpuStatsNow();
            synchronized (mProcessCpuThread) {
            synchronized (mProcessCpuTracker) {
                cpuInfo = mProcessCpuTracker.printCurrentState(anrTime);
            }
            info.append(processCpuTracker.printCurrentLoad());
@@ -13743,7 +13746,7 @@ public final class ActivityManagerService extends ActivityManagerNative
                    findPid = Integer.parseInt(args[opti]);
                } catch (NumberFormatException e) {
                }
                synchronized (mProcessCpuThread) {
                synchronized (mProcessCpuTracker) {
                    final int N = mProcessCpuTracker.countStats();
                    for (int i=0; i<N; i++) {
                        ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
@@ -13905,7 +13908,7 @@ public final class ActivityManagerService extends ActivityManagerNative
            // If we are showing aggregations, also look for native processes to
            // include so that our aggregations are more accurate.
            updateCpuStatsNow();
            synchronized (mProcessCpuThread) {
            synchronized (mProcessCpuTracker) {
                final int N = mProcessCpuTracker.countStats();
                for (int i=0; i<N; i++) {
                    ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
+3 −8
Original line number Diff line number Diff line
@@ -1042,11 +1042,8 @@ final class ActivityStack {

            if (prev.app != null && prev.cpuTimeAtResume > 0
                    && mService.mBatteryStatsService.isOnBattery()) {
                long diff;
                synchronized (mService.mProcessCpuThread) {
                    diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
                long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
                        - prev.cpuTimeAtResume;
                }
                if (diff > 0) {
                    BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
                    synchronized (bsi) {
@@ -1097,9 +1094,7 @@ final class ActivityStack {
        // 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 (mService.mProcessCpuThread) {
            next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
            }
        } else {
            next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
        }