builtin-kmem.c 17 KB

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