ftrace, ia64: IA64 dynamic ftrace support
[linux-2.6-block.git] / kernel / trace / trace_stat.c
CommitLineData
dbd0b4b3
FW
1/*
2 * Infrastructure for statistic tracing (histogram output).
3 *
4 * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
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>
13#include <linux/seq_file.h>
14#include <linux/debugfs.h>
15#include "trace.h"
16
17
18/* List of stat entries from a tracer */
19struct trace_stat_list {
20 struct list_head list;
21 void *stat;
22};
23
034939b6
FW
24/* A stat session is the stats output in one file */
25struct tracer_stat_session {
26 struct tracer_stat *ts;
27 struct list_head stat_list;
28 struct mutex stat_mutex;
29};
dbd0b4b3 30
034939b6
FW
31/* All of the sessions currently in use. Each stat file embeed one session */
32static struct tracer_stat_session **all_stat_sessions;
33static int nb_sessions;
34static struct dentry *stat_dir, **stat_files;
dbd0b4b3
FW
35
36
034939b6 37static void reset_stat_session(struct tracer_stat_session *session)
dbd0b4b3 38{
ff288b27 39 struct trace_stat_list *node, *next;
dbd0b4b3 40
034939b6 41 list_for_each_entry_safe(node, next, &session->stat_list, list)
dbd0b4b3 42 kfree(node);
dbd0b4b3 43
034939b6 44 INIT_LIST_HEAD(&session->stat_list);
dbd0b4b3
FW
45}
46
034939b6
FW
47/* Called when a tracer is initialized */
48static int init_all_sessions(int nb, struct tracer_stat *ts)
dbd0b4b3 49{
034939b6
FW
50 int i, j;
51 struct tracer_stat_session *session;
52
53 nb_sessions = 0;
54
55 if (all_stat_sessions) {
56 for (i = 0; i < nb_sessions; i++) {
57 session = all_stat_sessions[i];
58 reset_stat_session(session);
59 mutex_destroy(&session->stat_mutex);
60 kfree(session);
61 }
62 }
63 all_stat_sessions = kmalloc(sizeof(struct tracer_stat_session *) * nb,
64 GFP_KERNEL);
65 if (!all_stat_sessions)
66 return -ENOMEM;
67
68 for (i = 0; i < nb; i++) {
69 session = kmalloc(sizeof(struct tracer_stat_session) * nb,
70 GFP_KERNEL);
71 if (!session)
72 goto free_sessions;
73
74 INIT_LIST_HEAD(&session->stat_list);
75 mutex_init(&session->stat_mutex);
76 session->ts = &ts[i];
77 all_stat_sessions[i] = session;
78 }
79 nb_sessions = nb;
80 return 0;
81
82free_sessions:
83
84 for (j = 0; j < i; j++)
85 kfree(all_stat_sessions[i]);
86
87 kfree(all_stat_sessions);
88 all_stat_sessions = NULL;
89
90 return -ENOMEM;
91}
92
93static int basic_tracer_stat_checks(struct tracer_stat *ts)
94{
95 int i;
96
97 if (!ts)
98 return 0;
99
100 for (i = 0; ts[i].name; i++) {
101 if (!ts[i].stat_start || !ts[i].stat_next || !ts[i].stat_show)
102 return -EBUSY;
103 }
104 return i;
dbd0b4b3
FW
105}
106
107/*
108 * For tracers that don't provide a stat_cmp callback.
109 * This one will force an immediate insertion on tail of
110 * the list.
111 */
112static int dummy_cmp(void *p1, void *p2)
113{
114 return 1;
115}
116
117/*
118 * Initialize the stat list at each trace_stat file opening.
119 * All of these copies and sorting are required on all opening
120 * since the stats could have changed between two file sessions.
121 */
034939b6 122static int stat_seq_init(struct tracer_stat_session *session)
dbd0b4b3
FW
123{
124 struct trace_stat_list *iter_entry, *new_entry;
034939b6 125 struct tracer_stat *ts = session->ts;
dbd0b4b3
FW
126 void *prev_stat;
127 int ret = 0;
128 int i;
129
034939b6
FW
130 mutex_lock(&session->stat_mutex);
131 reset_stat_session(session);
dbd0b4b3 132
034939b6
FW
133 if (!ts->stat_cmp)
134 ts->stat_cmp = dummy_cmp;
dbd0b4b3
FW
135
136 /*
137 * The first entry. Actually this is the second, but the first
138 * one (the stat_list head) is pointless.
139 */
140 new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
141 if (!new_entry) {
142 ret = -ENOMEM;
143 goto exit;
144 }
145
146 INIT_LIST_HEAD(&new_entry->list);
dbd0b4b3 147
034939b6
FW
148 list_add(&new_entry->list, &session->stat_list);
149
150 new_entry->stat = ts->stat_start();
dbd0b4b3
FW
151 prev_stat = new_entry->stat;
152
153 /*
154 * Iterate over the tracer stat entries and store them in a sorted
155 * list.
156 */
157 for (i = 1; ; i++) {
158 new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
159 if (!new_entry) {
160 ret = -ENOMEM;
161 goto exit_free_list;
162 }
163
164 INIT_LIST_HEAD(&new_entry->list);
034939b6 165 new_entry->stat = ts->stat_next(prev_stat, i);
dbd0b4b3
FW
166
167 /* End of insertion */
168 if (!new_entry->stat)
169 break;
170
034939b6
FW
171 list_for_each_entry(iter_entry, &session->stat_list, list) {
172
dbd0b4b3 173 /* Insertion with a descendent sorting */
034939b6 174 if (ts->stat_cmp(new_entry->stat,
dbd0b4b3
FW
175 iter_entry->stat) > 0) {
176
177 list_add_tail(&new_entry->list,
178 &iter_entry->list);
179 break;
180
181 /* The current smaller value */
182 } else if (list_is_last(&iter_entry->list,
034939b6 183 &session->stat_list)) {
dbd0b4b3
FW
184 list_add(&new_entry->list, &iter_entry->list);
185 break;
186 }
187 }
188
189 prev_stat = new_entry->stat;
190 }
191exit:
034939b6 192 mutex_unlock(&session->stat_mutex);
dbd0b4b3
FW
193 return ret;
194
195exit_free_list:
034939b6
FW
196 reset_stat_session(session);
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{
034939b6 204 struct tracer_stat_session *session = s->private;
dbd0b4b3
FW
205
206 /* Prevent from tracer switch or stat_list modification */
034939b6 207 mutex_lock(&session->stat_mutex);
dbd0b4b3
FW
208
209 /* If we are in the beginning of the file, print the headers */
034939b6
FW
210 if (!*pos && session->ts->stat_headers)
211 session->ts->stat_headers(s);
dbd0b4b3 212
034939b6 213 return seq_list_start(&session->stat_list, *pos);
dbd0b4b3
FW
214}
215
216static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
217{
034939b6 218 struct tracer_stat_session *session = s->private;
dbd0b4b3 219
034939b6 220 return seq_list_next(p, &session->stat_list, pos);
dbd0b4b3
FW
221}
222
034939b6 223static void stat_seq_stop(struct seq_file *s, void *p)
dbd0b4b3 224{
034939b6
FW
225 struct tracer_stat_session *session = s->private;
226 mutex_unlock(&session->stat_mutex);
dbd0b4b3
FW
227}
228
229static int stat_seq_show(struct seq_file *s, void *v)
230{
034939b6
FW
231 struct tracer_stat_session *session = s->private;
232 struct trace_stat_list *l = list_entry(v, struct trace_stat_list, list);
ff288b27 233
034939b6 234 return session->ts->stat_show(s, l->stat);
dbd0b4b3
FW
235}
236
237static const struct seq_operations trace_stat_seq_ops = {
238 .start = stat_seq_start,
239 .next = stat_seq_next,
240 .stop = stat_seq_stop,
241 .show = stat_seq_show
242};
243
034939b6 244/* The session stat is refilled and resorted at each stat file opening */
dbd0b4b3
FW
245static int tracing_stat_open(struct inode *inode, struct file *file)
246{
247 int ret;
248
034939b6
FW
249 struct tracer_stat_session *session = inode->i_private;
250
dbd0b4b3
FW
251 ret = seq_open(file, &trace_stat_seq_ops);
252 if (!ret) {
253 struct seq_file *m = file->private_data;
034939b6
FW
254 m->private = session;
255 ret = stat_seq_init(session);
dbd0b4b3
FW
256 }
257
258 return ret;
259}
260
261
262/*
263 * Avoid consuming memory with our now useless list.
264 */
265static int tracing_stat_release(struct inode *i, struct file *f)
266{
034939b6
FW
267 struct tracer_stat_session *session = i->i_private;
268
269 mutex_lock(&session->stat_mutex);
270 reset_stat_session(session);
271 mutex_unlock(&session->stat_mutex);
272
dbd0b4b3
FW
273 return 0;
274}
275
276static const struct file_operations tracing_stat_fops = {
277 .open = tracing_stat_open,
278 .read = seq_read,
279 .llseek = seq_lseek,
280 .release = tracing_stat_release
281};
282
034939b6
FW
283
284static void destroy_trace_stat_files(void)
285{
286 int i;
287
288 if (stat_files) {
289 for (i = 0; i < nb_sessions; i++)
290 debugfs_remove(stat_files[i]);
291 kfree(stat_files);
292 stat_files = NULL;
293 }
294}
295
296static void init_trace_stat_files(void)
297{
298 int i;
299
300 if (!stat_dir || !nb_sessions)
301 return;
302
303 stat_files = kmalloc(sizeof(struct dentry *) * nb_sessions, GFP_KERNEL);
304
305 if (!stat_files) {
306 pr_warning("trace stat: not enough memory\n");
307 return;
308 }
309
310 for (i = 0; i < nb_sessions; i++) {
311 struct tracer_stat_session *session = all_stat_sessions[i];
312 stat_files[i] = debugfs_create_file(session->ts->name, 0644,
313 stat_dir,
314 session, &tracing_stat_fops);
315 if (!stat_files[i])
316 pr_warning("cannot create %s entry\n",
317 session->ts->name);
318 }
319}
320
321void init_tracer_stat(struct tracer *trace)
322{
323 int nb = basic_tracer_stat_checks(trace->stats);
324
325 destroy_trace_stat_files();
326
327 if (nb < 0) {
328 pr_warning("stat tracing: missing stat callback on %s\n",
329 trace->name);
330 return;
331 }
332 if (!nb)
333 return;
334
335 init_all_sessions(nb, trace->stats);
336 init_trace_stat_files();
337}
338
dbd0b4b3
FW
339static int __init tracing_stat_init(void)
340{
341 struct dentry *d_tracing;
dbd0b4b3 342
dbd0b4b3
FW
343 d_tracing = tracing_init_dentry();
344
034939b6
FW
345 stat_dir = debugfs_create_dir("trace_stat", d_tracing);
346 if (!stat_dir)
dbd0b4b3
FW
347 pr_warning("Could not create debugfs "
348 "'trace_stat' entry\n");
349 return 0;
350}
351fs_initcall(tracing_stat_init);