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";
36 static int *cpunode_map
;
37 static int max_cpu_num
;
52 static struct rb_root root_alloc_stat
;
53 static struct rb_root root_alloc_sorted
;
54 static struct rb_root root_caller_stat
;
55 static struct rb_root root_caller_sorted
;
57 static unsigned long total_requested
, total_allocated
;
58 static unsigned long nr_allocs
, nr_cross_allocs
;
60 struct raw_event_sample
{
65 #define PATH_SYS_NODE "/sys/devices/system/node"
67 static void init_cpunode_map(void)
72 fp
= fopen("/sys/devices/system/cpu/kernel_max", "r");
78 if (fscanf(fp
, "%d", &max_cpu_num
) < 1)
79 die("Failed to read 'kernel_max' from sysfs");
82 cpunode_map
= calloc(max_cpu_num
, sizeof(int));
85 for (i
= 0; i
< max_cpu_num
; i
++)
90 static void setup_cpunode_map(void)
92 struct dirent
*dent1
, *dent2
;
94 unsigned int cpu
, mem
;
99 dir1
= opendir(PATH_SYS_NODE
);
104 dent1
= readdir(dir1
);
108 if (sscanf(dent1
->d_name
, "node%u", &mem
) < 1)
111 snprintf(buf
, PATH_MAX
, "%s/%s", PATH_SYS_NODE
, dent1
->d_name
);
116 dent2
= readdir(dir2
);
119 if (sscanf(dent2
->d_name
, "cpu%u", &cpu
) < 1)
121 cpunode_map
[cpu
] = mem
;
126 static void insert_alloc_stat(unsigned long call_site
, unsigned long ptr
,
127 int bytes_req
, int bytes_alloc
, int cpu
)
129 struct rb_node
**node
= &root_alloc_stat
.rb_node
;
130 struct rb_node
*parent
= NULL
;
131 struct alloc_stat
*data
= NULL
;
135 data
= rb_entry(*node
, struct alloc_stat
, node
);
138 node
= &(*node
)->rb_right
;
139 else if (ptr
< data
->ptr
)
140 node
= &(*node
)->rb_left
;
145 if (data
&& data
->ptr
== ptr
) {
147 data
->bytes_req
+= bytes_req
;
148 data
->bytes_alloc
+= bytes_req
;
150 data
= malloc(sizeof(*data
));
156 data
->bytes_req
= bytes_req
;
157 data
->bytes_alloc
= bytes_alloc
;
159 rb_link_node(&data
->node
, parent
, node
);
160 rb_insert_color(&data
->node
, &root_alloc_stat
);
162 data
->call_site
= call_site
;
163 data
->alloc_cpu
= cpu
;
166 static void insert_caller_stat(unsigned long call_site
,
167 int bytes_req
, int bytes_alloc
)
169 struct rb_node
**node
= &root_caller_stat
.rb_node
;
170 struct rb_node
*parent
= NULL
;
171 struct alloc_stat
*data
= NULL
;
175 data
= rb_entry(*node
, struct alloc_stat
, node
);
177 if (call_site
> data
->call_site
)
178 node
= &(*node
)->rb_right
;
179 else if (call_site
< data
->call_site
)
180 node
= &(*node
)->rb_left
;
185 if (data
&& data
->call_site
== call_site
) {
187 data
->bytes_req
+= bytes_req
;
188 data
->bytes_alloc
+= bytes_req
;
190 data
= malloc(sizeof(*data
));
193 data
->call_site
= call_site
;
196 data
->bytes_req
= bytes_req
;
197 data
->bytes_alloc
= bytes_alloc
;
199 rb_link_node(&data
->node
, parent
, node
);
200 rb_insert_color(&data
->node
, &root_caller_stat
);
204 static void process_alloc_event(struct raw_event_sample
*raw
,
207 u64 timestamp __used
,
208 struct thread
*thread __used
,
211 unsigned long call_site
;
217 ptr
= raw_field_value(event
, "ptr", raw
->data
);
218 call_site
= raw_field_value(event
, "call_site", raw
->data
);
219 bytes_req
= raw_field_value(event
, "bytes_req", raw
->data
);
220 bytes_alloc
= raw_field_value(event
, "bytes_alloc", raw
->data
);
222 insert_alloc_stat(call_site
, ptr
, bytes_req
, bytes_alloc
, cpu
);
223 insert_caller_stat(call_site
, bytes_req
, bytes_alloc
);
225 total_requested
+= bytes_req
;
226 total_allocated
+= bytes_alloc
;
229 node1
= cpunode_map
[cpu
];
230 node2
= raw_field_value(event
, "node", raw
->data
);
237 static int ptr_cmp(struct alloc_stat
*, struct alloc_stat
*);
238 static int callsite_cmp(struct alloc_stat
*, struct alloc_stat
*);
240 static struct alloc_stat
*search_alloc_stat(unsigned long ptr
,
241 unsigned long call_site
,
242 struct rb_root
*root
,
245 struct rb_node
*node
= root
->rb_node
;
246 struct alloc_stat key
= { .ptr
= ptr
, .call_site
= call_site
};
249 struct alloc_stat
*data
;
252 data
= rb_entry(node
, struct alloc_stat
, node
);
254 cmp
= sort_fn(&key
, data
);
256 node
= node
->rb_left
;
258 node
= node
->rb_right
;
265 static void process_free_event(struct raw_event_sample
*raw
,
268 u64 timestamp __used
,
269 struct thread
*thread __used
)
272 struct alloc_stat
*s_alloc
, *s_caller
;
274 ptr
= raw_field_value(event
, "ptr", raw
->data
);
276 s_alloc
= search_alloc_stat(ptr
, 0, &root_alloc_stat
, ptr_cmp
);
280 if (cpu
!= s_alloc
->alloc_cpu
) {
283 s_caller
= search_alloc_stat(0, s_alloc
->call_site
,
284 &root_caller_stat
, callsite_cmp
);
286 s_caller
->pingpong
++;
288 s_alloc
->alloc_cpu
= -1;
292 process_raw_event(event_t
*raw_event __used
, u32 size
, void *data
,
293 int cpu
, u64 timestamp
, struct thread
*thread
)
295 struct raw_event_sample
*raw
;
299 raw
= malloc_or_die(sizeof(*raw
)+size
);
301 memcpy(raw
->data
, data
, size
);
303 type
= trace_parse_common_type(raw
->data
);
304 event
= trace_find_event(type
);
306 if (!strcmp(event
->name
, "kmalloc") ||
307 !strcmp(event
->name
, "kmem_cache_alloc")) {
308 process_alloc_event(raw
, event
, cpu
, timestamp
, thread
, 0);
312 if (!strcmp(event
->name
, "kmalloc_node") ||
313 !strcmp(event
->name
, "kmem_cache_alloc_node")) {
314 process_alloc_event(raw
, event
, cpu
, timestamp
, thread
, 1);
318 if (!strcmp(event
->name
, "kfree") ||
319 !strcmp(event
->name
, "kmem_cache_free")) {
320 process_free_event(raw
, event
, cpu
, timestamp
, thread
);
325 static int process_sample_event(event_t
*event
)
327 struct sample_data data
;
328 struct thread
*thread
;
330 memset(&data
, 0, sizeof(data
));
335 event__parse_sample(event
, sample_type
, &data
);
337 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
340 (void *)(long)data
.ip
,
341 (long long)data
.period
);
343 thread
= threads__findnew(event
->ip
.pid
);
344 if (thread
== NULL
) {
345 pr_debug("problem processing %d event, skipping it.\n",
350 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
352 process_raw_event(event
, data
.raw_size
, data
.raw_data
, data
.cpu
,
358 static int sample_type_check(u64 type
)
362 if (!(sample_type
& PERF_SAMPLE_RAW
)) {
364 "No trace sample to read. Did you call perf record "
372 static struct perf_file_handler file_handler
= {
373 .process_sample_event
= process_sample_event
,
374 .process_comm_event
= event__process_comm
,
375 .sample_type_check
= sample_type_check
,
378 static int read_events(void)
380 register_idle_thread();
381 register_perf_file_handler(&file_handler
);
383 return mmap_dispatch_perf_file(&header
, input_name
, 0, 0,
384 &event__cwdlen
, &event__cwd
);
387 static double fragmentation(unsigned long n_req
, unsigned long n_alloc
)
392 return 100.0 - (100.0 * n_req
/ n_alloc
);
395 static void __print_result(struct rb_root
*root
, int n_lines
, int is_caller
)
397 struct rb_node
*next
;
399 printf("%.102s\n", graph_dotted_line
);
400 printf(" %-34s |", is_caller
? "Callsite": "Alloc Ptr");
401 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
402 printf("%.102s\n", graph_dotted_line
);
404 next
= rb_first(root
);
406 while (next
&& n_lines
--) {
407 struct alloc_stat
*data
= rb_entry(next
, struct alloc_stat
,
409 struct symbol
*sym
= NULL
;
414 addr
= data
->call_site
;
416 sym
= thread__find_function(kthread
, addr
, NULL
);
421 snprintf(buf
, sizeof(buf
), "%s+%Lx", sym
->name
,
424 snprintf(buf
, sizeof(buf
), "%#Lx", addr
);
425 printf(" %-34s |", buf
);
427 printf(" %9llu/%-5lu | %9llu/%-5lu | %6lu | %8lu | %6.3f%%\n",
428 (unsigned long long)data
->bytes_alloc
,
429 (unsigned long)data
->bytes_alloc
/ data
->hit
,
430 (unsigned long long)data
->bytes_req
,
431 (unsigned long)data
->bytes_req
/ data
->hit
,
432 (unsigned long)data
->hit
,
433 (unsigned long)data
->pingpong
,
434 fragmentation(data
->bytes_req
, data
->bytes_alloc
));
436 next
= rb_next(next
);
440 printf(" ... | ... | ... | ... | ... | ... \n");
442 printf("%.102s\n", graph_dotted_line
);
445 static void print_summary(void)
447 printf("\nSUMMARY\n=======\n");
448 printf("Total bytes requested: %lu\n", total_requested
);
449 printf("Total bytes allocated: %lu\n", total_allocated
);
450 printf("Total bytes wasted on internal fragmentation: %lu\n",
451 total_allocated
- total_requested
);
452 printf("Internal fragmentation: %f%%\n",
453 fragmentation(total_requested
, total_allocated
));
454 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs
, nr_allocs
);
457 static void print_result(void)
460 __print_result(&root_caller_sorted
, caller_lines
, 1);
462 __print_result(&root_alloc_sorted
, alloc_lines
, 0);
466 struct sort_dimension
{
469 struct list_head list
;
472 static LIST_HEAD(caller_sort
);
473 static LIST_HEAD(alloc_sort
);
475 static void sort_insert(struct rb_root
*root
, struct alloc_stat
*data
,
476 struct list_head
*sort_list
)
478 struct rb_node
**new = &(root
->rb_node
);
479 struct rb_node
*parent
= NULL
;
480 struct sort_dimension
*sort
;
483 struct alloc_stat
*this;
486 this = rb_entry(*new, struct alloc_stat
, node
);
489 list_for_each_entry(sort
, sort_list
, list
) {
490 cmp
= sort
->cmp(data
, this);
496 new = &((*new)->rb_left
);
498 new = &((*new)->rb_right
);
501 rb_link_node(&data
->node
, parent
, new);
502 rb_insert_color(&data
->node
, root
);
505 static void __sort_result(struct rb_root
*root
, struct rb_root
*root_sorted
,
506 struct list_head
*sort_list
)
508 struct rb_node
*node
;
509 struct alloc_stat
*data
;
512 node
= rb_first(root
);
516 rb_erase(node
, root
);
517 data
= rb_entry(node
, struct alloc_stat
, node
);
518 sort_insert(root_sorted
, data
, sort_list
);
522 static void sort_result(void)
524 __sort_result(&root_alloc_stat
, &root_alloc_sorted
, &alloc_sort
);
525 __sort_result(&root_caller_stat
, &root_caller_sorted
, &caller_sort
);
528 static int __cmd_kmem(void)
538 static const char * const kmem_usage
[] = {
539 "perf kmem [<options>] {record}",
543 static int ptr_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
547 else if (l
->ptr
> r
->ptr
)
552 static struct sort_dimension ptr_sort_dimension
= {
557 static int callsite_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
559 if (l
->call_site
< r
->call_site
)
561 else if (l
->call_site
> r
->call_site
)
566 static struct sort_dimension callsite_sort_dimension
= {
571 static int hit_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
575 else if (l
->hit
> r
->hit
)
580 static struct sort_dimension hit_sort_dimension
= {
585 static int bytes_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
587 if (l
->bytes_alloc
< r
->bytes_alloc
)
589 else if (l
->bytes_alloc
> r
->bytes_alloc
)
594 static struct sort_dimension bytes_sort_dimension
= {
599 static int frag_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
603 x
= fragmentation(l
->bytes_req
, l
->bytes_alloc
);
604 y
= fragmentation(r
->bytes_req
, r
->bytes_alloc
);
613 static struct sort_dimension frag_sort_dimension
= {
618 static int pingpong_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
620 if (l
->pingpong
< r
->pingpong
)
622 else if (l
->pingpong
> r
->pingpong
)
627 static struct sort_dimension pingpong_sort_dimension
= {
632 static struct sort_dimension
*avail_sorts
[] = {
634 &callsite_sort_dimension
,
636 &bytes_sort_dimension
,
637 &frag_sort_dimension
,
638 &pingpong_sort_dimension
,
641 #define NUM_AVAIL_SORTS \
642 (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
644 static int sort_dimension__add(const char *tok
, struct list_head
*list
)
646 struct sort_dimension
*sort
;
649 for (i
= 0; i
< NUM_AVAIL_SORTS
; i
++) {
650 if (!strcmp(avail_sorts
[i
]->name
, tok
)) {
651 sort
= malloc(sizeof(*sort
));
654 memcpy(sort
, avail_sorts
[i
], sizeof(*sort
));
655 list_add_tail(&sort
->list
, list
);
663 static int setup_sorting(struct list_head
*sort_list
, const char *arg
)
666 char *str
= strdup(arg
);
672 tok
= strsep(&str
, ",");
675 if (sort_dimension__add(tok
, sort_list
) < 0) {
676 error("Unknown --sort key: '%s'", tok
);
685 static int parse_sort_opt(const struct option
*opt __used
,
686 const char *arg
, int unset __used
)
691 if (caller_flag
> alloc_flag
)
692 return setup_sorting(&caller_sort
, arg
);
694 return setup_sorting(&alloc_sort
, arg
);
699 static int parse_stat_opt(const struct option
*opt __used
,
700 const char *arg
, int unset __used
)
705 if (strcmp(arg
, "alloc") == 0)
706 alloc_flag
= (caller_flag
+ 1);
707 else if (strcmp(arg
, "caller") == 0)
708 caller_flag
= (alloc_flag
+ 1);
714 static int parse_line_opt(const struct option
*opt __used
,
715 const char *arg
, int unset __used
)
722 lines
= strtoul(arg
, NULL
, 10);
724 if (caller_flag
> alloc_flag
)
725 caller_lines
= lines
;
732 static const struct option kmem_options
[] = {
733 OPT_STRING('i', "input", &input_name
, "file",
735 OPT_CALLBACK(0, "stat", NULL
, "<alloc>|<caller>",
736 "stat selector, Pass 'alloc' or 'caller'.",
738 OPT_CALLBACK('s', "sort", NULL
, "key[,key2...]",
739 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
741 OPT_CALLBACK('l', "line", NULL
, "num",
744 OPT_BOOLEAN(0, "raw-ip", &raw_ip
, "show raw ip instead of symbol"),
748 static const char *record_args
[] = {
755 "-e", "kmem:kmalloc",
756 "-e", "kmem:kmalloc_node",
758 "-e", "kmem:kmem_cache_alloc",
759 "-e", "kmem:kmem_cache_alloc_node",
760 "-e", "kmem:kmem_cache_free",
763 static int __cmd_record(int argc
, const char **argv
)
765 unsigned int rec_argc
, i
, j
;
766 const char **rec_argv
;
768 rec_argc
= ARRAY_SIZE(record_args
) + argc
- 1;
769 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
771 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
772 rec_argv
[i
] = strdup(record_args
[i
]);
774 for (j
= 1; j
< (unsigned int)argc
; j
++, i
++)
775 rec_argv
[i
] = argv
[j
];
777 return cmd_record(i
, rec_argv
, NULL
);
780 int cmd_kmem(int argc
, const char **argv
, const char *prefix __used
)
784 argc
= parse_options(argc
, argv
, kmem_options
, kmem_usage
, 0);
786 if (argc
&& !strncmp(argv
[0], "rec", 3))
787 return __cmd_record(argc
, argv
);
789 usage_with_options(kmem_usage
, kmem_options
);
791 if (list_empty(&caller_sort
))
792 setup_sorting(&caller_sort
, default_sort_order
);
793 if (list_empty(&alloc_sort
))
794 setup_sorting(&alloc_sort
, default_sort_order
);