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