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