builtin-kmem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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)
  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 = perf_session__findnew(session, 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. err = perf_session__process_events(session, &event_ops);
  303. perf_session__delete(session);
  304. return err;
  305. }
  306. static double fragmentation(unsigned long n_req, unsigned long n_alloc)
  307. {
  308. if (n_alloc == 0)
  309. return 0.0;
  310. else
  311. return 100.0 - (100.0 * n_req / n_alloc);
  312. }
  313. static void __print_result(struct rb_root *root, int n_lines, int is_caller)
  314. {
  315. struct rb_node *next;
  316. printf("%.102s\n", graph_dotted_line);
  317. printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
  318. printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
  319. printf("%.102s\n", graph_dotted_line);
  320. next = rb_first(root);
  321. while (next && n_lines--) {
  322. struct alloc_stat *data = rb_entry(next, struct alloc_stat,
  323. node);
  324. struct symbol *sym = NULL;
  325. char buf[BUFSIZ];
  326. u64 addr;
  327. if (is_caller) {
  328. addr = data->call_site;
  329. if (!raw_ip)
  330. sym = map_groups__find_function(kmaps, addr, NULL);
  331. } else
  332. addr = data->ptr;
  333. if (sym != NULL)
  334. snprintf(buf, sizeof(buf), "%s+%Lx", sym->name,
  335. addr - sym->start);
  336. else
  337. snprintf(buf, sizeof(buf), "%#Lx", addr);
  338. printf(" %-34s |", buf);
  339. printf(" %9llu/%-5lu | %9llu/%-5lu | %6lu | %8lu | %6.3f%%\n",
  340. (unsigned long long)data->bytes_alloc,
  341. (unsigned long)data->bytes_alloc / data->hit,
  342. (unsigned long long)data->bytes_req,
  343. (unsigned long)data->bytes_req / data->hit,
  344. (unsigned long)data->hit,
  345. (unsigned long)data->pingpong,
  346. fragmentation(data->bytes_req, data->bytes_alloc));
  347. next = rb_next(next);
  348. }
  349. if (n_lines == -1)
  350. printf(" ... | ... | ... | ... | ... | ... \n");
  351. printf("%.102s\n", graph_dotted_line);
  352. }
  353. static void print_summary(void)
  354. {
  355. printf("\nSUMMARY\n=======\n");
  356. printf("Total bytes requested: %lu\n", total_requested);
  357. printf("Total bytes allocated: %lu\n", total_allocated);
  358. printf("Total bytes wasted on internal fragmentation: %lu\n",
  359. total_allocated - total_requested);
  360. printf("Internal fragmentation: %f%%\n",
  361. fragmentation(total_requested, total_allocated));
  362. printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs);
  363. }
  364. static void print_result(void)
  365. {
  366. if (caller_flag)
  367. __print_result(&root_caller_sorted, caller_lines, 1);
  368. if (alloc_flag)
  369. __print_result(&root_alloc_sorted, alloc_lines, 0);
  370. print_summary();
  371. }
  372. struct sort_dimension {
  373. const char name[20];
  374. sort_fn_t cmp;
  375. struct list_head list;
  376. };
  377. static LIST_HEAD(caller_sort);
  378. static LIST_HEAD(alloc_sort);
  379. static void sort_insert(struct rb_root *root, struct alloc_stat *data,
  380. struct list_head *sort_list)
  381. {
  382. struct rb_node **new = &(root->rb_node);
  383. struct rb_node *parent = NULL;
  384. struct sort_dimension *sort;
  385. while (*new) {
  386. struct alloc_stat *this;
  387. int cmp = 0;
  388. this = rb_entry(*new, struct alloc_stat, node);
  389. parent = *new;
  390. list_for_each_entry(sort, sort_list, list) {
  391. cmp = sort->cmp(data, this);
  392. if (cmp)
  393. break;
  394. }
  395. if (cmp > 0)
  396. new = &((*new)->rb_left);
  397. else
  398. new = &((*new)->rb_right);
  399. }
  400. rb_link_node(&data->node, parent, new);
  401. rb_insert_color(&data->node, root);
  402. }
  403. static void __sort_result(struct rb_root *root, struct rb_root *root_sorted,
  404. struct list_head *sort_list)
  405. {
  406. struct rb_node *node;
  407. struct alloc_stat *data;
  408. for (;;) {
  409. node = rb_first(root);
  410. if (!node)
  411. break;
  412. rb_erase(node, root);
  413. data = rb_entry(node, struct alloc_stat, node);
  414. sort_insert(root_sorted, data, sort_list);
  415. }
  416. }
  417. static void sort_result(void)
  418. {
  419. __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort);
  420. __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort);
  421. }
  422. static int __cmd_kmem(void)
  423. {
  424. setup_pager();
  425. read_events();
  426. sort_result();
  427. print_result();
  428. return 0;
  429. }
  430. static const char * const kmem_usage[] = {
  431. "perf kmem [<options>] {record|stat}",
  432. NULL
  433. };
  434. static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
  435. {
  436. if (l->ptr < r->ptr)
  437. return -1;
  438. else if (l->ptr > r->ptr)
  439. return 1;
  440. return 0;
  441. }
  442. static struct sort_dimension ptr_sort_dimension = {
  443. .name = "ptr",
  444. .cmp = ptr_cmp,
  445. };
  446. static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
  447. {
  448. if (l->call_site < r->call_site)
  449. return -1;
  450. else if (l->call_site > r->call_site)
  451. return 1;
  452. return 0;
  453. }
  454. static struct sort_dimension callsite_sort_dimension = {
  455. .name = "callsite",
  456. .cmp = callsite_cmp,
  457. };
  458. static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
  459. {
  460. if (l->hit < r->hit)
  461. return -1;
  462. else if (l->hit > r->hit)
  463. return 1;
  464. return 0;
  465. }
  466. static struct sort_dimension hit_sort_dimension = {
  467. .name = "hit",
  468. .cmp = hit_cmp,
  469. };
  470. static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
  471. {
  472. if (l->bytes_alloc < r->bytes_alloc)
  473. return -1;
  474. else if (l->bytes_alloc > r->bytes_alloc)
  475. return 1;
  476. return 0;
  477. }
  478. static struct sort_dimension bytes_sort_dimension = {
  479. .name = "bytes",
  480. .cmp = bytes_cmp,
  481. };
  482. static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
  483. {
  484. double x, y;
  485. x = fragmentation(l->bytes_req, l->bytes_alloc);
  486. y = fragmentation(r->bytes_req, r->bytes_alloc);
  487. if (x < y)
  488. return -1;
  489. else if (x > y)
  490. return 1;
  491. return 0;
  492. }
  493. static struct sort_dimension frag_sort_dimension = {
  494. .name = "frag",
  495. .cmp = frag_cmp,
  496. };
  497. static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
  498. {
  499. if (l->pingpong < r->pingpong)
  500. return -1;
  501. else if (l->pingpong > r->pingpong)
  502. return 1;
  503. return 0;
  504. }
  505. static struct sort_dimension pingpong_sort_dimension = {
  506. .name = "pingpong",
  507. .cmp = pingpong_cmp,
  508. };
  509. static struct sort_dimension *avail_sorts[] = {
  510. &ptr_sort_dimension,
  511. &callsite_sort_dimension,
  512. &hit_sort_dimension,
  513. &bytes_sort_dimension,
  514. &frag_sort_dimension,
  515. &pingpong_sort_dimension,
  516. };
  517. #define NUM_AVAIL_SORTS \
  518. (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *))
  519. static int sort_dimension__add(const char *tok, struct list_head *list)
  520. {
  521. struct sort_dimension *sort;
  522. int i;
  523. for (i = 0; i < NUM_AVAIL_SORTS; i++) {
  524. if (!strcmp(avail_sorts[i]->name, tok)) {
  525. sort = malloc(sizeof(*sort));
  526. if (!sort)
  527. die("malloc");
  528. memcpy(sort, avail_sorts[i], sizeof(*sort));
  529. list_add_tail(&sort->list, list);
  530. return 0;
  531. }
  532. }
  533. return -1;
  534. }
  535. static int setup_sorting(struct list_head *sort_list, const char *arg)
  536. {
  537. char *tok;
  538. char *str = strdup(arg);
  539. if (!str)
  540. die("strdup");
  541. while (true) {
  542. tok = strsep(&str, ",");
  543. if (!tok)
  544. break;
  545. if (sort_dimension__add(tok, sort_list) < 0) {
  546. error("Unknown --sort key: '%s'", tok);
  547. return -1;
  548. }
  549. }
  550. free(str);
  551. return 0;
  552. }
  553. static int parse_sort_opt(const struct option *opt __used,
  554. const char *arg, int unset __used)
  555. {
  556. if (!arg)
  557. return -1;
  558. if (caller_flag > alloc_flag)
  559. return setup_sorting(&caller_sort, arg);
  560. else
  561. return setup_sorting(&alloc_sort, arg);
  562. return 0;
  563. }
  564. static int parse_caller_opt(const struct option *opt __used,
  565. const char *arg __used, int unset __used)
  566. {
  567. caller_flag = (alloc_flag + 1);
  568. return 0;
  569. }
  570. static int parse_alloc_opt(const struct option *opt __used,
  571. const char *arg __used, int unset __used)
  572. {
  573. alloc_flag = (caller_flag + 1);
  574. return 0;
  575. }
  576. static int parse_line_opt(const struct option *opt __used,
  577. const char *arg, int unset __used)
  578. {
  579. int lines;
  580. if (!arg)
  581. return -1;
  582. lines = strtoul(arg, NULL, 10);
  583. if (caller_flag > alloc_flag)
  584. caller_lines = lines;
  585. else
  586. alloc_lines = lines;
  587. return 0;
  588. }
  589. static const struct option kmem_options[] = {
  590. OPT_STRING('i', "input", &input_name, "file",
  591. "input file name"),
  592. OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
  593. "show per-callsite statistics",
  594. parse_caller_opt),
  595. OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
  596. "show per-allocation statistics",
  597. parse_alloc_opt),
  598. OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
  599. "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
  600. parse_sort_opt),
  601. OPT_CALLBACK('l', "line", NULL, "num",
  602. "show n lines",
  603. parse_line_opt),
  604. OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
  605. OPT_END()
  606. };
  607. static const char *record_args[] = {
  608. "record",
  609. "-a",
  610. "-R",
  611. "-M",
  612. "-f",
  613. "-c", "1",
  614. "-e", "kmem:kmalloc",
  615. "-e", "kmem:kmalloc_node",
  616. "-e", "kmem:kfree",
  617. "-e", "kmem:kmem_cache_alloc",
  618. "-e", "kmem:kmem_cache_alloc_node",
  619. "-e", "kmem:kmem_cache_free",
  620. };
  621. static int __cmd_record(int argc, const char **argv)
  622. {
  623. unsigned int rec_argc, i, j;
  624. const char **rec_argv;
  625. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  626. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  627. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  628. rec_argv[i] = strdup(record_args[i]);
  629. for (j = 1; j < (unsigned int)argc; j++, i++)
  630. rec_argv[i] = argv[j];
  631. return cmd_record(i, rec_argv, NULL);
  632. }
  633. int cmd_kmem(int argc, const char **argv, const char *prefix __used)
  634. {
  635. symbol__init(0);
  636. argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
  637. if (!argc)
  638. usage_with_options(kmem_usage, kmem_options);
  639. if (!strncmp(argv[0], "rec", 3)) {
  640. return __cmd_record(argc, argv);
  641. } else if (!strcmp(argv[0], "stat")) {
  642. setup_cpunode_map();
  643. if (list_empty(&caller_sort))
  644. setup_sorting(&caller_sort, default_sort_order);
  645. if (list_empty(&alloc_sort))
  646. setup_sorting(&alloc_sort, default_sort_order);
  647. return __cmd_kmem();
  648. }
  649. return 0;
  650. }