blob: dc86f1e64b66b2ca8831d366607e5c2f7051bbaa [file] [log] [blame]
Li Zefanba77c9e2009-11-20 15:53:25 +08001#include "builtin.h"
2#include "perf.h"
3
4#include "util/util.h"
5#include "util/cache.h"
6#include "util/symbol.h"
7#include "util/thread.h"
8#include "util/header.h"
9
10#include "util/parse-options.h"
11#include "util/trace-event.h"
12
13#include "util/debug.h"
14#include "util/data_map.h"
15
16#include <linux/rbtree.h>
17
18struct alloc_stat;
19typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
20
21static char const *input_name = "perf.data";
22
23static struct perf_header *header;
24static u64 sample_type;
25
26static int alloc_flag;
27static int caller_flag;
28
Li Zefanba77c9e2009-11-20 15:53:25 +080029static int alloc_lines = -1;
30static int caller_lines = -1;
31
Li Zefan7707b6b2009-11-24 13:25:48 +080032static bool raw_ip;
33
Li Zefan29b3e152009-11-24 13:26:10 +080034static char default_sort_order[] = "frag,hit,bytes";
35
Li Zefanba77c9e2009-11-20 15:53:25 +080036static char *cwd;
37static int cwdlen;
38
39struct alloc_stat {
40 union {
Li Zefan7707b6b2009-11-24 13:25:48 +080041 u64 call_site;
Li Zefanba77c9e2009-11-20 15:53:25 +080042 u64 ptr;
43 };
44 u64 bytes_req;
45 u64 bytes_alloc;
46 u32 hit;
47
48 struct rb_node node;
49};
50
51static struct rb_root root_alloc_stat;
52static struct rb_root root_alloc_sorted;
53static struct rb_root root_caller_stat;
54static struct rb_root root_caller_sorted;
55
56static unsigned long total_requested, total_allocated;
57
58struct raw_event_sample {
59 u32 size;
60 char data[0];
61};
62
63static int
64process_comm_event(event_t *event, unsigned long offset, unsigned long head)
65{
66 struct thread *thread = threads__findnew(event->comm.pid);
67
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);
72
73 if (thread == NULL ||
74 thread__set_comm(thread, event->comm.comm)) {
75 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
76 return -1;
77 }
78
79 return 0;
80}
81
82static void insert_alloc_stat(unsigned long ptr,
83 int bytes_req, int bytes_alloc)
84{
85 struct rb_node **node = &root_alloc_stat.rb_node;
86 struct rb_node *parent = NULL;
87 struct alloc_stat *data = NULL;
88
89 if (!alloc_flag)
90 return;
91
92 while (*node) {
93 parent = *node;
94 data = rb_entry(*node, struct alloc_stat, node);
95
96 if (ptr > data->ptr)
97 node = &(*node)->rb_right;
98 else if (ptr < data->ptr)
99 node = &(*node)->rb_left;
100 else
101 break;
102 }
103
104 if (data && data->ptr == ptr) {
105 data->hit++;
106 data->bytes_req += bytes_req;
107 data->bytes_alloc += bytes_req;
108 } else {
109 data = malloc(sizeof(*data));
110 data->ptr = ptr;
111 data->hit = 1;
112 data->bytes_req = bytes_req;
113 data->bytes_alloc = bytes_alloc;
114
115 rb_link_node(&data->node, parent, node);
116 rb_insert_color(&data->node, &root_alloc_stat);
117 }
118}
119
120static void insert_caller_stat(unsigned long call_site,
121 int bytes_req, int bytes_alloc)
122{
123 struct rb_node **node = &root_caller_stat.rb_node;
124 struct rb_node *parent = NULL;
125 struct alloc_stat *data = NULL;
126
127 if (!caller_flag)
128 return;
129
130 while (*node) {
131 parent = *node;
132 data = rb_entry(*node, struct alloc_stat, node);
133
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;
138 else
139 break;
140 }
141
142 if (data && data->call_site == call_site) {
143 data->hit++;
144 data->bytes_req += bytes_req;
145 data->bytes_alloc += bytes_req;
146 } else {
147 data = malloc(sizeof(*data));
148 data->call_site = call_site;
149 data->hit = 1;
150 data->bytes_req = bytes_req;
151 data->bytes_alloc = bytes_alloc;
152
153 rb_link_node(&data->node, parent, node);
154 rb_insert_color(&data->node, &root_caller_stat);
155 }
156}
157
158static void process_alloc_event(struct raw_event_sample *raw,
159 struct event *event,
160 int cpu __used,
161 u64 timestamp __used,
162 struct thread *thread __used,
163 int node __used)
164{
165 unsigned long call_site;
166 unsigned long ptr;
167 int bytes_req;
168 int bytes_alloc;
169
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);
174
175 insert_alloc_stat(ptr, bytes_req, bytes_alloc);
176 insert_caller_stat(call_site, bytes_req, bytes_alloc);
177
178 total_requested += bytes_req;
179 total_allocated += bytes_alloc;
180}
181
182static void process_free_event(struct raw_event_sample *raw __used,
183 struct event *event __used,
184 int cpu __used,
185 u64 timestamp __used,
186 struct thread *thread __used)
187{
188}
189
190static void
191process_raw_event(event_t *raw_event __used, void *more_data,
192 int cpu, u64 timestamp, struct thread *thread)
193{
194 struct raw_event_sample *raw = more_data;
195 struct event *event;
196 int type;
197
198 type = trace_parse_common_type(raw->data);
199 event = trace_find_event(type);
200
201 if (!strcmp(event->name, "kmalloc") ||
202 !strcmp(event->name, "kmem_cache_alloc")) {
203 process_alloc_event(raw, event, cpu, timestamp, thread, 0);
204 return;
205 }
206
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);
210 return;
211 }
212
213 if (!strcmp(event->name, "kfree") ||
214 !strcmp(event->name, "kmem_cache_free")) {
215 process_free_event(raw, event, cpu, timestamp, thread);
216 return;
217 }
218}
219
220static int
221process_sample_event(event_t *event, unsigned long offset, unsigned long head)
222{
223 u64 ip = event->ip.ip;
224 u64 timestamp = -1;
225 u32 cpu = -1;
226 u64 period = 1;
227 void *more_data = event->ip.__more_data;
228 struct thread *thread = threads__findnew(event->ip.pid);
229
230 if (sample_type & PERF_SAMPLE_TIME) {
231 timestamp = *(u64 *)more_data;
232 more_data += sizeof(u64);
233 }
234
235 if (sample_type & PERF_SAMPLE_CPU) {
236 cpu = *(u32 *)more_data;
237 more_data += sizeof(u32);
238 more_data += sizeof(u32); /* reserved */
239 }
240
241 if (sample_type & PERF_SAMPLE_PERIOD) {
242 period = *(u64 *)more_data;
243 more_data += sizeof(u64);
244 }
245
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),
249 event->header.misc,
250 event->ip.pid, event->ip.tid,
251 (void *)(long)ip,
252 (long long)period);
253
254 if (thread == NULL) {
255 pr_debug("problem processing %d event, skipping it.\n",
256 event->header.type);
257 return -1;
258 }
259
260 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
261
262 process_raw_event(event, more_data, cpu, timestamp, thread);
263
264 return 0;
265}
266
267static int sample_type_check(u64 type)
268{
269 sample_type = type;
270
271 if (!(sample_type & PERF_SAMPLE_RAW)) {
272 fprintf(stderr,
273 "No trace sample to read. Did you call perf record "
274 "without -R?");
275 return -1;
276 }
277
278 return 0;
279}
280
281static 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,
285};
286
287static int read_events(void)
288{
289 register_idle_thread();
290 register_perf_file_handler(&file_handler);
291
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -0200292 return mmap_dispatch_perf_file(&header, input_name, NULL, false, 0, 0,
Li Zefanba77c9e2009-11-20 15:53:25 +0800293 &cwdlen, &cwd);
294}
295
296static double fragmentation(unsigned long n_req, unsigned long n_alloc)
297{
298 if (n_alloc == 0)
299 return 0.0;
300 else
301 return 100.0 - (100.0 * n_req / n_alloc);
302}
303
304static void __print_result(struct rb_root *root, int n_lines, int is_caller)
305{
306 struct rb_node *next;
307
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200308 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);
Li Zefanba77c9e2009-11-20 15:53:25 +0800312
313 next = rb_first(root);
314
315 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200316 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
317 node);
318 struct symbol *sym = NULL;
319 char bf[BUFSIZ];
320 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800321
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200322 if (is_caller) {
323 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800324 if (!raw_ip)
325 sym = kernel_maps__find_symbol(addr,
326 NULL, NULL);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200327 } else
328 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800329
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200330 if (sym != NULL)
Li Zefan7707b6b2009-11-24 13:25:48 +0800331 snprintf(bf, sizeof(bf), "%s+%Lx", sym->name,
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200332 addr - sym->start);
333 else
334 snprintf(bf, sizeof(bf), "%#Lx", addr);
335
336 printf("%-28s|%8llu/%-6lu |%8llu/%-6lu|%6lu|%8.3f%%\n",
337 bf, (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +0800338 (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));
343
344 next = rb_next(next);
345 }
346
347 if (n_lines == -1)
Li Zefan7707b6b2009-11-24 13:25:48 +0800348 printf(" ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +0800349
Li Zefan7707b6b2009-11-24 13:25:48 +0800350 printf("%.78s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800351}
352
353static void print_summary(void)
354{
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));
362}
363
364static void print_result(void)
365{
366 if (caller_flag)
367 __print_result(&root_caller_sorted, caller_lines, 1);
368 if (alloc_flag)
369 __print_result(&root_alloc_sorted, alloc_lines, 0);
370 print_summary();
371}
372
Li Zefan29b3e152009-11-24 13:26:10 +0800373struct sort_dimension {
374 const char name[20];
375 sort_fn_t cmp;
376 struct list_head list;
377};
378
379static LIST_HEAD(caller_sort);
380static LIST_HEAD(alloc_sort);
381
Li Zefanba77c9e2009-11-20 15:53:25 +0800382static void sort_insert(struct rb_root *root, struct alloc_stat *data,
Li Zefan29b3e152009-11-24 13:26:10 +0800383 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800384{
385 struct rb_node **new = &(root->rb_node);
386 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +0800387 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +0800388
389 while (*new) {
390 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +0800391 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800392
393 this = rb_entry(*new, struct alloc_stat, node);
394 parent = *new;
395
Li Zefan29b3e152009-11-24 13:26:10 +0800396 list_for_each_entry(sort, sort_list, list) {
397 cmp = sort->cmp(data, this);
398 if (cmp)
399 break;
400 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800401
402 if (cmp > 0)
403 new = &((*new)->rb_left);
404 else
405 new = &((*new)->rb_right);
406 }
407
408 rb_link_node(&data->node, parent, new);
409 rb_insert_color(&data->node, root);
410}
411
412static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
Li Zefan29b3e152009-11-24 13:26:10 +0800413 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800414{
415 struct rb_node *node;
416 struct alloc_stat *data;
417
418 for (;;) {
419 node = rb_first(root);
420 if (!node)
421 break;
422
423 rb_erase(node, root);
424 data = rb_entry(node, struct alloc_stat, node);
Li Zefan29b3e152009-11-24 13:26:10 +0800425 sort_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +0800426 }
427}
428
429static void sort_result(void)
430{
Li Zefan29b3e152009-11-24 13:26:10 +0800431 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
432 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
Li Zefanba77c9e2009-11-20 15:53:25 +0800433}
434
435static int __cmd_kmem(void)
436{
437 setup_pager();
438 read_events();
439 sort_result();
440 print_result();
441
442 return 0;
443}
444
445static const char * const kmem_usage[] = {
446 "perf kmem [<options>] {record}",
447 NULL
448};
449
Li Zefanba77c9e2009-11-20 15:53:25 +0800450static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
451{
452 if (l->ptr < r->ptr)
453 return -1;
454 else if (l->ptr > r->ptr)
455 return 1;
456 return 0;
457}
458
Li Zefan29b3e152009-11-24 13:26:10 +0800459static struct sort_dimension ptr_sort_dimension = {
460 .name = "ptr",
461 .cmp = ptr_cmp,
462};
463
Li Zefanba77c9e2009-11-20 15:53:25 +0800464static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
465{
466 if (l->call_site < r->call_site)
467 return -1;
468 else if (l->call_site > r->call_site)
469 return 1;
470 return 0;
471}
472
Li Zefan29b3e152009-11-24 13:26:10 +0800473static struct sort_dimension callsite_sort_dimension = {
474 .name = "callsite",
475 .cmp = callsite_cmp,
476};
477
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200478static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
479{
480 if (l->hit < r->hit)
481 return -1;
482 else if (l->hit > r->hit)
483 return 1;
484 return 0;
485}
486
Li Zefan29b3e152009-11-24 13:26:10 +0800487static struct sort_dimension hit_sort_dimension = {
488 .name = "hit",
489 .cmp = hit_cmp,
490};
491
Li Zefanba77c9e2009-11-20 15:53:25 +0800492static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
493{
494 if (l->bytes_alloc < r->bytes_alloc)
495 return -1;
496 else if (l->bytes_alloc > r->bytes_alloc)
497 return 1;
498 return 0;
499}
500
Li Zefan29b3e152009-11-24 13:26:10 +0800501static struct sort_dimension bytes_sort_dimension = {
502 .name = "bytes",
503 .cmp = bytes_cmp,
504};
505
Pekka Enbergf3ced7c2009-11-22 11:58:00 +0200506static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
507{
508 double x, y;
509
510 x = fragmentation(l->bytes_req, l->bytes_alloc);
511 y = fragmentation(r->bytes_req, r->bytes_alloc);
512
513 if (x < y)
514 return -1;
515 else if (x > y)
516 return 1;
517 return 0;
518}
519
Li Zefan29b3e152009-11-24 13:26:10 +0800520static struct sort_dimension frag_sort_dimension = {
521 .name = "frag",
522 .cmp = frag_cmp,
523};
524
525static struct sort_dimension *avail_sorts[] = {
526 &ptr_sort_dimension,
527 &callsite_sort_dimension,
528 &hit_sort_dimension,
529 &bytes_sort_dimension,
530 &frag_sort_dimension,
531};
532
533#define NUM_AVAIL_SORTS \
534 (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
535
536static int sort_dimension__add(const char *tok, struct list_head *list)
537{
538 struct sort_dimension *sort;
539 int i;
540
541 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
542 if (!strcmp(avail_sorts[i]->name, tok)) {
543 sort = malloc(sizeof(*sort));
544 if (!sort)
545 die("malloc");
546 memcpy(sort, avail_sorts[i], sizeof(*sort));
547 list_add_tail(&sort->list, list);
548 return 0;
549 }
550 }
551
552 return -1;
553}
554
555static int setup_sorting(struct list_head *sort_list, const char *arg)
556{
557 char *tok;
558 char *str = strdup(arg);
559
560 if (!str)
561 die("strdup");
562
563 while (true) {
564 tok = strsep(&str, ",");
565 if (!tok)
566 break;
567 if (sort_dimension__add(tok, sort_list) < 0) {
568 error("Unknown --sort key: '%s'", tok);
569 return -1;
570 }
571 }
572
573 free(str);
574 return 0;
575}
576
Li Zefanba77c9e2009-11-20 15:53:25 +0800577static int parse_sort_opt(const struct option *opt __used,
578 const char *arg, int unset __used)
579{
Li Zefanba77c9e2009-11-20 15:53:25 +0800580 if (!arg)
581 return -1;
582
Li Zefanba77c9e2009-11-20 15:53:25 +0800583 if (caller_flag > alloc_flag)
Li Zefan29b3e152009-11-24 13:26:10 +0800584 return setup_sorting(&caller_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800585 else
Li Zefan29b3e152009-11-24 13:26:10 +0800586 return setup_sorting(&alloc_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +0800587
588 return 0;
589}
590
591static int parse_stat_opt(const struct option *opt __used,
592 const char *arg, int unset __used)
593{
594 if (!arg)
595 return -1;
596
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);
601 else
602 return -1;
603 return 0;
604}
605
606static int parse_line_opt(const struct option *opt __used,
607 const char *arg, int unset __used)
608{
609 int lines;
610
611 if (!arg)
612 return -1;
613
614 lines = strtoul(arg, NULL, 10);
615
616 if (caller_flag > alloc_flag)
617 caller_lines = lines;
618 else
619 alloc_lines = lines;
620
621 return 0;
622}
623
624static const struct option kmem_options[] = {
625 OPT_STRING('i', "input", &input_name, "file",
626 "input file name"),
627 OPT_CALLBACK(0, "stat", NULL, "<alloc>|<caller>",
628 "stat selector, Pass 'alloc' or 'caller'.",
629 parse_stat_opt),
Li Zefan29b3e152009-11-24 13:26:10 +0800630 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
631 "sort by key(s): ptr, call_site, bytes, hit, frag",
Li Zefanba77c9e2009-11-20 15:53:25 +0800632 parse_sort_opt),
633 OPT_CALLBACK('l', "line", NULL, "num",
634 "show n lins",
635 parse_line_opt),
Li Zefan7707b6b2009-11-24 13:25:48 +0800636 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Li Zefanba77c9e2009-11-20 15:53:25 +0800637 OPT_END()
638};
639
640static const char *record_args[] = {
641 "record",
642 "-a",
643 "-R",
644 "-M",
645 "-f",
646 "-c", "1",
647 "-e", "kmem:kmalloc",
648 "-e", "kmem:kmalloc_node",
649 "-e", "kmem:kfree",
650 "-e", "kmem:kmem_cache_alloc",
651 "-e", "kmem:kmem_cache_alloc_node",
652 "-e", "kmem:kmem_cache_free",
653};
654
655static int __cmd_record(int argc, const char **argv)
656{
657 unsigned int rec_argc, i, j;
658 const char **rec_argv;
659
660 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
661 rec_argv = calloc(rec_argc + 1, sizeof(char *));
662
663 for (i = 0; i < ARRAY_SIZE(record_args); i++)
664 rec_argv[i] = strdup(record_args[i]);
665
666 for (j = 1; j < (unsigned int)argc; j++, i++)
667 rec_argv[i] = argv[j];
668
669 return cmd_record(i, rec_argv, NULL);
670}
671
672int cmd_kmem(int argc, const char **argv, const char *prefix __used)
673{
674 symbol__init(0);
675
676 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
677
678 if (argc && !strncmp(argv[0], "rec", 3))
679 return __cmd_record(argc, argv);
680 else if (argc)
681 usage_with_options(kmem_usage, kmem_options);
682
Li Zefan29b3e152009-11-24 13:26:10 +0800683 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);
Li Zefanba77c9e2009-11-20 15:53:25 +0800687
688 return __cmd_kmem();
689}
690