builtin-kmem.c 17 KB

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