7253d0c1c32f1cafceb5752c12a912169d9a7973
[linux-2.6.git] / kernel / trace / kmemtrace.c
1 /*
2  * Memory allocator tracing
3  *
4  * Copyright (C) 2008 Eduard - Gabriel Munteanu
5  * Copyright (C) 2008 Pekka Enberg <penberg@cs.helsinki.fi>
6  * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
7  */
8
9 #include <linux/tracepoint.h>
10 #include <linux/seq_file.h>
11 #include <linux/debugfs.h>
12 #include <linux/dcache.h>
13 #include <linux/fs.h>
14
15 #include <linux/kmemtrace.h>
16
17 #include "trace_output.h"
18 #include "trace.h"
19
20 /* Select an alternative, minimalistic output than the original one */
21 #define TRACE_KMEM_OPT_MINIMAL  0x1
22
23 static struct tracer_opt kmem_opts[] = {
24         /* Default disable the minimalistic output */
25         { TRACER_OPT(kmem_minimalistic, TRACE_KMEM_OPT_MINIMAL) },
26         { }
27 };
28
29 static struct tracer_flags kmem_tracer_flags = {
30         .val                    = 0,
31         .opts                   = kmem_opts
32 };
33
34 static struct trace_array *kmemtrace_array;
35
36 /* Trace allocations */
37 static inline void kmemtrace_alloc(enum kmemtrace_type_id type_id,
38                                    unsigned long call_site,
39                                    const void *ptr,
40                                    size_t bytes_req,
41                                    size_t bytes_alloc,
42                                    gfp_t gfp_flags,
43                                    int node)
44 {
45         struct ftrace_event_call *call = &event_kmem_alloc;
46         struct trace_array *tr = kmemtrace_array;
47         struct kmemtrace_alloc_entry *entry;
48         struct ring_buffer_event *event;
49
50         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry));
51         if (!event)
52                 return;
53
54         entry = ring_buffer_event_data(event);
55         tracing_generic_entry_update(&entry->ent, 0, 0);
56
57         entry->ent.type         = TRACE_KMEM_ALLOC;
58         entry->type_id          = type_id;
59         entry->call_site        = call_site;
60         entry->ptr              = ptr;
61         entry->bytes_req        = bytes_req;
62         entry->bytes_alloc      = bytes_alloc;
63         entry->gfp_flags        = gfp_flags;
64         entry->node             = node;
65
66         if (!filter_check_discard(call, entry, tr->buffer, event))
67                 ring_buffer_unlock_commit(tr->buffer, event);
68
69         trace_wake_up();
70 }
71
72 static inline void kmemtrace_free(enum kmemtrace_type_id type_id,
73                                   unsigned long call_site,
74                                   const void *ptr)
75 {
76         struct ftrace_event_call *call = &event_kmem_free;
77         struct trace_array *tr = kmemtrace_array;
78         struct kmemtrace_free_entry *entry;
79         struct ring_buffer_event *event;
80
81         event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry));
82         if (!event)
83                 return;
84         entry   = ring_buffer_event_data(event);
85         tracing_generic_entry_update(&entry->ent, 0, 0);
86
87         entry->ent.type         = TRACE_KMEM_FREE;
88         entry->type_id          = type_id;
89         entry->call_site        = call_site;
90         entry->ptr              = ptr;
91
92         if (!filter_check_discard(call, entry, tr->buffer, event))
93                 ring_buffer_unlock_commit(tr->buffer, event);
94
95         trace_wake_up();
96 }
97
98 static void kmemtrace_kmalloc(void *ignore,
99                               unsigned long call_site,
100                               const void *ptr,
101                               size_t bytes_req,
102                               size_t bytes_alloc,
103                               gfp_t gfp_flags)
104 {
105         kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC, call_site, ptr,
106                         bytes_req, bytes_alloc, gfp_flags, -1);
107 }
108
109 static void kmemtrace_kmem_cache_alloc(void *ignore,
110                                        unsigned long call_site,
111                                        const void *ptr,
112                                        size_t bytes_req,
113                                        size_t bytes_alloc,
114                                        gfp_t gfp_flags)
115 {
116         kmemtrace_alloc(KMEMTRACE_TYPE_CACHE, call_site, ptr,
117                         bytes_req, bytes_alloc, gfp_flags, -1);
118 }
119
120 static void kmemtrace_kmalloc_node(void *ignore,
121                                    unsigned long call_site,
122                                    const void *ptr,
123                                    size_t bytes_req,
124                                    size_t bytes_alloc,
125                                    gfp_t gfp_flags,
126                                    int node)
127 {
128         kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC, call_site, ptr,
129                         bytes_req, bytes_alloc, gfp_flags, node);
130 }
131
132 static void kmemtrace_kmem_cache_alloc_node(void *ignore,
133                                             unsigned long call_site,
134                                             const void *ptr,
135                                             size_t bytes_req,
136                                             size_t bytes_alloc,
137                                             gfp_t gfp_flags,
138                                             int node)
139 {
140         kmemtrace_alloc(KMEMTRACE_TYPE_CACHE, call_site, ptr,
141                         bytes_req, bytes_alloc, gfp_flags, node);
142 }
143
144 static void
145 kmemtrace_kfree(void *ignore, unsigned long call_site, const void *ptr)
146 {
147         kmemtrace_free(KMEMTRACE_TYPE_KMALLOC, call_site, ptr);
148 }
149
150 static void kmemtrace_kmem_cache_free(void *ignore,
151                                       unsigned long call_site, const void *ptr)
152 {
153         kmemtrace_free(KMEMTRACE_TYPE_CACHE, call_site, ptr);
154 }
155
156 static int kmemtrace_start_probes(void)
157 {
158         int err;
159
160         err = register_trace_kmalloc(kmemtrace_kmalloc, NULL);
161         if (err)
162                 return err;
163         err = register_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc, NULL);
164         if (err)
165                 return err;
166         err = register_trace_kmalloc_node(kmemtrace_kmalloc_node, NULL);
167         if (err)
168                 return err;
169         err = register_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node, NULL);
170         if (err)
171                 return err;
172         err = register_trace_kfree(kmemtrace_kfree, NULL);
173         if (err)
174                 return err;
175         err = register_trace_kmem_cache_free(kmemtrace_kmem_cache_free, NULL);
176
177         return err;
178 }
179
180 static void kmemtrace_stop_probes(void)
181 {
182         unregister_trace_kmalloc(kmemtrace_kmalloc, NULL);
183         unregister_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc, NULL);
184         unregister_trace_kmalloc_node(kmemtrace_kmalloc_node, NULL);
185         unregister_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node, NULL);
186         unregister_trace_kfree(kmemtrace_kfree, NULL);
187         unregister_trace_kmem_cache_free(kmemtrace_kmem_cache_free, NULL);
188 }
189
190 static int kmem_trace_init(struct trace_array *tr)
191 {
192         kmemtrace_array = tr;
193
194         tracing_reset_online_cpus(tr);
195
196         kmemtrace_start_probes();
197
198         return 0;
199 }
200
201 static void kmem_trace_reset(struct trace_array *tr)
202 {
203         kmemtrace_stop_probes();
204 }
205
206 static void kmemtrace_headers(struct seq_file *s)
207 {
208         /* Don't need headers for the original kmemtrace output */
209         if (!(kmem_tracer_flags.val & TRACE_KMEM_OPT_MINIMAL))
210                 return;
211
212         seq_printf(s, "#\n");
213         seq_printf(s, "# ALLOC  TYPE  REQ   GIVEN  FLAGS     "
214                         "      POINTER         NODE    CALLER\n");
215         seq_printf(s, "# FREE   |      |     |       |       "
216                         "       |   |            |        |\n");
217         seq_printf(s, "# |\n\n");
218 }
219
220 /*
221  * The following functions give the original output from kmemtrace,
222  * plus the origin CPU, since reordering occurs in-kernel now.
223  */
224
225 #define KMEMTRACE_USER_ALLOC    0
226 #define KMEMTRACE_USER_FREE     1
227
228 struct kmemtrace_user_event {
229         u8                      event_id;
230         u8                      type_id;
231         u16                     event_size;
232         u32                     cpu;
233         u64                     timestamp;
234         unsigned long           call_site;
235         unsigned long           ptr;
236 };
237
238 struct kmemtrace_user_event_alloc {
239         size_t                  bytes_req;
240         size_t                  bytes_alloc;
241         unsigned                gfp_flags;
242         int                     node;
243 };
244
245 static enum print_line_t
246 kmemtrace_print_alloc(struct trace_iterator *iter, int flags)
247 {
248         struct trace_seq *s = &iter->seq;
249         struct kmemtrace_alloc_entry *entry;
250         int ret;
251
252         trace_assign_type(entry, iter->ent);
253
254         ret = trace_seq_printf(s, "type_id %d call_site %pF ptr %lu "
255             "bytes_req %lu bytes_alloc %lu gfp_flags %lu node %d\n",
256             entry->type_id, (void *)entry->call_site, (unsigned long)entry->ptr,
257             (unsigned long)entry->bytes_req, (unsigned long)entry->bytes_alloc,
258             (unsigned long)entry->gfp_flags, entry->node);
259
260         if (!ret)
261                 return TRACE_TYPE_PARTIAL_LINE;
262         return TRACE_TYPE_HANDLED;
263 }
264
265 static enum print_line_t
266 kmemtrace_print_free(struct trace_iterator *iter, int flags)
267 {
268         struct trace_seq *s = &iter->seq;
269         struct kmemtrace_free_entry *entry;
270         int ret;
271
272         trace_assign_type(entry, iter->ent);
273
274         ret = trace_seq_printf(s, "type_id %d call_site %pF ptr %lu\n",
275                                entry->type_id, (void *)entry->call_site,
276                                (unsigned long)entry->ptr);
277
278         if (!ret)
279                 return TRACE_TYPE_PARTIAL_LINE;
280         return TRACE_TYPE_HANDLED;
281 }
282
283 static enum print_line_t
284 kmemtrace_print_alloc_user(struct trace_iterator *iter, int flags)
285 {
286         struct trace_seq *s = &iter->seq;
287         struct kmemtrace_alloc_entry *entry;
288         struct kmemtrace_user_event *ev;
289         struct kmemtrace_user_event_alloc *ev_alloc;
290
291         trace_assign_type(entry, iter->ent);
292
293         ev = trace_seq_reserve(s, sizeof(*ev));
294         if (!ev)
295                 return TRACE_TYPE_PARTIAL_LINE;
296
297         ev->event_id            = KMEMTRACE_USER_ALLOC;
298         ev->type_id             = entry->type_id;
299         ev->event_size          = sizeof(*ev) + sizeof(*ev_alloc);
300         ev->cpu                 = iter->cpu;
301         ev->timestamp           = iter->ts;
302         ev->call_site           = entry->call_site;
303         ev->ptr                 = (unsigned long)entry->ptr;
304
305         ev_alloc = trace_seq_reserve(s, sizeof(*ev_alloc));
306         if (!ev_alloc)
307                 return TRACE_TYPE_PARTIAL_LINE;
308
309         ev_alloc->bytes_req     = entry->bytes_req;
310         ev_alloc->bytes_alloc   = entry->bytes_alloc;
311         ev_alloc->gfp_flags     = entry->gfp_flags;
312         ev_alloc->node          = entry->node;
313
314         return TRACE_TYPE_HANDLED;
315 }
316
317 static enum print_line_t
318 kmemtrace_print_free_user(struct trace_iterator *iter, int flags)
319 {
320         struct trace_seq *s = &iter->seq;
321         struct kmemtrace_free_entry *entry;
322         struct kmemtrace_user_event *ev;
323
324         trace_assign_type(entry, iter->ent);
325
326         ev = trace_seq_reserve(s, sizeof(*ev));
327         if (!ev)
328                 return TRACE_TYPE_PARTIAL_LINE;
329
330         ev->event_id            = KMEMTRACE_USER_FREE;
331         ev->type_id             = entry->type_id;
332         ev->event_size          = sizeof(*ev);
333         ev->cpu                 = iter->cpu;
334         ev->timestamp           = iter->ts;
335         ev->call_site           = entry->call_site;
336         ev->ptr                 = (unsigned long)entry->ptr;
337
338         return TRACE_TYPE_HANDLED;
339 }
340
341 /* The two other following provide a more minimalistic output */
342 static enum print_line_t
343 kmemtrace_print_alloc_compress(struct trace_iterator *iter)
344 {
345         struct kmemtrace_alloc_entry *entry;
346         struct trace_seq *s = &iter->seq;
347         int ret;
348
349         trace_assign_type(entry, iter->ent);
350
351         /* Alloc entry */
352         ret = trace_seq_printf(s, "  +      ");
353         if (!ret)
354                 return TRACE_TYPE_PARTIAL_LINE;
355
356         /* Type */
357         switch (entry->type_id) {
358         case KMEMTRACE_TYPE_KMALLOC:
359                 ret = trace_seq_printf(s, "K   ");
360                 break;
361         case KMEMTRACE_TYPE_CACHE:
362                 ret = trace_seq_printf(s, "C   ");
363                 break;
364         case KMEMTRACE_TYPE_PAGES:
365                 ret = trace_seq_printf(s, "P   ");
366                 break;
367         default:
368                 ret = trace_seq_printf(s, "?   ");
369         }
370
371         if (!ret)
372                 return TRACE_TYPE_PARTIAL_LINE;
373
374         /* Requested */
375         ret = trace_seq_printf(s, "%4zu   ", entry->bytes_req);
376         if (!ret)
377                 return TRACE_TYPE_PARTIAL_LINE;
378
379         /* Allocated */
380         ret = trace_seq_printf(s, "%4zu   ", entry->bytes_alloc);
381         if (!ret)
382                 return TRACE_TYPE_PARTIAL_LINE;
383
384         /* Flags
385          * TODO: would be better to see the name of the GFP flag names
386          */
387         ret = trace_seq_printf(s, "%08x   ", entry->gfp_flags);
388         if (!ret)
389                 return TRACE_TYPE_PARTIAL_LINE;
390
391         /* Pointer to allocated */
392         ret = trace_seq_printf(s, "0x%tx   ", (ptrdiff_t)entry->ptr);
393         if (!ret)
394                 return TRACE_TYPE_PARTIAL_LINE;
395
396         /* Node and call site*/
397         ret = trace_seq_printf(s, "%4d   %pf\n", entry->node,
398                                                  (void *)entry->call_site);
399         if (!ret)
400                 return TRACE_TYPE_PARTIAL_LINE;
401
402         return TRACE_TYPE_HANDLED;
403 }
404
405 static enum print_line_t
406 kmemtrace_print_free_compress(struct trace_iterator *iter)
407 {
408         struct kmemtrace_free_entry *entry;
409         struct trace_seq *s = &iter->seq;
410         int ret;
411
412         trace_assign_type(entry, iter->ent);
413
414         /* Free entry */
415         ret = trace_seq_printf(s, "  -      ");
416         if (!ret)
417                 return TRACE_TYPE_PARTIAL_LINE;
418
419         /* Type */
420         switch (entry->type_id) {
421         case KMEMTRACE_TYPE_KMALLOC:
422                 ret = trace_seq_printf(s, "K     ");
423                 break;
424         case KMEMTRACE_TYPE_CACHE:
425                 ret = trace_seq_printf(s, "C     ");
426                 break;
427         case KMEMTRACE_TYPE_PAGES:
428                 ret = trace_seq_printf(s, "P     ");
429                 break;
430         default:
431                 ret = trace_seq_printf(s, "?     ");
432         }
433
434         if (!ret)
435                 return TRACE_TYPE_PARTIAL_LINE;
436
437         /* Skip requested/allocated/flags */
438         ret = trace_seq_printf(s, "                       ");
439         if (!ret)
440                 return TRACE_TYPE_PARTIAL_LINE;
441
442         /* Pointer to allocated */
443         ret = trace_seq_printf(s, "0x%tx   ", (ptrdiff_t)entry->ptr);
444         if (!ret)
445                 return TRACE_TYPE_PARTIAL_LINE;
446
447         /* Skip node and print call site*/
448         ret = trace_seq_printf(s, "       %pf\n", (void *)entry->call_site);
449         if (!ret)
450                 return TRACE_TYPE_PARTIAL_LINE;
451
452         return TRACE_TYPE_HANDLED;
453 }
454
455 static enum print_line_t kmemtrace_print_line(struct trace_iterator *iter)
456 {
457         struct trace_entry *entry = iter->ent;
458
459         if (!(kmem_tracer_flags.val & TRACE_KMEM_OPT_MINIMAL))
460                 return TRACE_TYPE_UNHANDLED;
461
462         switch (entry->type) {
463         case TRACE_KMEM_ALLOC:
464                 return kmemtrace_print_alloc_compress(iter);
465         case TRACE_KMEM_FREE:
466                 return kmemtrace_print_free_compress(iter);
467         default:
468                 return TRACE_TYPE_UNHANDLED;
469         }
470 }
471
472 static struct trace_event kmem_trace_alloc = {
473         .type                   = TRACE_KMEM_ALLOC,
474         .trace                  = kmemtrace_print_alloc,
475         .binary                 = kmemtrace_print_alloc_user,
476 };
477
478 static struct trace_event kmem_trace_free = {
479         .type                   = TRACE_KMEM_FREE,
480         .trace                  = kmemtrace_print_free,
481         .binary                 = kmemtrace_print_free_user,
482 };
483
484 static struct tracer kmem_tracer __read_mostly = {
485         .name                   = "kmemtrace",
486         .init                   = kmem_trace_init,
487         .reset                  = kmem_trace_reset,
488         .print_line             = kmemtrace_print_line,
489         .print_header           = kmemtrace_headers,
490         .flags                  = &kmem_tracer_flags
491 };
492
493 void kmemtrace_init(void)
494 {
495         /* earliest opportunity to start kmem tracing */
496 }
497
498 static int __init init_kmem_tracer(void)
499 {
500         if (!register_ftrace_event(&kmem_trace_alloc)) {
501                 pr_warning("Warning: could not register kmem events\n");
502                 return 1;
503         }
504
505         if (!register_ftrace_event(&kmem_trace_free)) {
506                 pr_warning("Warning: could not register kmem events\n");
507                 return 1;
508         }
509
510         if (register_tracer(&kmem_tracer) != 0) {
511                 pr_warning("Warning: could not register the kmem tracer\n");
512                 return 1;
513         }
514
515         return 0;
516 }
517 device_initcall(init_kmem_tracer);