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

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

Merge "Remove UID from kernel cpu accounting when uninstalled" into mnc-dev

parents b99e6d69 b83ffee5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -8425,6 +8425,7 @@ public final class BatteryStatsImpl extends BatteryStats {
     * Remove the statistics object for a particular uid.
     */
    public void removeUidStatsLocked(int uid) {
        mKernelUidCpuTimeReader.removeUid(uid);
        mUidStats.remove(uid);
    }

+36 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.util.SparseLongArray;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
@@ -37,6 +38,7 @@ import java.io.IOException;
public class KernelUidCpuTimeReader {
    private static final String TAG = "KernelUidCpuTimeReader";
    private static final String sProcFile = "/proc/uid_cputime/show_uid_stat";
    private static final String sRemoveUidProcFile = "/proc/uid_cputime/remove_uid_range";

    /**
     * Callback interface for processing each line of the proc file.
@@ -66,12 +68,22 @@ public class KernelUidCpuTimeReader {
                final long systemTimeUs = Long.parseLong(splitter.next(), 10);

                if (callback != null) {
                    long userTimeDeltaUs = userTimeUs;
                    long systemTimeDeltaUs = systemTimeUs;
                    int index = mLastUserTimeUs.indexOfKey(uid);
                    if (index < 0) {
                        callback.onUidCpuTime(uid, userTimeUs, systemTimeUs);
                    } else {
                        callback.onUidCpuTime(uid, userTimeUs - mLastUserTimeUs.valueAt(index),
                                systemTimeUs - mLastSystemTimeUs.valueAt(index));
                    if (index >= 0) {
                        userTimeDeltaUs -= mLastUserTimeUs.valueAt(index);
                        systemTimeDeltaUs -= mLastSystemTimeUs.valueAt(index);

                        if (userTimeDeltaUs < 0 || systemTimeDeltaUs < 0) {
                            // The UID must have been removed from accounting, then added back.
                            userTimeDeltaUs = userTimeUs;
                            systemTimeDeltaUs = systemTimeUs;
                        }
                    }

                    if (userTimeDeltaUs != 0 || systemTimeDeltaUs != 0) {
                        callback.onUidCpuTime(uid, userTimeDeltaUs, systemTimeDeltaUs);
                    }
                }
                mLastUserTimeUs.put(uid, userTimeUs);
@@ -81,4 +93,23 @@ public class KernelUidCpuTimeReader {
            Slog.e(TAG, "Failed to read uid_cputime", e);
        }
    }

    /**
     * Removes the UID from the kernel module and from internal accounting data.
     * @param uid The UID to remove.
     */
    public void removeUid(int uid) {
        int index = mLastUserTimeUs.indexOfKey(uid);
        if (index >= 0) {
            mLastUserTimeUs.removeAt(index);
            mLastSystemTimeUs.removeAt(index);
        }

        try (FileWriter writer = new FileWriter(sRemoveUidProcFile)) {
            writer.write(Integer.toString(uid) + "-" + Integer.toString(uid));
            writer.flush();
        } catch (IOException e) {
            Slog.e(TAG, "failed to remove uid from uid_cputime module", e);
        }
    }
}
+1 −4
Original line number Diff line number Diff line
@@ -16143,10 +16143,7 @@ public final class ActivityManagerService extends ActivityManagerNative
                            final int uid = intentExtras != null
                                    ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
                            if (uid >= 0) {
                                BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
                                synchronized (bs) {
                                    bs.removeUidStatsLocked(uid);
                                }
                                mBatteryStatsService.removeUid(uid);
                                mAppOpsService.uidRemoved(uid);
                            }
                            break;
+9 −0
Original line number Diff line number Diff line
@@ -178,6 +178,15 @@ public final class BatteryStatsService extends IBatteryStats.Stub

    // These are for direct use by the activity manager...

    /**
     * Remove a UID from the BatteryStats and BatteryStats' external dependencies.
     */
    void removeUid(int uid) {
        synchronized (mStats) {
            mStats.removeUidStatsLocked(uid);
        }
    }

    void addIsolatedUid(int isolatedUid, int appUid) {
        synchronized (mStats) {
            mStats.addIsolatedUidLocked(isolatedUid, appUid);