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(unsigned long call_site
,
104 kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC
, call_site
, ptr
,
105 bytes_req
, bytes_alloc
, gfp_flags
, -1);
108 static void kmemtrace_kmem_cache_alloc(unsigned long call_site
,
114 kmemtrace_alloc(KMEMTRACE_TYPE_CACHE
, call_site
, ptr
,
115 bytes_req
, bytes_alloc
, gfp_flags
, -1);
118 static void kmemtrace_kmalloc_node(unsigned long call_site
,
125 kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC
, call_site
, ptr
,
126 bytes_req
, bytes_alloc
, gfp_flags
, node
);
129 static void kmemtrace_kmem_cache_alloc_node(unsigned long call_site
,
136 kmemtrace_alloc(KMEMTRACE_TYPE_CACHE
, call_site
, ptr
,
137 bytes_req
, bytes_alloc
, gfp_flags
, node
);
140 static void kmemtrace_kfree(unsigned long call_site
, const void *ptr
)
142 kmemtrace_free(KMEMTRACE_TYPE_KMALLOC
, call_site
, ptr
);
145 static void kmemtrace_kmem_cache_free(unsigned long call_site
, const void *ptr
)
147 kmemtrace_free(KMEMTRACE_TYPE_CACHE
, call_site
, ptr
);
150 static int kmemtrace_start_probes(void)
154 err
= register_trace_kmalloc(kmemtrace_kmalloc
);
157 err
= register_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc
);
160 err
= register_trace_kmalloc_node(kmemtrace_kmalloc_node
);
163 err
= register_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node
);
166 err
= register_trace_kfree(kmemtrace_kfree
);
169 err
= register_trace_kmem_cache_free(kmemtrace_kmem_cache_free
);
174 static void kmemtrace_stop_probes(void)
176 unregister_trace_kmalloc(kmemtrace_kmalloc
);
177 unregister_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc
);
178 unregister_trace_kmalloc_node(kmemtrace_kmalloc_node
);
179 unregister_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node
);
180 unregister_trace_kfree(kmemtrace_kfree
);
181 unregister_trace_kmem_cache_free(kmemtrace_kmem_cache_free
);
184 static int kmem_trace_init(struct trace_array
*tr
)
186 kmemtrace_array
= tr
;
188 tracing_reset_online_cpus(tr
);
190 kmemtrace_start_probes();
195 static void kmem_trace_reset(struct trace_array
*tr
)
197 kmemtrace_stop_probes();
200 static void kmemtrace_headers(struct seq_file
*s
)
202 /* Don't need headers for the original kmemtrace output */
203 if (!(kmem_tracer_flags
.val
& TRACE_KMEM_OPT_MINIMAL
))
206 seq_printf(s
, "#\n");
207 seq_printf(s
, "# ALLOC TYPE REQ GIVEN FLAGS "
208 " POINTER NODE CALLER\n");
209 seq_printf(s
, "# FREE | | | | "
211 seq_printf(s
, "# |\n\n");
215 * The following functions give the original output from kmemtrace,
216 * plus the origin CPU, since reordering occurs in-kernel now.
219 #define KMEMTRACE_USER_ALLOC 0
220 #define KMEMTRACE_USER_FREE 1
222 struct kmemtrace_user_event
{
228 unsigned long call_site
;
232 struct kmemtrace_user_event_alloc
{
239 static enum print_line_t
240 kmemtrace_print_alloc(struct trace_iterator
*iter
, int flags
)
242 struct trace_seq
*s
= &iter
->seq
;
243 struct kmemtrace_alloc_entry
*entry
;
246 trace_assign_type(entry
, iter
->ent
);
248 ret
= trace_seq_printf(s
, "type_id %d call_site %pF ptr %lu "
249 "bytes_req %lu bytes_alloc %lu gfp_flags %lu node %d\n",
250 entry
->type_id
, (void *)entry
->call_site
, (unsigned long)entry
->ptr
,
251 (unsigned long)entry
->bytes_req
, (unsigned long)entry
->bytes_alloc
,
252 (unsigned long)entry
->gfp_flags
, entry
->node
);
255 return TRACE_TYPE_PARTIAL_LINE
;
256 return TRACE_TYPE_HANDLED
;
259 static enum print_line_t
260 kmemtrace_print_free(struct trace_iterator
*iter
, int flags
)
262 struct trace_seq
*s
= &iter
->seq
;
263 struct kmemtrace_free_entry
*entry
;
266 trace_assign_type(entry
, iter
->ent
);
268 ret
= trace_seq_printf(s
, "type_id %d call_site %pF ptr %lu\n",
269 entry
->type_id
, (void *)entry
->call_site
,
270 (unsigned long)entry
->ptr
);
273 return TRACE_TYPE_PARTIAL_LINE
;
274 return TRACE_TYPE_HANDLED
;
277 static enum print_line_t
278 kmemtrace_print_alloc_user(struct trace_iterator
*iter
, int flags
)
280 struct trace_seq
*s
= &iter
->seq
;
281 struct kmemtrace_alloc_entry
*entry
;
282 struct kmemtrace_user_event
*ev
;
283 struct kmemtrace_user_event_alloc
*ev_alloc
;
285 trace_assign_type(entry
, iter
->ent
);
287 ev
= trace_seq_reserve(s
, sizeof(*ev
));
289 return TRACE_TYPE_PARTIAL_LINE
;
291 ev
->event_id
= KMEMTRACE_USER_ALLOC
;
292 ev
->type_id
= entry
->type_id
;
293 ev
->event_size
= sizeof(*ev
) + sizeof(*ev_alloc
);
295 ev
->timestamp
= iter
->ts
;
296 ev
->call_site
= entry
->call_site
;
297 ev
->ptr
= (unsigned long)entry
->ptr
;
299 ev_alloc
= trace_seq_reserve(s
, sizeof(*ev_alloc
));
301 return TRACE_TYPE_PARTIAL_LINE
;
303 ev_alloc
->bytes_req
= entry
->bytes_req
;
304 ev_alloc
->bytes_alloc
= entry
->bytes_alloc
;
305 ev_alloc
->gfp_flags
= entry
->gfp_flags
;
306 ev_alloc
->node
= entry
->node
;
308 return TRACE_TYPE_HANDLED
;
311 static enum print_line_t
312 kmemtrace_print_free_user(struct trace_iterator
*iter
, int flags
)
314 struct trace_seq
*s
= &iter
->seq
;
315 struct kmemtrace_free_entry
*entry
;
316 struct kmemtrace_user_event
*ev
;
318 trace_assign_type(entry
, iter
->ent
);
320 ev
= trace_seq_reserve(s
, sizeof(*ev
));
322 return TRACE_TYPE_PARTIAL_LINE
;
324 ev
->event_id
= KMEMTRACE_USER_FREE
;
325 ev
->type_id
= entry
->type_id
;
326 ev
->event_size
= sizeof(*ev
);
328 ev
->timestamp
= iter
->ts
;
329 ev
->call_site
= entry
->call_site
;
330 ev
->ptr
= (unsigned long)entry
->ptr
;
332 return TRACE_TYPE_HANDLED
;
335 /* The two other following provide a more minimalistic output */
336 static enum print_line_t
337 kmemtrace_print_alloc_compress(struct trace_iterator
*iter
)
339 struct kmemtrace_alloc_entry
*entry
;
340 struct trace_seq
*s
= &iter
->seq
;
343 trace_assign_type(entry
, iter
->ent
);
346 ret
= trace_seq_printf(s
, " + ");
348 return TRACE_TYPE_PARTIAL_LINE
;
351 switch (entry
->type_id
) {
352 case KMEMTRACE_TYPE_KMALLOC
:
353 ret
= trace_seq_printf(s
, "K ");
355 case KMEMTRACE_TYPE_CACHE
:
356 ret
= trace_seq_printf(s
, "C ");
358 case KMEMTRACE_TYPE_PAGES
:
359 ret
= trace_seq_printf(s
, "P ");
362 ret
= trace_seq_printf(s
, "? ");
366 return TRACE_TYPE_PARTIAL_LINE
;
369 ret
= trace_seq_printf(s
, "%4zu ", entry
->bytes_req
);
371 return TRACE_TYPE_PARTIAL_LINE
;
374 ret
= trace_seq_printf(s
, "%4zu ", entry
->bytes_alloc
);
376 return TRACE_TYPE_PARTIAL_LINE
;
379 * TODO: would be better to see the name of the GFP flag names
381 ret
= trace_seq_printf(s
, "%08x ", entry
->gfp_flags
);
383 return TRACE_TYPE_PARTIAL_LINE
;
385 /* Pointer to allocated */
386 ret
= trace_seq_printf(s
, "0x%tx ", (ptrdiff_t)entry
->ptr
);
388 return TRACE_TYPE_PARTIAL_LINE
;
390 /* Node and call site*/
391 ret
= trace_seq_printf(s
, "%4d %pf\n", entry
->node
,
392 (void *)entry
->call_site
);
394 return TRACE_TYPE_PARTIAL_LINE
;
396 return TRACE_TYPE_HANDLED
;
399 static enum print_line_t
400 kmemtrace_print_free_compress(struct trace_iterator
*iter
)
402 struct kmemtrace_free_entry
*entry
;
403 struct trace_seq
*s
= &iter
->seq
;
406 trace_assign_type(entry
, iter
->ent
);
409 ret
= trace_seq_printf(s
, " - ");
411 return TRACE_TYPE_PARTIAL_LINE
;
414 switch (entry
->type_id
) {
415 case KMEMTRACE_TYPE_KMALLOC
:
416 ret
= trace_seq_printf(s
, "K ");
418 case KMEMTRACE_TYPE_CACHE
:
419 ret
= trace_seq_printf(s
, "C ");
421 case KMEMTRACE_TYPE_PAGES
:
422 ret
= trace_seq_printf(s
, "P ");
425 ret
= trace_seq_printf(s
, "? ");
429 return TRACE_TYPE_PARTIAL_LINE
;
431 /* Skip requested/allocated/flags */
432 ret
= trace_seq_printf(s
, " ");
434 return TRACE_TYPE_PARTIAL_LINE
;
436 /* Pointer to allocated */
437 ret
= trace_seq_printf(s
, "0x%tx ", (ptrdiff_t)entry
->ptr
);
439 return TRACE_TYPE_PARTIAL_LINE
;
441 /* Skip node and print call site*/
442 ret
= trace_seq_printf(s
, " %pf\n", (void *)entry
->call_site
);
444 return TRACE_TYPE_PARTIAL_LINE
;
446 return TRACE_TYPE_HANDLED
;
449 static enum print_line_t
kmemtrace_print_line(struct trace_iterator
*iter
)
451 struct trace_entry
*entry
= iter
->ent
;
453 if (!(kmem_tracer_flags
.val
& TRACE_KMEM_OPT_MINIMAL
))
454 return TRACE_TYPE_UNHANDLED
;
456 switch (entry
->type
) {
457 case TRACE_KMEM_ALLOC
:
458 return kmemtrace_print_alloc_compress(iter
);
459 case TRACE_KMEM_FREE
:
460 return kmemtrace_print_free_compress(iter
);
462 return TRACE_TYPE_UNHANDLED
;
466 static struct trace_event kmem_trace_alloc
= {
467 .type
= TRACE_KMEM_ALLOC
,
468 .trace
= kmemtrace_print_alloc
,
469 .binary
= kmemtrace_print_alloc_user
,
472 static struct trace_event kmem_trace_free
= {
473 .type
= TRACE_KMEM_FREE
,
474 .trace
= kmemtrace_print_free
,
475 .binary
= kmemtrace_print_free_user
,
478 static struct tracer kmem_tracer __read_mostly
= {
480 .init
= kmem_trace_init
,
481 .reset
= kmem_trace_reset
,
482 .print_line
= kmemtrace_print_line
,
483 .print_header
= kmemtrace_headers
,
484 .flags
= &kmem_tracer_flags
487 void kmemtrace_init(void)
489 /* earliest opportunity to start kmem tracing */
492 static int __init
init_kmem_tracer(void)
494 if (!register_ftrace_event(&kmem_trace_alloc
)) {
495 pr_warning("Warning: could not register kmem events\n");
499 if (!register_ftrace_event(&kmem_trace_free
)) {
500 pr_warning("Warning: could not register kmem events\n");
504 if (register_tracer(&kmem_tracer
) != 0) {
505 pr_warning("Warning: could not register the kmem tracer\n");
511 device_initcall(init_kmem_tracer
);