Re: [PATCH] alloc_tag: avoid mem alloc and iter reset when reading allocinfo

From: David Wang
Date: Thu May 08 2025 - 12:58:57 EST



At 2025-05-09 00:34:27, "Kent Overstreet" <kent.overstreet@xxxxxxxxx> wrote:
>On Fri, May 09, 2025 at 12:24:56AM +0800, David Wang wrote:
>> At 2025-05-08 21:33:50, "Kent Overstreet" <kent.overstreet@xxxxxxxxx> wrote:
>> >The first question is - does it matter? If the optimization is just for
>> >/proc/allocinfo, who's reading it at a high enough rate that we care?
>> >
>> >If it's only being used interactively, it doesn't matter. If it's being
>> >read at a high rate by some sort of profiling program, we'd want to skip
>> >the text interface entirely and add an ioctl to read the data out in a
>> >binary format.
>> ...^_^, Actually, I have been running tools parsing /proc/allocinfo every 5 seconds
>> ,and feeding data to a prometheus server for a quite long while...
>> 5 seconds seems not that frequent, but I also have all other proc files to read,
>> I would like optimization for all the proc files......
>>
>> Ioctl or other binary interfaces are indeed more efficient, but most are
>> not well documented, while most proc files are self-documented. If proc files
>> are efficient enough, I think I would stay with proc files even with a binary
>> interface alternate tens of fold faster.
>
>This would be a perfect place for a binary interface, you just want to
>return an array of
>
>struct allocated_by_ip {
> u64 ip;
> u64 bytes;
>};



>
>Printing it in text form requires symbol table lookup, what you're
>optimizing is noise compared to that and vsnprintf().

Oh, no, this optimization is mostly achieved by avoiding iter rewinding, I think
I talk about the extra memory allocation "too much"....
These lines of code:
- while ((ct = codetag_next_ct(&priv->iter)) != NULL && node)
- node--;
have accumulated way too much.
Think it this way, advancing iterator n times takes 1%, reasonable noise
compared to symbol lookup and printf(). The problem is seq_file() would
restart about 80 times to read out all content of /proc/allocinfo, accumulated
to a total 40*n iterator advancement, hence 1% become 40*1%, noise become significant.

My test result shows an improvement from 7ms to 4ms:

Timings before:
$ time cat /proc/allocinfo > /dev/null

real 0m0.007s
user 0m0.000s
sys 0m0.007s
read-syscalls get slower and slower:
read(3, "allocinfo - version: 1.0\n# <"..., 131072) = 4085 <0.000062>
...
read(3, " 0 0 drivers/gp"..., 131072) = 4046 <0.000135>
read(3, " 0 0 sound/core"..., 131072) = 4021 <0.000150>
...

and with the change:
$ time cat /proc/allocinfo > /dev/null

real 0m0.004s
user 0m0.000s
sys 0m0.003s