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