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
This commit is contained in:
Greg Kaiser 2025-03-21 10:19:58 -07:00
parent 7f1fb15cb7
commit e51c5fead2
1 changed files with 4 additions and 0 deletions

View File

@ -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);