builtin-kmem.c 18 KB

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