4 #include "util/evlist.h"
5 #include "util/evsel.h"
7 #include "util/cache.h"
8 #include "util/symbol.h"
9 #include "util/thread.h"
10 #include "util/header.h"
11 #include "util/session.h"
12 #include "util/tool.h"
14 #include "util/parse-options.h"
15 #include "util/trace-event.h"
17 #include "util/debug.h"
19 #include <linux/rbtree.h>
22 typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
24 static const char *input_name;
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 #define PATH_SYS_NODE "/sys/devices/system/node"
62 static int init_cpunode_map(void)
67 fp = fopen("/sys/devices/system/cpu/kernel_max", "r");
73 if (fscanf(fp, "%d", &max_cpu_num) < 1) {
74 pr_err("Failed to read 'kernel_max' from sysfs");
80 cpunode_map = calloc(max_cpu_num, sizeof(int));
82 pr_err("%s: calloc failed\n", __func__);
86 for (i = 0; i < max_cpu_num; i++)
95 static int setup_cpunode_map(void)
97 struct dirent *dent1, *dent2;
99 unsigned int cpu, mem;
102 if (init_cpunode_map())
105 dir1 = opendir(PATH_SYS_NODE);
109 while ((dent1 = readdir(dir1)) != NULL) {
110 if (dent1->d_type != DT_DIR ||
111 sscanf(dent1->d_name, "node%u", &mem) < 1)
114 snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
118 while ((dent2 = readdir(dir2)) != NULL) {
119 if (dent2->d_type != DT_LNK ||
120 sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
122 cpunode_map[cpu] = mem;
130 static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
131 int bytes_req, int bytes_alloc, int cpu)
133 struct rb_node **node = &root_alloc_stat.rb_node;
134 struct rb_node *parent = NULL;
135 struct alloc_stat *data = NULL;
139 data = rb_entry(*node, struct alloc_stat, node);
142 node = &(*node)->rb_right;
143 else if (ptr < data->ptr)
144 node = &(*node)->rb_left;
149 if (data && data->ptr == ptr) {
151 data->bytes_req += bytes_req;
152 data->bytes_alloc += bytes_alloc;
154 data = malloc(sizeof(*data));
156 pr_err("%s: malloc failed\n", __func__);
162 data->bytes_req = bytes_req;
163 data->bytes_alloc = bytes_alloc;
165 rb_link_node(&data->node, parent, node);
166 rb_insert_color(&data->node, &root_alloc_stat);
168 data->call_site = call_site;
169 data->alloc_cpu = cpu;
173 static int insert_caller_stat(unsigned long call_site,
174 int bytes_req, int bytes_alloc)
176 struct rb_node **node = &root_caller_stat.rb_node;
177 struct rb_node *parent = NULL;
178 struct alloc_stat *data = NULL;
182 data = rb_entry(*node, struct alloc_stat, node);
184 if (call_site > data->call_site)
185 node = &(*node)->rb_right;
186 else if (call_site < data->call_site)
187 node = &(*node)->rb_left;
192 if (data && data->call_site == call_site) {
194 data->bytes_req += bytes_req;
195 data->bytes_alloc += bytes_alloc;
197 data = malloc(sizeof(*data));
199 pr_err("%s: malloc failed\n", __func__);
202 data->call_site = call_site;
205 data->bytes_req = bytes_req;
206 data->bytes_alloc = bytes_alloc;
208 rb_link_node(&data->node, parent, node);
209 rb_insert_color(&data->node, &root_caller_stat);
215 static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
216 struct perf_sample *sample)
218 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
219 call_site = perf_evsel__intval(evsel, sample, "call_site");
220 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
221 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
223 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
224 insert_caller_stat(call_site, bytes_req, bytes_alloc))
227 total_requested += bytes_req;
228 total_allocated += bytes_alloc;
234 static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
235 struct perf_sample *sample)
237 int ret = perf_evsel__process_alloc_event(evsel, sample);
240 int node1 = cpunode_map[sample->cpu],
241 node2 = perf_evsel__intval(evsel, sample, "node");
250 static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
251 static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
253 static struct alloc_stat *search_alloc_stat(unsigned long ptr,
254 unsigned long call_site,
255 struct rb_root *root,
258 struct rb_node *node = root->rb_node;
259 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
262 struct alloc_stat *data;
265 data = rb_entry(node, struct alloc_stat, node);
267 cmp = sort_fn(&key, data);
269 node = node->rb_left;
271 node = node->rb_right;
278 static int perf_evsel__process_free_event(struct perf_evsel *evsel,
279 struct perf_sample *sample)
281 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
282 struct alloc_stat *s_alloc, *s_caller;
284 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
288 if ((short)sample->cpu != s_alloc->alloc_cpu) {
291 s_caller = search_alloc_stat(0, s_alloc->call_site,
292 &root_caller_stat, callsite_cmp);
295 s_caller->pingpong++;
297 s_alloc->alloc_cpu = -1;
302 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
303 struct perf_sample *sample);
305 static int process_sample_event(struct perf_tool *tool __maybe_unused,
306 union perf_event *event,
307 struct perf_sample *sample,
308 struct perf_evsel *evsel,
309 struct machine *machine)
311 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
313 if (thread == NULL) {
314 pr_debug("problem processing %d event, skipping it.\n",
319 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
321 if (evsel->handler.func != NULL) {
322 tracepoint_handler f = evsel->handler.func;
323 return f(evsel, sample);
329 static struct perf_tool perf_kmem = {
330 .sample = process_sample_event,
331 .comm = perf_event__process_comm,
332 .ordered_samples = true,
335 static double fragmentation(unsigned long n_req, unsigned long n_alloc)
340 return 100.0 - (100.0 * n_req / n_alloc);
343 static void __print_result(struct rb_root *root, struct perf_session *session,
344 int n_lines, int is_caller)
346 struct rb_node *next;
347 struct machine *machine;
349 printf("%.102s\n", graph_dotted_line);
350 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
351 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
352 printf("%.102s\n", graph_dotted_line);
354 next = rb_first(root);
356 machine = perf_session__find_host_machine(session);
358 pr_err("__print_result: couldn't find kernel information\n");
361 while (next && n_lines--) {
362 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
364 struct symbol *sym = NULL;
370 addr = data->call_site;
372 sym = machine__find_kernel_function(machine, addr, &map, NULL);
377 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
378 addr - map->unmap_ip(map, sym->start));
380 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
381 printf(" %-34s |", buf);
383 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
384 (unsigned long long)data->bytes_alloc,
385 (unsigned long)data->bytes_alloc / data->hit,
386 (unsigned long long)data->bytes_req,
387 (unsigned long)data->bytes_req / data->hit,
388 (unsigned long)data->hit,
389 (unsigned long)data->pingpong,
390 fragmentation(data->bytes_req, data->bytes_alloc));
392 next = rb_next(next);
396 printf(" ... | ... | ... | ... | ... | ... \n");
398 printf("%.102s\n", graph_dotted_line);
401 static void print_summary(void)
403 printf("\nSUMMARY\n=======\n");
404 printf("Total bytes requested: %lu\n", total_requested);
405 printf("Total bytes allocated: %lu\n", total_allocated);
406 printf("Total bytes wasted on internal fragmentation: %lu\n",
407 total_allocated - total_requested);
408 printf("Internal fragmentation: %f%%\n",
409 fragmentation(total_requested, total_allocated));
410 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
413 static void print_result(struct perf_session *session)
416 __print_result(&root_caller_sorted, session, caller_lines, 1);
418 __print_result(&root_alloc_sorted, session, alloc_lines, 0);
422 struct sort_dimension {
425 struct list_head list;
428 static LIST_HEAD(caller_sort);
429 static LIST_HEAD(alloc_sort);
431 static void sort_insert(struct rb_root *root, struct alloc_stat *data,
432 struct list_head *sort_list)
434 struct rb_node **new = &(root->rb_node);
435 struct rb_node *parent = NULL;
436 struct sort_dimension *sort;
439 struct alloc_stat *this;
442 this = rb_entry(*new, struct alloc_stat, node);
445 list_for_each_entry(sort, sort_list, list) {
446 cmp = sort->cmp(data, this);
452 new = &((*new)->rb_left);
454 new = &((*new)->rb_right);
457 rb_link_node(&data->node, parent, new);
458 rb_insert_color(&data->node, root);
461 static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
462 struct list_head *sort_list)
464 struct rb_node *node;
465 struct alloc_stat *data;
468 node = rb_first(root);
472 rb_erase(node, root);
473 data = rb_entry(node, struct alloc_stat, node);
474 sort_insert(root_sorted, data, sort_list);
478 static void sort_result(void)
480 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
481 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
484 static int __cmd_kmem(void)
487 struct perf_session *session;
488 const struct perf_evsel_str_handler kmem_tracepoints[] = {
489 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
490 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
491 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
492 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
493 { "kmem:kfree", perf_evsel__process_free_event, },
494 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
497 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem);
501 if (perf_session__create_kernel_maps(session) < 0)
504 if (!perf_session__has_traces(session, "kmem record"))
507 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
508 pr_err("Initializing perf session tracepoint handlers failed\n");
513 err = perf_session__process_events(session, &perf_kmem);
517 print_result(session);
519 perf_session__delete(session);
523 static const char * const kmem_usage[] = {
524 "perf kmem [<options>] {record|stat}",
528 static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
532 else if (l->ptr > r->ptr)
537 static struct sort_dimension ptr_sort_dimension = {
542 static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
544 if (l->call_site < r->call_site)
546 else if (l->call_site > r->call_site)
551 static struct sort_dimension callsite_sort_dimension = {
556 static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
560 else if (l->hit > r->hit)
565 static struct sort_dimension hit_sort_dimension = {
570 static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
572 if (l->bytes_alloc < r->bytes_alloc)
574 else if (l->bytes_alloc > r->bytes_alloc)
579 static struct sort_dimension bytes_sort_dimension = {
584 static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
588 x = fragmentation(l->bytes_req, l->bytes_alloc);
589 y = fragmentation(r->bytes_req, r->bytes_alloc);
598 static struct sort_dimension frag_sort_dimension = {
603 static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
605 if (l->pingpong < r->pingpong)
607 else if (l->pingpong > r->pingpong)
612 static struct sort_dimension pingpong_sort_dimension = {
617 static struct sort_dimension *avail_sorts[] = {
619 &callsite_sort_dimension,
621 &bytes_sort_dimension,
622 &frag_sort_dimension,
623 &pingpong_sort_dimension,
626 #define NUM_AVAIL_SORTS \
627 (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
629 static int sort_dimension__add(const char *tok, struct list_head *list)
631 struct sort_dimension *sort;
634 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
635 if (!strcmp(avail_sorts[i]->name, tok)) {
636 sort = malloc(sizeof(*sort));
638 pr_err("%s: malloc failed\n", __func__);
641 memcpy(sort, avail_sorts[i], sizeof(*sort));
642 list_add_tail(&sort->list, list);
650 static int setup_sorting(struct list_head *sort_list, const char *arg)
653 char *str = strdup(arg);
656 pr_err("%s: strdup failed\n", __func__);
661 tok = strsep(&str, ",");
664 if (sort_dimension__add(tok, sort_list) < 0) {
665 error("Unknown --sort key: '%s'", tok);
675 static int parse_sort_opt(const struct option *opt __maybe_unused,
676 const char *arg, int unset __maybe_unused)
681 if (caller_flag > alloc_flag)
682 return setup_sorting(&caller_sort, arg);
684 return setup_sorting(&alloc_sort, arg);
689 static int parse_caller_opt(const struct option *opt __maybe_unused,
690 const char *arg __maybe_unused,
691 int unset __maybe_unused)
693 caller_flag = (alloc_flag + 1);
697 static int parse_alloc_opt(const struct option *opt __maybe_unused,
698 const char *arg __maybe_unused,
699 int unset __maybe_unused)
701 alloc_flag = (caller_flag + 1);
705 static int parse_line_opt(const struct option *opt __maybe_unused,
706 const char *arg, int unset __maybe_unused)
713 lines = strtoul(arg, NULL, 10);
715 if (caller_flag > alloc_flag)
716 caller_lines = lines;
723 static const struct option kmem_options[] = {
724 OPT_STRING('i', "input", &input_name, "file",
726 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
727 "show per-callsite statistics",
729 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
730 "show per-allocation statistics",
732 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
733 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
735 OPT_CALLBACK('l', "line", NULL, "num",
738 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
742 static const char *record_args[] = {
748 "-e", "kmem:kmalloc",
749 "-e", "kmem:kmalloc_node",
751 "-e", "kmem:kmem_cache_alloc",
752 "-e", "kmem:kmem_cache_alloc_node",
753 "-e", "kmem:kmem_cache_free",
756 static int __cmd_record(int argc, const char **argv)
758 unsigned int rec_argc, i, j;
759 const char **rec_argv;
761 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
762 rec_argv = calloc(rec_argc + 1, sizeof(char *));
764 if (rec_argv == NULL)
767 for (i = 0; i < ARRAY_SIZE(record_args); i++)
768 rec_argv[i] = strdup(record_args[i]);
770 for (j = 1; j < (unsigned int)argc; j++, i++)
771 rec_argv[i] = argv[j];
773 return cmd_record(i, rec_argv, NULL);
776 int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
778 argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
781 usage_with_options(kmem_usage, kmem_options);
785 if (!strncmp(argv[0], "rec", 3)) {
786 return __cmd_record(argc, argv);
787 } else if (!strcmp(argv[0], "stat")) {
788 if (setup_cpunode_map())
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);
798 usage_with_options(kmem_usage, kmem_options);