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

Commit fe45b29a authored by Adam Lesinski's avatar Adam Lesinski Committed by Android (Google) Code Review
Browse files

Merge "BatteryStats: Remove isolated UIDs from /proc/uid_cputime when they are...

Merge "BatteryStats: Remove isolated UIDs from /proc/uid_cputime when they are not in use anymore" into mnc-dev
parents c81ecb6d 61db88fc
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -183,6 +183,7 @@ public final class BatteryStatsImpl extends BatteryStats {
    public interface ExternalStatsSync {
        void scheduleSync(String reason);
        void scheduleWifiSync(String reason);
        void scheduleCpuSyncDueToRemovedUid(int uid);
    }

    public final MyHandler mHandler;
@@ -2522,13 +2523,28 @@ public final class BatteryStatsImpl extends BatteryStats {
        mIsolatedUids.put(isolatedUid, appUid);
    }

    public void removeIsolatedUidLocked(int isolatedUid, int appUid) {
    /**
     * Schedules a read of the latest cpu times before removing the isolated UID.
     * @see #removeIsolatedUidLocked(int)
     */
    public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
        int curUid = mIsolatedUids.get(isolatedUid, -1);
        if (curUid == appUid) {
            mIsolatedUids.delete(isolatedUid);
            if (mExternalSync != null) {
                mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
            }
        }
    }

    /**
     * This should only be called after the cpu times have been read.
     * @see #scheduleRemoveIsolatedUidLocked(int, int)
     */
    public void removeIsolatedUidLocked(int isolatedUid) {
        mIsolatedUids.delete(isolatedUid);
        mKernelUidCpuTimeReader.removeUid(isolatedUid);
    }

    public int mapUid(int uid) {
        int isolated = mIsolatedUids.get(uid, -1);
        return isolated > 0 ? isolated : uid;
+29 −8
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import android.os.WorkSource;
import android.telephony.DataConnectionRealTimeInfo;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.IntArray;
import android.util.Slog;

import android.util.TimeUtils;
@@ -82,6 +83,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
        public static final int MSG_SYNC_EXTERNAL_STATS = 1;
        public static final int MSG_WRITE_TO_DISK = 2;
        private int mUpdateFlags = 0;
        private IntArray mUidsToRemove = new IntArray();

        public BatteryStatsHandler(Looper looper) {
            super(looper);
@@ -98,6 +100,15 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                        mUpdateFlags = 0;
                    }
                    updateExternalStats((String)msg.obj, updateFlags);
                    synchronized (this) {
                        synchronized (mStats) {
                            final int numUidsToRemove = mUidsToRemove.size();
                            for (int i = 0; i < numUidsToRemove; i++) {
                                mStats.removeIsolatedUidLocked(mUidsToRemove.get(i));
                            }
                        }
                        mUidsToRemove.clear();
                    }
                    break;

                case MSG_WRITE_TO_DISK:
@@ -111,23 +122,33 @@ public final class BatteryStatsService extends IBatteryStats.Stub

        @Override
        public void scheduleSync(String reason) {
            scheduleSyncImpl(reason, UPDATE_ALL);
            synchronized (this) {
                scheduleSyncLocked(reason, UPDATE_ALL);
            }
        }

        @Override
        public void scheduleWifiSync(String reason) {
            scheduleSyncImpl(reason, UPDATE_WIFI);
            synchronized (this) {
                scheduleSyncLocked(reason, UPDATE_WIFI);
            }
        }

        private void scheduleSyncImpl(String reason, int updateFlags) {
        @Override
        public void scheduleCpuSyncDueToRemovedUid(int uid) {
            synchronized (this) {
                scheduleSyncLocked("remove-uid", UPDATE_CPU);
                mUidsToRemove.add(uid);
            }
        }

        private void scheduleSyncLocked(String reason, int updateFlags) {
            if (mUpdateFlags == 0) {
                sendMessage(Message.obtain(this, MSG_SYNC_EXTERNAL_STATS, reason));
            }
            mUpdateFlags |= updateFlags;
        }
    }
    }

    BatteryStatsService(File systemDir, Handler handler) {
        // Our handler here will be accessing the disk, use a different thread than
@@ -220,7 +241,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub

    void removeIsolatedUid(int isolatedUid, int appUid) {
        synchronized (mStats) {
            mStats.removeIsolatedUidLocked(isolatedUid, appUid);
            mStats.scheduleRemoveIsolatedUidLocked(isolatedUid, appUid);
        }
    }