1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure for statistic tracing (histogram output).
5 * Copyright (C) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
7 * Based on the code from trace_branch.c which is
8 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
12 #include <linux/security.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/rbtree.h>
16 #include <linux/tracefs.h>
17 #include "trace_stat.h"
22 * List of stat red-black nodes from a tracer
23 * We use a such tree to sort quickly the stat
24 * entries from the tracer.
31 /* A stat session is the stats output in one file */
33 struct list_head session_list;
34 struct tracer_stat *ts;
35 struct rb_root stat_root;
36 struct mutex stat_mutex;
40 /* All of the sessions currently in use. Each stat file embed one session */
41 static LIST_HEAD(all_stat_sessions);
42 static DEFINE_MUTEX(all_stat_sessions_mutex);
44 /* The root directory for all stat files */
45 static struct dentry *stat_dir;
47 static void __reset_stat_session(struct stat_session *session)
49 struct stat_node *snode, *n;
51 rbtree_postorder_for_each_entry_safe(snode, n, &session->stat_root, node) {
52 if (session->ts->stat_release)
53 session->ts->stat_release(snode->stat);
57 session->stat_root = RB_ROOT;
60 static void reset_stat_session(struct stat_session *session)
62 mutex_lock(&session->stat_mutex);
63 __reset_stat_session(session);
64 mutex_unlock(&session->stat_mutex);
67 static void destroy_session(struct stat_session *session)
69 tracefs_remove(session->file);
70 __reset_stat_session(session);
71 mutex_destroy(&session->stat_mutex);
75 static int insert_stat(struct rb_root *root, void *stat, cmp_func_t cmp)
77 struct rb_node **new = &(root->rb_node), *parent = NULL;
78 struct stat_node *data;
80 data = kzalloc(sizeof(*data), GFP_KERNEL);
86 * Figure out where to put new node
87 * This is a descendent sorting
90 struct stat_node *this;
93 this = container_of(*new, struct stat_node, node);
94 result = cmp(data->stat, this->stat);
98 new = &((*new)->rb_left);
100 new = &((*new)->rb_right);
103 rb_link_node(&data->node, parent, new);
104 rb_insert_color(&data->node, root);
109 * For tracers that don't provide a stat_cmp callback.
110 * This one will force an insertion as right-most node
113 static int dummy_cmp(const void *p1, const void *p2)
119 * Initialize the stat rbtree at each trace_stat file opening.
120 * All of these copies and sorting are required on all opening
121 * since the stats could have changed between two file sessions.
123 static int stat_seq_init(struct stat_session *session)
125 struct tracer_stat *ts = session->ts;
126 struct rb_root *root = &session->stat_root;
131 guard(mutex)(&session->stat_mutex);
132 __reset_stat_session(session);
135 ts->stat_cmp = dummy_cmp;
137 stat = ts->stat_start(ts);
141 ret = insert_stat(root, stat, ts->stat_cmp);
146 * Iterate over the tracer stat entries and store them in an rbtree.
149 stat = ts->stat_next(stat, i);
151 /* End of insertion */
155 ret = insert_stat(root, stat, ts->stat_cmp);
157 goto exit_free_rbtree;
163 __reset_stat_session(session);
168 static void *stat_seq_start(struct seq_file *s, loff_t *pos)
170 struct stat_session *session = s->private;
171 struct rb_node *node;
175 /* Prevent from tracer switch or rbtree modification */
176 mutex_lock(&session->stat_mutex);
178 /* If we are in the beginning of the file, print the headers */
179 if (session->ts->stat_headers) {
181 return SEQ_START_TOKEN;
185 node = rb_first(&session->stat_root);
186 for (i = 0; node && i < n; i++)
187 node = rb_next(node);
192 static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
194 struct stat_session *session = s->private;
195 struct rb_node *node = p;
199 if (p == SEQ_START_TOKEN)
200 return rb_first(&session->stat_root);
202 return rb_next(node);
205 static void stat_seq_stop(struct seq_file *s, void *p)
207 struct stat_session *session = s->private;
208 mutex_unlock(&session->stat_mutex);
211 static int stat_seq_show(struct seq_file *s, void *v)
213 struct stat_session *session = s->private;
214 struct stat_node *l = container_of(v, struct stat_node, node);
216 if (v == SEQ_START_TOKEN)
217 return session->ts->stat_headers(s);
219 return session->ts->stat_show(s, l->stat);
222 static const struct seq_operations trace_stat_seq_ops = {
223 .start = stat_seq_start,
224 .next = stat_seq_next,
225 .stop = stat_seq_stop,
226 .show = stat_seq_show
229 /* The session stat is refilled and resorted at each stat file opening */
230 static int tracing_stat_open(struct inode *inode, struct file *file)
234 struct stat_session *session = inode->i_private;
236 ret = security_locked_down(LOCKDOWN_TRACEFS);
240 ret = stat_seq_init(session);
244 ret = seq_open(file, &trace_stat_seq_ops);
246 reset_stat_session(session);
250 m = file->private_data;
251 m->private = session;
256 * Avoid consuming memory with our now useless rbtree.
258 static int tracing_stat_release(struct inode *i, struct file *f)
260 struct stat_session *session = i->i_private;
262 reset_stat_session(session);
264 return seq_release(i, f);
267 static const struct file_operations tracing_stat_fops = {
268 .open = tracing_stat_open,
271 .release = tracing_stat_release
274 static int tracing_stat_init(void)
278 ret = tracing_init_dentry();
282 stat_dir = tracefs_create_dir("trace_stat", NULL);
284 pr_warn("Could not create tracefs 'trace_stat' entry\n");
290 static int init_stat_file(struct stat_session *session)
294 if (!stat_dir && (ret = tracing_stat_init()))
297 session->file = tracefs_create_file(session->ts->name, TRACE_MODE_WRITE,
305 int register_stat_tracer(struct tracer_stat *trace)
307 struct stat_session *session, *node;
313 if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
316 guard(mutex)(&all_stat_sessions_mutex);
318 /* Already registered? */
319 list_for_each_entry(node, &all_stat_sessions, session_list) {
320 if (node->ts == trace)
324 /* Init the session */
325 session = kzalloc(sizeof(*session), GFP_KERNEL);
330 INIT_LIST_HEAD(&session->session_list);
331 mutex_init(&session->stat_mutex);
333 ret = init_stat_file(session);
335 destroy_session(session);
340 list_add_tail(&session->session_list, &all_stat_sessions);
345 void unregister_stat_tracer(struct tracer_stat *trace)
347 struct stat_session *node, *tmp;
349 mutex_lock(&all_stat_sessions_mutex);
350 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
351 if (node->ts == trace) {
352 list_del(&node->session_list);
353 destroy_session(node);
357 mutex_unlock(&all_stat_sessions_mutex);