Merge remote-tracking branches 'asoc/topic/cx20442' and 'asoc/topic/davinci' into...
[linux-2.6-block.git] / tools / perf / builtin-kmem.c
CommitLineData
ba77c9e1
LZ
1#include "builtin.h"
2#include "perf.h"
3
0f7d2f1b 4#include "util/evlist.h"
fcf65bf1 5#include "util/evsel.h"
ba77c9e1
LZ
6#include "util/util.h"
7#include "util/cache.h"
8#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
94c744b6 11#include "util/session.h"
45694aa7 12#include "util/tool.h"
ba77c9e1
LZ
13
14#include "util/parse-options.h"
15#include "util/trace-event.h"
f5fc1412 16#include "util/data.h"
4b627957 17#include "util/cpumap.h"
ba77c9e1
LZ
18
19#include "util/debug.h"
ba77c9e1
LZ
20
21#include <linux/rbtree.h>
8d9233f2 22#include <linux/string.h>
ba77c9e1
LZ
23
24struct alloc_stat;
25typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
26
ba77c9e1
LZ
27static int alloc_flag;
28static int caller_flag;
29
ba77c9e1
LZ
30static int alloc_lines = -1;
31static int caller_lines = -1;
32
7707b6b6
LZ
33static bool raw_ip;
34
ba77c9e1 35struct alloc_stat {
079d3f65
LZ
36 u64 call_site;
37 u64 ptr;
ba77c9e1
LZ
38 u64 bytes_req;
39 u64 bytes_alloc;
40 u32 hit;
079d3f65
LZ
41 u32 pingpong;
42
43 short alloc_cpu;
ba77c9e1
LZ
44
45 struct rb_node node;
46};
47
48static struct rb_root root_alloc_stat;
49static struct rb_root root_alloc_sorted;
50static struct rb_root root_caller_stat;
51static struct rb_root root_caller_sorted;
52
53static unsigned long total_requested, total_allocated;
7d0d3945 54static unsigned long nr_allocs, nr_cross_allocs;
ba77c9e1 55
2814eb05
ACM
56static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
57 int bytes_req, int bytes_alloc, int cpu)
ba77c9e1
LZ
58{
59 struct rb_node **node = &root_alloc_stat.rb_node;
60 struct rb_node *parent = NULL;
61 struct alloc_stat *data = NULL;
62
ba77c9e1
LZ
63 while (*node) {
64 parent = *node;
65 data = rb_entry(*node, struct alloc_stat, node);
66
67 if (ptr > data->ptr)
68 node = &(*node)->rb_right;
69 else if (ptr < data->ptr)
70 node = &(*node)->rb_left;
71 else
72 break;
73 }
74
75 if (data && data->ptr == ptr) {
76 data->hit++;
77 data->bytes_req += bytes_req;
4efb5290 78 data->bytes_alloc += bytes_alloc;
ba77c9e1
LZ
79 } else {
80 data = malloc(sizeof(*data));
2814eb05
ACM
81 if (!data) {
82 pr_err("%s: malloc failed\n", __func__);
83 return -1;
84 }
ba77c9e1 85 data->ptr = ptr;
079d3f65 86 data->pingpong = 0;
ba77c9e1
LZ
87 data->hit = 1;
88 data->bytes_req = bytes_req;
89 data->bytes_alloc = bytes_alloc;
90
91 rb_link_node(&data->node, parent, node);
92 rb_insert_color(&data->node, &root_alloc_stat);
93 }
079d3f65
LZ
94 data->call_site = call_site;
95 data->alloc_cpu = cpu;
2814eb05 96 return 0;
ba77c9e1
LZ
97}
98
2814eb05 99static int insert_caller_stat(unsigned long call_site,
ba77c9e1
LZ
100 int bytes_req, int bytes_alloc)
101{
102 struct rb_node **node = &root_caller_stat.rb_node;
103 struct rb_node *parent = NULL;
104 struct alloc_stat *data = NULL;
105
ba77c9e1
LZ
106 while (*node) {
107 parent = *node;
108 data = rb_entry(*node, struct alloc_stat, node);
109
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;
114 else
115 break;
116 }
117
118 if (data && data->call_site == call_site) {
119 data->hit++;
120 data->bytes_req += bytes_req;
4efb5290 121 data->bytes_alloc += bytes_alloc;
ba77c9e1
LZ
122 } else {
123 data = malloc(sizeof(*data));
2814eb05
ACM
124 if (!data) {
125 pr_err("%s: malloc failed\n", __func__);
126 return -1;
127 }
ba77c9e1 128 data->call_site = call_site;
079d3f65 129 data->pingpong = 0;
ba77c9e1
LZ
130 data->hit = 1;
131 data->bytes_req = bytes_req;
132 data->bytes_alloc = bytes_alloc;
133
134 rb_link_node(&data->node, parent, node);
135 rb_insert_color(&data->node, &root_caller_stat);
136 }
2814eb05
ACM
137
138 return 0;
ba77c9e1
LZ
139}
140
2814eb05 141static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
0f7d2f1b 142 struct perf_sample *sample)
ba77c9e1 143{
0f7d2f1b
ACM
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");
148
149 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
2814eb05
ACM
150 insert_caller_stat(call_site, bytes_req, bytes_alloc))
151 return -1;
ba77c9e1
LZ
152
153 total_requested += bytes_req;
154 total_allocated += bytes_alloc;
7d0d3945 155
0f7d2f1b
ACM
156 nr_allocs++;
157 return 0;
158}
159
160static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
161 struct perf_sample *sample)
162{
163 int ret = perf_evsel__process_alloc_event(evsel, sample);
164
165 if (!ret) {
4b627957 166 int node1 = cpu__get_node(sample->cpu),
0f7d2f1b
ACM
167 node2 = perf_evsel__intval(evsel, sample, "node");
168
7d0d3945
LZ
169 if (node1 != node2)
170 nr_cross_allocs++;
171 }
0f7d2f1b
ACM
172
173 return ret;
ba77c9e1
LZ
174}
175
079d3f65
LZ
176static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
177static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
178
179static struct alloc_stat *search_alloc_stat(unsigned long ptr,
180 unsigned long call_site,
181 struct rb_root *root,
182 sort_fn_t sort_fn)
183{
184 struct rb_node *node = root->rb_node;
185 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
186
187 while (node) {
188 struct alloc_stat *data;
189 int cmp;
190
191 data = rb_entry(node, struct alloc_stat, node);
192
193 cmp = sort_fn(&key, data);
194 if (cmp < 0)
195 node = node->rb_left;
196 else if (cmp > 0)
197 node = node->rb_right;
198 else
199 return data;
200 }
201 return NULL;
202}
203
2814eb05
ACM
204static int perf_evsel__process_free_event(struct perf_evsel *evsel,
205 struct perf_sample *sample)
ba77c9e1 206{
0f7d2f1b 207 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
079d3f65
LZ
208 struct alloc_stat *s_alloc, *s_caller;
209
079d3f65
LZ
210 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
211 if (!s_alloc)
2814eb05 212 return 0;
079d3f65 213
22ad798c 214 if ((short)sample->cpu != s_alloc->alloc_cpu) {
079d3f65
LZ
215 s_alloc->pingpong++;
216
217 s_caller = search_alloc_stat(0, s_alloc->call_site,
218 &root_caller_stat, callsite_cmp);
2814eb05
ACM
219 if (!s_caller)
220 return -1;
079d3f65
LZ
221 s_caller->pingpong++;
222 }
223 s_alloc->alloc_cpu = -1;
2814eb05
ACM
224
225 return 0;
ba77c9e1
LZ
226}
227
0f7d2f1b
ACM
228typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
229 struct perf_sample *sample);
ba77c9e1 230
1d037ca1 231static int process_sample_event(struct perf_tool *tool __maybe_unused,
d20deb64 232 union perf_event *event,
8115d60c 233 struct perf_sample *sample,
fcf65bf1 234 struct perf_evsel *evsel,
743eb868 235 struct machine *machine)
ba77c9e1 236{
ef89325f 237 struct thread *thread = machine__findnew_thread(machine, sample->pid,
13ce34df 238 sample->tid);
ba77c9e1 239
ba77c9e1
LZ
240 if (thread == NULL) {
241 pr_debug("problem processing %d event, skipping it.\n",
242 event->header.type);
243 return -1;
244 }
245
b9c5143a 246 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
ba77c9e1 247
744a9719
ACM
248 if (evsel->handler != NULL) {
249 tracepoint_handler f = evsel->handler;
0f7d2f1b
ACM
250 return f(evsel, sample);
251 }
252
253 return 0;
ba77c9e1
LZ
254}
255
fcf65bf1
ACM
256static struct perf_tool perf_kmem = {
257 .sample = process_sample_event,
258 .comm = perf_event__process_comm,
64c40908
NK
259 .mmap = perf_event__process_mmap,
260 .mmap2 = perf_event__process_mmap2,
0a8cb85c 261 .ordered_events = true,
ba77c9e1
LZ
262};
263
ba77c9e1
LZ
264static double fragmentation(unsigned long n_req, unsigned long n_alloc)
265{
266 if (n_alloc == 0)
267 return 0.0;
268 else
269 return 100.0 - (100.0 * n_req / n_alloc);
270}
271
4aa65636
ACM
272static void __print_result(struct rb_root *root, struct perf_session *session,
273 int n_lines, int is_caller)
ba77c9e1
LZ
274{
275 struct rb_node *next;
34ba5122 276 struct machine *machine = &session->machines.host;
ba77c9e1 277
079d3f65
LZ
278 printf("%.102s\n", graph_dotted_line);
279 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
47103277 280 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
079d3f65 281 printf("%.102s\n", graph_dotted_line);
ba77c9e1
LZ
282
283 next = rb_first(root);
284
285 while (next && n_lines--) {
1b145ae5
ACM
286 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
287 node);
288 struct symbol *sym = NULL;
71cf8b8f 289 struct map *map;
079d3f65 290 char buf[BUFSIZ];
1b145ae5
ACM
291 u64 addr;
292
293 if (is_caller) {
294 addr = data->call_site;
7707b6b6 295 if (!raw_ip)
5c0541d5 296 sym = machine__find_kernel_function(machine, addr, &map, NULL);
1b145ae5
ACM
297 } else
298 addr = data->ptr;
299
300 if (sym != NULL)
9486aa38 301 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
71cf8b8f 302 addr - map->unmap_ip(map, sym->start));
1b145ae5 303 else
9486aa38 304 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
079d3f65 305 printf(" %-34s |", buf);
ba77c9e1 306
47103277 307 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
079d3f65 308 (unsigned long long)data->bytes_alloc,
ba77c9e1
LZ
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,
079d3f65 313 (unsigned long)data->pingpong,
ba77c9e1
LZ
314 fragmentation(data->bytes_req, data->bytes_alloc));
315
316 next = rb_next(next);
317 }
318
319 if (n_lines == -1)
079d3f65 320 printf(" ... | ... | ... | ... | ... | ... \n");
ba77c9e1 321
079d3f65 322 printf("%.102s\n", graph_dotted_line);
ba77c9e1
LZ
323}
324
325static void print_summary(void)
326{
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));
7d0d3945 334 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
ba77c9e1
LZ
335}
336
4aa65636 337static void print_result(struct perf_session *session)
ba77c9e1
LZ
338{
339 if (caller_flag)
4aa65636 340 __print_result(&root_caller_sorted, session, caller_lines, 1);
ba77c9e1 341 if (alloc_flag)
4aa65636 342 __print_result(&root_alloc_sorted, session, alloc_lines, 0);
ba77c9e1
LZ
343 print_summary();
344}
345
29b3e152
LZ
346struct sort_dimension {
347 const char name[20];
348 sort_fn_t cmp;
349 struct list_head list;
350};
351
352static LIST_HEAD(caller_sort);
353static LIST_HEAD(alloc_sort);
354
ba77c9e1 355static void sort_insert(struct rb_root *root, struct alloc_stat *data,
29b3e152 356 struct list_head *sort_list)
ba77c9e1
LZ
357{
358 struct rb_node **new = &(root->rb_node);
359 struct rb_node *parent = NULL;
29b3e152 360 struct sort_dimension *sort;
ba77c9e1
LZ
361
362 while (*new) {
363 struct alloc_stat *this;
29b3e152 364 int cmp = 0;
ba77c9e1
LZ
365
366 this = rb_entry(*new, struct alloc_stat, node);
367 parent = *new;
368
29b3e152
LZ
369 list_for_each_entry(sort, sort_list, list) {
370 cmp = sort->cmp(data, this);
371 if (cmp)
372 break;
373 }
ba77c9e1
LZ
374
375 if (cmp > 0)
376 new = &((*new)->rb_left);
377 else
378 new = &((*new)->rb_right);
379 }
380
381 rb_link_node(&data->node, parent, new);
382 rb_insert_color(&data->node, root);
383}
384
385static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
29b3e152 386 struct list_head *sort_list)
ba77c9e1
LZ
387{
388 struct rb_node *node;
389 struct alloc_stat *data;
390
391 for (;;) {
392 node = rb_first(root);
393 if (!node)
394 break;
395
396 rb_erase(node, root);
397 data = rb_entry(node, struct alloc_stat, node);
29b3e152 398 sort_insert(root_sorted, data, sort_list);
ba77c9e1
LZ
399 }
400}
401
402static void sort_result(void)
403{
29b3e152
LZ
404 __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
405 __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
ba77c9e1
LZ
406}
407
2b2b2c68 408static int __cmd_kmem(struct perf_session *session)
ba77c9e1 409{
d549c769 410 int err = -EINVAL;
0f7d2f1b
ACM
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, },
418 };
4aa65636 419
d549c769 420 if (!perf_session__has_traces(session, "kmem record"))
2b2b2c68 421 goto out;
d549c769 422
0f7d2f1b
ACM
423 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
424 pr_err("Initializing perf session tracepoint handlers failed\n");
2b2b2c68 425 goto out;
0f7d2f1b
ACM
426 }
427
ba77c9e1 428 setup_pager();
fcf65bf1 429 err = perf_session__process_events(session, &perf_kmem);
4aa65636 430 if (err != 0)
2b2b2c68 431 goto out;
ba77c9e1 432 sort_result();
4aa65636 433 print_result(session);
2b2b2c68 434out:
4aa65636 435 return err;
ba77c9e1
LZ
436}
437
ba77c9e1
LZ
438static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
439{
440 if (l->ptr < r->ptr)
441 return -1;
442 else if (l->ptr > r->ptr)
443 return 1;
444 return 0;
445}
446
29b3e152
LZ
447static struct sort_dimension ptr_sort_dimension = {
448 .name = "ptr",
449 .cmp = ptr_cmp,
450};
451
ba77c9e1
LZ
452static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
453{
454 if (l->call_site < r->call_site)
455 return -1;
456 else if (l->call_site > r->call_site)
457 return 1;
458 return 0;
459}
460
29b3e152
LZ
461static struct sort_dimension callsite_sort_dimension = {
462 .name = "callsite",
463 .cmp = callsite_cmp,
464};
465
f3ced7cd
PE
466static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
467{
468 if (l->hit < r->hit)
469 return -1;
470 else if (l->hit > r->hit)
471 return 1;
472 return 0;
473}
474
29b3e152
LZ
475static struct sort_dimension hit_sort_dimension = {
476 .name = "hit",
477 .cmp = hit_cmp,
478};
479
ba77c9e1
LZ
480static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
481{
482 if (l->bytes_alloc < r->bytes_alloc)
483 return -1;
484 else if (l->bytes_alloc > r->bytes_alloc)
485 return 1;
486 return 0;
487}
488
29b3e152
LZ
489static struct sort_dimension bytes_sort_dimension = {
490 .name = "bytes",
491 .cmp = bytes_cmp,
492};
493
f3ced7cd
PE
494static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
495{
496 double x, y;
497
498 x = fragmentation(l->bytes_req, l->bytes_alloc);
499 y = fragmentation(r->bytes_req, r->bytes_alloc);
500
501 if (x < y)
502 return -1;
503 else if (x > y)
504 return 1;
505 return 0;
506}
507
29b3e152
LZ
508static struct sort_dimension frag_sort_dimension = {
509 .name = "frag",
510 .cmp = frag_cmp,
511};
512
079d3f65
LZ
513static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
514{
515 if (l->pingpong < r->pingpong)
516 return -1;
517 else if (l->pingpong > r->pingpong)
518 return 1;
519 return 0;
520}
521
522static struct sort_dimension pingpong_sort_dimension = {
523 .name = "pingpong",
524 .cmp = pingpong_cmp,
525};
526
29b3e152
LZ
527static struct sort_dimension *avail_sorts[] = {
528 &ptr_sort_dimension,
529 &callsite_sort_dimension,
530 &hit_sort_dimension,
531 &bytes_sort_dimension,
532 &frag_sort_dimension,
079d3f65 533 &pingpong_sort_dimension,
29b3e152
LZ
534};
535
49e4ba54 536#define NUM_AVAIL_SORTS ((int)ARRAY_SIZE(avail_sorts))
29b3e152
LZ
537
538static int sort_dimension__add(const char *tok, struct list_head *list)
539{
540 struct sort_dimension *sort;
541 int i;
542
543 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
544 if (!strcmp(avail_sorts[i]->name, tok)) {
8d9233f2 545 sort = memdup(avail_sorts[i], sizeof(*avail_sorts[i]));
2814eb05 546 if (!sort) {
8d9233f2 547 pr_err("%s: memdup failed\n", __func__);
2814eb05
ACM
548 return -1;
549 }
29b3e152
LZ
550 list_add_tail(&sort->list, list);
551 return 0;
552 }
553 }
554
555 return -1;
556}
557
558static int setup_sorting(struct list_head *sort_list, const char *arg)
559{
560 char *tok;
561 char *str = strdup(arg);
562
2814eb05
ACM
563 if (!str) {
564 pr_err("%s: strdup failed\n", __func__);
565 return -1;
566 }
29b3e152
LZ
567
568 while (true) {
569 tok = strsep(&str, ",");
570 if (!tok)
571 break;
572 if (sort_dimension__add(tok, sort_list) < 0) {
573 error("Unknown --sort key: '%s'", tok);
1b22859d 574 free(str);
29b3e152
LZ
575 return -1;
576 }
577 }
578
579 free(str);
580 return 0;
581}
582
1d037ca1
IT
583static int parse_sort_opt(const struct option *opt __maybe_unused,
584 const char *arg, int unset __maybe_unused)
ba77c9e1 585{
ba77c9e1
LZ
586 if (!arg)
587 return -1;
588
ba77c9e1 589 if (caller_flag > alloc_flag)
29b3e152 590 return setup_sorting(&caller_sort, arg);
ba77c9e1 591 else
29b3e152 592 return setup_sorting(&alloc_sort, arg);
ba77c9e1
LZ
593
594 return 0;
595}
596
1d037ca1
IT
597static int parse_caller_opt(const struct option *opt __maybe_unused,
598 const char *arg __maybe_unused,
599 int unset __maybe_unused)
ba77c9e1 600{
90b86a9f
LZ
601 caller_flag = (alloc_flag + 1);
602 return 0;
603}
ba77c9e1 604
1d037ca1
IT
605static int parse_alloc_opt(const struct option *opt __maybe_unused,
606 const char *arg __maybe_unused,
607 int unset __maybe_unused)
90b86a9f
LZ
608{
609 alloc_flag = (caller_flag + 1);
ba77c9e1
LZ
610 return 0;
611}
612
1d037ca1
IT
613static int parse_line_opt(const struct option *opt __maybe_unused,
614 const char *arg, int unset __maybe_unused)
ba77c9e1
LZ
615{
616 int lines;
617
618 if (!arg)
619 return -1;
620
621 lines = strtoul(arg, NULL, 10);
622
623 if (caller_flag > alloc_flag)
624 caller_lines = lines;
625 else
626 alloc_lines = lines;
627
628 return 0;
629}
630
0433ffbe
ACM
631static int __cmd_record(int argc, const char **argv)
632{
633 const char * const record_args[] = {
4a4d371a 634 "record", "-a", "-R", "-c", "1",
ba77c9e1
LZ
635 "-e", "kmem:kmalloc",
636 "-e", "kmem:kmalloc_node",
637 "-e", "kmem:kfree",
638 "-e", "kmem:kmem_cache_alloc",
639 "-e", "kmem:kmem_cache_alloc_node",
640 "-e", "kmem:kmem_cache_free",
0433ffbe 641 };
ba77c9e1
LZ
642 unsigned int rec_argc, i, j;
643 const char **rec_argv;
644
645 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
646 rec_argv = calloc(rec_argc + 1, sizeof(char *));
647
ce47dc56
CS
648 if (rec_argv == NULL)
649 return -ENOMEM;
650
ba77c9e1
LZ
651 for (i = 0; i < ARRAY_SIZE(record_args); i++)
652 rec_argv[i] = strdup(record_args[i]);
653
654 for (j = 1; j < (unsigned int)argc; j++, i++)
655 rec_argv[i] = argv[j];
656
657 return cmd_record(i, rec_argv, NULL);
658}
659
1d037ca1 660int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
ba77c9e1 661{
0433ffbe 662 const char * const default_sort_order = "frag,hit,bytes";
0433ffbe
ACM
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",
671 parse_sort_opt),
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"),
674 OPT_END()
675 };
3bca2354
RR
676 const char *const kmem_subcommands[] = { "record", "stat", NULL };
677 const char *kmem_usage[] = {
678 NULL,
0433ffbe
ACM
679 NULL
680 };
2b2b2c68
NK
681 struct perf_session *session;
682 struct perf_data_file file = {
683 .path = input_name,
684 .mode = PERF_DATA_MODE_READ,
685 };
686 int ret = -1;
687
3bca2354
RR
688 argc = parse_options_subcommand(argc, argv, kmem_options,
689 kmem_subcommands, kmem_usage, 0);
ba77c9e1 690
90b86a9f 691 if (!argc)
ba77c9e1
LZ
692 usage_with_options(kmem_usage, kmem_options);
693
90b86a9f 694 if (!strncmp(argv[0], "rec", 3)) {
0a7e6d1b 695 symbol__init(NULL);
90b86a9f 696 return __cmd_record(argc, argv);
2b2b2c68
NK
697 }
698
699 session = perf_session__new(&file, false, &perf_kmem);
700 if (session == NULL)
52e02834 701 return -1;
2b2b2c68 702
0a7e6d1b 703 symbol__init(&session->header.env);
2b2b2c68
NK
704
705 if (!strcmp(argv[0], "stat")) {
4b627957 706 if (cpu__setup_cpunode_map())
2b2b2c68 707 goto out_delete;
90b86a9f
LZ
708
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);
ba77c9e1 713
2b2b2c68 714 ret = __cmd_kmem(session);
b00eca8c
PE
715 } else
716 usage_with_options(kmem_usage, kmem_options);
7d0d3945 717
2b2b2c68
NK
718out_delete:
719 perf_session__delete(session);
720
721 return ret;
ba77c9e1
LZ
722}
723