2 * Infrastructure for statistic tracing (histogram output).
4 * Copyright (C) 2008 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/debugfs.h>
14 #include "trace_stat.h"
18 /* List of stat entries from a tracer */
19 struct trace_stat_list {
20 struct list_head list;
24 /* A stat session is the stats output in one file */
25 struct tracer_stat_session {
26 struct list_head session_list;
27 struct tracer_stat *ts;
28 struct list_head stat_list;
29 struct mutex stat_mutex;
33 /* All of the sessions currently in use. Each stat file embed one session */
34 static LIST_HEAD(all_stat_sessions);
35 static DEFINE_MUTEX(all_stat_sessions_mutex);
37 /* The root directory for all stat files */
38 static struct dentry *stat_dir;
41 static void reset_stat_session(struct tracer_stat_session *session)
43 struct trace_stat_list *node, *next;
45 list_for_each_entry_safe(node, next, &session->stat_list, list)
48 INIT_LIST_HEAD(&session->stat_list);
51 static void destroy_session(struct tracer_stat_session *session)
53 debugfs_remove(session->file);
54 reset_stat_session(session);
55 mutex_destroy(&session->stat_mutex);
60 * For tracers that don't provide a stat_cmp callback.
61 * This one will force an immediate insertion on tail of
64 static int dummy_cmp(void *p1, void *p2)
70 * Initialize the stat list at each trace_stat file opening.
71 * All of these copies and sorting are required on all opening
72 * since the stats could have changed between two file sessions.
74 static int stat_seq_init(struct tracer_stat_session *session)
76 struct trace_stat_list *iter_entry, *new_entry;
77 struct tracer_stat *ts = session->ts;
82 mutex_lock(&session->stat_mutex);
83 reset_stat_session(session);
86 ts->stat_cmp = dummy_cmp;
88 stat = ts->stat_start();
93 * The first entry. Actually this is the second, but the first
94 * one (the stat_list head) is pointless.
96 new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
102 INIT_LIST_HEAD(&new_entry->list);
104 list_add(&new_entry->list, &session->stat_list);
106 new_entry->stat = stat;
109 * Iterate over the tracer stat entries and store them in a sorted
113 stat = ts->stat_next(stat, i);
115 /* End of insertion */
119 new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
125 INIT_LIST_HEAD(&new_entry->list);
126 new_entry->stat = stat;
128 list_for_each_entry(iter_entry, &session->stat_list, list) {
130 /* Insertion with a descendent sorting */
131 if (ts->stat_cmp(new_entry->stat,
132 iter_entry->stat) > 0) {
134 list_add_tail(&new_entry->list,
138 /* The current smaller value */
139 } else if (list_is_last(&iter_entry->list,
140 &session->stat_list)) {
141 list_add(&new_entry->list, &iter_entry->list);
147 mutex_unlock(&session->stat_mutex);
151 reset_stat_session(session);
152 mutex_unlock(&session->stat_mutex);
157 static void *stat_seq_start(struct seq_file *s, loff_t *pos)
159 struct tracer_stat_session *session = s->private;
161 /* Prevent from tracer switch or stat_list modification */
162 mutex_lock(&session->stat_mutex);
164 /* If we are in the beginning of the file, print the headers */
165 if (!*pos && session->ts->stat_headers)
166 return SEQ_START_TOKEN;
168 return seq_list_start(&session->stat_list, *pos);
171 static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
173 struct tracer_stat_session *session = s->private;
175 if (p == SEQ_START_TOKEN)
176 return seq_list_start(&session->stat_list, *pos);
178 return seq_list_next(p, &session->stat_list, pos);
181 static void stat_seq_stop(struct seq_file *s, void *p)
183 struct tracer_stat_session *session = s->private;
184 mutex_unlock(&session->stat_mutex);
187 static int stat_seq_show(struct seq_file *s, void *v)
189 struct tracer_stat_session *session = s->private;
190 struct trace_stat_list *l = list_entry(v, struct trace_stat_list, list);
192 if (v == SEQ_START_TOKEN)
193 return session->ts->stat_headers(s);
195 return session->ts->stat_show(s, l->stat);
198 static const struct seq_operations trace_stat_seq_ops = {
199 .start = stat_seq_start,
200 .next = stat_seq_next,
201 .stop = stat_seq_stop,
202 .show = stat_seq_show
205 /* The session stat is refilled and resorted at each stat file opening */
206 static int tracing_stat_open(struct inode *inode, struct file *file)
210 struct tracer_stat_session *session = inode->i_private;
212 ret = seq_open(file, &trace_stat_seq_ops);
214 struct seq_file *m = file->private_data;
215 m->private = session;
216 ret = stat_seq_init(session);
223 * Avoid consuming memory with our now useless list.
225 static int tracing_stat_release(struct inode *i, struct file *f)
227 struct tracer_stat_session *session = i->i_private;
229 mutex_lock(&session->stat_mutex);
230 reset_stat_session(session);
231 mutex_unlock(&session->stat_mutex);
236 static const struct file_operations tracing_stat_fops = {
237 .open = tracing_stat_open,
240 .release = tracing_stat_release
243 static int tracing_stat_init(void)
245 struct dentry *d_tracing;
247 d_tracing = tracing_init_dentry();
249 stat_dir = debugfs_create_dir("trace_stat", d_tracing);
251 pr_warning("Could not create debugfs "
252 "'trace_stat' entry\n");
256 static int init_stat_file(struct tracer_stat_session *session)
258 if (!stat_dir && tracing_stat_init())
261 session->file = debugfs_create_file(session->ts->name, 0644,
263 session, &tracing_stat_fops);
269 int register_stat_tracer(struct tracer_stat *trace)
271 struct tracer_stat_session *session, *node, *tmp;
277 if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
280 /* Already registered? */
281 mutex_lock(&all_stat_sessions_mutex);
282 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
283 if (node->ts == trace) {
284 mutex_unlock(&all_stat_sessions_mutex);
288 mutex_unlock(&all_stat_sessions_mutex);
290 /* Init the session */
291 session = kmalloc(sizeof(struct tracer_stat_session), GFP_KERNEL);
296 INIT_LIST_HEAD(&session->session_list);
297 INIT_LIST_HEAD(&session->stat_list);
298 mutex_init(&session->stat_mutex);
299 session->file = NULL;
301 ret = init_stat_file(session);
303 destroy_session(session);
308 mutex_lock(&all_stat_sessions_mutex);
309 list_add_tail(&session->session_list, &all_stat_sessions);
310 mutex_unlock(&all_stat_sessions_mutex);
315 void unregister_stat_tracer(struct tracer_stat *trace)
317 struct tracer_stat_session *node, *tmp;
319 mutex_lock(&all_stat_sessions_mutex);
320 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
321 if (node->ts == trace) {
322 list_del(&node->session_list);
323 destroy_session(node);
327 mutex_unlock(&all_stat_sessions_mutex);