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

Skip to content
Commit 8fdaa6ea authored by T.J. Mercier's avatar T.J. Mercier
Browse files

Don't restart procfs reads from scratch

android_os_Process_readProcFile is mainly used for small files. It
tries to minimize memory use by first attempting to read into a stack
buffer, and only after that buffer is found to be too small does it use
a heap-allocated buffer. The heap buffer starts out small and grows as
the file size is found to exceed the size of the buffer. However on both
the transition from the stack buffer to the heap buffer, and when
enlarging the heap buffer, the data that has already been read gets
thrown away and the read of the file restarts from the beginning.

The dominating runtime cost for the function is the kernel work to
produce the output. Copying the already-read data into a larger buffer
in userspace is more efficient than throwing it away and rereading from
the beginning. So this commit changes the function to do that.

This reduces cpu-cycles by about 35% for reads of the largest
/proc/pid/maps file on Android. Tested on a Pixel 6 Pro with
system_server's maps file across 20 runs:

         Simpleperf event count (cpu-cycles)
         Copy         Reread
Average  13,338,222   20,254,826
StdDev    4,791,640    9,644,942
Min       7,460,801   12,576,877
Max      25,403,534   45,759,654

Eliminating the 1024 byte stack buffer and just using an expandable heap
buffer indicates further improvement with an average of 12,835,802
cycles, code simplification, and a .text savings of between about 50-200
bytes depending on compiler optimizations. I suspect the memory savings
from attempting to read small files into the stack buffer is not that
meaningful, however I didn't measure peak memory use for either small or
large reads so I did not remove it in this commit.

Bug: 351917521
Test: cuttlefish boot
Change-Id: Ifa4a739ca43415b1d8dd85177a7c50eb86955160
parent 016bd093
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment