blob: 96cffb269e7360e131e445c8c0013c90419f0921 [file] [log] [blame]
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -08001/*
2 * Infrastructure for statistic tracing (histogram output).
3 *
Frederic Weisbecker8f184f22009-05-16 06:24:36 +02004 * Copyright (C) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -08005 *
6 * Based on the code from trace_branch.c which is
7 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
8 *
9 */
10
11
12#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020014#include <linux/rbtree.h>
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080015#include <linux/debugfs.h>
Frederic Weisbecker002bb862009-01-10 11:34:13 -080016#include "trace_stat.h"
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080017#include "trace.h"
18
19
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020020/*
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.
24 */
25struct stat_node {
26 struct rb_node node;
Ingo Molnar55922172009-01-15 11:31:21 +010027 void *stat;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080028};
29
Frederic Weisbecker034939b2009-01-08 10:03:56 -080030/* A stat session is the stats output in one file */
Frederic Weisbecker0d64f832009-05-16 05:58:49 +020031struct stat_session {
Frederic Weisbecker002bb862009-01-10 11:34:13 -080032 struct list_head session_list;
Ingo Molnar55922172009-01-15 11:31:21 +010033 struct tracer_stat *ts;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020034 struct rb_root stat_root;
Ingo Molnar55922172009-01-15 11:31:21 +010035 struct mutex stat_mutex;
Frederic Weisbecker002bb862009-01-10 11:34:13 -080036 struct dentry *file;
Frederic Weisbecker034939b2009-01-08 10:03:56 -080037};
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080038
Wenji Huang73d8b8b2009-02-17 01:10:02 -050039/* All of the sessions currently in use. Each stat file embed one session */
Frederic Weisbecker002bb862009-01-10 11:34:13 -080040static LIST_HEAD(all_stat_sessions);
41static DEFINE_MUTEX(all_stat_sessions_mutex);
42
43/* The root directory for all stat files */
Ingo Molnar55922172009-01-15 11:31:21 +010044static struct dentry *stat_dir;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080045
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020046/*
47 * Iterate through the rbtree using a post order traversal path
48 * to release the next node.
49 * It won't necessary release one at each iteration
50 * but it will at least advance closer to the next one
51 * to be released.
52 */
Lai Jiangshand8ea37d2009-07-06 16:10:18 +080053static struct rb_node *release_next(struct tracer_stat *ts,
54 struct rb_node *node)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020055{
56 struct stat_node *snode;
57 struct rb_node *parent = rb_parent(node);
58
59 if (node->rb_left)
60 return node->rb_left;
61 else if (node->rb_right)
62 return node->rb_right;
63 else {
64 if (!parent)
Li Zefane1622802009-05-27 11:04:48 +080065 ;
66 else if (parent->rb_left == node)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020067 parent->rb_left = NULL;
68 else
69 parent->rb_right = NULL;
70
71 snode = container_of(node, struct stat_node, node);
Lai Jiangshand8ea37d2009-07-06 16:10:18 +080072 if (ts->stat_release)
73 ts->stat_release(snode->stat);
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020074 kfree(snode);
75
76 return parent;
77 }
78}
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080079
Li Zefan636eace2009-07-23 11:29:47 +080080static void __reset_stat_session(struct stat_session *session)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080081{
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020082 struct rb_node *node = session->stat_root.rb_node;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080083
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020084 while (node)
Lai Jiangshand8ea37d2009-07-06 16:10:18 +080085 node = release_next(session->ts, node);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080086
Frederic Weisbecker8f184f22009-05-16 06:24:36 +020087 session->stat_root = RB_ROOT;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080088}
89
Li Zefan636eace2009-07-23 11:29:47 +080090static void reset_stat_session(struct stat_session *session)
91{
92 mutex_lock(&session->stat_mutex);
93 __reset_stat_session(session);
94 mutex_unlock(&session->stat_mutex);
95}
96
Frederic Weisbecker0d64f832009-05-16 05:58:49 +020097static void destroy_session(struct stat_session *session)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -080098{
Frederic Weisbecker002bb862009-01-10 11:34:13 -080099 debugfs_remove(session->file);
Li Zefan636eace2009-07-23 11:29:47 +0800100 __reset_stat_session(session);
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800101 mutex_destroy(&session->stat_mutex);
102 kfree(session);
103}
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800104
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200105typedef int (*cmp_stat_t)(void *, void *);
106
Li Zefandbd3fbd2009-05-27 11:42:46 +0800107static int insert_stat(struct rb_root *root, void *stat, cmp_stat_t cmp)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200108{
109 struct rb_node **new = &(root->rb_node), *parent = NULL;
Li Zefandbd3fbd2009-05-27 11:42:46 +0800110 struct stat_node *data;
111
112 data = kzalloc(sizeof(*data), GFP_KERNEL);
113 if (!data)
114 return -ENOMEM;
115 data->stat = stat;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200116
117 /*
118 * Figure out where to put new node
119 * This is a descendent sorting
120 */
121 while (*new) {
122 struct stat_node *this;
123 int result;
124
125 this = container_of(*new, struct stat_node, node);
126 result = cmp(data->stat, this->stat);
127
128 parent = *new;
129 if (result >= 0)
130 new = &((*new)->rb_left);
131 else
132 new = &((*new)->rb_right);
133 }
134
135 rb_link_node(&data->node, parent, new);
136 rb_insert_color(&data->node, root);
Li Zefandbd3fbd2009-05-27 11:42:46 +0800137 return 0;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200138}
139
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800140/*
141 * For tracers that don't provide a stat_cmp callback.
Li Zefandbd3fbd2009-05-27 11:42:46 +0800142 * This one will force an insertion as right-most node
143 * in the rbtree.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800144 */
145static int dummy_cmp(void *p1, void *p2)
146{
Li Zefanb3dd7ba2009-05-27 11:04:26 +0800147 return -1;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800148}
149
150/*
Li Zefandbd3fbd2009-05-27 11:42:46 +0800151 * Initialize the stat rbtree at each trace_stat file opening.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800152 * All of these copies and sorting are required on all opening
153 * since the stats could have changed between two file sessions.
154 */
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200155static int stat_seq_init(struct stat_session *session)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800156{
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800157 struct tracer_stat *ts = session->ts;
Li Zefandbd3fbd2009-05-27 11:42:46 +0800158 struct rb_root *root = &session->stat_root;
Steven Rostedt09833522009-03-21 02:44:50 -0400159 void *stat;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800160 int ret = 0;
161 int i;
162
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800163 mutex_lock(&session->stat_mutex);
Li Zefan636eace2009-07-23 11:29:47 +0800164 __reset_stat_session(session);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800165
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800166 if (!ts->stat_cmp)
167 ts->stat_cmp = dummy_cmp;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800168
Steven Rostedt42548002009-03-24 13:38:36 -0400169 stat = ts->stat_start(ts);
Steven Rostedt09833522009-03-21 02:44:50 -0400170 if (!stat)
171 goto exit;
172
Li Zefandbd3fbd2009-05-27 11:42:46 +0800173 ret = insert_stat(root, stat, ts->stat_cmp);
174 if (ret)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800175 goto exit;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800176
177 /*
Li Zefandbd3fbd2009-05-27 11:42:46 +0800178 * Iterate over the tracer stat entries and store them in an rbtree.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800179 */
180 for (i = 1; ; i++) {
Steven Rostedt09833522009-03-21 02:44:50 -0400181 stat = ts->stat_next(stat, i);
182
183 /* End of insertion */
184 if (!stat)
185 break;
186
Li Zefandbd3fbd2009-05-27 11:42:46 +0800187 ret = insert_stat(root, stat, ts->stat_cmp);
188 if (ret)
189 goto exit_free_rbtree;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800190 }
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200191
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800192exit:
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800193 mutex_unlock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800194 return ret;
195
Li Zefandbd3fbd2009-05-27 11:42:46 +0800196exit_free_rbtree:
Li Zefan636eace2009-07-23 11:29:47 +0800197 __reset_stat_session(session);
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800198 mutex_unlock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800199 return ret;
200}
201
202
203static void *stat_seq_start(struct seq_file *s, loff_t *pos)
204{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200205 struct stat_session *session = s->private;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200206 struct rb_node *node;
Li Zefan97d53202009-08-17 16:52:53 +0800207 int n = *pos;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200208 int i;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800209
Li Zefandbd3fbd2009-05-27 11:42:46 +0800210 /* Prevent from tracer switch or rbtree modification */
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800211 mutex_lock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800212
213 /* If we are in the beginning of the file, print the headers */
Li Zefan97d53202009-08-17 16:52:53 +0800214 if (session->ts->stat_headers) {
215 if (n == 0)
216 return SEQ_START_TOKEN;
217 n--;
218 }
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800219
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200220 node = rb_first(&session->stat_root);
Li Zefan97d53202009-08-17 16:52:53 +0800221 for (i = 0; node && i < n; i++)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200222 node = rb_next(node);
223
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200224 return node;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800225}
226
227static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
228{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200229 struct stat_session *session = s->private;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200230 struct rb_node *node = p;
231
232 (*pos)++;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800233
Lai Jiangshane6f48902009-03-25 16:27:17 +0800234 if (p == SEQ_START_TOKEN)
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200235 return rb_first(&session->stat_root);
Lai Jiangshane6f48902009-03-25 16:27:17 +0800236
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200237 return rb_next(node);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800238}
239
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800240static void stat_seq_stop(struct seq_file *s, void *p)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800241{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200242 struct stat_session *session = s->private;
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800243 mutex_unlock(&session->stat_mutex);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800244}
245
246static int stat_seq_show(struct seq_file *s, void *v)
247{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200248 struct stat_session *session = s->private;
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200249 struct stat_node *l = container_of(v, struct stat_node, node);
Steven Rostedte8a9cbf2009-01-06 22:02:35 -0500250
Lai Jiangshane6f48902009-03-25 16:27:17 +0800251 if (v == SEQ_START_TOKEN)
252 return session->ts->stat_headers(s);
253
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800254 return session->ts->stat_show(s, l->stat);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800255}
256
257static const struct seq_operations trace_stat_seq_ops = {
Ingo Molnar55922172009-01-15 11:31:21 +0100258 .start = stat_seq_start,
259 .next = stat_seq_next,
260 .stop = stat_seq_stop,
261 .show = stat_seq_show
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800262};
263
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800264/* The session stat is refilled and resorted at each stat file opening */
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800265static int tracing_stat_open(struct inode *inode, struct file *file)
266{
267 int ret;
Li Zefan636eace2009-07-23 11:29:47 +0800268 struct seq_file *m;
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200269 struct stat_session *session = inode->i_private;
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800270
Li Zefan636eace2009-07-23 11:29:47 +0800271 ret = stat_seq_init(session);
272 if (ret)
273 return ret;
274
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800275 ret = seq_open(file, &trace_stat_seq_ops);
Li Zefan636eace2009-07-23 11:29:47 +0800276 if (ret) {
277 reset_stat_session(session);
278 return ret;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800279 }
280
Li Zefan636eace2009-07-23 11:29:47 +0800281 m = file->private_data;
282 m->private = session;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800283 return ret;
284}
285
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800286/*
Li Zefandbd3fbd2009-05-27 11:42:46 +0800287 * Avoid consuming memory with our now useless rbtree.
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800288 */
289static int tracing_stat_release(struct inode *i, struct file *f)
290{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200291 struct stat_session *session = i->i_private;
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800292
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800293 reset_stat_session(session);
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800294
Li Zefan636eace2009-07-23 11:29:47 +0800295 return seq_release(i, f);
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800296}
297
298static const struct file_operations tracing_stat_fops = {
299 .open = tracing_stat_open,
300 .read = seq_read,
301 .llseek = seq_lseek,
302 .release = tracing_stat_release
303};
304
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800305static int tracing_stat_init(void)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800306{
307 struct dentry *d_tracing;
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800308
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800309 d_tracing = tracing_init_dentry();
310
Frederic Weisbecker034939b2009-01-08 10:03:56 -0800311 stat_dir = debugfs_create_dir("trace_stat", d_tracing);
312 if (!stat_dir)
Frederic Weisbeckerdbd0b4b2008-12-28 20:44:51 -0800313 pr_warning("Could not create debugfs "
314 "'trace_stat' entry\n");
315 return 0;
316}
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800317
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200318static int init_stat_file(struct stat_session *session)
Frederic Weisbecker002bb862009-01-10 11:34:13 -0800319{
320 if (!stat_dir && tracing_stat_init())
321 return -ENODEV;
322
323 session->file = debugfs_create_file(session->ts->name, 0644,
324 stat_dir,
325 session, &tracing_stat_fops);
326 if (!session->file)
327 return -ENOMEM;
328 return 0;
329}
Ingo Molnar55922172009-01-15 11:31:21 +0100330
331int register_stat_tracer(struct tracer_stat *trace)
332{
Frederic Weisbecker43bd1232009-05-30 04:25:30 +0200333 struct stat_session *session, *node;
Ingo Molnar55922172009-01-15 11:31:21 +0100334 int ret;
335
336 if (!trace)
337 return -EINVAL;
338
339 if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
340 return -EINVAL;
341
342 /* Already registered? */
343 mutex_lock(&all_stat_sessions_mutex);
Frederic Weisbecker43bd1232009-05-30 04:25:30 +0200344 list_for_each_entry(node, &all_stat_sessions, session_list) {
Ingo Molnar55922172009-01-15 11:31:21 +0100345 if (node->ts == trace) {
346 mutex_unlock(&all_stat_sessions_mutex);
347 return -EINVAL;
348 }
349 }
350 mutex_unlock(&all_stat_sessions_mutex);
351
352 /* Init the session */
Frederic Weisbecker8f184f22009-05-16 06:24:36 +0200353 session = kzalloc(sizeof(*session), GFP_KERNEL);
Ingo Molnar55922172009-01-15 11:31:21 +0100354 if (!session)
355 return -ENOMEM;
356
357 session->ts = trace;
358 INIT_LIST_HEAD(&session->session_list);
Ingo Molnar55922172009-01-15 11:31:21 +0100359 mutex_init(&session->stat_mutex);
Ingo Molnar55922172009-01-15 11:31:21 +0100360
361 ret = init_stat_file(session);
362 if (ret) {
363 destroy_session(session);
364 return ret;
365 }
366
367 /* Register */
368 mutex_lock(&all_stat_sessions_mutex);
369 list_add_tail(&session->session_list, &all_stat_sessions);
370 mutex_unlock(&all_stat_sessions_mutex);
371
372 return 0;
373}
374
375void unregister_stat_tracer(struct tracer_stat *trace)
376{
Frederic Weisbecker0d64f832009-05-16 05:58:49 +0200377 struct stat_session *node, *tmp;
Ingo Molnar55922172009-01-15 11:31:21 +0100378
379 mutex_lock(&all_stat_sessions_mutex);
380 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
381 if (node->ts == trace) {
382 list_del(&node->session_list);
383 destroy_session(node);
384 break;
385 }
386 }
387 mutex_unlock(&all_stat_sessions_mutex);
388}