builtin-kmem.c 17 KB

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