builtin-kmem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/evsel.h"
  4. #include "util/util.h"
  5. #include "util/cache.h"
  6. #include "util/symbol.h"
  7. #include "util/thread.h"
  8. #include "util/header.h"
  9. #include "util/session.h"
  10. #include "util/tool.h"
  11. #include "util/parse-options.h"
  12. #include "util/trace-event.h"
  13. #include "util/debug.h"
  14. #include <linux/rbtree.h>
  15. struct alloc_stat;
  16. typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
  17. static const char *input_name;
  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 char default_sort_order[] = "frag,hit,bytes";
  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, int node)
  175. {
  176. struct event_format *event = evsel->tp_format;
  177. void *data = sample->raw_data;
  178. unsigned long call_site;
  179. unsigned long ptr;
  180. int bytes_req, cpu = sample->cpu;
  181. int bytes_alloc;
  182. int node1, node2;
  183. ptr = raw_field_value(event, "ptr", data);
  184. call_site = raw_field_value(event, "call_site", data);
  185. bytes_req = raw_field_value(event, "bytes_req", data);
  186. bytes_alloc = raw_field_value(event, "bytes_alloc", data);
  187. if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, cpu) ||
  188. insert_caller_stat(call_site, bytes_req, bytes_alloc))
  189. return -1;
  190. total_requested += bytes_req;
  191. total_allocated += bytes_alloc;
  192. if (node) {
  193. node1 = cpunode_map[cpu];
  194. node2 = raw_field_value(event, "node", data);
  195. if (node1 != node2)
  196. nr_cross_allocs++;
  197. }
  198. nr_allocs++;
  199. return 0;
  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 = raw_field_value(evsel->tp_format, "ptr",
  228. sample->raw_data);
  229. struct alloc_stat *s_alloc, *s_caller;
  230. s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
  231. if (!s_alloc)
  232. return 0;
  233. if ((short)sample->cpu != s_alloc->alloc_cpu) {
  234. s_alloc->pingpong++;
  235. s_caller = search_alloc_stat(0, s_alloc->call_site,
  236. &root_caller_stat, callsite_cmp);
  237. if (!s_caller)
  238. return -1;
  239. s_caller->pingpong++;
  240. }
  241. s_alloc->alloc_cpu = -1;
  242. return 0;
  243. }
  244. static int perf_evsel__process_kmem_event(struct perf_evsel *evsel,
  245. struct perf_sample *sample)
  246. {
  247. struct event_format *event = evsel->tp_format;
  248. if (!strcmp(event->name, "kmalloc") ||
  249. !strcmp(event->name, "kmem_cache_alloc")) {
  250. return perf_evsel__process_alloc_event(evsel, sample, 0);
  251. }
  252. if (!strcmp(event->name, "kmalloc_node") ||
  253. !strcmp(event->name, "kmem_cache_alloc_node")) {
  254. return perf_evsel__process_alloc_event(evsel, sample, 1);
  255. }
  256. if (!strcmp(event->name, "kfree") ||
  257. !strcmp(event->name, "kmem_cache_free")) {
  258. return perf_evsel__process_free_event(evsel, sample);
  259. }
  260. return 0;
  261. }
  262. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  263. union perf_event *event,
  264. struct perf_sample *sample,
  265. struct perf_evsel *evsel,
  266. struct machine *machine)
  267. {
  268. struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
  269. if (thread == NULL) {
  270. pr_debug("problem processing %d event, skipping it.\n",
  271. event->header.type);
  272. return -1;
  273. }
  274. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  275. return perf_evsel__process_kmem_event(evsel, sample);
  276. }
  277. static struct perf_tool perf_kmem = {
  278. .sample = process_sample_event,
  279. .comm = perf_event__process_comm,
  280. .ordered_samples = true,
  281. };
  282. static double fragmentation(unsigned long n_req, unsigned long n_alloc)
  283. {
  284. if (n_alloc == 0)
  285. return 0.0;
  286. else
  287. return 100.0 - (100.0 * n_req / n_alloc);
  288. }
  289. static void __print_result(struct rb_root *root, struct perf_session *session,
  290. int n_lines, int is_caller)
  291. {
  292. struct rb_node *next;
  293. struct machine *machine;
  294. printf("%.102s\n", graph_dotted_line);
  295. printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
  296. printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
  297. printf("%.102s\n", graph_dotted_line);
  298. next = rb_first(root);
  299. machine = perf_session__find_host_machine(session);
  300. if (!machine) {
  301. pr_err("__print_result: couldn't find kernel information\n");
  302. return;
  303. }
  304. while (next && n_lines--) {
  305. struct alloc_stat *data = rb_entry(next, struct alloc_stat,
  306. node);
  307. struct symbol *sym = NULL;
  308. struct map *map;
  309. char buf[BUFSIZ];
  310. u64 addr;
  311. if (is_caller) {
  312. addr = data->call_site;
  313. if (!raw_ip)
  314. sym = machine__find_kernel_function(machine, addr, &map, NULL);
  315. } else
  316. addr = data->ptr;
  317. if (sym != NULL)
  318. snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
  319. addr - map->unmap_ip(map, sym->start));
  320. else
  321. snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
  322. printf(" %-34s |", buf);
  323. printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n",
  324. (unsigned long long)data->bytes_alloc,
  325. (unsigned long)data->bytes_alloc / data->hit,
  326. (unsigned long long)data->bytes_req,
  327. (unsigned long)data->bytes_req / data->hit,
  328. (unsigned long)data->hit,
  329. (unsigned long)data->pingpong,
  330. fragmentation(data->bytes_req, data->bytes_alloc));
  331. next = rb_next(next);
  332. }
  333. if (n_lines == -1)
  334. printf(" ... | ... | ... | ... | ... | ... \n");
  335. printf("%.102s\n", graph_dotted_line);
  336. }
  337. static void print_summary(void)
  338. {
  339. printf("\nSUMMARY\n=======\n");
  340. printf("Total bytes requested: %lu\n", total_requested);
  341. printf("Total bytes allocated: %lu\n", total_allocated);
  342. printf("Total bytes wasted on internal fragmentation: %lu\n",
  343. total_allocated - total_requested);
  344. printf("Internal fragmentation: %f%%\n",
  345. fragmentation(total_requested, total_allocated));
  346. printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
  347. }
  348. static void print_result(struct perf_session *session)
  349. {
  350. if (caller_flag)
  351. __print_result(&root_caller_sorted, session, caller_lines, 1);
  352. if (alloc_flag)
  353. __print_result(&root_alloc_sorted, session, alloc_lines, 0);
  354. print_summary();
  355. }
  356. struct sort_dimension {
  357. const char name[20];
  358. sort_fn_t cmp;
  359. struct list_head list;
  360. };
  361. static LIST_HEAD(caller_sort);
  362. static LIST_HEAD(alloc_sort);
  363. static void sort_insert(struct rb_root *root, struct alloc_stat *data,
  364. struct list_head *sort_list)
  365. {
  366. struct rb_node **new = &(root->rb_node);
  367. struct rb_node *parent = NULL;
  368. struct sort_dimension *sort;
  369. while (*new) {
  370. struct alloc_stat *this;
  371. int cmp = 0;
  372. this = rb_entry(*new, struct alloc_stat, node);
  373. parent = *new;
  374. list_for_each_entry(sort, sort_list, list) {
  375. cmp = sort->cmp(data, this);
  376. if (cmp)
  377. break;
  378. }
  379. if (cmp > 0)
  380. new = &((*new)->rb_left);
  381. else
  382. new = &((*new)->rb_right);
  383. }
  384. rb_link_node(&data->node, parent, new);
  385. rb_insert_color(&data->node, root);
  386. }
  387. static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
  388. struct list_head *sort_list)
  389. {
  390. struct rb_node *node;
  391. struct alloc_stat *data;
  392. for (;;) {
  393. node = rb_first(root);
  394. if (!node)
  395. break;
  396. rb_erase(node, root);
  397. data = rb_entry(node, struct alloc_stat, node);
  398. sort_insert(root_sorted, data, sort_list);
  399. }
  400. }
  401. static void sort_result(void)
  402. {
  403. __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
  404. __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
  405. }
  406. static int __cmd_kmem(void)
  407. {
  408. int err = -EINVAL;
  409. struct perf_session *session;
  410. session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem);
  411. if (session == NULL)
  412. return -ENOMEM;
  413. if (perf_session__create_kernel_maps(session) < 0)
  414. goto out_delete;
  415. if (!perf_session__has_traces(session, "kmem record"))
  416. goto out_delete;
  417. setup_pager();
  418. err = perf_session__process_events(session, &perf_kmem);
  419. if (err != 0)
  420. goto out_delete;
  421. sort_result();
  422. print_result(session);
  423. out_delete:
  424. perf_session__delete(session);
  425. return err;
  426. }
  427. static const char * const kmem_usage[] = {
  428. "perf kmem [<options>] {record|stat}",
  429. NULL
  430. };
  431. static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
  432. {
  433. if (l->ptr < r->ptr)
  434. return -1;
  435. else if (l->ptr > r->ptr)
  436. return 1;
  437. return 0;
  438. }
  439. static struct sort_dimension ptr_sort_dimension = {
  440. .name = "ptr",
  441. .cmp = ptr_cmp,
  442. };
  443. static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
  444. {
  445. if (l->call_site < r->call_site)
  446. return -1;
  447. else if (l->call_site > r->call_site)
  448. return 1;
  449. return 0;
  450. }
  451. static struct sort_dimension callsite_sort_dimension = {
  452. .name = "callsite",
  453. .cmp = callsite_cmp,
  454. };
  455. static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
  456. {
  457. if (l->hit < r->hit)
  458. return -1;
  459. else if (l->hit > r->hit)
  460. return 1;
  461. return 0;
  462. }
  463. static struct sort_dimension hit_sort_dimension = {
  464. .name = "hit",
  465. .cmp = hit_cmp,
  466. };
  467. static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
  468. {
  469. if (l->bytes_alloc < r->bytes_alloc)
  470. return -1;
  471. else if (l->bytes_alloc > r->bytes_alloc)
  472. return 1;
  473. return 0;
  474. }
  475. static struct sort_dimension bytes_sort_dimension = {
  476. .name = "bytes",
  477. .cmp = bytes_cmp,
  478. };
  479. static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
  480. {
  481. double x, y;
  482. x = fragmentation(l->bytes_req, l->bytes_alloc);
  483. y = fragmentation(r->bytes_req, r->bytes_alloc);
  484. if (x < y)
  485. return -1;
  486. else if (x > y)
  487. return 1;
  488. return 0;
  489. }
  490. static struct sort_dimension frag_sort_dimension = {
  491. .name = "frag",
  492. .cmp = frag_cmp,
  493. };
  494. static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
  495. {
  496. if (l->pingpong < r->pingpong)
  497. return -1;
  498. else if (l->pingpong > r->pingpong)
  499. return 1;
  500. return 0;
  501. }
  502. static struct sort_dimension pingpong_sort_dimension = {
  503. .name = "pingpong",
  504. .cmp = pingpong_cmp,
  505. };
  506. static struct sort_dimension *avail_sorts[] = {
  507. &ptr_sort_dimension,
  508. &callsite_sort_dimension,
  509. &hit_sort_dimension,
  510. &bytes_sort_dimension,
  511. &frag_sort_dimension,
  512. &pingpong_sort_dimension,
  513. };
  514. #define NUM_AVAIL_SORTS \
  515. (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
  516. static int sort_dimension__add(const char *tok, struct list_head *list)
  517. {
  518. struct sort_dimension *sort;
  519. int i;
  520. for (i = 0; i < NUM_AVAIL_SORTS; i++) {
  521. if (!strcmp(avail_sorts[i]->name, tok)) {
  522. sort = malloc(sizeof(*sort));
  523. if (!sort) {
  524. pr_err("%s: malloc failed\n", __func__);
  525. return -1;
  526. }
  527. memcpy(sort, avail_sorts[i], sizeof(*sort));
  528. list_add_tail(&sort->list, list);
  529. return 0;
  530. }
  531. }
  532. return -1;
  533. }
  534. static int setup_sorting(struct list_head *sort_list, const char *arg)
  535. {
  536. char *tok;
  537. char *str = strdup(arg);
  538. if (!str) {
  539. pr_err("%s: strdup failed\n", __func__);
  540. return -1;
  541. }
  542. while (true) {
  543. tok = strsep(&str, ",");
  544. if (!tok)
  545. break;
  546. if (sort_dimension__add(tok, sort_list) < 0) {
  547. error("Unknown --sort key: '%s'", tok);
  548. free(str);
  549. return -1;
  550. }
  551. }
  552. free(str);
  553. return 0;
  554. }
  555. static int parse_sort_opt(const struct option *opt __maybe_unused,
  556. const char *arg, int unset __maybe_unused)
  557. {
  558. if (!arg)
  559. return -1;
  560. if (caller_flag > alloc_flag)
  561. return setup_sorting(&caller_sort, arg);
  562. else
  563. return setup_sorting(&alloc_sort, arg);
  564. return 0;
  565. }
  566. static int parse_caller_opt(const struct option *opt __maybe_unused,
  567. const char *arg __maybe_unused,
  568. int unset __maybe_unused)
  569. {
  570. caller_flag = (alloc_flag + 1);
  571. return 0;
  572. }
  573. static int parse_alloc_opt(const struct option *opt __maybe_unused,
  574. const char *arg __maybe_unused,
  575. int unset __maybe_unused)
  576. {
  577. alloc_flag = (caller_flag + 1);
  578. return 0;
  579. }
  580. static int parse_line_opt(const struct option *opt __maybe_unused,
  581. const char *arg, int unset __maybe_unused)
  582. {
  583. int lines;
  584. if (!arg)
  585. return -1;
  586. lines = strtoul(arg, NULL, 10);
  587. if (caller_flag > alloc_flag)
  588. caller_lines = lines;
  589. else
  590. alloc_lines = lines;
  591. return 0;
  592. }
  593. static const struct option kmem_options[] = {
  594. OPT_STRING('i', "input", &input_name, "file",
  595. "input file name"),
  596. OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
  597. "show per-callsite statistics",
  598. parse_caller_opt),
  599. OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
  600. "show per-allocation statistics",
  601. parse_alloc_opt),
  602. OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
  603. "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
  604. parse_sort_opt),
  605. OPT_CALLBACK('l', "line", NULL, "num",
  606. "show n lines",
  607. parse_line_opt),
  608. OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
  609. OPT_END()
  610. };
  611. static const char *record_args[] = {
  612. "record",
  613. "-a",
  614. "-R",
  615. "-f",
  616. "-c", "1",
  617. "-e", "kmem:kmalloc",
  618. "-e", "kmem:kmalloc_node",
  619. "-e", "kmem:kfree",
  620. "-e", "kmem:kmem_cache_alloc",
  621. "-e", "kmem:kmem_cache_alloc_node",
  622. "-e", "kmem:kmem_cache_free",
  623. };
  624. static int __cmd_record(int argc, const char **argv)
  625. {
  626. unsigned int rec_argc, i, j;
  627. const char **rec_argv;
  628. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  629. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  630. if (rec_argv == NULL)
  631. return -ENOMEM;
  632. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  633. rec_argv[i] = strdup(record_args[i]);
  634. for (j = 1; j < (unsigned int)argc; j++, i++)
  635. rec_argv[i] = argv[j];
  636. return cmd_record(i, rec_argv, NULL);
  637. }
  638. int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
  639. {
  640. argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
  641. if (!argc)
  642. usage_with_options(kmem_usage, kmem_options);
  643. symbol__init();
  644. if (!strncmp(argv[0], "rec", 3)) {
  645. return __cmd_record(argc, argv);
  646. } else if (!strcmp(argv[0], "stat")) {
  647. if (setup_cpunode_map())
  648. return -1;
  649. if (list_empty(&caller_sort))
  650. setup_sorting(&caller_sort, default_sort_order);
  651. if (list_empty(&alloc_sort))
  652. setup_sorting(&alloc_sort, default_sort_order);
  653. return __cmd_kmem();
  654. } else
  655. usage_with_options(kmem_usage, kmem_options);
  656. return 0;
  657. }