builtin-kmem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 const char *input_name;
  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 char default_sort_order[] = "frag,hit,bytes";
  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 -1;
  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, event->ip.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->pid);
  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;
  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. machine = perf_session__find_host_machine(session);
  287. if (!machine) {
  288. pr_err("__print_result: couldn't find kernel information\n");
  289. return;
  290. }
  291. while (next && n_lines--) {
  292. struct alloc_stat *data = rb_entry(next, struct alloc_stat,
  293. node);
  294. struct symbol *sym = NULL;
  295. struct map *map;
  296. char buf[BUFSIZ];
  297. u64 addr;
  298. if (is_caller) {
  299. addr = data->call_site;
  300. if (!raw_ip)
  301. sym = machine__find_kernel_function(machine, addr, &map, NULL);
  302. } else
  303. addr = data->ptr;
  304. if (sym != NULL)
  305. snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
  306. addr - map->unmap_ip(map, sym->start));
  307. else
  308. snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
  309. printf(" %-34s |", buf);
  310. printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
  311. (unsigned long long)data->bytes_alloc,
  312. (unsigned long)data->bytes_alloc / data->hit,
  313. (unsigned long long)data->bytes_req,
  314. (unsigned long)data->bytes_req / data->hit,
  315. (unsigned long)data->hit,
  316. (unsigned long)data->pingpong,
  317. fragmentation(data->bytes_req, data->bytes_alloc));
  318. next = rb_next(next);
  319. }
  320. if (n_lines == -1)
  321. printf(" ... | ... | ... | ... | ... | ... \n");
  322. printf("%.102s\n", graph_dotted_line);
  323. }
  324. static void print_summary(void)
  325. {
  326. printf("\nSUMMARY\n=======\n");
  327. printf("Total bytes requested: %lu\n", total_requested);
  328. printf("Total bytes allocated: %lu\n", total_allocated);
  329. printf("Total bytes wasted on internal fragmentation: %lu\n",
  330. total_allocated - total_requested);
  331. printf("Internal fragmentation: %f%%\n",
  332. fragmentation(total_requested, total_allocated));
  333. printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
  334. }
  335. static void print_result(struct perf_session *session)
  336. {
  337. if (caller_flag)
  338. __print_result(&root_caller_sorted, session, caller_lines, 1);
  339. if (alloc_flag)
  340. __print_result(&root_alloc_sorted, session, alloc_lines, 0);
  341. print_summary();
  342. }
  343. struct sort_dimension {
  344. const char name[20];
  345. sort_fn_t cmp;
  346. struct list_head list;
  347. };
  348. static LIST_HEAD(caller_sort);
  349. static LIST_HEAD(alloc_sort);
  350. static void sort_insert(struct rb_root *root, struct alloc_stat *data,
  351. struct list_head *sort_list)
  352. {
  353. struct rb_node **new = &(root->rb_node);
  354. struct rb_node *parent = NULL;
  355. struct sort_dimension *sort;
  356. while (*new) {
  357. struct alloc_stat *this;
  358. int cmp = 0;
  359. this = rb_entry(*new, struct alloc_stat, node);
  360. parent = *new;
  361. list_for_each_entry(sort, sort_list, list) {
  362. cmp = sort->cmp(data, this);
  363. if (cmp)
  364. break;
  365. }
  366. if (cmp > 0)
  367. new = &((*new)->rb_left);
  368. else
  369. new = &((*new)->rb_right);
  370. }
  371. rb_link_node(&data->node, parent, new);
  372. rb_insert_color(&data->node, root);
  373. }
  374. static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
  375. struct list_head *sort_list)
  376. {
  377. struct rb_node *node;
  378. struct alloc_stat *data;
  379. for (;;) {
  380. node = rb_first(root);
  381. if (!node)
  382. break;
  383. rb_erase(node, root);
  384. data = rb_entry(node, struct alloc_stat, node);
  385. sort_insert(root_sorted, data, sort_list);
  386. }
  387. }
  388. static void sort_result(void)
  389. {
  390. __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
  391. __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
  392. }
  393. static int __cmd_kmem(void)
  394. {
  395. int err = -EINVAL;
  396. struct perf_session *session;
  397. const struct perf_evsel_str_handler kmem_tracepoints[] = {
  398. { "kmem:kmalloc", perf_evsel__process_alloc_event, },
  399. { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
  400. { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
  401. { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
  402. { "kmem:kfree", perf_evsel__process_free_event, },
  403. { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
  404. };
  405. session = perf_session__new(input_name, O_RDONLY, 0, 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 const char * const kmem_usage[] = {
  427. "perf kmem [<options>] {record|stat}",
  428. NULL
  429. };
  430. static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
  431. {
  432. if (l->ptr < r->ptr)
  433. return -1;
  434. else if (l->ptr > r->ptr)
  435. return 1;
  436. return 0;
  437. }
  438. static struct sort_dimension ptr_sort_dimension = {
  439. .name = "ptr",
  440. .cmp = ptr_cmp,
  441. };
  442. static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
  443. {
  444. if (l->call_site < r->call_site)
  445. return -1;
  446. else if (l->call_site > r->call_site)
  447. return 1;
  448. return 0;
  449. }
  450. static struct sort_dimension callsite_sort_dimension = {
  451. .name = "callsite",
  452. .cmp = callsite_cmp,
  453. };
  454. static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
  455. {
  456. if (l->hit < r->hit)
  457. return -1;
  458. else if (l->hit > r->hit)
  459. return 1;
  460. return 0;
  461. }
  462. static struct sort_dimension hit_sort_dimension = {
  463. .name = "hit",
  464. .cmp = hit_cmp,
  465. };
  466. static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
  467. {
  468. if (l->bytes_alloc < r->bytes_alloc)
  469. return -1;
  470. else if (l->bytes_alloc > r->bytes_alloc)
  471. return 1;
  472. return 0;
  473. }
  474. static struct sort_dimension bytes_sort_dimension = {
  475. .name = "bytes",
  476. .cmp = bytes_cmp,
  477. };
  478. static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
  479. {
  480. double x, y;
  481. x = fragmentation(l->bytes_req, l->bytes_alloc);
  482. y = fragmentation(r->bytes_req, r->bytes_alloc);
  483. if (x < y)
  484. return -1;
  485. else if (x > y)
  486. return 1;
  487. return 0;
  488. }
  489. static struct sort_dimension frag_sort_dimension = {
  490. .name = "frag",
  491. .cmp = frag_cmp,
  492. };
  493. static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
  494. {
  495. if (l->pingpong < r->pingpong)
  496. return -1;
  497. else if (l->pingpong > r->pingpong)
  498. return 1;
  499. return 0;
  500. }
  501. static struct sort_dimension pingpong_sort_dimension = {
  502. .name = "pingpong",
  503. .cmp = pingpong_cmp,
  504. };
  505. static struct sort_dimension *avail_sorts[] = {
  506. &ptr_sort_dimension,
  507. &callsite_sort_dimension,
  508. &hit_sort_dimension,
  509. &bytes_sort_dimension,
  510. &frag_sort_dimension,
  511. &pingpong_sort_dimension,
  512. };
  513. #define NUM_AVAIL_SORTS \
  514. (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
  515. static int sort_dimension__add(const char *tok, struct list_head *list)
  516. {
  517. struct sort_dimension *sort;
  518. int i;
  519. for (i = 0; i < NUM_AVAIL_SORTS; i++) {
  520. if (!strcmp(avail_sorts[i]->name, tok)) {
  521. sort = malloc(sizeof(*sort));
  522. if (!sort) {
  523. pr_err("%s: malloc failed\n", __func__);
  524. return -1;
  525. }
  526. memcpy(sort, avail_sorts[i], sizeof(*sort));
  527. list_add_tail(&sort->list, list);
  528. return 0;
  529. }
  530. }
  531. return -1;
  532. }
  533. static int setup_sorting(struct list_head *sort_list, const char *arg)
  534. {
  535. char *tok;
  536. char *str = strdup(arg);
  537. if (!str) {
  538. pr_err("%s: strdup failed\n", __func__);
  539. return -1;
  540. }
  541. while (true) {
  542. tok = strsep(&str, ",");
  543. if (!tok)
  544. break;
  545. if (sort_dimension__add(tok, sort_list) < 0) {
  546. error("Unknown --sort key: '%s'", tok);
  547. free(str);
  548. return -1;
  549. }
  550. }
  551. free(str);
  552. return 0;
  553. }
  554. static int parse_sort_opt(const struct option *opt __maybe_unused,
  555. const char *arg, int unset __maybe_unused)
  556. {
  557. if (!arg)
  558. return -1;
  559. if (caller_flag > alloc_flag)
  560. return setup_sorting(&caller_sort, arg);
  561. else
  562. return setup_sorting(&alloc_sort, arg);
  563. return 0;
  564. }
  565. static int parse_caller_opt(const struct option *opt __maybe_unused,
  566. const char *arg __maybe_unused,
  567. int unset __maybe_unused)
  568. {
  569. caller_flag = (alloc_flag + 1);
  570. return 0;
  571. }
  572. static int parse_alloc_opt(const struct option *opt __maybe_unused,
  573. const char *arg __maybe_unused,
  574. int unset __maybe_unused)
  575. {
  576. alloc_flag = (caller_flag + 1);
  577. return 0;
  578. }
  579. static int parse_line_opt(const struct option *opt __maybe_unused,
  580. const char *arg, int unset __maybe_unused)
  581. {
  582. int lines;
  583. if (!arg)
  584. return -1;
  585. lines = strtoul(arg, NULL, 10);
  586. if (caller_flag > alloc_flag)
  587. caller_lines = lines;
  588. else
  589. alloc_lines = lines;
  590. return 0;
  591. }
  592. static const struct option kmem_options[] = {
  593. OPT_STRING('i', "input", &input_name, "file",
  594. "input file name"),
  595. OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
  596. "show per-callsite statistics",
  597. parse_caller_opt),
  598. OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
  599. "show per-allocation statistics",
  600. parse_alloc_opt),
  601. OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
  602. "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
  603. parse_sort_opt),
  604. OPT_CALLBACK('l', "line", NULL, "num",
  605. "show n lines",
  606. parse_line_opt),
  607. OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
  608. OPT_END()
  609. };
  610. static const char *record_args[] = {
  611. "record",
  612. "-a",
  613. "-R",
  614. "-f",
  615. "-c", "1",
  616. "-e", "kmem:kmalloc",
  617. "-e", "kmem:kmalloc_node",
  618. "-e", "kmem:kfree",
  619. "-e", "kmem:kmem_cache_alloc",
  620. "-e", "kmem:kmem_cache_alloc_node",
  621. "-e", "kmem:kmem_cache_free",
  622. };
  623. static int __cmd_record(int argc, const char **argv)
  624. {
  625. unsigned int rec_argc, i, j;
  626. const char **rec_argv;
  627. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  628. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  629. if (rec_argv == NULL)
  630. return -ENOMEM;
  631. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  632. rec_argv[i] = strdup(record_args[i]);
  633. for (j = 1; j < (unsigned int)argc; j++, i++)
  634. rec_argv[i] = argv[j];
  635. return cmd_record(i, rec_argv, NULL);
  636. }
  637. int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
  638. {
  639. argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
  640. if (!argc)
  641. usage_with_options(kmem_usage, kmem_options);
  642. symbol__init();
  643. if (!strncmp(argv[0], "rec", 3)) {
  644. return __cmd_record(argc, argv);
  645. } else if (!strcmp(argv[0], "stat")) {
  646. if (setup_cpunode_map())
  647. return -1;
  648. if (list_empty(&caller_sort))
  649. setup_sorting(&caller_sort, default_sort_order);
  650. if (list_empty(&alloc_sort))
  651. setup_sorting(&alloc_sort, default_sort_order);
  652. return __cmd_kmem();
  653. } else
  654. usage_with_options(kmem_usage, kmem_options);
  655. return 0;
  656. }