builtin-kmem.c 18 KB

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