Message ID | 20240221194052.927623-32-surenb@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | Memory allocation profiling | expand |
On 2/21/24 20:40, Suren Baghdasaryan wrote: > Include allocations in show_mem reports. > > Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> > Signed-off-by: Suren Baghdasaryan <surenb@google.com> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Nit: there's pr_notice() that's shorter than printk(KERN_NOTICE > --- > include/linux/alloc_tag.h | 7 +++++++ > include/linux/codetag.h | 1 + > lib/alloc_tag.c | 38 ++++++++++++++++++++++++++++++++++++++ > lib/codetag.c | 5 +++++ > mm/show_mem.c | 26 ++++++++++++++++++++++++++ > 5 files changed, 77 insertions(+) > > diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h > index 29636719b276..85a24a027403 100644 > --- a/include/linux/alloc_tag.h > +++ b/include/linux/alloc_tag.h > @@ -30,6 +30,13 @@ struct alloc_tag { > > #ifdef CONFIG_MEM_ALLOC_PROFILING > > +struct codetag_bytes { > + struct codetag *ct; > + s64 bytes; > +}; > + > +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep); > + > static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct) > { > return container_of(ct, struct alloc_tag, ct); > diff --git a/include/linux/codetag.h b/include/linux/codetag.h > index bfd0ba5c4185..c2a579ccd455 100644 > --- a/include/linux/codetag.h > +++ b/include/linux/codetag.h > @@ -61,6 +61,7 @@ struct codetag_iterator { > } > > void codetag_lock_module_list(struct codetag_type *cttype, bool lock); > +bool codetag_trylock_module_list(struct codetag_type *cttype); > struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype); > struct codetag *codetag_next_ct(struct codetag_iterator *iter); > > diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c > index cb5adec4b2e2..ec54f29482dc 100644 > --- a/lib/alloc_tag.c > +++ b/lib/alloc_tag.c > @@ -86,6 +86,44 @@ static const struct seq_operations allocinfo_seq_op = { > .show = allocinfo_show, > }; > > +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep) > +{ > + struct codetag_iterator iter; > + struct codetag *ct; > + struct codetag_bytes n; > + unsigned int i, nr = 0; > + > + if (can_sleep) > + codetag_lock_module_list(alloc_tag_cttype, true); > + else if (!codetag_trylock_module_list(alloc_tag_cttype)) > + return 0; > + > + iter = codetag_get_ct_iter(alloc_tag_cttype); > + while ((ct = codetag_next_ct(&iter))) { > + struct alloc_tag_counters counter = alloc_tag_read(ct_to_alloc_tag(ct)); > + > + n.ct = ct; > + n.bytes = counter.bytes; > + > + for (i = 0; i < nr; i++) > + if (n.bytes > tags[i].bytes) > + break; > + > + if (i < count) { > + nr -= nr == count; > + memmove(&tags[i + 1], > + &tags[i], > + sizeof(tags[0]) * (nr - i)); > + nr++; > + tags[i] = n; > + } > + } > + > + codetag_lock_module_list(alloc_tag_cttype, false); > + > + return nr; > +} > + > static void __init procfs_init(void) > { > proc_create_seq("allocinfo", 0444, NULL, &allocinfo_seq_op); > diff --git a/lib/codetag.c b/lib/codetag.c > index b13412ca57cc..7b39cec9648a 100644 > --- a/lib/codetag.c > +++ b/lib/codetag.c > @@ -36,6 +36,11 @@ void codetag_lock_module_list(struct codetag_type *cttype, bool lock) > up_read(&cttype->mod_lock); > } > > +bool codetag_trylock_module_list(struct codetag_type *cttype) > +{ > + return down_read_trylock(&cttype->mod_lock) != 0; > +} > + > struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype) > { > struct codetag_iterator iter = { > diff --git a/mm/show_mem.c b/mm/show_mem.c > index 8dcfafbd283c..1e41f8d6e297 100644 > --- a/mm/show_mem.c > +++ b/mm/show_mem.c > @@ -423,4 +423,30 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) > #ifdef CONFIG_MEMORY_FAILURE > printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages)); > #endif > +#ifdef CONFIG_MEM_ALLOC_PROFILING > + { > + struct codetag_bytes tags[10]; > + size_t i, nr; > + > + nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false); > + if (nr) { > + printk(KERN_NOTICE "Memory allocations:\n"); > + for (i = 0; i < nr; i++) { > + struct codetag *ct = tags[i].ct; > + struct alloc_tag *tag = ct_to_alloc_tag(ct); > + struct alloc_tag_counters counter = alloc_tag_read(tag); > + > + /* Same as alloc_tag_to_text() but w/o intermediate buffer */ > + if (ct->modname) > + printk(KERN_NOTICE "%12lli %8llu %s:%u [%s] func:%s\n", > + counter.bytes, counter.calls, ct->filename, > + ct->lineno, ct->modname, ct->function); > + else > + printk(KERN_NOTICE "%12lli %8llu %s:%u func:%s\n", > + counter.bytes, counter.calls, ct->filename, > + ct->lineno, ct->function); > + } > + } > + } > +#endif > }
On Tue, Feb 27, 2024 at 5:18 AM Vlastimil Babka <vbabka@suse.cz> wrote: > > On 2/21/24 20:40, Suren Baghdasaryan wrote: > > Include allocations in show_mem reports. > > > > Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> > > Signed-off-by: Suren Baghdasaryan <surenb@google.com> > > Reviewed-by: Vlastimil Babka <vbabka@suse.cz> > > Nit: there's pr_notice() that's shorter than printk(KERN_NOTICE I used printk() since other parts of show_mem() used it but I can change if that's preferable. > > > --- > > include/linux/alloc_tag.h | 7 +++++++ > > include/linux/codetag.h | 1 + > > lib/alloc_tag.c | 38 ++++++++++++++++++++++++++++++++++++++ > > lib/codetag.c | 5 +++++ > > mm/show_mem.c | 26 ++++++++++++++++++++++++++ > > 5 files changed, 77 insertions(+) > > > > diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h > > index 29636719b276..85a24a027403 100644 > > --- a/include/linux/alloc_tag.h > > +++ b/include/linux/alloc_tag.h > > @@ -30,6 +30,13 @@ struct alloc_tag { > > > > #ifdef CONFIG_MEM_ALLOC_PROFILING > > > > +struct codetag_bytes { > > + struct codetag *ct; > > + s64 bytes; > > +}; > > + > > +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep); > > + > > static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct) > > { > > return container_of(ct, struct alloc_tag, ct); > > diff --git a/include/linux/codetag.h b/include/linux/codetag.h > > index bfd0ba5c4185..c2a579ccd455 100644 > > --- a/include/linux/codetag.h > > +++ b/include/linux/codetag.h > > @@ -61,6 +61,7 @@ struct codetag_iterator { > > } > > > > void codetag_lock_module_list(struct codetag_type *cttype, bool lock); > > +bool codetag_trylock_module_list(struct codetag_type *cttype); > > struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype); > > struct codetag *codetag_next_ct(struct codetag_iterator *iter); > > > > diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c > > index cb5adec4b2e2..ec54f29482dc 100644 > > --- a/lib/alloc_tag.c > > +++ b/lib/alloc_tag.c > > @@ -86,6 +86,44 @@ static const struct seq_operations allocinfo_seq_op = { > > .show = allocinfo_show, > > }; > > > > +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep) > > +{ > > + struct codetag_iterator iter; > > + struct codetag *ct; > > + struct codetag_bytes n; > > + unsigned int i, nr = 0; > > + > > + if (can_sleep) > > + codetag_lock_module_list(alloc_tag_cttype, true); > > + else if (!codetag_trylock_module_list(alloc_tag_cttype)) > > + return 0; > > + > > + iter = codetag_get_ct_iter(alloc_tag_cttype); > > + while ((ct = codetag_next_ct(&iter))) { > > + struct alloc_tag_counters counter = alloc_tag_read(ct_to_alloc_tag(ct)); > > + > > + n.ct = ct; > > + n.bytes = counter.bytes; > > + > > + for (i = 0; i < nr; i++) > > + if (n.bytes > tags[i].bytes) > > + break; > > + > > + if (i < count) { > > + nr -= nr == count; > > + memmove(&tags[i + 1], > > + &tags[i], > > + sizeof(tags[0]) * (nr - i)); > > + nr++; > > + tags[i] = n; > > + } > > + } > > + > > + codetag_lock_module_list(alloc_tag_cttype, false); > > + > > + return nr; > > +} > > + > > static void __init procfs_init(void) > > { > > proc_create_seq("allocinfo", 0444, NULL, &allocinfo_seq_op); > > diff --git a/lib/codetag.c b/lib/codetag.c > > index b13412ca57cc..7b39cec9648a 100644 > > --- a/lib/codetag.c > > +++ b/lib/codetag.c > > @@ -36,6 +36,11 @@ void codetag_lock_module_list(struct codetag_type *cttype, bool lock) > > up_read(&cttype->mod_lock); > > } > > > > +bool codetag_trylock_module_list(struct codetag_type *cttype) > > +{ > > + return down_read_trylock(&cttype->mod_lock) != 0; > > +} > > + > > struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype) > > { > > struct codetag_iterator iter = { > > diff --git a/mm/show_mem.c b/mm/show_mem.c > > index 8dcfafbd283c..1e41f8d6e297 100644 > > --- a/mm/show_mem.c > > +++ b/mm/show_mem.c > > @@ -423,4 +423,30 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) > > #ifdef CONFIG_MEMORY_FAILURE > > printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages)); > > #endif > > +#ifdef CONFIG_MEM_ALLOC_PROFILING > > + { > > + struct codetag_bytes tags[10]; > > + size_t i, nr; > > + > > + nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false); > > + if (nr) { > > + printk(KERN_NOTICE "Memory allocations:\n"); > > + for (i = 0; i < nr; i++) { > > + struct codetag *ct = tags[i].ct; > > + struct alloc_tag *tag = ct_to_alloc_tag(ct); > > + struct alloc_tag_counters counter = alloc_tag_read(tag); > > + > > + /* Same as alloc_tag_to_text() but w/o intermediate buffer */ > > + if (ct->modname) > > + printk(KERN_NOTICE "%12lli %8llu %s:%u [%s] func:%s\n", > > + counter.bytes, counter.calls, ct->filename, > > + ct->lineno, ct->modname, ct->function); > > + else > > + printk(KERN_NOTICE "%12lli %8llu %s:%u func:%s\n", > > + counter.bytes, counter.calls, ct->filename, > > + ct->lineno, ct->function); > > + } > > + } > > + } > > +#endif > > } > > -- > To unsubscribe from this group and stop receiving emails from it, send an email to kernel-team+unsubscribe@android.com. >
diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 29636719b276..85a24a027403 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -30,6 +30,13 @@ struct alloc_tag { #ifdef CONFIG_MEM_ALLOC_PROFILING +struct codetag_bytes { + struct codetag *ct; + s64 bytes; +}; + +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep); + static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct) { return container_of(ct, struct alloc_tag, ct); diff --git a/include/linux/codetag.h b/include/linux/codetag.h index bfd0ba5c4185..c2a579ccd455 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -61,6 +61,7 @@ struct codetag_iterator { } void codetag_lock_module_list(struct codetag_type *cttype, bool lock); +bool codetag_trylock_module_list(struct codetag_type *cttype); struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype); struct codetag *codetag_next_ct(struct codetag_iterator *iter); diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index cb5adec4b2e2..ec54f29482dc 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -86,6 +86,44 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep) +{ + struct codetag_iterator iter; + struct codetag *ct; + struct codetag_bytes n; + unsigned int i, nr = 0; + + if (can_sleep) + codetag_lock_module_list(alloc_tag_cttype, true); + else if (!codetag_trylock_module_list(alloc_tag_cttype)) + return 0; + + iter = codetag_get_ct_iter(alloc_tag_cttype); + while ((ct = codetag_next_ct(&iter))) { + struct alloc_tag_counters counter = alloc_tag_read(ct_to_alloc_tag(ct)); + + n.ct = ct; + n.bytes = counter.bytes; + + for (i = 0; i < nr; i++) + if (n.bytes > tags[i].bytes) + break; + + if (i < count) { + nr -= nr == count; + memmove(&tags[i + 1], + &tags[i], + sizeof(tags[0]) * (nr - i)); + nr++; + tags[i] = n; + } + } + + codetag_lock_module_list(alloc_tag_cttype, false); + + return nr; +} + static void __init procfs_init(void) { proc_create_seq("allocinfo", 0444, NULL, &allocinfo_seq_op); diff --git a/lib/codetag.c b/lib/codetag.c index b13412ca57cc..7b39cec9648a 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -36,6 +36,11 @@ void codetag_lock_module_list(struct codetag_type *cttype, bool lock) up_read(&cttype->mod_lock); } +bool codetag_trylock_module_list(struct codetag_type *cttype) +{ + return down_read_trylock(&cttype->mod_lock) != 0; +} + struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype) { struct codetag_iterator iter = { diff --git a/mm/show_mem.c b/mm/show_mem.c index 8dcfafbd283c..1e41f8d6e297 100644 --- a/mm/show_mem.c +++ b/mm/show_mem.c @@ -423,4 +423,30 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) #ifdef CONFIG_MEMORY_FAILURE printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages)); #endif +#ifdef CONFIG_MEM_ALLOC_PROFILING + { + struct codetag_bytes tags[10]; + size_t i, nr; + + nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false); + if (nr) { + printk(KERN_NOTICE "Memory allocations:\n"); + for (i = 0; i < nr; i++) { + struct codetag *ct = tags[i].ct; + struct alloc_tag *tag = ct_to_alloc_tag(ct); + struct alloc_tag_counters counter = alloc_tag_read(tag); + + /* Same as alloc_tag_to_text() but w/o intermediate buffer */ + if (ct->modname) + printk(KERN_NOTICE "%12lli %8llu %s:%u [%s] func:%s\n", + counter.bytes, counter.calls, ct->filename, + ct->lineno, ct->modname, ct->function); + else + printk(KERN_NOTICE "%12lli %8llu %s:%u func:%s\n", + counter.bytes, counter.calls, ct->filename, + ct->lineno, ct->function); + } + } + } +#endif }