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