builtin-kmem.c 17 KB

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