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

Commit 10595666 authored by Adam Lesinski's avatar Adam Lesinski Committed by Android Git Automerger
Browse files

am 038959e8: Merge "Remove UID from kernel cpu accounting when uninstalled" into mnc-dev

* commit '038959e8':
  Remove UID from kernel cpu accounting when uninstalled
parents 1e9352ae 038959e8
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -8425,6 +8425,7 @@ public final class BatteryStatsImpl extends BatteryStats {
     * Remove the statistics object for a particular uid.
     * Remove the statistics object for a particular uid.
     */
     */
    public void removeUidStatsLocked(int uid) {
    public void removeUidStatsLocked(int uid) {
        mKernelUidCpuTimeReader.removeUid(uid);
        mUidStats.remove(uid);
        mUidStats.remove(uid);
    }
    }


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


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


/**
/**
@@ -37,6 +38,7 @@ import java.io.IOException;
public class KernelUidCpuTimeReader {
public class KernelUidCpuTimeReader {
    private static final String TAG = "KernelUidCpuTimeReader";
    private static final String TAG = "KernelUidCpuTimeReader";
    private static final String sProcFile = "/proc/uid_cputime/show_uid_stat";
    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.
     * 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);
                final long systemTimeUs = Long.parseLong(splitter.next(), 10);


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

                                systemTimeUs - 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);
                mLastUserTimeUs.put(uid, userTimeUs);
@@ -81,4 +93,23 @@ public class KernelUidCpuTimeReader {
            Slog.e(TAG, "Failed to read uid_cputime", e);
            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 Original line Diff line number Diff line
@@ -16143,10 +16143,7 @@ public final class ActivityManagerService extends ActivityManagerNative
                            final int uid = intentExtras != null
                            final int uid = intentExtras != null
                                    ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
                                    ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
                            if (uid >= 0) {
                            if (uid >= 0) {
                                BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
                                mBatteryStatsService.removeUid(uid);
                                synchronized (bs) {
                                    bs.removeUidStatsLocked(uid);
                                }
                                mAppOpsService.uidRemoved(uid);
                                mAppOpsService.uidRemoved(uid);
                            }
                            }
                            break;
                            break;
+9 −0
Original line number Original line 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...
    // 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) {
    void addIsolatedUid(int isolatedUid, int appUid) {
        synchronized (mStats) {
        synchronized (mStats) {
            mStats.addIsolatedUidLocked(isolatedUid, appUid);
            mStats.addIsolatedUidLocked(isolatedUid, appUid);