builtin-kmem.c 17 KB

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