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"
16 #include "util/data.h"
17 #include "util/cpumap.h"
19 #include "util/debug.h"
21 #include <linux/rbtree.h>
22 #include <linux/string.h>
25 typedef int (*sort_fn_t
)(struct alloc_stat
*, struct alloc_stat
*);
27 static int alloc_flag
;
28 static int caller_flag
;
30 static int alloc_lines
= -1;
31 static int caller_lines
= -1;
48 static struct rb_root root_alloc_stat
;
49 static struct rb_root root_alloc_sorted
;
50 static struct rb_root root_caller_stat
;
51 static struct rb_root root_caller_sorted
;
53 static unsigned long total_requested
, total_allocated
;
54 static unsigned long nr_allocs
, nr_cross_allocs
;
56 static int insert_alloc_stat(unsigned long call_site
, unsigned long ptr
,
57 int bytes_req
, int bytes_alloc
, int cpu
)
59 struct rb_node
**node
= &root_alloc_stat
.rb_node
;
60 struct rb_node
*parent
= NULL
;
61 struct alloc_stat
*data
= NULL
;
65 data
= rb_entry(*node
, struct alloc_stat
, node
);
68 node
= &(*node
)->rb_right
;
69 else if (ptr
< data
->ptr
)
70 node
= &(*node
)->rb_left
;
75 if (data
&& data
->ptr
== ptr
) {
77 data
->bytes_req
+= bytes_req
;
78 data
->bytes_alloc
+= bytes_alloc
;
80 data
= malloc(sizeof(*data
));
82 pr_err("%s: malloc failed\n", __func__
);
88 data
->bytes_req
= bytes_req
;
89 data
->bytes_alloc
= bytes_alloc
;
91 rb_link_node(&data
->node
, parent
, node
);
92 rb_insert_color(&data
->node
, &root_alloc_stat
);
94 data
->call_site
= call_site
;
95 data
->alloc_cpu
= cpu
;
99 static int insert_caller_stat(unsigned long call_site
,
100 int bytes_req
, int bytes_alloc
)
102 struct rb_node
**node
= &root_caller_stat
.rb_node
;
103 struct rb_node
*parent
= NULL
;
104 struct alloc_stat
*data
= NULL
;
108 data
= rb_entry(*node
, struct alloc_stat
, node
);
110 if (call_site
> data
->call_site
)
111 node
= &(*node
)->rb_right
;
112 else if (call_site
< data
->call_site
)
113 node
= &(*node
)->rb_left
;
118 if (data
&& data
->call_site
== call_site
) {
120 data
->bytes_req
+= bytes_req
;
121 data
->bytes_alloc
+= bytes_alloc
;
123 data
= malloc(sizeof(*data
));
125 pr_err("%s: malloc failed\n", __func__
);
128 data
->call_site
= call_site
;
131 data
->bytes_req
= bytes_req
;
132 data
->bytes_alloc
= bytes_alloc
;
134 rb_link_node(&data
->node
, parent
, node
);
135 rb_insert_color(&data
->node
, &root_caller_stat
);
141 static int perf_evsel__process_alloc_event(struct perf_evsel
*evsel
,
142 struct perf_sample
*sample
)
144 unsigned long ptr
= perf_evsel__intval(evsel
, sample
, "ptr"),
145 call_site
= perf_evsel__intval(evsel
, sample
, "call_site");
146 int bytes_req
= perf_evsel__intval(evsel
, sample
, "bytes_req"),
147 bytes_alloc
= perf_evsel__intval(evsel
, sample
, "bytes_alloc");
149 if (insert_alloc_stat(call_site
, ptr
, bytes_req
, bytes_alloc
, sample
->cpu
) ||
150 insert_caller_stat(call_site
, bytes_req
, bytes_alloc
))
153 total_requested
+= bytes_req
;
154 total_allocated
+= bytes_alloc
;
160 static int perf_evsel__process_alloc_node_event(struct perf_evsel
*evsel
,
161 struct perf_sample
*sample
)
163 int ret
= perf_evsel__process_alloc_event(evsel
, sample
);
166 int node1
= cpu__get_node(sample
->cpu
),
167 node2
= perf_evsel__intval(evsel
, sample
, "node");
176 static int ptr_cmp(struct alloc_stat
*, struct alloc_stat
*);
177 static int callsite_cmp(struct alloc_stat
*, struct alloc_stat
*);
179 static struct alloc_stat
*search_alloc_stat(unsigned long ptr
,
180 unsigned long call_site
,
181 struct rb_root
*root
,
184 struct rb_node
*node
= root
->rb_node
;
185 struct alloc_stat key
= { .ptr
= ptr
, .call_site
= call_site
};
188 struct alloc_stat
*data
;
191 data
= rb_entry(node
, struct alloc_stat
, node
);
193 cmp
= sort_fn(&key
, data
);
195 node
= node
->rb_left
;
197 node
= node
->rb_right
;
204 static int perf_evsel__process_free_event(struct perf_evsel
*evsel
,
205 struct perf_sample
*sample
)
207 unsigned long ptr
= perf_evsel__intval(evsel
, sample
, "ptr");
208 struct alloc_stat
*s_alloc
, *s_caller
;
210 s_alloc
= search_alloc_stat(ptr
, 0, &root_alloc_stat
, ptr_cmp
);
214 if ((short)sample
->cpu
!= s_alloc
->alloc_cpu
) {
217 s_caller
= search_alloc_stat(0, s_alloc
->call_site
,
218 &root_caller_stat
, callsite_cmp
);
221 s_caller
->pingpong
++;
223 s_alloc
->alloc_cpu
= -1;
228 typedef int (*tracepoint_handler
)(struct perf_evsel
*evsel
,
229 struct perf_sample
*sample
);
231 static int process_sample_event(struct perf_tool
*tool __maybe_unused
,
232 union perf_event
*event
,
233 struct perf_sample
*sample
,
234 struct perf_evsel
*evsel
,
235 struct machine
*machine
)
237 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
240 if (thread
== NULL
) {
241 pr_debug("problem processing %d event, skipping it.\n",
246 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread
), thread
->tid
);
248 if (evsel
->handler
!= NULL
) {
249 tracepoint_handler f
= evsel
->handler
;
250 return f(evsel
, sample
);
256 static struct perf_tool perf_kmem
= {
257 .sample
= process_sample_event
,
258 .comm
= perf_event__process_comm
,
259 .mmap
= perf_event__process_mmap
,
260 .mmap2
= perf_event__process_mmap2
,
261 .ordered_events
= true,
264 static double fragmentation(unsigned long n_req
, unsigned long n_alloc
)
269 return 100.0 - (100.0 * n_req
/ n_alloc
);
272 static void __print_result(struct rb_root
*root
, struct perf_session
*session
,
273 int n_lines
, int is_caller
)
275 struct rb_node
*next
;
276 struct machine
*machine
= &session
->machines
.host
;
278 printf("%.102s\n", graph_dotted_line
);
279 printf(" %-34s |", is_caller
? "Callsite": "Alloc Ptr");
280 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
281 printf("%.102s\n", graph_dotted_line
);
283 next
= rb_first(root
);
285 while (next
&& n_lines
--) {
286 struct alloc_stat
*data
= rb_entry(next
, struct alloc_stat
,
288 struct symbol
*sym
= NULL
;
294 addr
= data
->call_site
;
296 sym
= machine__find_kernel_function(machine
, addr
, &map
, NULL
);
301 snprintf(buf
, sizeof(buf
), "%s+%" PRIx64
"", sym
->name
,
302 addr
- map
->unmap_ip(map
, sym
->start
));
304 snprintf(buf
, sizeof(buf
), "%#" PRIx64
"", addr
);
305 printf(" %-34s |", buf
);
307 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
308 (unsigned long long)data
->bytes_alloc
,
309 (unsigned long)data
->bytes_alloc
/ data
->hit
,
310 (unsigned long long)data
->bytes_req
,
311 (unsigned long)data
->bytes_req
/ data
->hit
,
312 (unsigned long)data
->hit
,
313 (unsigned long)data
->pingpong
,
314 fragmentation(data
->bytes_req
, data
->bytes_alloc
));
316 next
= rb_next(next
);
320 printf(" ... | ... | ... | ... | ... | ... \n");
322 printf("%.102s\n", graph_dotted_line
);
325 static void print_summary(void)
327 printf("\nSUMMARY\n=======\n");
328 printf("Total bytes requested: %lu\n", total_requested
);
329 printf("Total bytes allocated: %lu\n", total_allocated
);
330 printf("Total bytes wasted on internal fragmentation: %lu\n",
331 total_allocated
- total_requested
);
332 printf("Internal fragmentation: %f%%\n",
333 fragmentation(total_requested
, total_allocated
));
334 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs
, nr_allocs
);
337 static void print_result(struct perf_session
*session
)
340 __print_result(&root_caller_sorted
, session
, caller_lines
, 1);
342 __print_result(&root_alloc_sorted
, session
, alloc_lines
, 0);
346 struct sort_dimension
{
349 struct list_head list
;
352 static LIST_HEAD(caller_sort
);
353 static LIST_HEAD(alloc_sort
);
355 static void sort_insert(struct rb_root
*root
, struct alloc_stat
*data
,
356 struct list_head
*sort_list
)
358 struct rb_node
**new = &(root
->rb_node
);
359 struct rb_node
*parent
= NULL
;
360 struct sort_dimension
*sort
;
363 struct alloc_stat
*this;
366 this = rb_entry(*new, struct alloc_stat
, node
);
369 list_for_each_entry(sort
, sort_list
, list
) {
370 cmp
= sort
->cmp(data
, this);
376 new = &((*new)->rb_left
);
378 new = &((*new)->rb_right
);
381 rb_link_node(&data
->node
, parent
, new);
382 rb_insert_color(&data
->node
, root
);
385 static void __sort_result(struct rb_root
*root
, struct rb_root
*root_sorted
,
386 struct list_head
*sort_list
)
388 struct rb_node
*node
;
389 struct alloc_stat
*data
;
392 node
= rb_first(root
);
396 rb_erase(node
, root
);
397 data
= rb_entry(node
, struct alloc_stat
, node
);
398 sort_insert(root_sorted
, data
, sort_list
);
402 static void sort_result(void)
404 __sort_result(&root_alloc_stat
, &root_alloc_sorted
, &alloc_sort
);
405 __sort_result(&root_caller_stat
, &root_caller_sorted
, &caller_sort
);
408 static int __cmd_kmem(struct perf_session
*session
)
411 const struct perf_evsel_str_handler kmem_tracepoints
[] = {
412 { "kmem:kmalloc", perf_evsel__process_alloc_event
, },
413 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event
, },
414 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event
, },
415 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event
, },
416 { "kmem:kfree", perf_evsel__process_free_event
, },
417 { "kmem:kmem_cache_free", perf_evsel__process_free_event
, },
420 if (!perf_session__has_traces(session
, "kmem record"))
423 if (perf_session__set_tracepoints_handlers(session
, kmem_tracepoints
)) {
424 pr_err("Initializing perf session tracepoint handlers failed\n");
429 err
= perf_session__process_events(session
, &perf_kmem
);
433 print_result(session
);
438 static int ptr_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
442 else if (l
->ptr
> r
->ptr
)
447 static struct sort_dimension ptr_sort_dimension
= {
452 static int callsite_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
454 if (l
->call_site
< r
->call_site
)
456 else if (l
->call_site
> r
->call_site
)
461 static struct sort_dimension callsite_sort_dimension
= {
466 static int hit_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
470 else if (l
->hit
> r
->hit
)
475 static struct sort_dimension hit_sort_dimension
= {
480 static int bytes_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
482 if (l
->bytes_alloc
< r
->bytes_alloc
)
484 else if (l
->bytes_alloc
> r
->bytes_alloc
)
489 static struct sort_dimension bytes_sort_dimension
= {
494 static int frag_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
498 x
= fragmentation(l
->bytes_req
, l
->bytes_alloc
);
499 y
= fragmentation(r
->bytes_req
, r
->bytes_alloc
);
508 static struct sort_dimension frag_sort_dimension
= {
513 static int pingpong_cmp(struct alloc_stat
*l
, struct alloc_stat
*r
)
515 if (l
->pingpong
< r
->pingpong
)
517 else if (l
->pingpong
> r
->pingpong
)
522 static struct sort_dimension pingpong_sort_dimension
= {
527 static struct sort_dimension
*avail_sorts
[] = {
529 &callsite_sort_dimension
,
531 &bytes_sort_dimension
,
532 &frag_sort_dimension
,
533 &pingpong_sort_dimension
,
536 #define NUM_AVAIL_SORTS ((int)ARRAY_SIZE(avail_sorts))
538 static int sort_dimension__add(const char *tok
, struct list_head
*list
)
540 struct sort_dimension
*sort
;
543 for (i
= 0; i
< NUM_AVAIL_SORTS
; i
++) {
544 if (!strcmp(avail_sorts
[i
]->name
, tok
)) {
545 sort
= memdup(avail_sorts
[i
], sizeof(*avail_sorts
[i
]));
547 pr_err("%s: memdup failed\n", __func__
);
550 list_add_tail(&sort
->list
, list
);
558 static int setup_sorting(struct list_head
*sort_list
, const char *arg
)
561 char *str
= strdup(arg
);
564 pr_err("%s: strdup failed\n", __func__
);
569 tok
= strsep(&str
, ",");
572 if (sort_dimension__add(tok
, sort_list
) < 0) {
573 error("Unknown --sort key: '%s'", tok
);
583 static int parse_sort_opt(const struct option
*opt __maybe_unused
,
584 const char *arg
, int unset __maybe_unused
)
589 if (caller_flag
> alloc_flag
)
590 return setup_sorting(&caller_sort
, arg
);
592 return setup_sorting(&alloc_sort
, arg
);
597 static int parse_caller_opt(const struct option
*opt __maybe_unused
,
598 const char *arg __maybe_unused
,
599 int unset __maybe_unused
)
601 caller_flag
= (alloc_flag
+ 1);
605 static int parse_alloc_opt(const struct option
*opt __maybe_unused
,
606 const char *arg __maybe_unused
,
607 int unset __maybe_unused
)
609 alloc_flag
= (caller_flag
+ 1);
613 static int parse_line_opt(const struct option
*opt __maybe_unused
,
614 const char *arg
, int unset __maybe_unused
)
621 lines
= strtoul(arg
, NULL
, 10);
623 if (caller_flag
> alloc_flag
)
624 caller_lines
= lines
;
631 static int __cmd_record(int argc
, const char **argv
)
633 const char * const record_args
[] = {
634 "record", "-a", "-R", "-c", "1",
635 "-e", "kmem:kmalloc",
636 "-e", "kmem:kmalloc_node",
638 "-e", "kmem:kmem_cache_alloc",
639 "-e", "kmem:kmem_cache_alloc_node",
640 "-e", "kmem:kmem_cache_free",
642 unsigned int rec_argc
, i
, j
;
643 const char **rec_argv
;
645 rec_argc
= ARRAY_SIZE(record_args
) + argc
- 1;
646 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
648 if (rec_argv
== NULL
)
651 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
652 rec_argv
[i
] = strdup(record_args
[i
]);
654 for (j
= 1; j
< (unsigned int)argc
; j
++, i
++)
655 rec_argv
[i
] = argv
[j
];
657 return cmd_record(i
, rec_argv
, NULL
);
660 int cmd_kmem(int argc
, const char **argv
, const char *prefix __maybe_unused
)
662 const char * const default_sort_order
= "frag,hit,bytes";
663 const struct option kmem_options
[] = {
664 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
665 OPT_CALLBACK_NOOPT(0, "caller", NULL
, NULL
,
666 "show per-callsite statistics", parse_caller_opt
),
667 OPT_CALLBACK_NOOPT(0, "alloc", NULL
, NULL
,
668 "show per-allocation statistics", parse_alloc_opt
),
669 OPT_CALLBACK('s', "sort", NULL
, "key[,key2...]",
670 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
672 OPT_CALLBACK('l', "line", NULL
, "num", "show n lines", parse_line_opt
),
673 OPT_BOOLEAN(0, "raw-ip", &raw_ip
, "show raw ip instead of symbol"),
676 const char *const kmem_subcommands
[] = { "record", "stat", NULL
};
677 const char *kmem_usage
[] = {
681 struct perf_session
*session
;
682 struct perf_data_file file
= {
684 .mode
= PERF_DATA_MODE_READ
,
688 argc
= parse_options_subcommand(argc
, argv
, kmem_options
,
689 kmem_subcommands
, kmem_usage
, 0);
692 usage_with_options(kmem_usage
, kmem_options
);
694 if (!strncmp(argv
[0], "rec", 3)) {
696 return __cmd_record(argc
, argv
);
699 session
= perf_session__new(&file
, false, &perf_kmem
);
703 symbol__init(&session
->header
.env
);
705 if (!strcmp(argv
[0], "stat")) {
706 if (cpu__setup_cpunode_map())
709 if (list_empty(&caller_sort
))
710 setup_sorting(&caller_sort
, default_sort_order
);
711 if (list_empty(&alloc_sort
))
712 setup_sorting(&alloc_sort
, default_sort_order
);
714 ret
= __cmd_kmem(session
);
716 usage_with_options(kmem_usage
, kmem_options
);
719 perf_session__delete(session
);