]> Pileus Git - ~andy/linux/blob - tools/perf/builtin-kmem.c
Merge branch 'writeback-for-next' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~andy/linux] / tools / perf / builtin-kmem.c
1 #include "builtin.h"
2 #include "perf.h"
3
4 #include "util/evlist.h"
5 #include "util/evsel.h"
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"
11 #include "util/session.h"
12 #include "util/tool.h"
13
14 #include "util/parse-options.h"
15 #include "util/trace-event.h"
16
17 #include "util/debug.h"
18
19 #include <linux/rbtree.h>
20
21 struct alloc_stat;
22 typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
23
24 static const char               *input_name;
25
26 static int                      alloc_flag;
27 static int                      caller_flag;
28
29 static int                      alloc_lines = -1;
30 static int                      caller_lines = -1;
31
32 static bool                     raw_ip;
33
34 static char                     default_sort_order[] = "frag,hit,bytes";
35
36 static int                      *cpunode_map;
37 static int                      max_cpu_num;
38
39 struct alloc_stat {
40         u64     call_site;
41         u64     ptr;
42         u64     bytes_req;
43         u64     bytes_alloc;
44         u32     hit;
45         u32     pingpong;
46
47         short   alloc_cpu;
48
49         struct rb_node node;
50 };
51
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;
56
57 static unsigned long total_requested, total_allocated;
58 static unsigned long nr_allocs, nr_cross_allocs;
59
60 #define PATH_SYS_NODE   "/sys/devices/system/node"
61
62 static int init_cpunode_map(void)
63 {
64         FILE *fp;
65         int i, err = -1;
66
67         fp = fopen("/sys/devices/system/cpu/kernel_max", "r");
68         if (!fp) {
69                 max_cpu_num = 4096;
70                 return 0;
71         }
72
73         if (fscanf(fp, "%d", &max_cpu_num) < 1) {
74                 pr_err("Failed to read 'kernel_max' from sysfs");
75                 goto out_close;
76         }
77
78         max_cpu_num++;
79
80         cpunode_map = calloc(max_cpu_num, sizeof(int));
81         if (!cpunode_map) {
82                 pr_err("%s: calloc failed\n", __func__);
83                 goto out_close;
84         }
85
86         for (i = 0; i < max_cpu_num; i++)
87                 cpunode_map[i] = -1;
88
89         err = 0;
90 out_close:
91         fclose(fp);
92         return err;
93 }
94
95 static int setup_cpunode_map(void)
96 {
97         struct dirent *dent1, *dent2;
98         DIR *dir1, *dir2;
99         unsigned int cpu, mem;
100         char buf[PATH_MAX];
101
102         if (init_cpunode_map())
103                 return -1;
104
105         dir1 = opendir(PATH_SYS_NODE);
106         if (!dir1)
107                 return -1;
108
109         while ((dent1 = readdir(dir1)) != NULL) {
110                 if (dent1->d_type != DT_DIR ||
111                     sscanf(dent1->d_name, "node%u", &mem) < 1)
112                         continue;
113
114                 snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
115                 dir2 = opendir(buf);
116                 if (!dir2)
117                         continue;
118                 while ((dent2 = readdir(dir2)) != NULL) {
119                         if (dent2->d_type != DT_LNK ||
120                             sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
121                                 continue;
122                         cpunode_map[cpu] = mem;
123                 }
124                 closedir(dir2);
125         }
126         closedir(dir1);
127         return 0;
128 }
129
130 static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
131                              int bytes_req, int bytes_alloc, int cpu)
132 {
133         struct rb_node **node = &root_alloc_stat.rb_node;
134         struct rb_node *parent = NULL;
135         struct alloc_stat *data = NULL;
136
137         while (*node) {
138                 parent = *node;
139                 data = rb_entry(*node, struct alloc_stat, node);
140
141                 if (ptr > data->ptr)
142                         node = &(*node)->rb_right;
143                 else if (ptr < data->ptr)
144                         node = &(*node)->rb_left;
145                 else
146                         break;
147         }
148
149         if (data && data->ptr == ptr) {
150                 data->hit++;
151                 data->bytes_req += bytes_req;
152                 data->bytes_alloc += bytes_alloc;
153         } else {
154                 data = malloc(sizeof(*data));
155                 if (!data) {
156                         pr_err("%s: malloc failed\n", __func__);
157                         return -1;
158                 }
159                 data->ptr = ptr;
160                 data->pingpong = 0;
161                 data->hit = 1;
162                 data->bytes_req = bytes_req;
163                 data->bytes_alloc = bytes_alloc;
164
165                 rb_link_node(&data->node, parent, node);
166                 rb_insert_color(&data->node, &root_alloc_stat);
167         }
168         data->call_site = call_site;
169         data->alloc_cpu = cpu;
170         return 0;
171 }
172
173 static int insert_caller_stat(unsigned long call_site,
174                               int bytes_req, int bytes_alloc)
175 {
176         struct rb_node **node = &root_caller_stat.rb_node;
177         struct rb_node *parent = NULL;
178         struct alloc_stat *data = NULL;
179
180         while (*node) {
181                 parent = *node;
182                 data = rb_entry(*node, struct alloc_stat, node);
183
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;
188                 else
189                         break;
190         }
191
192         if (data && data->call_site == call_site) {
193                 data->hit++;
194                 data->bytes_req += bytes_req;
195                 data->bytes_alloc += bytes_alloc;
196         } else {
197                 data = malloc(sizeof(*data));
198                 if (!data) {
199                         pr_err("%s: malloc failed\n", __func__);
200                         return -1;
201                 }
202                 data->call_site = call_site;
203                 data->pingpong = 0;
204                 data->hit = 1;
205                 data->bytes_req = bytes_req;
206                 data->bytes_alloc = bytes_alloc;
207
208                 rb_link_node(&data->node, parent, node);
209                 rb_insert_color(&data->node, &root_caller_stat);
210         }
211
212         return 0;
213 }
214
215 static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
216                                            struct perf_sample *sample)
217 {
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");
222
223         if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
224             insert_caller_stat(call_site, bytes_req, bytes_alloc))
225                 return -1;
226
227         total_requested += bytes_req;
228         total_allocated += bytes_alloc;
229
230         nr_allocs++;
231         return 0;
232 }
233
234 static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
235                                                 struct perf_sample *sample)
236 {
237         int ret = perf_evsel__process_alloc_event(evsel, sample);
238
239         if (!ret) {
240                 int node1 = cpunode_map[sample->cpu],
241                     node2 = perf_evsel__intval(evsel, sample, "node");
242
243                 if (node1 != node2)
244                         nr_cross_allocs++;
245         }
246
247         return ret;
248 }
249
250 static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
251 static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
252
253 static struct alloc_stat *search_alloc_stat(unsigned long ptr,
254                                             unsigned long call_site,
255                                             struct rb_root *root,
256                                             sort_fn_t sort_fn)
257 {
258         struct rb_node *node = root->rb_node;
259         struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
260
261         while (node) {
262                 struct alloc_stat *data;
263                 int cmp;
264
265                 data = rb_entry(node, struct alloc_stat, node);
266
267                 cmp = sort_fn(&key, data);
268                 if (cmp < 0)
269                         node = node->rb_left;
270                 else if (cmp > 0)
271                         node = node->rb_right;
272                 else
273                         return data;
274         }
275         return NULL;
276 }
277
278 static int perf_evsel__process_free_event(struct perf_evsel *evsel,
279                                           struct perf_sample *sample)
280 {
281         unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
282         struct alloc_stat *s_alloc, *s_caller;
283
284         s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
285         if (!s_alloc)
286                 return 0;
287
288         if ((short)sample->cpu != s_alloc->alloc_cpu) {
289                 s_alloc->pingpong++;
290
291                 s_caller = search_alloc_stat(0, s_alloc->call_site,
292                                              &root_caller_stat, callsite_cmp);
293                 if (!s_caller)
294                         return -1;
295                 s_caller->pingpong++;
296         }
297         s_alloc->alloc_cpu = -1;
298
299         return 0;
300 }
301
302 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
303                                   struct perf_sample *sample);
304
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)
310 {
311         struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
312
313         if (thread == NULL) {
314                 pr_debug("problem processing %d event, skipping it.\n",
315                          event->header.type);
316                 return -1;
317         }
318
319         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
320
321         if (evsel->handler.func != NULL) {
322                 tracepoint_handler f = evsel->handler.func;
323                 return f(evsel, sample);
324         }
325
326         return 0;
327 }
328
329 static struct perf_tool perf_kmem = {
330         .sample          = process_sample_event,
331         .comm            = perf_event__process_comm,
332         .ordered_samples = true,
333 };
334
335 static double fragmentation(unsigned long n_req, unsigned long n_alloc)
336 {
337         if (n_alloc == 0)
338                 return 0.0;
339         else
340                 return 100.0 - (100.0 * n_req / n_alloc);
341 }
342
343 static void __print_result(struct rb_root *root, struct perf_session *session,
344                            int n_lines, int is_caller)
345 {
346         struct rb_node *next;
347         struct machine *machine;
348
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);
353
354         next = rb_first(root);
355
356         machine = perf_session__find_host_machine(session);
357         if (!machine) {
358                 pr_err("__print_result: couldn't find kernel information\n");
359                 return;
360         }
361         while (next && n_lines--) {
362                 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
363                                                    node);
364                 struct symbol *sym = NULL;
365                 struct map *map;
366                 char buf[BUFSIZ];
367                 u64 addr;
368
369                 if (is_caller) {
370                         addr = data->call_site;
371                         if (!raw_ip)
372                                 sym = machine__find_kernel_function(machine, addr, &map, NULL);
373                 } else
374                         addr = data->ptr;
375
376                 if (sym != NULL)
377                         snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
378                                  addr - map->unmap_ip(map, sym->start));
379                 else
380                         snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
381                 printf(" %-34s |", buf);
382
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));
391
392                 next = rb_next(next);
393         }
394
395         if (n_lines == -1)
396                 printf(" ...                                | ...             | ...             | ...    | ...      | ...   \n");
397
398         printf("%.102s\n", graph_dotted_line);
399 }
400
401 static void print_summary(void)
402 {
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);
411 }
412
413 static void print_result(struct perf_session *session)
414 {
415         if (caller_flag)
416                 __print_result(&root_caller_sorted, session, caller_lines, 1);
417         if (alloc_flag)
418                 __print_result(&root_alloc_sorted, session, alloc_lines, 0);
419         print_summary();
420 }
421
422 struct sort_dimension {
423         const char              name[20];
424         sort_fn_t               cmp;
425         struct list_head        list;
426 };
427
428 static LIST_HEAD(caller_sort);
429 static LIST_HEAD(alloc_sort);
430
431 static void sort_insert(struct rb_root *root, struct alloc_stat *data,
432                         struct list_head *sort_list)
433 {
434         struct rb_node **new = &(root->rb_node);
435         struct rb_node *parent = NULL;
436         struct sort_dimension *sort;
437
438         while (*new) {
439                 struct alloc_stat *this;
440                 int cmp = 0;
441
442                 this = rb_entry(*new, struct alloc_stat, node);
443                 parent = *new;
444
445                 list_for_each_entry(sort, sort_list, list) {
446                         cmp = sort->cmp(data, this);
447                         if (cmp)
448                                 break;
449                 }
450
451                 if (cmp > 0)
452                         new = &((*new)->rb_left);
453                 else
454                         new = &((*new)->rb_right);
455         }
456
457         rb_link_node(&data->node, parent, new);
458         rb_insert_color(&data->node, root);
459 }
460
461 static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
462                           struct list_head *sort_list)
463 {
464         struct rb_node *node;
465         struct alloc_stat *data;
466
467         for (;;) {
468                 node = rb_first(root);
469                 if (!node)
470                         break;
471
472                 rb_erase(node, root);
473                 data = rb_entry(node, struct alloc_stat, node);
474                 sort_insert(root_sorted, data, sort_list);
475         }
476 }
477
478 static void sort_result(void)
479 {
480         __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
481         __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
482 }
483
484 static int __cmd_kmem(void)
485 {
486         int err = -EINVAL;
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, },
495         };
496
497         session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem);
498         if (session == NULL)
499                 return -ENOMEM;
500
501         if (perf_session__create_kernel_maps(session) < 0)
502                 goto out_delete;
503
504         if (!perf_session__has_traces(session, "kmem record"))
505                 goto out_delete;
506
507         if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
508                 pr_err("Initializing perf session tracepoint handlers failed\n");
509                 return -1;
510         }
511
512         setup_pager();
513         err = perf_session__process_events(session, &perf_kmem);
514         if (err != 0)
515                 goto out_delete;
516         sort_result();
517         print_result(session);
518 out_delete:
519         perf_session__delete(session);
520         return err;
521 }
522
523 static const char * const kmem_usage[] = {
524         "perf kmem [<options>] {record|stat}",
525         NULL
526 };
527
528 static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
529 {
530         if (l->ptr < r->ptr)
531                 return -1;
532         else if (l->ptr > r->ptr)
533                 return 1;
534         return 0;
535 }
536
537 static struct sort_dimension ptr_sort_dimension = {
538         .name   = "ptr",
539         .cmp    = ptr_cmp,
540 };
541
542 static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
543 {
544         if (l->call_site < r->call_site)
545                 return -1;
546         else if (l->call_site > r->call_site)
547                 return 1;
548         return 0;
549 }
550
551 static struct sort_dimension callsite_sort_dimension = {
552         .name   = "callsite",
553         .cmp    = callsite_cmp,
554 };
555
556 static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
557 {
558         if (l->hit < r->hit)
559                 return -1;
560         else if (l->hit > r->hit)
561                 return 1;
562         return 0;
563 }
564
565 static struct sort_dimension hit_sort_dimension = {
566         .name   = "hit",
567         .cmp    = hit_cmp,
568 };
569
570 static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
571 {
572         if (l->bytes_alloc < r->bytes_alloc)
573                 return -1;
574         else if (l->bytes_alloc > r->bytes_alloc)
575                 return 1;
576         return 0;
577 }
578
579 static struct sort_dimension bytes_sort_dimension = {
580         .name   = "bytes",
581         .cmp    = bytes_cmp,
582 };
583
584 static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
585 {
586         double x, y;
587
588         x = fragmentation(l->bytes_req, l->bytes_alloc);
589         y = fragmentation(r->bytes_req, r->bytes_alloc);
590
591         if (x < y)
592                 return -1;
593         else if (x > y)
594                 return 1;
595         return 0;
596 }
597
598 static struct sort_dimension frag_sort_dimension = {
599         .name   = "frag",
600         .cmp    = frag_cmp,
601 };
602
603 static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
604 {
605         if (l->pingpong < r->pingpong)
606                 return -1;
607         else if (l->pingpong > r->pingpong)
608                 return 1;
609         return 0;
610 }
611
612 static struct sort_dimension pingpong_sort_dimension = {
613         .name   = "pingpong",
614         .cmp    = pingpong_cmp,
615 };
616
617 static struct sort_dimension *avail_sorts[] = {
618         &ptr_sort_dimension,
619         &callsite_sort_dimension,
620         &hit_sort_dimension,
621         &bytes_sort_dimension,
622         &frag_sort_dimension,
623         &pingpong_sort_dimension,
624 };
625
626 #define NUM_AVAIL_SORTS \
627         (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
628
629 static int sort_dimension__add(const char *tok, struct list_head *list)
630 {
631         struct sort_dimension *sort;
632         int i;
633
634         for (i = 0; i < NUM_AVAIL_SORTS; i++) {
635                 if (!strcmp(avail_sorts[i]->name, tok)) {
636                         sort = malloc(sizeof(*sort));
637                         if (!sort) {
638                                 pr_err("%s: malloc failed\n", __func__);
639                                 return -1;
640                         }
641                         memcpy(sort, avail_sorts[i], sizeof(*sort));
642                         list_add_tail(&sort->list, list);
643                         return 0;
644                 }
645         }
646
647         return -1;
648 }
649
650 static int setup_sorting(struct list_head *sort_list, const char *arg)
651 {
652         char *tok;
653         char *str = strdup(arg);
654
655         if (!str) {
656                 pr_err("%s: strdup failed\n", __func__);
657                 return -1;
658         }
659
660         while (true) {
661                 tok = strsep(&str, ",");
662                 if (!tok)
663                         break;
664                 if (sort_dimension__add(tok, sort_list) < 0) {
665                         error("Unknown --sort key: '%s'", tok);
666                         free(str);
667                         return -1;
668                 }
669         }
670
671         free(str);
672         return 0;
673 }
674
675 static int parse_sort_opt(const struct option *opt __maybe_unused,
676                           const char *arg, int unset __maybe_unused)
677 {
678         if (!arg)
679                 return -1;
680
681         if (caller_flag > alloc_flag)
682                 return setup_sorting(&caller_sort, arg);
683         else
684                 return setup_sorting(&alloc_sort, arg);
685
686         return 0;
687 }
688
689 static int parse_caller_opt(const struct option *opt __maybe_unused,
690                             const char *arg __maybe_unused,
691                             int unset __maybe_unused)
692 {
693         caller_flag = (alloc_flag + 1);
694         return 0;
695 }
696
697 static int parse_alloc_opt(const struct option *opt __maybe_unused,
698                            const char *arg __maybe_unused,
699                            int unset __maybe_unused)
700 {
701         alloc_flag = (caller_flag + 1);
702         return 0;
703 }
704
705 static int parse_line_opt(const struct option *opt __maybe_unused,
706                           const char *arg, int unset __maybe_unused)
707 {
708         int lines;
709
710         if (!arg)
711                 return -1;
712
713         lines = strtoul(arg, NULL, 10);
714
715         if (caller_flag > alloc_flag)
716                 caller_lines = lines;
717         else
718                 alloc_lines = lines;
719
720         return 0;
721 }
722
723 static const struct option kmem_options[] = {
724         OPT_STRING('i', "input", &input_name, "file",
725                    "input file name"),
726         OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
727                            "show per-callsite statistics",
728                            parse_caller_opt),
729         OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
730                            "show per-allocation statistics",
731                            parse_alloc_opt),
732         OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
733                      "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
734                      parse_sort_opt),
735         OPT_CALLBACK('l', "line", NULL, "num",
736                      "show n lines",
737                      parse_line_opt),
738         OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
739         OPT_END()
740 };
741
742 static const char *record_args[] = {
743         "record",
744         "-a",
745         "-R",
746         "-f",
747         "-c", "1",
748         "-e", "kmem:kmalloc",
749         "-e", "kmem:kmalloc_node",
750         "-e", "kmem:kfree",
751         "-e", "kmem:kmem_cache_alloc",
752         "-e", "kmem:kmem_cache_alloc_node",
753         "-e", "kmem:kmem_cache_free",
754 };
755
756 static int __cmd_record(int argc, const char **argv)
757 {
758         unsigned int rec_argc, i, j;
759         const char **rec_argv;
760
761         rec_argc = ARRAY_SIZE(record_args) + argc - 1;
762         rec_argv = calloc(rec_argc + 1, sizeof(char *));
763
764         if (rec_argv == NULL)
765                 return -ENOMEM;
766
767         for (i = 0; i < ARRAY_SIZE(record_args); i++)
768                 rec_argv[i] = strdup(record_args[i]);
769
770         for (j = 1; j < (unsigned int)argc; j++, i++)
771                 rec_argv[i] = argv[j];
772
773         return cmd_record(i, rec_argv, NULL);
774 }
775
776 int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
777 {
778         argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
779
780         if (!argc)
781                 usage_with_options(kmem_usage, kmem_options);
782
783         symbol__init();
784
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())
789                         return -1;
790
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);
795
796                 return __cmd_kmem();
797         } else
798                 usage_with_options(kmem_usage, kmem_options);
799
800         return 0;
801 }
802