From: Li Pengfei <ljdlns1987@gmail.com>
To: rostedt@goodmis.org, mhiramat@kernel.org
Cc: mathieu.desnoyers@efficios.com, mark.rutland@arm.com,
corbet@lwn.net, skhan@linuxfoundation.org,
linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org,
zhangbo56@xiaomi.com, lipengfei28@xiaomi.com, lkp@intel.com
Subject: [PATCH v8 3/5] tracing: Add stackmap statistics interface
Date: Sat, 7 Nov 2026 13:51:37 +0800 [thread overview]
Message-ID: <20261107055139.3138633-4-lipengfei28@xiaomi.com> (raw)
In-Reply-To: <20261107055139.3138633-1-lipengfei28@xiaomi.com>
From: Pengfei Li <lipengfei28@xiaomi.com>
Add stack_map_stat to report the map generation, the elements used, the
stacks recorded as a cookie, and the stacks the map could not store
because the element pool or the probe limit was exhausted, so that users
can tell when the map is too small.
Assisted-by: LLM
Signed-off-by: Pengfei Li <lipengfei28@xiaomi.com>
---
kernel/trace/trace.c | 4 ++
kernel/trace/trace_stackmap.c | 78 +++++++++++++++++++++++++++++++++++
kernel/trace/trace_stackmap.h | 1 +
3 files changed, 83 insertions(+)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index cdae2976d224..e58e7936c867 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -2197,6 +2197,9 @@ static __init void stackmap_init_tracefs(void)
return;
}
+ trace_create_file("stack_map_stat", TRACE_MODE_READ, NULL,
+ &global_trace, &ftrace_stackmap_stat_fops);
+
guard(mutex)(&trace_types_lock);
stackmap_state = STACKMAP_INIT_READY;
/* trace_options=stackmap on the command line */
@@ -4399,6 +4402,7 @@ static const char readme_msg[] =
#ifdef CONFIG_FTRACE_STACKMAP
" stack_map\t\t- Stacks recorded as cookies with options/stackmap\n"
"\t\t\t Write '0' to reset it, 'free' to release it\n"
+ " stack_map_stat\t- Usage of the stack map\n"
#endif
#ifdef CONFIG_DYNAMIC_EVENTS
" dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
diff --git a/kernel/trace/trace_stackmap.c b/kernel/trace/trace_stackmap.c
index b02eb22c76a9..5d2a406f39b9 100644
--- a/kernel/trace/trace_stackmap.c
+++ b/kernel/trace/trace_stackmap.c
@@ -42,6 +42,8 @@ struct ftrace_stackmap {
atomic_t resetting;
u32 generation;
refcount_t ref;
+ atomic_long_t __percpu *successes;
+ atomic_long_t __percpu *fallbacks;
};
/* Serializes tracefs readers against reset. */
@@ -73,6 +75,8 @@ static struct stackmap_elt *stackmap_get_elt(struct ftrace_stackmap *smap)
static void ftrace_stackmap_destroy(struct ftrace_stackmap *smap)
{
+ free_percpu(smap->fallbacks);
+ free_percpu(smap->successes);
vfree(smap->elts);
vfree(smap->entries);
kfree(smap);
@@ -100,6 +104,14 @@ static struct ftrace_stackmap *ftrace_stackmap_create(void)
if (!smap->elts)
goto fail;
+ smap->successes = alloc_percpu(atomic_long_t);
+ if (!smap->successes)
+ goto fail;
+
+ smap->fallbacks = alloc_percpu(atomic_long_t);
+ if (!smap->fallbacks)
+ goto fail;
+
smap->hash_seed = get_random_u32();
smap->generation = stackmap_next_gen;
refcount_set(&smap->ref, 1);
@@ -149,6 +161,7 @@ static void stackmap_put(struct ftrace_stackmap *smap)
static void stackmap_reset(struct trace_array *tr)
{
struct ftrace_stackmap *smap = stackmap_locked(tr);
+ int cpu;
if (!smap)
return;
@@ -161,6 +174,10 @@ static void stackmap_reset(struct trace_array *tr)
memset(smap->entries, 0, sizeof(*smap->entries) * smap->map_size);
memset(smap->elts, 0, sizeof(*smap->elts) * smap->max_elts);
atomic_set(&smap->next_elt, 0);
+ for_each_possible_cpu(cpu) {
+ atomic_long_set(per_cpu_ptr(smap->successes, cpu), 0);
+ atomic_long_set(per_cpu_ptr(smap->fallbacks, cpu), 0);
+ }
smap->generation++;
}
@@ -194,6 +211,11 @@ static int stackmap_free(struct trace_array *tr, struct ftrace_stackmap *self)
return ret;
}
+static void stackmap_count(atomic_long_t __percpu *counter)
+{
+ atomic_long_add_unless(this_cpu_ptr(counter), 1, LONG_MAX);
+}
+
bool ftrace_stackmap_get_cookie(struct ftrace_stackmap *smap,
unsigned long *ips, unsigned int nr_entries,
u64 *cookie)
@@ -254,8 +276,10 @@ bool ftrace_stackmap_get_cookie(struct ftrace_stackmap *smap,
probes++;
}
+ stackmap_count(smap->fallbacks);
return false;
found:
+ stackmap_count(smap->successes);
*cookie = ((u64)smap->generation << FTRACE_STACKMAP_COOKIE_GEN_SHIFT) | idx;
return true;
}
@@ -415,3 +439,57 @@ const struct file_operations ftrace_stackmap_fops = {
.llseek = seq_lseek,
.release = stackmap_release,
};
+
+static int stackmap_stat_show(struct seq_file *m, void *v)
+{
+ struct trace_array *tr = m->private;
+ struct ftrace_stackmap *smap;
+ u64 successes = 0, fallbacks = 0;
+ int cpu;
+
+ /* Reset and free also take trace_types_lock. */
+ guard(mutex)(&trace_types_lock);
+ smap = stackmap_locked(tr);
+ if (!smap)
+ return 0;
+
+ for_each_possible_cpu(cpu) {
+ successes += atomic_long_read(per_cpu_ptr(smap->successes, cpu));
+ fallbacks += atomic_long_read(per_cpu_ptr(smap->fallbacks, cpu));
+ }
+
+ seq_printf(m, "generation: %u\n", smap->generation);
+ seq_printf(m, "entries: %d\n", atomic_read(&smap->next_elt));
+ seq_printf(m, "capacity: %u\n", smap->max_elts);
+ seq_printf(m, "successes: %llu\n", successes);
+ seq_printf(m, "fallbacks: %llu\n", fallbacks);
+ return 0;
+}
+
+static int stackmap_stat_open(struct inode *inode, struct file *file)
+{
+ struct trace_array *tr = inode->i_private;
+ int ret;
+
+ ret = tracing_check_open_get_tr(tr);
+ if (ret)
+ return ret;
+
+ ret = single_open(file, stackmap_stat_show, tr);
+ if (ret)
+ trace_array_put(tr);
+ return ret;
+}
+
+static int stackmap_stat_release(struct inode *inode, struct file *file)
+{
+ trace_array_put(inode->i_private);
+ return single_release(inode, file);
+}
+
+const struct file_operations ftrace_stackmap_stat_fops = {
+ .open = stackmap_stat_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = stackmap_stat_release,
+};
diff --git a/kernel/trace/trace_stackmap.h b/kernel/trace/trace_stackmap.h
index 6699988da390..a18e41c08494 100644
--- a/kernel/trace/trace_stackmap.h
+++ b/kernel/trace/trace_stackmap.h
@@ -18,5 +18,6 @@ bool ftrace_stackmap_get_cookie(struct ftrace_stackmap *smap,
u64 *cookie);
extern const struct file_operations ftrace_stackmap_fops;
+extern const struct file_operations ftrace_stackmap_stat_fops;
#endif /* _TRACE_STACKMAP_H */
--
2.34.1
next prev parent reply other threads:[~2026-11-07 5:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-07 5:51 [PATCH v8 0/5] tracing: Add stack trace deduplication for the ring buffer Li Pengfei
2026-11-07 5:51 ` [PATCH v8 1/5] tracing: Add lock-free stackmap for stack trace deduplication Li Pengfei
2026-11-07 5:51 ` [PATCH v8 2/5] tracing: Use the stackmap from the ftrace stack recording path Li Pengfei
2026-11-07 22:35 ` kernel test robot
2026-11-07 5:51 ` Li Pengfei [this message]
[not found] ` <sashiko-outbox-202646@kernel.org>
2026-11-07 6:28 ` [PATCH v8 3/5] tracing: Add stackmap statistics interface Li Pengfei
2026-11-07 5:51 ` [PATCH v8 4/5] Documentation: tracing: Document the ftrace stackmap Li Pengfei
2026-11-07 5:51 ` [PATCH v8 5/5] selftests/ftrace: Add a stackmap basic functionality test Li Pengfei
[not found] ` <sashiko-outbox-202648@kernel.org>
2026-11-07 6:28 ` Li Pengfei
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261107055139.3138633-4-lipengfei28@xiaomi.com \
--to=ljdlns1987@gmail.com \
--cc=corbet@lwn.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=lipengfei28@xiaomi.com \
--cc=lkp@intel.com \
--cc=mark.rutland@arm.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=rostedt@goodmis.org \
--cc=skhan@linuxfoundation.org \
--cc=zhangbo56@xiaomi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®