builtin-kmem.c 17 KB

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