builtin-kmem.c 17 KB

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