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/seq_file.h>
14 #include <linux/debugfs.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 tracer_stat *ts;
27 struct list_head stat_list;
28 struct mutex stat_mutex;
31 /* All of the sessions currently in use. Each stat file embeed one session */
32 static struct tracer_stat_session **all_stat_sessions;
33 static int nb_sessions;
34 static struct dentry *stat_dir, **stat_files;
37 static void reset_stat_session(struct tracer_stat_session *session)
39 struct trace_stat_list *node, *next;
41 list_for_each_entry_safe(node, next, &session->stat_list, list)
44 INIT_LIST_HEAD(&session->stat_list);
47 /* Called when a tracer is initialized */
48 static int init_all_sessions(int nb, struct tracer_stat *ts)
51 struct tracer_stat_session *session;
55 if (all_stat_sessions) {
56 for (i = 0; i < nb_sessions; i++) {
57 session = all_stat_sessions[i];
58 reset_stat_session(session);
59 mutex_destroy(&session->stat_mutex);
63 all_stat_sessions = kmalloc(sizeof(struct tracer_stat_session *) * nb,
65 if (!all_stat_sessions)
68 for (i = 0; i < nb; i++) {
69 session = kmalloc(sizeof(struct tracer_stat_session) * nb,
74 INIT_LIST_HEAD(&session->stat_list);
75 mutex_init(&session->stat_mutex);
77 all_stat_sessions[i] = session;
84 for (j = 0; j < i; j++)
85 kfree(all_stat_sessions[i]);
87 kfree(all_stat_sessions);
88 all_stat_sessions = NULL;
93 static int basic_tracer_stat_checks(struct tracer_stat *ts)
100 for (i = 0; ts[i].name; i++) {
101 if (!ts[i].stat_start || !ts[i].stat_next || !ts[i].stat_show)
108 * For tracers that don't provide a stat_cmp callback.
109 * This one will force an immediate insertion on tail of
112 static int dummy_cmp(void *p1, void *p2)
118 * Initialize the stat list at each trace_stat file opening.
119 * All of these copies and sorting are required on all opening
120 * since the stats could have changed between two file sessions.
122 static int stat_seq_init(struct tracer_stat_session *session)
124 struct trace_stat_list *iter_entry, *new_entry;
125 struct tracer_stat *ts = session->ts;
130 mutex_lock(&session->stat_mutex);
131 reset_stat_session(session);
134 ts->stat_cmp = dummy_cmp;
137 * The first entry. Actually this is the second, but the first
138 * one (the stat_list head) is pointless.
140 new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
146 INIT_LIST_HEAD(&new_entry->list);
148 list_add(&new_entry->list, &session->stat_list);
150 new_entry->stat = ts->stat_start();
151 prev_stat = new_entry->stat;
154 * Iterate over the tracer stat entries and store them in a sorted
158 new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
164 INIT_LIST_HEAD(&new_entry->list);
165 new_entry->stat = ts->stat_next(prev_stat, i);
167 /* End of insertion */
168 if (!new_entry->stat)
171 list_for_each_entry(iter_entry, &session->stat_list, list) {
173 /* Insertion with a descendent sorting */
174 if (ts->stat_cmp(new_entry->stat,
175 iter_entry->stat) > 0) {
177 list_add_tail(&new_entry->list,
181 /* The current smaller value */
182 } else if (list_is_last(&iter_entry->list,
183 &session->stat_list)) {
184 list_add(&new_entry->list, &iter_entry->list);
189 prev_stat = new_entry->stat;
192 mutex_unlock(&session->stat_mutex);
196 reset_stat_session(session);
197 mutex_unlock(&session->stat_mutex);
202 static void *stat_seq_start(struct seq_file *s, loff_t *pos)
204 struct tracer_stat_session *session = s->private;
206 /* Prevent from tracer switch or stat_list modification */
207 mutex_lock(&session->stat_mutex);
209 /* If we are in the beginning of the file, print the headers */
210 if (!*pos && session->ts->stat_headers)
211 session->ts->stat_headers(s);
213 return seq_list_start(&session->stat_list, *pos);
216 static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
218 struct tracer_stat_session *session = s->private;
220 return seq_list_next(p, &session->stat_list, pos);
223 static void stat_seq_stop(struct seq_file *s, void *p)
225 struct tracer_stat_session *session = s->private;
226 mutex_unlock(&session->stat_mutex);
229 static int stat_seq_show(struct seq_file *s, void *v)
231 struct tracer_stat_session *session = s->private;
232 struct trace_stat_list *l = list_entry(v, struct trace_stat_list, list);
234 return session->ts->stat_show(s, l->stat);
237 static const struct seq_operations trace_stat_seq_ops = {
238 .start = stat_seq_start,
239 .next = stat_seq_next,
240 .stop = stat_seq_stop,
241 .show = stat_seq_show
244 /* The session stat is refilled and resorted at each stat file opening */
245 static int tracing_stat_open(struct inode *inode, struct file *file)
249 struct tracer_stat_session *session = inode->i_private;
251 ret = seq_open(file, &trace_stat_seq_ops);
253 struct seq_file *m = file->private_data;
254 m->private = session;
255 ret = stat_seq_init(session);
263 * Avoid consuming memory with our now useless list.
265 static int tracing_stat_release(struct inode *i, struct file *f)
267 struct tracer_stat_session *session = i->i_private;
269 mutex_lock(&session->stat_mutex);
270 reset_stat_session(session);
271 mutex_unlock(&session->stat_mutex);
276 static const struct file_operations tracing_stat_fops = {
277 .open = tracing_stat_open,
280 .release = tracing_stat_release
284 static void destroy_trace_stat_files(void)
289 for (i = 0; i < nb_sessions; i++)
290 debugfs_remove(stat_files[i]);
296 static void init_trace_stat_files(void)
300 if (!stat_dir || !nb_sessions)
303 stat_files = kmalloc(sizeof(struct dentry *) * nb_sessions, GFP_KERNEL);
306 pr_warning("trace stat: not enough memory\n");
310 for (i = 0; i < nb_sessions; i++) {
311 struct tracer_stat_session *session = all_stat_sessions[i];
312 stat_files[i] = debugfs_create_file(session->ts->name, 0644,
314 session, &tracing_stat_fops);
316 pr_warning("cannot create %s entry\n",
321 void init_tracer_stat(struct tracer *trace)
323 int nb = basic_tracer_stat_checks(trace->stats);
325 destroy_trace_stat_files();
328 pr_warning("stat tracing: missing stat callback on %s\n",
335 init_all_sessions(nb, trace->stats);
336 init_trace_stat_files();
339 static int __init tracing_stat_init(void)
341 struct dentry *d_tracing;
343 d_tracing = tracing_init_dentry();
345 stat_dir = debugfs_create_dir("trace_stat", d_tracing);
347 pr_warning("Could not create debugfs "
348 "'trace_stat' entry\n");
351 fs_initcall(tracing_stat_init);