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

Commit 474e7bec authored by Misha Wagner's avatar Misha Wagner
Browse files

Filter threads that have low total CPU usage for CpuTimePerThreadFreq

Test: KernelCpuThreadReader::testReader_filtersLowTotalCpuUsage
Bug: 120016054
Change-Id: If09c8c01e6efffd0d7315613dc7425138528d39c
parent 50f84e88
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -108,6 +108,13 @@ public class KernelCpuThreadReader {
    private static final Predicate<Integer> DEFAULT_UID_PREDICATE =
            uid -> 1000 <= uid && uid < 2000;

    /**
     * Do not report any threads that have a total CPU usage (across all frequencies) less than or
     * equal to this number. This significantly reduces the amount of reported threads without
     * losing any important information
     */
    private static final int TOTAL_CPU_USAGE_THRESHOLD_MILLIS = 20;

    /**
     * Value returned when there was an error getting an integer ID value (e.g. PID, UID)
     */
@@ -339,6 +346,15 @@ public class KernelCpuThreadReader {
        }
        int[] cpuUsages = mFrequencyBucketCreator.getBucketedValues(cpuUsagesLong);

        // Filter threads that have low total CPU usage
        int cpuUsageSum = 0;
        for (int i = 0; i < cpuUsages.length; i++) {
            cpuUsageSum += cpuUsages[i];
        }
        if (cpuUsageSum <= TOTAL_CPU_USAGE_THRESHOLD_MILLIS) {
            return null;
        }

        return new ThreadCpuUsage(threadId, threadName, cpuUsages);
    }

+42 −4
Original line number Diff line number Diff line
@@ -39,7 +39,9 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@SmallTest
@RunWith(AndroidJUnit4.class)
@@ -56,8 +58,8 @@ public class KernelCpuThreadReaderTest {
            1000, 2000, 3000, 4000,
    };
    private static final int[][] THREAD_CPU_TIMES = {
            {1, 0, 0, 1},
            {0, 0, 0, 0},
            {100, 0, 0, 100},
            {0, 0, 9999999, 0},
            {1000, 1000, 1000, 1000},
            {0, 1, 2, 3},
    };
@@ -107,6 +109,42 @@ public class KernelCpuThreadReaderTest {
                THREAD_IDS, PROCESS_NAME, THREAD_NAMES, THREAD_CPU_FREQUENCIES, THREAD_CPU_TIMES);
    }

    @Test
    public void testReader_filtersLowTotalCpuUsage() throws IOException {
        KernelCpuThreadReader.Injector processUtils =
                new KernelCpuThreadReader.Injector() {
                    @Override
                    public int myPid() {
                        return PROCESS_ID;
                    }

                    @Override
                    public int myUid() {
                        return UID;
                    }

                    @Override
                    public int getUidForPid(int pid) {
                        return 0;
                    }
                };
        setupDirectory(mProcDirectory.toPath().resolve("self"), new int[]{1, 2}, PROCESS_NAME,
                THREAD_NAMES, new int[]{1000, 2000}, new int[][]{{0, 1}, {100, 0}});

        final KernelCpuThreadReader kernelCpuThreadReader = new KernelCpuThreadReader(
                mProcDirectory.toPath(),
                mProcDirectory.toPath().resolve("self/task/1/time_in_state"),
                processUtils);
        final KernelCpuThreadReader.ProcessCpuUsage processCpuUsage =
                kernelCpuThreadReader.getCurrentProcessCpuUsage();

        List<Integer> threadIds = processCpuUsage.threadCpuUsages.stream()
                .map(t -> t.threadId)
                .collect(Collectors.toList());
        assertEquals(1, threadIds.size());
        assertEquals(2, (long) threadIds.get(0));
    }

    @Test
    public void testReader_byUids() throws IOException {
        int[] uids = new int[]{0, 2, 3, 4, 5, 6000};
@@ -134,7 +172,7 @@ public class KernelCpuThreadReaderTest {
            setupDirectory(mProcDirectory.toPath().resolve(String.valueOf(uid)),
                    new int[]{uid * 10},
                    "process" + uid, new String[]{"thread" + uid}, new int[]{1000},
                    new int[][]{{uid}});
                    new int[][]{{uid + 100}});
        }
        final KernelCpuThreadReader kernelCpuThreadReader = new KernelCpuThreadReader(
                mProcDirectory.toPath(),
@@ -151,7 +189,7 @@ public class KernelCpuThreadReaderTest {
            int uid = expectedUids[i];
            checkResults(processCpuUsage, kernelCpuThreadReader.getCpuFrequenciesKhz(),
                    uid, uid, new int[]{uid * 10}, "process" + uid, new String[]{"thread" + uid},
                    new int[]{1000}, new int[][]{{uid}});
                    new int[]{1000}, new int[][]{{uid + 100}});
        }
    }