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

Commit 01fad4a5 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick
Browse files

Ease contention on the mProcessStatsThread mutex in updateCpuStats.

BUG=2606839

Change-Id: I444af0bb4a7b0be7ebf9ee5887805f2f09a426d0
parent 975d86df
Loading
Loading
Loading
Loading
+17 −9
Original line number Diff line number Diff line
@@ -123,6 +123,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor {
    static final String TAG = "ActivityManager";
@@ -927,7 +929,9 @@ public final class ActivityManagerService extends ActivityManagerNative implemen
     */
    final ProcessStats mProcessStats = new ProcessStats(
            MONITOR_THREAD_CPU_USAGE);
    long mLastCpuTime = 0;
    final AtomicLong mLastCpuTime = new AtomicLong(0);
    final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true);
    long mLastWriteTime = 0;
    long mInitialStartTime = 0;
@@ -1430,7 +1434,7 @@ public final class ActivityManagerService extends ActivityManagerNative implemen
                        try {
                            synchronized(this) {
                                final long now = SystemClock.uptimeMillis();
                                long nextCpuDelay = (mLastCpuTime+MONITOR_CPU_MAX_TIME)-now;
                                long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
                                long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
                                //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
                                //        + ", write delay=" + nextWriteDelay);
@@ -1438,12 +1442,12 @@ public final class ActivityManagerService extends ActivityManagerNative implemen
                                    nextCpuDelay = nextWriteDelay;
                                }
                                if (nextCpuDelay > 0) {
                                    mProcessStatsMutexFree.set(true);
                                    this.wait(nextCpuDelay);
                                }
                            }
                        } catch (InterruptedException e) {
                        }
                        
                        updateCpuStatsNow();
                    } catch (Exception e) {
                        Slog.e(TAG, "Unexpected exception collecting process stats", e);
@@ -1470,9 +1474,12 @@ public final class ActivityManagerService extends ActivityManagerNative implemen
    }
    void updateCpuStats() {
        synchronized (mProcessStatsThread) {
        final long now = SystemClock.uptimeMillis();
            if (mLastCpuTime < (now-MONITOR_CPU_MIN_TIME)) {
        if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
            return;
        }
        if (mProcessStatsMutexFree.compareAndSet(true, false)) {
            synchronized (mProcessStatsThread) {
                mProcessStatsThread.notify();
            }
        }
@@ -1480,12 +1487,13 @@ public final class ActivityManagerService extends ActivityManagerNative implemen
    void updateCpuStatsNow() {
        synchronized (mProcessStatsThread) {
            mProcessStatsMutexFree.set(false);
            final long now = SystemClock.uptimeMillis();
            boolean haveNewCpuStats = false;
            if (MONITOR_CPU_USAGE &&
                    mLastCpuTime < (now-MONITOR_CPU_MIN_TIME)) {
                mLastCpuTime = now;
                    mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
                mLastCpuTime.set(now);
                haveNewCpuStats = true;
                mProcessStats.update();
                //Slog.i(TAG, mProcessStats.printCurrentState());