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/slab.h>
14 #include <linux/rbtree.h>
15 #include <linux/debugfs.h>
16 #include "trace_stat.h"
21 * List of stat red-black nodes from a tracer
22 * We use a such tree to sort quickly the stat
23 * entries from the tracer.
30 /* A stat session is the stats output in one file */
32 struct list_head session_list
;
33 struct tracer_stat
*ts
;
34 struct rb_root stat_root
;
35 struct mutex stat_mutex
;
39 /* All of the sessions currently in use. Each stat file embed one session */
40 static LIST_HEAD(all_stat_sessions
);
41 static DEFINE_MUTEX(all_stat_sessions_mutex
);
43 /* The root directory for all stat files */
44 static struct dentry
*stat_dir
;
46 static void __reset_stat_session(struct stat_session
*session
)
48 struct stat_node
*snode
, *n
;
50 rbtree_postorder_for_each_entry_safe(snode
, n
, &session
->stat_root
, node
) {
51 if (session
->ts
->stat_release
)
52 session
->ts
->stat_release(snode
->stat
);
56 session
->stat_root
= RB_ROOT
;
59 static void reset_stat_session(struct stat_session
*session
)
61 mutex_lock(&session
->stat_mutex
);
62 __reset_stat_session(session
);
63 mutex_unlock(&session
->stat_mutex
);
66 static void destroy_session(struct stat_session
*session
)
68 debugfs_remove(session
->file
);
69 __reset_stat_session(session
);
70 mutex_destroy(&session
->stat_mutex
);
74 typedef int (*cmp_stat_t
)(void *, void *);
76 static int insert_stat(struct rb_root
*root
, void *stat
, cmp_stat_t cmp
)
78 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
79 struct stat_node
*data
;
81 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
87 * Figure out where to put new node
88 * This is a descendent sorting
91 struct stat_node
*this;
94 this = container_of(*new, struct stat_node
, node
);
95 result
= cmp(data
->stat
, this->stat
);
99 new = &((*new)->rb_left
);
101 new = &((*new)->rb_right
);
104 rb_link_node(&data
->node
, parent
, new);
105 rb_insert_color(&data
->node
, root
);
110 * For tracers that don't provide a stat_cmp callback.
111 * This one will force an insertion as right-most node
114 static int dummy_cmp(void *p1
, void *p2
)
120 * Initialize the stat rbtree at each trace_stat file opening.
121 * All of these copies and sorting are required on all opening
122 * since the stats could have changed between two file sessions.
124 static int stat_seq_init(struct stat_session
*session
)
126 struct tracer_stat
*ts
= session
->ts
;
127 struct rb_root
*root
= &session
->stat_root
;
132 mutex_lock(&session
->stat_mutex
);
133 __reset_stat_session(session
);
136 ts
->stat_cmp
= dummy_cmp
;
138 stat
= ts
->stat_start(ts
);
142 ret
= insert_stat(root
, stat
, ts
->stat_cmp
);
147 * Iterate over the tracer stat entries and store them in an rbtree.
150 stat
= ts
->stat_next(stat
, i
);
152 /* End of insertion */
156 ret
= insert_stat(root
, stat
, ts
->stat_cmp
);
158 goto exit_free_rbtree
;
162 mutex_unlock(&session
->stat_mutex
);
166 __reset_stat_session(session
);
167 mutex_unlock(&session
->stat_mutex
);
172 static void *stat_seq_start(struct seq_file
*s
, loff_t
*pos
)
174 struct stat_session
*session
= s
->private;
175 struct rb_node
*node
;
179 /* Prevent from tracer switch or rbtree modification */
180 mutex_lock(&session
->stat_mutex
);
182 /* If we are in the beginning of the file, print the headers */
183 if (session
->ts
->stat_headers
) {
185 return SEQ_START_TOKEN
;
189 node
= rb_first(&session
->stat_root
);
190 for (i
= 0; node
&& i
< n
; i
++)
191 node
= rb_next(node
);
196 static void *stat_seq_next(struct seq_file
*s
, void *p
, loff_t
*pos
)
198 struct stat_session
*session
= s
->private;
199 struct rb_node
*node
= p
;
203 if (p
== SEQ_START_TOKEN
)
204 return rb_first(&session
->stat_root
);
206 return rb_next(node
);
209 static void stat_seq_stop(struct seq_file
*s
, void *p
)
211 struct stat_session
*session
= s
->private;
212 mutex_unlock(&session
->stat_mutex
);
215 static int stat_seq_show(struct seq_file
*s
, void *v
)
217 struct stat_session
*session
= s
->private;
218 struct stat_node
*l
= container_of(v
, struct stat_node
, node
);
220 if (v
== SEQ_START_TOKEN
)
221 return session
->ts
->stat_headers(s
);
223 return session
->ts
->stat_show(s
, l
->stat
);
226 static const struct seq_operations trace_stat_seq_ops
= {
227 .start
= stat_seq_start
,
228 .next
= stat_seq_next
,
229 .stop
= stat_seq_stop
,
230 .show
= stat_seq_show
233 /* The session stat is refilled and resorted at each stat file opening */
234 static int tracing_stat_open(struct inode
*inode
, struct file
*file
)
238 struct stat_session
*session
= inode
->i_private
;
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)
276 struct dentry
*d_tracing
;
278 d_tracing
= tracing_init_dentry();
282 stat_dir
= debugfs_create_dir("trace_stat", d_tracing
);
284 pr_warning("Could not create debugfs "
285 "'trace_stat' entry\n");
289 static int init_stat_file(struct stat_session
*session
)
291 if (!stat_dir
&& tracing_stat_init())
294 session
->file
= debugfs_create_file(session
->ts
->name
, 0644,
296 session
, &tracing_stat_fops
);
302 int register_stat_tracer(struct tracer_stat
*trace
)
304 struct stat_session
*session
, *node
;
310 if (!trace
->stat_start
|| !trace
->stat_next
|| !trace
->stat_show
)
313 /* Already registered? */
314 mutex_lock(&all_stat_sessions_mutex
);
315 list_for_each_entry(node
, &all_stat_sessions
, session_list
) {
316 if (node
->ts
== trace
) {
317 mutex_unlock(&all_stat_sessions_mutex
);
321 mutex_unlock(&all_stat_sessions_mutex
);
323 /* Init the session */
324 session
= kzalloc(sizeof(*session
), GFP_KERNEL
);
329 INIT_LIST_HEAD(&session
->session_list
);
330 mutex_init(&session
->stat_mutex
);
332 ret
= init_stat_file(session
);
334 destroy_session(session
);
339 mutex_lock(&all_stat_sessions_mutex
);
340 list_add_tail(&session
->session_list
, &all_stat_sessions
);
341 mutex_unlock(&all_stat_sessions_mutex
);
346 void unregister_stat_tracer(struct tracer_stat
*trace
)
348 struct stat_session
*node
, *tmp
;
350 mutex_lock(&all_stat_sessions_mutex
);
351 list_for_each_entry_safe(node
, tmp
, &all_stat_sessions
, session_list
) {
352 if (node
->ts
== trace
) {
353 list_del(&node
->session_list
);
354 destroy_session(node
);
358 mutex_unlock(&all_stat_sessions_mutex
);