2 * Infrastructure for statistic tracing (histogram output).
4 * Copyright (C) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
6 * Based on the code from trace_branch.c which is
7 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
12 #include <linux/list.h>
13 #include <linux/rbtree.h>
14 #include <linux/debugfs.h>
15 #include "trace_stat.h"
20 * List of stat red-black nodes from a tracer
21 * We use a such tree to sort quickly the stat
22 * entries from the tracer.
29 /* A stat session is the stats output in one file */
31 struct list_head session_list;
32 struct tracer_stat *ts;
33 struct rb_root stat_root;
34 struct mutex stat_mutex;
38 /* All of the sessions currently in use. Each stat file embed one session */
39 static LIST_HEAD(all_stat_sessions);
40 static DEFINE_MUTEX(all_stat_sessions_mutex);
42 /* The root directory for all stat files */
43 static struct dentry *stat_dir;
46 * Iterate through the rbtree using a post order traversal path
47 * to release the next node.
48 * It won't necessary release one at each iteration
49 * but it will at least advance closer to the next one
52 static struct rb_node *release_next(struct rb_node *node)
54 struct stat_node *snode;
55 struct rb_node *parent = rb_parent(node);
59 else if (node->rb_right)
60 return node->rb_right;
64 if (parent->rb_left == node)
65 parent->rb_left = NULL;
67 parent->rb_right = NULL;
69 snode = container_of(node, struct stat_node, node);
76 static void reset_stat_session(struct stat_session *session)
78 struct rb_node *node = session->stat_root.rb_node;
81 node = release_next(node);
83 session->stat_root = RB_ROOT;
86 static void destroy_session(struct stat_session *session)
88 debugfs_remove(session->file);
89 reset_stat_session(session);
90 mutex_destroy(&session->stat_mutex);
94 typedef int (*cmp_stat_t)(void *, void *);
97 insert_stat(struct rb_root *root, struct stat_node *data, cmp_stat_t cmp)
99 struct rb_node **new = &(root->rb_node), *parent = NULL;
102 * Figure out where to put new node
103 * This is a descendent sorting
106 struct stat_node *this;
109 this = container_of(*new, struct stat_node, node);
110 result = cmp(data->stat, this->stat);
114 new = &((*new)->rb_left);
116 new = &((*new)->rb_right);
119 rb_link_node(&data->node, parent, new);
120 rb_insert_color(&data->node, root);
124 * For tracers that don't provide a stat_cmp callback.
125 * This one will force an immediate insertion on tail of
128 static int dummy_cmp(void *p1, void *p2)
134 * Initialize the stat list at each trace_stat file opening.
135 * All of these copies and sorting are required on all opening
136 * since the stats could have changed between two file sessions.
138 static int stat_seq_init(struct stat_session *session)
140 struct tracer_stat *ts = session->ts;
141 struct stat_node *new_entry;
142 struct rb_root *root;
147 mutex_lock(&session->stat_mutex);
148 reset_stat_session(session);
151 ts->stat_cmp = dummy_cmp;
153 stat = ts->stat_start(ts);
158 * The first entry. Actually this is the second, but the first
159 * one (the stat_list head) is pointless.
161 new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
166 root = &session->stat_root;
167 insert_stat(root, new_entry, dummy_cmp);
169 new_entry->stat = stat;
172 * Iterate over the tracer stat entries and store them in a sorted
176 stat = ts->stat_next(stat, i);
178 /* End of insertion */
182 new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
188 new_entry->stat = stat;
190 insert_stat(root, new_entry, ts->stat_cmp);
194 mutex_unlock(&session->stat_mutex);
198 reset_stat_session(session);
199 mutex_unlock(&session->stat_mutex);
204 static void *stat_seq_start(struct seq_file *s, loff_t *pos)
206 struct stat_session *session = s->private;
207 struct rb_node *node;
210 /* Prevent from tracer switch or stat_list modification */
211 mutex_lock(&session->stat_mutex);
213 /* If we are in the beginning of the file, print the headers */
214 if (!*pos && session->ts->stat_headers) {
216 return SEQ_START_TOKEN;
219 node = rb_first(&session->stat_root);
220 for (i = 0; node && i < *pos; i++)
221 node = rb_next(node);
228 static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
230 struct stat_session *session = s->private;
231 struct rb_node *node = p;
235 if (p == SEQ_START_TOKEN)
236 return rb_first(&session->stat_root);
238 return rb_next(node);
241 static void stat_seq_stop(struct seq_file *s, void *p)
243 struct stat_session *session = s->private;
244 mutex_unlock(&session->stat_mutex);
247 static int stat_seq_show(struct seq_file *s, void *v)
249 struct stat_session *session = s->private;
250 struct stat_node *l = container_of(v, struct stat_node, node);
252 if (v == SEQ_START_TOKEN)
253 return session->ts->stat_headers(s);
255 return session->ts->stat_show(s, l->stat);
258 static const struct seq_operations trace_stat_seq_ops = {
259 .start = stat_seq_start,
260 .next = stat_seq_next,
261 .stop = stat_seq_stop,
262 .show = stat_seq_show
265 /* The session stat is refilled and resorted at each stat file opening */
266 static int tracing_stat_open(struct inode *inode, struct file *file)
270 struct stat_session *session = inode->i_private;
272 ret = seq_open(file, &trace_stat_seq_ops);
274 struct seq_file *m = file->private_data;
275 m->private = session;
276 ret = stat_seq_init(session);
283 * Avoid consuming memory with our now useless list.
285 static int tracing_stat_release(struct inode *i, struct file *f)
287 struct stat_session *session = i->i_private;
289 mutex_lock(&session->stat_mutex);
290 reset_stat_session(session);
291 mutex_unlock(&session->stat_mutex);
296 static const struct file_operations tracing_stat_fops = {
297 .open = tracing_stat_open,
300 .release = tracing_stat_release
303 static int tracing_stat_init(void)
305 struct dentry *d_tracing;
307 d_tracing = tracing_init_dentry();
309 stat_dir = debugfs_create_dir("trace_stat", d_tracing);
311 pr_warning("Could not create debugfs "
312 "'trace_stat' entry\n");
316 static int init_stat_file(struct stat_session *session)
318 if (!stat_dir && tracing_stat_init())
321 session->file = debugfs_create_file(session->ts->name, 0644,
323 session, &tracing_stat_fops);
329 int register_stat_tracer(struct tracer_stat *trace)
331 struct stat_session *session, *node, *tmp;
337 if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
340 /* Already registered? */
341 mutex_lock(&all_stat_sessions_mutex);
342 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
343 if (node->ts == trace) {
344 mutex_unlock(&all_stat_sessions_mutex);
348 mutex_unlock(&all_stat_sessions_mutex);
350 /* Init the session */
351 session = kzalloc(sizeof(*session), GFP_KERNEL);
356 INIT_LIST_HEAD(&session->session_list);
357 mutex_init(&session->stat_mutex);
359 ret = init_stat_file(session);
361 destroy_session(session);
366 mutex_lock(&all_stat_sessions_mutex);
367 list_add_tail(&session->session_list, &all_stat_sessions);
368 mutex_unlock(&all_stat_sessions_mutex);
373 void unregister_stat_tracer(struct tracer_stat *trace)
375 struct stat_session *node, *tmp;
377 mutex_lock(&all_stat_sessions_mutex);
378 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
379 if (node->ts == trace) {
380 list_del(&node->session_list);
381 destroy_session(node);
385 mutex_unlock(&all_stat_sessions_mutex);