Re: [PATCH] Add accumulated call counter for memory allocation profiling

From: Suren Baghdasaryan
Date: Sun Jun 30 2024 - 15:33:34 EST


On Mon, Jun 17, 2024 at 8:33 AM David Wang <00107082@xxxxxxx> wrote:
>
> Accumulated call counter can be used to evaluate rate
> of memory allocation via delta(counters)/delta(time).
> This metrics can help analysis performance behaviours,
> e.g. tuning cache size, etc.

Sorry for the delay, David.
IIUC with this counter you can identify the number of allocations ever
made from a specific code location. Could you please clarify the usage
a bit more? Is the goal to see which locations are the most active and
the rate at which allocations are made there? How will that
information be used?
I'm a bit cautious here because each counter will take more space and
use some additional cpu cycles.
Thanks,
Suren.

>
> Signed-off-by: David Wang <00107082@xxxxxxx>
> ---
> include/linux/alloc_tag.h | 11 +++++++----
> lib/alloc_tag.c | 7 +++----
> 2 files changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h
> index abd24016a900..62734244c0b9 100644
> --- a/include/linux/alloc_tag.h
> +++ b/include/linux/alloc_tag.h
> @@ -18,6 +18,7 @@
> struct alloc_tag_counters {
> u64 bytes;
> u64 calls;
> + u64 accu_calls;
> };
>
> /*
> @@ -102,14 +103,15 @@ static inline bool mem_alloc_profiling_enabled(void)
>
> static inline struct alloc_tag_counters alloc_tag_read(struct alloc_tag *tag)
> {
> - struct alloc_tag_counters v = { 0, 0 };
> + struct alloc_tag_counters v = { 0, 0, 0 };
> struct alloc_tag_counters *counter;
> int cpu;
>
> for_each_possible_cpu(cpu) {
> - counter = per_cpu_ptr(tag->counters, cpu);
> - v.bytes += counter->bytes;
> - v.calls += counter->calls;
> + counter = per_cpu_ptr(tag->counters, cpu);
> + v.bytes += counter->bytes;
> + v.calls += counter->calls;
> + v.accu_calls += counter->accu_calls;
> }
>
> return v;
> @@ -145,6 +147,7 @@ static inline void __alloc_tag_ref_set(union codetag_ref *ref, struct alloc_tag
> * counter because when we free each part the counter will be decremented.
> */
> this_cpu_inc(tag->counters->calls);
> + this_cpu_inc(tag->counters->accu_calls);
> }
>
> static inline void alloc_tag_ref_set(union codetag_ref *ref, struct alloc_tag *tag)
> diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c
> index 11ed973ac359..c4059362d828 100644
> --- a/lib/alloc_tag.c
> +++ b/lib/alloc_tag.c
> @@ -66,8 +66,8 @@ static void allocinfo_stop(struct seq_file *m, void *arg)
> static void print_allocinfo_header(struct seq_buf *buf)
> {
> /* Output format version, so we can change it. */
> - seq_buf_printf(buf, "allocinfo - version: 1.0\n");
> - seq_buf_printf(buf, "# <size> <calls> <tag info>\n");
> + seq_buf_printf(buf, "allocinfo - version: 1.1\n");
> + seq_buf_printf(buf, "# <size> <calls> <tag info> <accumulated calls>\n");
> }
>
> static void alloc_tag_to_text(struct seq_buf *out, struct codetag *ct)
> @@ -78,8 +78,7 @@ static void alloc_tag_to_text(struct seq_buf *out, struct codetag *ct)
>
> seq_buf_printf(out, "%12lli %8llu ", bytes, counter.calls);
> codetag_to_text(out, ct);
> - seq_buf_putc(out, ' ');
> - seq_buf_putc(out, '\n');
> + seq_buf_printf(out, " %llu\n", counter.accu_calls);
> }
>
> static int allocinfo_show(struct seq_file *m, void *arg)
> --
> 2.39.2
>