2 * Memory allocator tracing
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>
9 #include <linux/tracepoint.h>
10 #include <linux/seq_file.h>
11 #include <linux/debugfs.h>
12 #include <linux/dcache.h>
15 #include <linux/kmemtrace.h>
17 #include "trace_output.h"
20 /* Select an alternative, minimalistic output than the original one */
21 #define TRACE_KMEM_OPT_MINIMAL 0x1
23 static struct tracer_opt kmem_opts
[] = {
24 /* Default disable the minimalistic output */
25 { TRACER_OPT(kmem_minimalistic
, TRACE_KMEM_OPT_MINIMAL
) },
29 static struct tracer_flags kmem_tracer_flags
= {
34 static struct trace_array
*kmemtrace_array
;
36 /* Trace allocations */
37 static inline void kmemtrace_alloc(enum kmemtrace_type_id type_id
,
38 unsigned long call_site
,
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
;
50 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
));
54 entry
= ring_buffer_event_data(event
);
55 tracing_generic_entry_update(&entry
->ent
, 0, 0);
57 entry
->ent
.type
= TRACE_KMEM_ALLOC
;
58 entry
->type_id
= type_id
;
59 entry
->call_site
= call_site
;
61 entry
->bytes_req
= bytes_req
;
62 entry
->bytes_alloc
= bytes_alloc
;
63 entry
->gfp_flags
= gfp_flags
;
66 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
67 ring_buffer_unlock_commit(tr
->buffer
, event
);
72 static inline void kmemtrace_free(enum kmemtrace_type_id type_id
,
73 unsigned long call_site
,
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
;
81 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
));
84 entry
= ring_buffer_event_data(event
);
85 tracing_generic_entry_update(&entry
->ent
, 0, 0);
87 entry
->ent
.type
= TRACE_KMEM_FREE
;
88 entry
->type_id
= type_id
;
89 entry
->call_site
= call_site
;
92 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
93 ring_buffer_unlock_commit(tr
->buffer
, event
);
98 static void kmemtrace_kmalloc(void *ignore
,
99 unsigned long call_site
,
105 kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC
, call_site
, ptr
,
106 bytes_req
, bytes_alloc
, gfp_flags
, -1);
109 static void kmemtrace_kmem_cache_alloc(void *ignore
,
110 unsigned long call_site
,
116 kmemtrace_alloc(KMEMTRACE_TYPE_CACHE
, call_site
, ptr
,
117 bytes_req
, bytes_alloc
, gfp_flags
, -1);
120 static void kmemtrace_kmalloc_node(void *ignore
,
121 unsigned long call_site
,
128 kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC
, call_site
, ptr
,
129 bytes_req
, bytes_alloc
, gfp_flags
, node
);
132 static void kmemtrace_kmem_cache_alloc_node(void *ignore
,
133 unsigned long call_site
,
140 kmemtrace_alloc(KMEMTRACE_TYPE_CACHE
, call_site
, ptr
,
141 bytes_req
, bytes_alloc
, gfp_flags
, node
);
145 kmemtrace_kfree(void *ignore
, unsigned long call_site
, const void *ptr
)
147 kmemtrace_free(KMEMTRACE_TYPE_KMALLOC
, call_site
, ptr
);
150 static void kmemtrace_kmem_cache_free(void *ignore
,
151 unsigned long call_site
, const void *ptr
)
153 kmemtrace_free(KMEMTRACE_TYPE_CACHE
, call_site
, ptr
);
156 static int kmemtrace_start_probes(void)
160 err
= register_trace_kmalloc(kmemtrace_kmalloc
, NULL
);
163 err
= register_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc
, NULL
);
166 err
= register_trace_kmalloc_node(kmemtrace_kmalloc_node
, NULL
);
169 err
= register_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node
, NULL
);
172 err
= register_trace_kfree(kmemtrace_kfree
, NULL
);
175 err
= register_trace_kmem_cache_free(kmemtrace_kmem_cache_free
, NULL
);
180 static void kmemtrace_stop_probes(void)
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
);
190 static int kmem_trace_init(struct trace_array
*tr
)
192 kmemtrace_array
= tr
;
194 tracing_reset_online_cpus(tr
);
196 kmemtrace_start_probes();
201 static void kmem_trace_reset(struct trace_array
*tr
)
203 kmemtrace_stop_probes();
206 static void kmemtrace_headers(struct seq_file
*s
)
208 /* Don't need headers for the original kmemtrace output */
209 if (!(kmem_tracer_flags
.val
& TRACE_KMEM_OPT_MINIMAL
))
212 seq_printf(s
, "#\n");
213 seq_printf(s
, "# ALLOC TYPE REQ GIVEN FLAGS "
214 " POINTER NODE CALLER\n");
215 seq_printf(s
, "# FREE | | | | "
217 seq_printf(s
, "# |\n\n");
221 * The following functions give the original output from kmemtrace,
222 * plus the origin CPU, since reordering occurs in-kernel now.
225 #define KMEMTRACE_USER_ALLOC 0
226 #define KMEMTRACE_USER_FREE 1
228 struct kmemtrace_user_event
{
234 unsigned long call_site
;
238 struct kmemtrace_user_event_alloc
{
245 static enum print_line_t
246 kmemtrace_print_alloc(struct trace_iterator
*iter
, int flags
,
247 struct trace_event
*event
)
249 struct trace_seq
*s
= &iter
->seq
;
250 struct kmemtrace_alloc_entry
*entry
;
253 trace_assign_type(entry
, iter
->ent
);
255 ret
= trace_seq_printf(s
, "type_id %d call_site %pF ptr %lu "
256 "bytes_req %lu bytes_alloc %lu gfp_flags %lu node %d\n",
257 entry
->type_id
, (void *)entry
->call_site
, (unsigned long)entry
->ptr
,
258 (unsigned long)entry
->bytes_req
, (unsigned long)entry
->bytes_alloc
,
259 (unsigned long)entry
->gfp_flags
, entry
->node
);
262 return TRACE_TYPE_PARTIAL_LINE
;
263 return TRACE_TYPE_HANDLED
;
266 static enum print_line_t
267 kmemtrace_print_free(struct trace_iterator
*iter
, int flags
,
268 struct trace_event
*event
)
270 struct trace_seq
*s
= &iter
->seq
;
271 struct kmemtrace_free_entry
*entry
;
274 trace_assign_type(entry
, iter
->ent
);
276 ret
= trace_seq_printf(s
, "type_id %d call_site %pF ptr %lu\n",
277 entry
->type_id
, (void *)entry
->call_site
,
278 (unsigned long)entry
->ptr
);
281 return TRACE_TYPE_PARTIAL_LINE
;
282 return TRACE_TYPE_HANDLED
;
285 static enum print_line_t
286 kmemtrace_print_alloc_user(struct trace_iterator
*iter
, int flags
,
287 struct trace_event
*event
)
289 struct trace_seq
*s
= &iter
->seq
;
290 struct kmemtrace_alloc_entry
*entry
;
291 struct kmemtrace_user_event
*ev
;
292 struct kmemtrace_user_event_alloc
*ev_alloc
;
294 trace_assign_type(entry
, iter
->ent
);
296 ev
= trace_seq_reserve(s
, sizeof(*ev
));
298 return TRACE_TYPE_PARTIAL_LINE
;
300 ev
->event_id
= KMEMTRACE_USER_ALLOC
;
301 ev
->type_id
= entry
->type_id
;
302 ev
->event_size
= sizeof(*ev
) + sizeof(*ev_alloc
);
304 ev
->timestamp
= iter
->ts
;
305 ev
->call_site
= entry
->call_site
;
306 ev
->ptr
= (unsigned long)entry
->ptr
;
308 ev_alloc
= trace_seq_reserve(s
, sizeof(*ev_alloc
));
310 return TRACE_TYPE_PARTIAL_LINE
;
312 ev_alloc
->bytes_req
= entry
->bytes_req
;
313 ev_alloc
->bytes_alloc
= entry
->bytes_alloc
;
314 ev_alloc
->gfp_flags
= entry
->gfp_flags
;
315 ev_alloc
->node
= entry
->node
;
317 return TRACE_TYPE_HANDLED
;
320 static enum print_line_t
321 kmemtrace_print_free_user(struct trace_iterator
*iter
, int flags
,
322 struct trace_event
*event
)
324 struct trace_seq
*s
= &iter
->seq
;
325 struct kmemtrace_free_entry
*entry
;
326 struct kmemtrace_user_event
*ev
;
328 trace_assign_type(entry
, iter
->ent
);
330 ev
= trace_seq_reserve(s
, sizeof(*ev
));
332 return TRACE_TYPE_PARTIAL_LINE
;
334 ev
->event_id
= KMEMTRACE_USER_FREE
;
335 ev
->type_id
= entry
->type_id
;
336 ev
->event_size
= sizeof(*ev
);
338 ev
->timestamp
= iter
->ts
;
339 ev
->call_site
= entry
->call_site
;
340 ev
->ptr
= (unsigned long)entry
->ptr
;
342 return TRACE_TYPE_HANDLED
;
345 /* The two other following provide a more minimalistic output */
346 static enum print_line_t
347 kmemtrace_print_alloc_compress(struct trace_iterator
*iter
)
349 struct kmemtrace_alloc_entry
*entry
;
350 struct trace_seq
*s
= &iter
->seq
;
353 trace_assign_type(entry
, iter
->ent
);
356 ret
= trace_seq_printf(s
, " + ");
358 return TRACE_TYPE_PARTIAL_LINE
;
361 switch (entry
->type_id
) {
362 case KMEMTRACE_TYPE_KMALLOC
:
363 ret
= trace_seq_printf(s
, "K ");
365 case KMEMTRACE_TYPE_CACHE
:
366 ret
= trace_seq_printf(s
, "C ");
368 case KMEMTRACE_TYPE_PAGES
:
369 ret
= trace_seq_printf(s
, "P ");
372 ret
= trace_seq_printf(s
, "? ");
376 return TRACE_TYPE_PARTIAL_LINE
;
379 ret
= trace_seq_printf(s
, "%4zu ", entry
->bytes_req
);
381 return TRACE_TYPE_PARTIAL_LINE
;
384 ret
= trace_seq_printf(s
, "%4zu ", entry
->bytes_alloc
);
386 return TRACE_TYPE_PARTIAL_LINE
;
389 * TODO: would be better to see the name of the GFP flag names
391 ret
= trace_seq_printf(s
, "%08x ", entry
->gfp_flags
);
393 return TRACE_TYPE_PARTIAL_LINE
;
395 /* Pointer to allocated */
396 ret
= trace_seq_printf(s
, "0x%tx ", (ptrdiff_t)entry
->ptr
);
398 return TRACE_TYPE_PARTIAL_LINE
;
400 /* Node and call site*/
401 ret
= trace_seq_printf(s
, "%4d %pf\n", entry
->node
,
402 (void *)entry
->call_site
);
404 return TRACE_TYPE_PARTIAL_LINE
;
406 return TRACE_TYPE_HANDLED
;
409 static enum print_line_t
410 kmemtrace_print_free_compress(struct trace_iterator
*iter
)
412 struct kmemtrace_free_entry
*entry
;
413 struct trace_seq
*s
= &iter
->seq
;
416 trace_assign_type(entry
, iter
->ent
);
419 ret
= trace_seq_printf(s
, " - ");
421 return TRACE_TYPE_PARTIAL_LINE
;
424 switch (entry
->type_id
) {
425 case KMEMTRACE_TYPE_KMALLOC
:
426 ret
= trace_seq_printf(s
, "K ");
428 case KMEMTRACE_TYPE_CACHE
:
429 ret
= trace_seq_printf(s
, "C ");
431 case KMEMTRACE_TYPE_PAGES
:
432 ret
= trace_seq_printf(s
, "P ");
435 ret
= trace_seq_printf(s
, "? ");
439 return TRACE_TYPE_PARTIAL_LINE
;
441 /* Skip requested/allocated/flags */
442 ret
= trace_seq_printf(s
, " ");
444 return TRACE_TYPE_PARTIAL_LINE
;
446 /* Pointer to allocated */
447 ret
= trace_seq_printf(s
, "0x%tx ", (ptrdiff_t)entry
->ptr
);
449 return TRACE_TYPE_PARTIAL_LINE
;
451 /* Skip node and print call site*/
452 ret
= trace_seq_printf(s
, " %pf\n", (void *)entry
->call_site
);
454 return TRACE_TYPE_PARTIAL_LINE
;
456 return TRACE_TYPE_HANDLED
;
459 static enum print_line_t
kmemtrace_print_line(struct trace_iterator
*iter
)
461 struct trace_entry
*entry
= iter
->ent
;
463 if (!(kmem_tracer_flags
.val
& TRACE_KMEM_OPT_MINIMAL
))
464 return TRACE_TYPE_UNHANDLED
;
466 switch (entry
->type
) {
467 case TRACE_KMEM_ALLOC
:
468 return kmemtrace_print_alloc_compress(iter
);
469 case TRACE_KMEM_FREE
:
470 return kmemtrace_print_free_compress(iter
);
472 return TRACE_TYPE_UNHANDLED
;
476 static struct trace_event_functions kmem_trace_alloc_funcs
= {
477 .trace
= kmemtrace_print_alloc
,
478 .binary
= kmemtrace_print_alloc_user
,
481 static struct trace_event kmem_trace_alloc
= {
482 .type
= TRACE_KMEM_ALLOC
,
483 .funcs
= &kmem_trace_alloc_funcs
,
486 static struct trace_event_functions kmem_trace_free_funcs
= {
487 .trace
= kmemtrace_print_free
,
488 .binary
= kmemtrace_print_free_user
,
491 static struct trace_event kmem_trace_free
= {
492 .type
= TRACE_KMEM_FREE
,
493 .funcs
= &kmem_trace_free_funcs
,
496 static struct tracer kmem_tracer __read_mostly
= {
498 .init
= kmem_trace_init
,
499 .reset
= kmem_trace_reset
,
500 .print_line
= kmemtrace_print_line
,
501 .print_header
= kmemtrace_headers
,
502 .flags
= &kmem_tracer_flags
505 void kmemtrace_init(void)
507 /* earliest opportunity to start kmem tracing */
510 static int __init
init_kmem_tracer(void)
512 if (!register_ftrace_event(&kmem_trace_alloc
)) {
513 pr_warning("Warning: could not register kmem events\n");
517 if (!register_ftrace_event(&kmem_trace_free
)) {
518 pr_warning("Warning: could not register kmem events\n");
522 if (register_tracer(&kmem_tracer
) != 0) {
523 pr_warning("Warning: could not register the kmem tracer\n");
529 device_initcall(init_kmem_tracer
);