From e51c5fead2ca54ca293ca5dcefbe10f3e7a53b26 Mon Sep 17 00:00:00 2001 From: Greg Kaiser Date: Fri, 21 Mar 2025 10:19:58 -0700 Subject: [PATCH] lmkd: Speed up proc_get_heaviest() in one case When a proc list for an oomadj is non-empty, we currently read from disk to get the size of every process in our list, so we can know which is the largest/heaviest process. However, if there's only a single process in our list, we already know it's going to be our heaviest [*]. So we don't need to do the (relatively expensive) disk access to figure out its size, and can just directly return this process. [*] There's the case where our attempt to read from /proc fails for the process. The old code would then instantly remove this stale pid (and return NULL if that was the only process in the list). This new code will end up returning this stale process instead. Since proc_get_heaviest() is meant to be used in the same way as proc_adj_tail(), and proc_adj_tail() returns processes without checking if they are stale, we don't consider this an issue. (Note that in the current code, the only calling site of proc_get_heaviest() will remove this stale process when it calls kill_one_process().) Bug: 405391096 Test: TreeHugger Change-Id: Iaf2f5c57dcbf2d4e45c2545a8322736b5985337c --- lmkd.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lmkd.cpp b/lmkd.cpp index 16ee231..9c833af 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -2233,6 +2233,10 @@ static struct proc *proc_get_heaviest(int oomadj) { struct adjslot_list *curr = head->next; struct proc *maxprocp = NULL; int maxsize = 0; + if ((curr != head) && (curr->next == head)) { + // Our list only has one process.  No need to access procfs for its size. + return (struct proc *)curr; + } while (curr != head) { int pid = ((struct proc *)curr)->pid; int tasksize = proc_get_size(pid);