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