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