builtin-kmem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/evlist.h"
  4. #include "util/evsel.h"
  5. #include "util/util.h"
  6. #include "util/cache.h"
  7. #include "util/symbol.h"
  8. #include "util/thread.h"
  9. #include "util/header.h"
  10. #include "util/session.h"
  11. #include "util/tool.h"
  12. #include "util/parse-options.h"
  13. #include "util/trace-event.h"
  14. #include "util/debug.h"
  15. #include <linux/rbtree.h>
  16. struct alloc_stat;
  17. typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
  18. static int alloc_flag;
  19. static int caller_flag;
  20. static int alloc_lines = -1;
  21. static int caller_lines = -1;
  22. static bool raw_ip;
  23. static int *cpunode_map;
  24. static int max_cpu_num;
  25. struct alloc_stat {
  26. u64 call_site;
  27. u64 ptr;
  28. u64 bytes_req;
  29. u64 bytes_alloc;
  30. u32 hit;
  31. u32 pingpong;
  32. short alloc_cpu;
  33. struct rb_node node;
  34. };
  35. static struct rb_root root_alloc_stat;
  36. static struct rb_root root_alloc_sorted;
  37. static struct rb_root root_caller_stat;
  38. static struct rb_root root_caller_sorted;
  39. static unsigned long total_requested, total_allocated;
  40. static unsigned long nr_allocs, nr_cross_allocs;
  41. #define PATH_SYS_NODE "/sys/devices/system/node"
  42. static int init_cpunode_map(void)
  43. {
  44. FILE *fp;
  45. int i, err = -1;
  46. fp = fopen("/sys/devices/system/cpu/kernel_max", "r");
  47. if (!fp) {
  48. max_cpu_num = 4096;
  49. return 0;
  50. }
  51. if (fscanf(fp, "%d", &max_cpu_num) < 1) {
  52. pr_err("Failed to read 'kernel_max' from sysfs");
  53. goto out_close;
  54. }
  55. max_cpu_num++;
  56. cpunode_map = calloc(max_cpu_num, sizeof(int));
  57. if (!cpunode_map) {
  58. pr_err("%s: calloc failed\n", __func__);
  59. goto out_close;
  60. }
  61. for (i = 0; i < max_cpu_num; i++)
  62. cpunode_map[i] = -1;
  63. err = 0;
  64. out_close:
  65. fclose(fp);
  66. return err;
  67. }
  68. static int setup_cpunode_map(void)
  69. {
  70. struct dirent *dent1, *dent2;
  71. DIR *dir1, *dir2;
  72. unsigned int cpu, mem;
  73. char buf[PATH_MAX];
  74. if (init_cpunode_map())
  75. return -1;
  76. dir1 = opendir(PATH_SYS_NODE);
  77. if (!dir1)
  78. return -1;
  79. while ((dent1 = readdir(dir1)) != NULL) {
  80. if (dent1->d_type != DT_DIR ||
  81. sscanf(dent1->d_name, "node%u", &mem) < 1)
  82. continue;
  83. snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name);
  84. dir2 = opendir(buf);
  85. if (!dir2)
  86. continue;
  87. while ((dent2 = readdir(dir2)) != NULL) {
  88. if (dent2->d_type != DT_LNK ||
  89. sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
  90. continue;
  91. cpunode_map[cpu] = mem;
  92. }
  93. closedir(dir2);
  94. }
  95. closedir(dir1);
  96. return 0;
  97. }
  98. static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
  99. int bytes_req, int bytes_alloc, int cpu)
  100. {
  101. struct rb_node **node = &root_alloc_stat.rb_node;
  102. struct rb_node *parent = NULL;
  103. struct alloc_stat *data = NULL;
  104. while (*node) {
  105. parent = *node;
  106. data = rb_entry(*node, struct alloc_stat, node);
  107. if (ptr > data->ptr)
  108. node = &(*node)->rb_right;
  109. else if (ptr < data->ptr)
  110. node = &(*node)->rb_left;
  111. else
  112. break;
  113. }
  114. if (data && data->ptr == ptr) {
  115. data->hit++;
  116. data->bytes_req += bytes_req;
  117. data->bytes_alloc += bytes_alloc;
  118. } else {
  119. data = malloc(sizeof(*data));
  120. if (!data) {
  121. pr_err("%s: malloc failed\n", __func__);
  122. return -1;
  123. }
  124. data->ptr = ptr;
  125. data->pingpong = 0;
  126. data->hit = 1;
  127. data->bytes_req = bytes_req;
  128. data->bytes_alloc = bytes_alloc;
  129. rb_link_node(&data->node, parent, node);
  130. rb_insert_color(&data->node, &root_alloc_stat);
  131. }
  132. data->call_site = call_site;
  133. data->alloc_cpu = cpu;
  134. return 0;
  135. }
  136. static int insert_caller_stat(unsigned long call_site,
  137. int bytes_req, int bytes_alloc)
  138. {
  139. struct rb_node **node = &root_caller_stat.rb_node;
  140. struct rb_node *parent = NULL;
  141. struct alloc_stat *data = NULL;
  142. while (*node) {
  143. parent = *node;
  144. data = rb_entry(*node, struct alloc_stat, node);
  145. if (call_site > data->call_site)
  146. node = &(*node)->rb_right;
  147. else if (call_site < data->call_site)
  148. node = &(*node)->rb_left;
  149. else
  150. break;
  151. }
  152. if (data && data->call_site == call_site) {
  153. data->hit++;
  154. data->bytes_req += bytes_req;
  155. data->bytes_alloc += bytes_alloc;
  156. } else {
  157. data = malloc(sizeof(*data));
  158. if (!data) {
  159. pr_err("%s: malloc failed\n", __func__);
  160. return -1;
  161. }
  162. data->call_site = call_site;
  163. data->pingpong = 0;
  164. data->hit = 1;
  165. data->bytes_req = bytes_req;
  166. data->bytes_alloc = bytes_alloc;
  167. rb_link_node(&data->node, parent, node);
  168. rb_insert_color(&data->node, &root_caller_stat);
  169. }
  170. return 0;
  171. }
  172. static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
  173. struct perf_sample *sample)
  174. {
  175. unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
  176. call_site = perf_evsel__intval(evsel, sample, "call_site");
  177. int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
  178. bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
  179. if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
  180. insert_caller_stat(call_site, bytes_req, bytes_alloc))
  181. return -1;
  182. total_requested += bytes_req;
  183. total_allocated += bytes_alloc;
  184. nr_allocs++;
  185. return 0;
  186. }
  187. static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
  188. struct perf_sample *sample)
  189. {
  190. int ret = perf_evsel__process_alloc_event(evsel, sample);
  191. if (!ret) {
  192. int node1 = cpunode_map[sample->cpu],
  193. node2 = perf_evsel__intval(evsel, sample, "node");
  194. if (node1 != node2)
  195. nr_cross_allocs++;
  196. }
  197. return ret;
  198. }
  199. static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
  200. static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
  201. static struct alloc_stat *search_alloc_stat(unsigned long ptr,
  202. unsigned long call_site,
  203. struct rb_root *root,
  204. sort_fn_t sort_fn)
  205. {
  206. struct rb_node *node = root->rb_node;
  207. struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
  208. while (node) {
  209. struct alloc_stat *data;
  210. int cmp;
  211. data = rb_entry(node, struct alloc_stat, node);
  212. cmp = sort_fn(&key, data);
  213. if (cmp < 0)
  214. node = node->rb_left;
  215. else if (cmp > 0)
  216. node = node->rb_right;
  217. else
  218. return data;
  219. }
  220. return NULL;
  221. }
  222. static int perf_evsel__process_free_event(struct perf_evsel *evsel,
  223. struct perf_sample *sample)
  224. {
  225. unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
  226. struct alloc_stat *s_alloc, *s_caller;
  227. s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
  228. if (!s_alloc)
  229. return 0;
  230. if ((short)sample->cpu != s_alloc->alloc_cpu) {
  231. s_alloc->pingpong++;
  232. s_caller = search_alloc_stat(0, s_alloc->call_site,
  233. &root_caller_stat, callsite_cmp);
  234. if (!s_caller)
  235. return -1;
  236. s_caller->pingpong++;
  237. }
  238. s_alloc->alloc_cpu = -1;
  239. return 0;
  240. }
  241. typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
  242. struct perf_sample *sample);
  243. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  244. union perf_event *event,
  245. struct perf_sample *sample,
  246. struct perf_evsel *evsel,
  247. struct machine *machine)
  248. {
  249. struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
  250. if (thread == NULL) {
  251. pr_debug("problem processing %d event, skipping it.\n",
  252. event->header.type);
  253. return -1;
  254. }
  255. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  256. if (evsel->handler.func != NULL) {
  257. tracepoint_handler f = evsel->handler.func;
  258. return f(evsel, sample);
  259. }
  260. return 0;
  261. }
  262. static struct perf_tool perf_kmem = {
  263. .sample = process_sample_event,
  264. .comm = perf_event__process_comm,
  265. .ordered_samples = true,
  266. };
  267. static double fragmentation(unsigned long n_req, unsigned long n_alloc)
  268. {
  269. if (n_alloc == 0)
  270. return 0.0;
  271. else
  272. return 100.0 - (100.0 * n_req / n_alloc);
  273. }
  274. static void __print_result(struct rb_root *root, struct perf_session *session,
  275. int n_lines, int is_caller)
  276. {
  277. struct rb_node *next;
  278. struct machine *machine;
  279. printf("%.102s\n", graph_dotted_line);
  280. printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
  281. printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
  282. printf("%.102s\n", graph_dotted_line);
  283. next = rb_first(root);
  284. machine = perf_session__find_host_machine(session);
  285. if (!machine) {
  286. pr_err("__print_result: couldn't find kernel information\n");
  287. return;
  288. }
  289. while (next && n_lines--) {
  290. struct alloc_stat *data = rb_entry(next, struct alloc_stat,
  291. node);
  292. struct symbol *sym = NULL;
  293. struct map *map;
  294. char buf[BUFSIZ];
  295. u64 addr;
  296. if (is_caller) {
  297. addr = data->call_site;
  298. if (!raw_ip)
  299. sym = machine__find_kernel_function(machine, addr, &map, NULL);
  300. } else
  301. addr = data->ptr;
  302. if (sym != NULL)
  303. snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
  304. addr - map->unmap_ip(map, sym->start));
  305. else
  306. snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
  307. printf(" %-34s |", buf);
  308. printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
  309. (unsigned long long)data->bytes_alloc,
  310. (unsigned long)data->bytes_alloc / data->hit,
  311. (unsigned long long)data->bytes_req,
  312. (unsigned long)data->bytes_req / data->hit,
  313. (unsigned long)data->hit,
  314. (unsigned long)data->pingpong,
  315. fragmentation(data->bytes_req, data->bytes_alloc));
  316. next = rb_next(next);
  317. }
  318. if (n_lines == -1)
  319. printf(" ... | ... | ... | ... | ... | ... \n");
  320. printf("%.102s\n", graph_dotted_line);
  321. }
  322. static void print_summary(void)
  323. {
  324. printf("\nSUMMARY\n=======\n");
  325. printf("Total bytes requested: %lu\n", total_requested);
  326. printf("Total bytes allocated: %lu\n", total_allocated);
  327. printf("Total bytes wasted on internal fragmentation: %lu\n",
  328. total_allocated - total_requested);
  329. printf("Internal fragmentation: %f%%\n",
  330. fragmentation(total_requested, total_allocated));
  331. printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
  332. }
  333. static void print_result(struct perf_session *session)
  334. {
  335. if (caller_flag)
  336. __print_result(&root_caller_sorted, session, caller_lines, 1);
  337. if (alloc_flag)
  338. __print_result(&root_alloc_sorted, session, alloc_lines, 0);
  339. print_summary();
  340. }
  341. struct sort_dimension {
  342. const char name[20];
  343. sort_fn_t cmp;
  344. struct list_head list;
  345. };
  346. static LIST_HEAD(caller_sort);
  347. static LIST_HEAD(alloc_sort);
  348. static void sort_insert(struct rb_root *root, struct alloc_stat *data,
  349. struct list_head *sort_list)
  350. {
  351. struct rb_node **new = &(root->rb_node);
  352. struct rb_node *parent = NULL;
  353. struct sort_dimension *sort;
  354. while (*new) {
  355. struct alloc_stat *this;
  356. int cmp = 0;
  357. this = rb_entry(*new, struct alloc_stat, node);
  358. parent = *new;
  359. list_for_each_entry(sort, sort_list, list) {
  360. cmp = sort->cmp(data, this);
  361. if (cmp)
  362. break;
  363. }
  364. if (cmp > 0)
  365. new = &((*new)->rb_left);
  366. else
  367. new = &((*new)->rb_right);
  368. }
  369. rb_link_node(&data->node, parent, new);
  370. rb_insert_color(&data->node, root);
  371. }
  372. static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
  373. struct list_head *sort_list)
  374. {
  375. struct rb_node *node;
  376. struct alloc_stat *data;
  377. for (;;) {
  378. node = rb_first(root);
  379. if (!node)
  380. break;
  381. rb_erase(node, root);
  382. data = rb_entry(node, struct alloc_stat, node);
  383. sort_insert(root_sorted, data, sort_list);
  384. }
  385. }
  386. static void sort_result(void)
  387. {
  388. __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
  389. __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
  390. }
  391. static int __cmd_kmem(const char *input_name)
  392. {
  393. int err = -EINVAL;
  394. struct perf_session *session;
  395. const struct perf_evsel_str_handler kmem_tracepoints[] = {
  396. { "kmem:kmalloc", perf_evsel__process_alloc_event, },
  397. { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
  398. { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
  399. { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
  400. { "kmem:kfree", perf_evsel__process_free_event, },
  401. { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
  402. };
  403. session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem);
  404. if (session == NULL)
  405. return -ENOMEM;
  406. if (perf_session__create_kernel_maps(session) < 0)
  407. goto out_delete;
  408. if (!perf_session__has_traces(session, "kmem record"))
  409. goto out_delete;
  410. if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
  411. pr_err("Initializing perf session tracepoint handlers failed\n");
  412. return -1;
  413. }
  414. setup_pager();
  415. err = perf_session__process_events(session, &perf_kmem);
  416. if (err != 0)
  417. goto out_delete;
  418. sort_result();
  419. print_result(session);
  420. out_delete:
  421. perf_session__delete(session);
  422. return err;
  423. }
  424. static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
  425. {
  426. if (l->ptr < r->ptr)
  427. return -1;
  428. else if (l->ptr > r->ptr)
  429. return 1;
  430. return 0;
  431. }
  432. static struct sort_dimension ptr_sort_dimension = {
  433. .name = "ptr",
  434. .cmp = ptr_cmp,
  435. };
  436. static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
  437. {
  438. if (l->call_site < r->call_site)
  439. return -1;
  440. else if (l->call_site > r->call_site)
  441. return 1;
  442. return 0;
  443. }
  444. static struct sort_dimension callsite_sort_dimension = {
  445. .name = "callsite",
  446. .cmp = callsite_cmp,
  447. };
  448. static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
  449. {
  450. if (l->hit < r->hit)
  451. return -1;
  452. else if (l->hit > r->hit)
  453. return 1;
  454. return 0;
  455. }
  456. static struct sort_dimension hit_sort_dimension = {
  457. .name = "hit",
  458. .cmp = hit_cmp,
  459. };
  460. static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
  461. {
  462. if (l->bytes_alloc < r->bytes_alloc)
  463. return -1;
  464. else if (l->bytes_alloc > r->bytes_alloc)
  465. return 1;
  466. return 0;
  467. }
  468. static struct sort_dimension bytes_sort_dimension = {
  469. .name = "bytes",
  470. .cmp = bytes_cmp,
  471. };
  472. static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
  473. {
  474. double x, y;
  475. x = fragmentation(l->bytes_req, l->bytes_alloc);
  476. y = fragmentation(r->bytes_req, r->bytes_alloc);
  477. if (x < y)
  478. return -1;
  479. else if (x > y)
  480. return 1;
  481. return 0;
  482. }
  483. static struct sort_dimension frag_sort_dimension = {
  484. .name = "frag",
  485. .cmp = frag_cmp,
  486. };
  487. static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
  488. {
  489. if (l->pingpong < r->pingpong)
  490. return -1;
  491. else if (l->pingpong > r->pingpong)
  492. return 1;
  493. return 0;
  494. }
  495. static struct sort_dimension pingpong_sort_dimension = {
  496. .name = "pingpong",
  497. .cmp = pingpong_cmp,
  498. };
  499. static struct sort_dimension *avail_sorts[] = {
  500. &ptr_sort_dimension,
  501. &callsite_sort_dimension,
  502. &hit_sort_dimension,
  503. &bytes_sort_dimension,
  504. &frag_sort_dimension,
  505. &pingpong_sort_dimension,
  506. };
  507. #define NUM_AVAIL_SORTS \
  508. (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
  509. static int sort_dimension__add(const char *tok, struct list_head *list)
  510. {
  511. struct sort_dimension *sort;
  512. int i;
  513. for (i = 0; i < NUM_AVAIL_SORTS; i++) {
  514. if (!strcmp(avail_sorts[i]->name, tok)) {
  515. sort = malloc(sizeof(*sort));
  516. if (!sort) {
  517. pr_err("%s: malloc failed\n", __func__);
  518. return -1;
  519. }
  520. memcpy(sort, avail_sorts[i], sizeof(*sort));
  521. list_add_tail(&sort->list, list);
  522. return 0;
  523. }
  524. }
  525. return -1;
  526. }
  527. static int setup_sorting(struct list_head *sort_list, const char *arg)
  528. {
  529. char *tok;
  530. char *str = strdup(arg);
  531. if (!str) {
  532. pr_err("%s: strdup failed\n", __func__);
  533. return -1;
  534. }
  535. while (true) {
  536. tok = strsep(&str, ",");
  537. if (!tok)
  538. break;
  539. if (sort_dimension__add(tok, sort_list) < 0) {
  540. error("Unknown --sort key: '%s'", tok);
  541. free(str);
  542. return -1;
  543. }
  544. }
  545. free(str);
  546. return 0;
  547. }
  548. static int parse_sort_opt(const struct option *opt __maybe_unused,
  549. const char *arg, int unset __maybe_unused)
  550. {
  551. if (!arg)
  552. return -1;
  553. if (caller_flag > alloc_flag)
  554. return setup_sorting(&caller_sort, arg);
  555. else
  556. return setup_sorting(&alloc_sort, arg);
  557. return 0;
  558. }
  559. static int parse_caller_opt(const struct option *opt __maybe_unused,
  560. const char *arg __maybe_unused,
  561. int unset __maybe_unused)
  562. {
  563. caller_flag = (alloc_flag + 1);
  564. return 0;
  565. }
  566. static int parse_alloc_opt(const struct option *opt __maybe_unused,
  567. const char *arg __maybe_unused,
  568. int unset __maybe_unused)
  569. {
  570. alloc_flag = (caller_flag + 1);
  571. return 0;
  572. }
  573. static int parse_line_opt(const struct option *opt __maybe_unused,
  574. const char *arg, int unset __maybe_unused)
  575. {
  576. int lines;
  577. if (!arg)
  578. return -1;
  579. lines = strtoul(arg, NULL, 10);
  580. if (caller_flag > alloc_flag)
  581. caller_lines = lines;
  582. else
  583. alloc_lines = lines;
  584. return 0;
  585. }
  586. static int __cmd_record(int argc, const char **argv)
  587. {
  588. const char * const record_args[] = {
  589. "record", "-a", "-R", "-f", "-c", "1",
  590. "-e", "kmem:kmalloc",
  591. "-e", "kmem:kmalloc_node",
  592. "-e", "kmem:kfree",
  593. "-e", "kmem:kmem_cache_alloc",
  594. "-e", "kmem:kmem_cache_alloc_node",
  595. "-e", "kmem:kmem_cache_free",
  596. };
  597. unsigned int rec_argc, i, j;
  598. const char **rec_argv;
  599. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  600. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  601. if (rec_argv == NULL)
  602. return -ENOMEM;
  603. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  604. rec_argv[i] = strdup(record_args[i]);
  605. for (j = 1; j < (unsigned int)argc; j++, i++)
  606. rec_argv[i] = argv[j];
  607. return cmd_record(i, rec_argv, NULL);
  608. }
  609. int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
  610. {
  611. const char * const default_sort_order = "frag,hit,bytes";
  612. const char *input_name = NULL;
  613. const struct option kmem_options[] = {
  614. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  615. OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
  616. "show per-callsite statistics", parse_caller_opt),
  617. OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
  618. "show per-allocation statistics", parse_alloc_opt),
  619. OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
  620. "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
  621. parse_sort_opt),
  622. OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
  623. OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
  624. OPT_END()
  625. };
  626. const char * const kmem_usage[] = {
  627. "perf kmem [<options>] {record|stat}",
  628. NULL
  629. };
  630. argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
  631. if (!argc)
  632. usage_with_options(kmem_usage, kmem_options);
  633. symbol__init();
  634. if (!strncmp(argv[0], "rec", 3)) {
  635. return __cmd_record(argc, argv);
  636. } else if (!strcmp(argv[0], "stat")) {
  637. if (setup_cpunode_map())
  638. return -1;
  639. if (list_empty(&caller_sort))
  640. setup_sorting(&caller_sort, default_sort_order);
  641. if (list_empty(&alloc_sort))
  642. setup_sorting(&alloc_sort, default_sort_order);
  643. return __cmd_kmem(input_name);
  644. } else
  645. usage_with_options(kmem_usage, kmem_options);
  646. return 0;
  647. }