kmemtrace: Rename some functions
[linux-2.6-block.git] / kernel / trace / trace_stat.c
CommitLineData
dbd0b4b3
FW
1/*
2 * Infrastructure for statistic tracing (histogram output).
3 *
8f184f27 4 * Copyright (C) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
dbd0b4b3
FW
5 *
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>
8f184f27 13#include <linux/rbtree.h>
dbd0b4b3 14#include <linux/debugfs.h>
002bb86d 15#include "trace_stat.h"
dbd0b4b3
FW
16#include "trace.h"
17
18
8f184f27
FW
19/*
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.
23 */
24struct stat_node {
25 struct rb_node node;
55922173 26 void *stat;
dbd0b4b3
FW
27};
28
034939b6 29/* A stat session is the stats output in one file */
0d64f834 30struct stat_session {
002bb86d 31 struct list_head session_list;
55922173 32 struct tracer_stat *ts;
8f184f27 33 struct rb_root stat_root;
55922173 34 struct mutex stat_mutex;
002bb86d 35 struct dentry *file;
034939b6 36};
dbd0b4b3 37
73d8b8bc 38/* All of the sessions currently in use. Each stat file embed one session */
002bb86d
FW
39static LIST_HEAD(all_stat_sessions);
40static DEFINE_MUTEX(all_stat_sessions_mutex);
41
42/* The root directory for all stat files */
55922173 43static struct dentry *stat_dir;
dbd0b4b3 44
8f184f27
FW
45/*
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
50 * to be released.
51 */
52static struct rb_node *release_next(struct rb_node *node)
53{
54 struct stat_node *snode;
55 struct rb_node *parent = rb_parent(node);
56
57 if (node->rb_left)
58 return node->rb_left;
59 else if (node->rb_right)
60 return node->rb_right;
61 else {
62 if (!parent)
e1622806
LZ
63 ;
64 else if (parent->rb_left == node)
8f184f27
FW
65 parent->rb_left = NULL;
66 else
67 parent->rb_right = NULL;
68
69 snode = container_of(node, struct stat_node, node);
70 kfree(snode);
71
72 return parent;
73 }
74}
dbd0b4b3 75
0d64f834 76static void reset_stat_session(struct stat_session *session)
dbd0b4b3 77{
8f184f27 78 struct rb_node *node = session->stat_root.rb_node;
dbd0b4b3 79
8f184f27
FW
80 while (node)
81 node = release_next(node);
dbd0b4b3 82
8f184f27 83 session->stat_root = RB_ROOT;
dbd0b4b3
FW
84}
85
0d64f834 86static void destroy_session(struct stat_session *session)
dbd0b4b3 87{
002bb86d
FW
88 debugfs_remove(session->file);
89 reset_stat_session(session);
90 mutex_destroy(&session->stat_mutex);
91 kfree(session);
92}
034939b6 93
8f184f27
FW
94typedef int (*cmp_stat_t)(void *, void *);
95
dbd3fbdf 96static int insert_stat(struct rb_root *root, void *stat, cmp_stat_t cmp)
8f184f27
FW
97{
98 struct rb_node **new = &(root->rb_node), *parent = NULL;
dbd3fbdf
LZ
99 struct stat_node *data;
100
101 data = kzalloc(sizeof(*data), GFP_KERNEL);
102 if (!data)
103 return -ENOMEM;
104 data->stat = stat;
8f184f27
FW
105
106 /*
107 * Figure out where to put new node
108 * This is a descendent sorting
109 */
110 while (*new) {
111 struct stat_node *this;
112 int result;
113
114 this = container_of(*new, struct stat_node, node);
115 result = cmp(data->stat, this->stat);
116
117 parent = *new;
118 if (result >= 0)
119 new = &((*new)->rb_left);
120 else
121 new = &((*new)->rb_right);
122 }
123
124 rb_link_node(&data->node, parent, new);
125 rb_insert_color(&data->node, root);
dbd3fbdf 126 return 0;
8f184f27
FW
127}
128
dbd0b4b3
FW
129/*
130 * For tracers that don't provide a stat_cmp callback.
dbd3fbdf
LZ
131 * This one will force an insertion as right-most node
132 * in the rbtree.
dbd0b4b3
FW
133 */
134static int dummy_cmp(void *p1, void *p2)
135{
b3dd7ba7 136 return -1;
dbd0b4b3
FW
137}
138
139/*
dbd3fbdf 140 * Initialize the stat rbtree at each trace_stat file opening.
dbd0b4b3
FW
141 * All of these copies and sorting are required on all opening
142 * since the stats could have changed between two file sessions.
143 */
0d64f834 144static int stat_seq_init(struct stat_session *session)
dbd0b4b3 145{
034939b6 146 struct tracer_stat *ts = session->ts;
dbd3fbdf 147 struct rb_root *root = &session->stat_root;
09833521 148 void *stat;
dbd0b4b3
FW
149 int ret = 0;
150 int i;
151
034939b6
FW
152 mutex_lock(&session->stat_mutex);
153 reset_stat_session(session);
dbd0b4b3 154
034939b6
FW
155 if (!ts->stat_cmp)
156 ts->stat_cmp = dummy_cmp;
dbd0b4b3 157
42548008 158 stat = ts->stat_start(ts);
09833521
SR
159 if (!stat)
160 goto exit;
161
dbd3fbdf
LZ
162 ret = insert_stat(root, stat, ts->stat_cmp);
163 if (ret)
dbd0b4b3 164 goto exit;
dbd0b4b3
FW
165
166 /*
dbd3fbdf 167 * Iterate over the tracer stat entries and store them in an rbtree.
dbd0b4b3
FW
168 */
169 for (i = 1; ; i++) {
09833521
SR
170 stat = ts->stat_next(stat, i);
171
172 /* End of insertion */
173 if (!stat)
174 break;
175
dbd3fbdf
LZ
176 ret = insert_stat(root, stat, ts->stat_cmp);
177 if (ret)
178 goto exit_free_rbtree;
dbd0b4b3 179 }
8f184f27 180
dbd0b4b3 181exit:
034939b6 182 mutex_unlock(&session->stat_mutex);
dbd0b4b3
FW
183 return ret;
184
dbd3fbdf 185exit_free_rbtree:
034939b6
FW
186 reset_stat_session(session);
187 mutex_unlock(&session->stat_mutex);
dbd0b4b3
FW
188 return ret;
189}
190
191
192static void *stat_seq_start(struct seq_file *s, loff_t *pos)
193{
0d64f834 194 struct stat_session *session = s->private;
8f184f27
FW
195 struct rb_node *node;
196 int i;
dbd0b4b3 197
dbd3fbdf 198 /* Prevent from tracer switch or rbtree modification */
034939b6 199 mutex_lock(&session->stat_mutex);
dbd0b4b3
FW
200
201 /* If we are in the beginning of the file, print the headers */
2961bf34 202 if (!*pos && session->ts->stat_headers)
e6f48901 203 return SEQ_START_TOKEN;
dbd0b4b3 204
8f184f27
FW
205 node = rb_first(&session->stat_root);
206 for (i = 0; node && i < *pos; i++)
207 node = rb_next(node);
208
8f184f27 209 return node;
dbd0b4b3
FW
210}
211
212static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
213{
0d64f834 214 struct stat_session *session = s->private;
8f184f27
FW
215 struct rb_node *node = p;
216
217 (*pos)++;
dbd0b4b3 218
e6f48901 219 if (p == SEQ_START_TOKEN)
8f184f27 220 return rb_first(&session->stat_root);
e6f48901 221
8f184f27 222 return rb_next(node);
dbd0b4b3
FW
223}
224
034939b6 225static void stat_seq_stop(struct seq_file *s, void *p)
dbd0b4b3 226{
0d64f834 227 struct stat_session *session = s->private;
034939b6 228 mutex_unlock(&session->stat_mutex);
dbd0b4b3
FW
229}
230
231static int stat_seq_show(struct seq_file *s, void *v)
232{
0d64f834 233 struct stat_session *session = s->private;
8f184f27 234 struct stat_node *l = container_of(v, struct stat_node, node);
ff288b27 235
e6f48901
LJ
236 if (v == SEQ_START_TOKEN)
237 return session->ts->stat_headers(s);
238
034939b6 239 return session->ts->stat_show(s, l->stat);
dbd0b4b3
FW
240}
241
242static const struct seq_operations trace_stat_seq_ops = {
55922173
IM
243 .start = stat_seq_start,
244 .next = stat_seq_next,
245 .stop = stat_seq_stop,
246 .show = stat_seq_show
dbd0b4b3
FW
247};
248
034939b6 249/* The session stat is refilled and resorted at each stat file opening */
dbd0b4b3
FW
250static int tracing_stat_open(struct inode *inode, struct file *file)
251{
252 int ret;
253
0d64f834 254 struct stat_session *session = inode->i_private;
034939b6 255
dbd0b4b3
FW
256 ret = seq_open(file, &trace_stat_seq_ops);
257 if (!ret) {
258 struct seq_file *m = file->private_data;
034939b6
FW
259 m->private = session;
260 ret = stat_seq_init(session);
dbd0b4b3
FW
261 }
262
263 return ret;
264}
265
dbd0b4b3 266/*
dbd3fbdf 267 * Avoid consuming memory with our now useless rbtree.
dbd0b4b3
FW
268 */
269static int tracing_stat_release(struct inode *i, struct file *f)
270{
0d64f834 271 struct stat_session *session = i->i_private;
034939b6
FW
272
273 mutex_lock(&session->stat_mutex);
274 reset_stat_session(session);
275 mutex_unlock(&session->stat_mutex);
276
dbd0b4b3
FW
277 return 0;
278}
279
280static const struct file_operations tracing_stat_fops = {
281 .open = tracing_stat_open,
282 .read = seq_read,
283 .llseek = seq_lseek,
284 .release = tracing_stat_release
285};
286
002bb86d 287static int tracing_stat_init(void)
dbd0b4b3
FW
288{
289 struct dentry *d_tracing;
dbd0b4b3 290
dbd0b4b3
FW
291 d_tracing = tracing_init_dentry();
292
034939b6
FW
293 stat_dir = debugfs_create_dir("trace_stat", d_tracing);
294 if (!stat_dir)
dbd0b4b3
FW
295 pr_warning("Could not create debugfs "
296 "'trace_stat' entry\n");
297 return 0;
298}
002bb86d 299
0d64f834 300static int init_stat_file(struct stat_session *session)
002bb86d
FW
301{
302 if (!stat_dir && tracing_stat_init())
303 return -ENODEV;
304
305 session->file = debugfs_create_file(session->ts->name, 0644,
306 stat_dir,
307 session, &tracing_stat_fops);
308 if (!session->file)
309 return -ENOMEM;
310 return 0;
311}
55922173
IM
312
313int register_stat_tracer(struct tracer_stat *trace)
314{
43bd1236 315 struct stat_session *session, *node;
55922173
IM
316 int ret;
317
318 if (!trace)
319 return -EINVAL;
320
321 if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
322 return -EINVAL;
323
324 /* Already registered? */
325 mutex_lock(&all_stat_sessions_mutex);
43bd1236 326 list_for_each_entry(node, &all_stat_sessions, session_list) {
55922173
IM
327 if (node->ts == trace) {
328 mutex_unlock(&all_stat_sessions_mutex);
329 return -EINVAL;
330 }
331 }
332 mutex_unlock(&all_stat_sessions_mutex);
333
334 /* Init the session */
8f184f27 335 session = kzalloc(sizeof(*session), GFP_KERNEL);
55922173
IM
336 if (!session)
337 return -ENOMEM;
338
339 session->ts = trace;
340 INIT_LIST_HEAD(&session->session_list);
55922173 341 mutex_init(&session->stat_mutex);
55922173
IM
342
343 ret = init_stat_file(session);
344 if (ret) {
345 destroy_session(session);
346 return ret;
347 }
348
349 /* Register */
350 mutex_lock(&all_stat_sessions_mutex);
351 list_add_tail(&session->session_list, &all_stat_sessions);
352 mutex_unlock(&all_stat_sessions_mutex);
353
354 return 0;
355}
356
357void unregister_stat_tracer(struct tracer_stat *trace)
358{
0d64f834 359 struct stat_session *node, *tmp;
55922173
IM
360
361 mutex_lock(&all_stat_sessions_mutex);
362 list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
363 if (node->ts == trace) {
364 list_del(&node->session_list);
365 destroy_session(node);
366 break;
367 }
368 }
369 mutex_unlock(&all_stat_sessions_mutex);
370}