builtin-kmem.c 18 KB

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