5 #include "util/cache.h"
6 #include "util/symbol.h"
7 #include "util/thread.h"
8 #include "util/header.h"
10 #include "util/parse-options.h"
11 #include "util/trace-event.h"
13 #include "util/debug.h"
14 #include "util/data_map.h"
16 #include <linux/rbtree.h>
19 typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
21 static char const *input_name = "perf.data";
23 static struct perf_header *header;
24 static u64 sample_type;
26 static int alloc_flag;
27 static int caller_flag;
29 static int alloc_lines = -1;
30 static int caller_lines = -1;
34 static char default_sort_order[] = "frag,hit,bytes";
51 static struct rb_root root_alloc_stat;
52 static struct rb_root root_alloc_sorted;
53 static struct rb_root root_caller_stat;
54 static struct rb_root root_caller_sorted;
56 static unsigned long total_requested, total_allocated;
58 struct raw_event_sample {
64 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
66 struct thread *thread = threads__findnew(event->comm.pid);
68 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
69 (void *)(offset + head),
70 (void *)(long)(event->header.size),
71 event->comm.comm, event->comm.pid);
74 thread__set_comm(thread, event->comm.comm)) {
75 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
82 static void insert_alloc_stat(unsigned long ptr,
83 int bytes_req, int bytes_alloc)
85 struct rb_node **node = &root_alloc_stat.rb_node;
86 struct rb_node *parent = NULL;
87 struct alloc_stat *data = NULL;
94 data = rb_entry(*node, struct alloc_stat, node);
97 node = &(*node)->rb_right;
98 else if (ptr < data->ptr)
99 node = &(*node)->rb_left;
104 if (data && data->ptr == ptr) {
106 data->bytes_req += bytes_req;
107 data->bytes_alloc += bytes_req;
109 data = malloc(sizeof(*data));
112 data->bytes_req = bytes_req;
113 data->bytes_alloc = bytes_alloc;
115 rb_link_node(&data->node, parent, node);
116 rb_insert_color(&data->node, &root_alloc_stat);
120 static void insert_caller_stat(unsigned long call_site,
121 int bytes_req, int bytes_alloc)
123 struct rb_node **node = &root_caller_stat.rb_node;
124 struct rb_node *parent = NULL;
125 struct alloc_stat *data = NULL;
132 data = rb_entry(*node, struct alloc_stat, node);
134 if (call_site > data->call_site)
135 node = &(*node)->rb_right;
136 else if (call_site < data->call_site)
137 node = &(*node)->rb_left;
142 if (data && data->call_site == call_site) {
144 data->bytes_req += bytes_req;
145 data->bytes_alloc += bytes_req;
147 data = malloc(sizeof(*data));
148 data->call_site = call_site;
150 data->bytes_req = bytes_req;
151 data->bytes_alloc = bytes_alloc;
153 rb_link_node(&data->node, parent, node);
154 rb_insert_color(&data->node, &root_caller_stat);
158 static void process_alloc_event(struct raw_event_sample *raw,
161 u64 timestamp __used,
162 struct thread *thread __used,
165 unsigned long call_site;
170 ptr = raw_field_value(event, "ptr", raw->data);
171 call_site = raw_field_value(event, "call_site", raw->data);
172 bytes_req = raw_field_value(event, "bytes_req", raw->data);
173 bytes_alloc = raw_field_value(event, "bytes_alloc", raw->data);
175 insert_alloc_stat(ptr, bytes_req, bytes_alloc);
176 insert_caller_stat(call_site, bytes_req, bytes_alloc);
178 total_requested += bytes_req;
179 total_allocated += bytes_alloc;
182 static void process_free_event(struct raw_event_sample *raw __used,
183 struct event *event __used,
185 u64 timestamp __used,
186 struct thread *thread __used)
191 process_raw_event(event_t *raw_event __used, void *more_data,
192 int cpu, u64 timestamp, struct thread *thread)
194 struct raw_event_sample *raw = more_data;
198 type = trace_parse_common_type(raw->data);
199 event = trace_find_event(type);
201 if (!strcmp(event->name, "kmalloc") ||
202 !strcmp(event->name, "kmem_cache_alloc")) {
203 process_alloc_event(raw, event, cpu, timestamp, thread, 0);
207 if (!strcmp(event->name, "kmalloc_node") ||
208 !strcmp(event->name, "kmem_cache_alloc_node")) {
209 process_alloc_event(raw, event, cpu, timestamp, thread, 1);
213 if (!strcmp(event->name, "kfree") ||
214 !strcmp(event->name, "kmem_cache_free")) {
215 process_free_event(raw, event, cpu, timestamp, thread);
221 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
223 u64 ip = event->ip.ip;
227 void *more_data = event->ip.__more_data;
228 struct thread *thread = threads__findnew(event->ip.pid);
230 if (sample_type & PERF_SAMPLE_TIME) {
231 timestamp = *(u64 *)more_data;
232 more_data += sizeof(u64);
235 if (sample_type & PERF_SAMPLE_CPU) {
236 cpu = *(u32 *)more_data;
237 more_data += sizeof(u32);
238 more_data += sizeof(u32); /* reserved */
241 if (sample_type & PERF_SAMPLE_PERIOD) {
242 period = *(u64 *)more_data;
243 more_data += sizeof(u64);
246 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
247 (void *)(offset + head),
248 (void *)(long)(event->header.size),
250 event->ip.pid, event->ip.tid,
254 if (thread == NULL) {
255 pr_debug("problem processing %d event, skipping it.\n",
260 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
262 process_raw_event(event, more_data, cpu, timestamp, thread);
267 static int sample_type_check(u64 type)
271 if (!(sample_type & PERF_SAMPLE_RAW)) {
273 "No trace sample to read. Did you call perf record "
281 static struct perf_file_handler file_handler = {
282 .process_sample_event = process_sample_event,
283 .process_comm_event = process_comm_event,
284 .sample_type_check = sample_type_check,
287 static int read_events(void)
289 register_idle_thread();
290 register_perf_file_handler(&file_handler);
292 return mmap_dispatch_perf_file(&header, input_name, NULL, false, 0, 0,
296 static double fragmentation(unsigned long n_req, unsigned long n_alloc)
301 return 100.0 - (100.0 * n_req / n_alloc);
304 static void __print_result(struct rb_root *root, int n_lines, int is_caller)
306 struct rb_node *next;
308 printf("%.78s\n", graph_dotted_line);
309 printf("%-28s|", is_caller ? "Callsite": "Alloc Ptr");
310 printf("Total_alloc/Per | Total_req/Per | Hit | Frag\n");
311 printf("%.78s\n", graph_dotted_line);
313 next = rb_first(root);
315 while (next && n_lines--) {
316 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
318 struct symbol *sym = NULL;
323 addr = data->call_site;
325 sym = kernel_maps__find_symbol(addr,
331 snprintf(bf, sizeof(bf), "%s+%Lx", sym->name,
334 snprintf(bf, sizeof(bf), "%#Lx", addr);
336 printf("%-28s|%8llu/%-6lu |%8llu/%-6lu|%6lu|%8.3f%%\n",
337 bf, (unsigned long long)data->bytes_alloc,
338 (unsigned long)data->bytes_alloc / data->hit,
339 (unsigned long long)data->bytes_req,
340 (unsigned long)data->bytes_req / data->hit,
341 (unsigned long)data->hit,
342 fragmentation(data->bytes_req, data->bytes_alloc));
344 next = rb_next(next);
348 printf(" ... | ... | ... | ... | ... \n");
350 printf("%.78s\n", graph_dotted_line);
353 static void print_summary(void)
355 printf("\nSUMMARY\n=======\n");
356 printf("Total bytes requested: %lu\n", total_requested);
357 printf("Total bytes allocated: %lu\n", total_allocated);
358 printf("Total bytes wasted on internal fragmentation: %lu\n",
359 total_allocated - total_requested);
360 printf("Internal fragmentation: %f%%\n",
361 fragmentation(total_requested, total_allocated));
364 static void print_result(void)
367 __print_result(&root_caller_sorted, caller_lines, 1);
369 __print_result(&root_alloc_sorted, alloc_lines, 0);
373 struct sort_dimension {
376 struct list_head list;
379 static LIST_HEAD(caller_sort);
380 static LIST_HEAD(alloc_sort);
382 static void sort_insert(struct rb_root *root, struct alloc_stat *data,
383 struct list_head *sort_list)
385 struct rb_node **new = &(root->rb_node);
386 struct rb_node *parent = NULL;
387 struct sort_dimension *sort;
390 struct alloc_stat *this;
393 this = rb_entry(*new, struct alloc_stat, node);
396 list_for_each_entry(sort, sort_list, list) {
397 cmp = sort->cmp(data, this);
403 new = &((*new)->rb_left);
405 new = &((*new)->rb_right);
408 rb_link_node(&data->node, parent, new);
409 rb_insert_color(&data->node, root);
412 static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
413 struct list_head *sort_list)
415 struct rb_node *node;
416 struct alloc_stat *data;
419 node = rb_first(root);
423 rb_erase(node, root);
424 data = rb_entry(node, struct alloc_stat, node);
425 sort_insert(root_sorted, data, sort_list);
429 static void sort_result(void)
431 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
432 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
435 static int __cmd_kmem(void)
445 static const char * const kmem_usage[] = {
446 "perf kmem [<options>] {record}",
450 static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
454 else if (l->ptr > r->ptr)
459 static struct sort_dimension ptr_sort_dimension = {
464 static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
466 if (l->call_site < r->call_site)
468 else if (l->call_site > r->call_site)
473 static struct sort_dimension callsite_sort_dimension = {
478 static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
482 else if (l->hit > r->hit)
487 static struct sort_dimension hit_sort_dimension = {
492 static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
494 if (l->bytes_alloc < r->bytes_alloc)
496 else if (l->bytes_alloc > r->bytes_alloc)
501 static struct sort_dimension bytes_sort_dimension = {
506 static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
510 x = fragmentation(l->bytes_req, l->bytes_alloc);
511 y = fragmentation(r->bytes_req, r->bytes_alloc);
520 static struct sort_dimension frag_sort_dimension = {
525 static struct sort_dimension *avail_sorts[] = {
527 &callsite_sort_dimension,
529 &bytes_sort_dimension,
530 &frag_sort_dimension,
533 #define NUM_AVAIL_SORTS \
534 (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
536 static int sort_dimension__add(const char *tok, struct list_head *list)
538 struct sort_dimension *sort;
541 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
542 if (!strcmp(avail_sorts[i]->name, tok)) {
543 sort = malloc(sizeof(*sort));
546 memcpy(sort, avail_sorts[i], sizeof(*sort));
547 list_add_tail(&sort->list, list);
555 static int setup_sorting(struct list_head *sort_list, const char *arg)
558 char *str = strdup(arg);
564 tok = strsep(&str, ",");
567 if (sort_dimension__add(tok, sort_list) < 0) {
568 error("Unknown --sort key: '%s'", tok);
577 static int parse_sort_opt(const struct option *opt __used,
578 const char *arg, int unset __used)
583 if (caller_flag > alloc_flag)
584 return setup_sorting(&caller_sort, arg);
586 return setup_sorting(&alloc_sort, arg);
591 static int parse_stat_opt(const struct option *opt __used,
592 const char *arg, int unset __used)
597 if (strcmp(arg, "alloc") == 0)
598 alloc_flag = (caller_flag + 1);
599 else if (strcmp(arg, "caller") == 0)
600 caller_flag = (alloc_flag + 1);
606 static int parse_line_opt(const struct option *opt __used,
607 const char *arg, int unset __used)
614 lines = strtoul(arg, NULL, 10);
616 if (caller_flag > alloc_flag)
617 caller_lines = lines;
624 static const struct option kmem_options[] = {
625 OPT_STRING('i', "input", &input_name, "file",
627 OPT_CALLBACK(0, "stat", NULL, "<alloc>|<caller>",
628 "stat selector, Pass 'alloc' or 'caller'.",
630 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
631 "sort by key(s): ptr, call_site, bytes, hit, frag",
633 OPT_CALLBACK('l', "line", NULL, "num",
636 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
640 static const char *record_args[] = {
647 "-e", "kmem:kmalloc",
648 "-e", "kmem:kmalloc_node",
650 "-e", "kmem:kmem_cache_alloc",
651 "-e", "kmem:kmem_cache_alloc_node",
652 "-e", "kmem:kmem_cache_free",
655 static int __cmd_record(int argc, const char **argv)
657 unsigned int rec_argc, i, j;
658 const char **rec_argv;
660 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
661 rec_argv = calloc(rec_argc + 1, sizeof(char *));
663 for (i = 0; i < ARRAY_SIZE(record_args); i++)
664 rec_argv[i] = strdup(record_args[i]);
666 for (j = 1; j < (unsigned int)argc; j++, i++)
667 rec_argv[i] = argv[j];
669 return cmd_record(i, rec_argv, NULL);
672 int cmd_kmem(int argc, const char **argv, const char *prefix __used)
676 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
678 if (argc && !strncmp(argv[0], "rec", 3))
679 return __cmd_record(argc, argv);
681 usage_with_options(kmem_usage, kmem_options);
683 if (list_empty(&caller_sort))
684 setup_sorting(&caller_sort, default_sort_order);
685 if (list_empty(&alloc_sort))
686 setup_sorting(&alloc_sort, default_sort_order);