PageRenderTime 68ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/perf/builtin-kmem.c

http://github.com/torvalds/linux
C | 2026 lines | 1650 code | 357 blank | 19 comment | 282 complexity | 0b1aed7dc216ae0762593db7151b4364 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "builtin.h"
  3. #include "perf.h"
  4. #include "util/dso.h"
  5. #include "util/evlist.h"
  6. #include "util/evsel.h"
  7. #include "util/config.h"
  8. #include "util/map.h"
  9. #include "util/symbol.h"
  10. #include "util/thread.h"
  11. #include "util/header.h"
  12. #include "util/session.h"
  13. #include "util/tool.h"
  14. #include "util/callchain.h"
  15. #include "util/time-utils.h"
  16. #include <linux/err.h>
  17. #include <subcmd/pager.h>
  18. #include <subcmd/parse-options.h>
  19. #include "util/trace-event.h"
  20. #include "util/data.h"
  21. #include "util/cpumap.h"
  22. #include "util/debug.h"
  23. #include "util/string2.h"
  24. #include <linux/kernel.h>
  25. #include <linux/rbtree.h>
  26. #include <linux/string.h>
  27. #include <linux/zalloc.h>
  28. #include <errno.h>
  29. #include <inttypes.h>
  30. #include <locale.h>
  31. #include <regex.h>
  32. #include <linux/ctype.h>
  33. static int kmem_slab;
  34. static int kmem_page;
  35. static long kmem_page_size;
  36. static enum {
  37. KMEM_SLAB,
  38. KMEM_PAGE,
  39. } kmem_default = KMEM_SLAB; /* for backward compatibility */
  40. struct alloc_stat;
  41. typedef int (*sort_fn_t)(void *, void *);
  42. static int alloc_flag;
  43. static int caller_flag;
  44. static int alloc_lines = -1;
  45. static int caller_lines = -1;
  46. static bool raw_ip;
  47. struct alloc_stat {
  48. u64 call_site;
  49. u64 ptr;
  50. u64 bytes_req;
  51. u64 bytes_alloc;
  52. u64 last_alloc;
  53. u32 hit;
  54. u32 pingpong;
  55. short alloc_cpu;
  56. struct rb_node node;
  57. };
  58. static struct rb_root root_alloc_stat;
  59. static struct rb_root root_alloc_sorted;
  60. static struct rb_root root_caller_stat;
  61. static struct rb_root root_caller_sorted;
  62. static unsigned long total_requested, total_allocated, total_freed;
  63. static unsigned long nr_allocs, nr_cross_allocs;
  64. /* filters for controlling start and stop of time of analysis */
  65. static struct perf_time_interval ptime;
  66. const char *time_str;
  67. static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
  68. int bytes_req, int bytes_alloc, int cpu)
  69. {
  70. struct rb_node **node = &root_alloc_stat.rb_node;
  71. struct rb_node *parent = NULL;
  72. struct alloc_stat *data = NULL;
  73. while (*node) {
  74. parent = *node;
  75. data = rb_entry(*node, struct alloc_stat, node);
  76. if (ptr > data->ptr)
  77. node = &(*node)->rb_right;
  78. else if (ptr < data->ptr)
  79. node = &(*node)->rb_left;
  80. else
  81. break;
  82. }
  83. if (data && data->ptr == ptr) {
  84. data->hit++;
  85. data->bytes_req += bytes_req;
  86. data->bytes_alloc += bytes_alloc;
  87. } else {
  88. data = malloc(sizeof(*data));
  89. if (!data) {
  90. pr_err("%s: malloc failed\n", __func__);
  91. return -1;
  92. }
  93. data->ptr = ptr;
  94. data->pingpong = 0;
  95. data->hit = 1;
  96. data->bytes_req = bytes_req;
  97. data->bytes_alloc = bytes_alloc;
  98. rb_link_node(&data->node, parent, node);
  99. rb_insert_color(&data->node, &root_alloc_stat);
  100. }
  101. data->call_site = call_site;
  102. data->alloc_cpu = cpu;
  103. data->last_alloc = bytes_alloc;
  104. return 0;
  105. }
  106. static int insert_caller_stat(unsigned long call_site,
  107. int bytes_req, int bytes_alloc)
  108. {
  109. struct rb_node **node = &root_caller_stat.rb_node;
  110. struct rb_node *parent = NULL;
  111. struct alloc_stat *data = NULL;
  112. while (*node) {
  113. parent = *node;
  114. data = rb_entry(*node, struct alloc_stat, node);
  115. if (call_site > data->call_site)
  116. node = &(*node)->rb_right;
  117. else if (call_site < data->call_site)
  118. node = &(*node)->rb_left;
  119. else
  120. break;
  121. }
  122. if (data && data->call_site == call_site) {
  123. data->hit++;
  124. data->bytes_req += bytes_req;
  125. data->bytes_alloc += bytes_alloc;
  126. } else {
  127. data = malloc(sizeof(*data));
  128. if (!data) {
  129. pr_err("%s: malloc failed\n", __func__);
  130. return -1;
  131. }
  132. data->call_site = call_site;
  133. data->pingpong = 0;
  134. data->hit = 1;
  135. data->bytes_req = bytes_req;
  136. data->bytes_alloc = bytes_alloc;
  137. rb_link_node(&data->node, parent, node);
  138. rb_insert_color(&data->node, &root_caller_stat);
  139. }
  140. return 0;
  141. }
  142. static int perf_evsel__process_alloc_event(struct evsel *evsel,
  143. struct perf_sample *sample)
  144. {
  145. unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
  146. call_site = perf_evsel__intval(evsel, sample, "call_site");
  147. int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
  148. bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
  149. if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
  150. insert_caller_stat(call_site, bytes_req, bytes_alloc))
  151. return -1;
  152. total_requested += bytes_req;
  153. total_allocated += bytes_alloc;
  154. nr_allocs++;
  155. return 0;
  156. }
  157. static int perf_evsel__process_alloc_node_event(struct evsel *evsel,
  158. struct perf_sample *sample)
  159. {
  160. int ret = perf_evsel__process_alloc_event(evsel, sample);
  161. if (!ret) {
  162. int node1 = cpu__get_node(sample->cpu),
  163. node2 = perf_evsel__intval(evsel, sample, "node");
  164. if (node1 != node2)
  165. nr_cross_allocs++;
  166. }
  167. return ret;
  168. }
  169. static int ptr_cmp(void *, void *);
  170. static int slab_callsite_cmp(void *, void *);
  171. static struct alloc_stat *search_alloc_stat(unsigned long ptr,
  172. unsigned long call_site,
  173. struct rb_root *root,
  174. sort_fn_t sort_fn)
  175. {
  176. struct rb_node *node = root->rb_node;
  177. struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
  178. while (node) {
  179. struct alloc_stat *data;
  180. int cmp;
  181. data = rb_entry(node, struct alloc_stat, node);
  182. cmp = sort_fn(&key, data);
  183. if (cmp < 0)
  184. node = node->rb_left;
  185. else if (cmp > 0)
  186. node = node->rb_right;
  187. else
  188. return data;
  189. }
  190. return NULL;
  191. }
  192. static int perf_evsel__process_free_event(struct evsel *evsel,
  193. struct perf_sample *sample)
  194. {
  195. unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
  196. struct alloc_stat *s_alloc, *s_caller;
  197. s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
  198. if (!s_alloc)
  199. return 0;
  200. total_freed += s_alloc->last_alloc;
  201. if ((short)sample->cpu != s_alloc->alloc_cpu) {
  202. s_alloc->pingpong++;
  203. s_caller = search_alloc_stat(0, s_alloc->call_site,
  204. &root_caller_stat,
  205. slab_callsite_cmp);
  206. if (!s_caller)
  207. return -1;
  208. s_caller->pingpong++;
  209. }
  210. s_alloc->alloc_cpu = -1;
  211. return 0;
  212. }
  213. static u64 total_page_alloc_bytes;
  214. static u64 total_page_free_bytes;
  215. static u64 total_page_nomatch_bytes;
  216. static u64 total_page_fail_bytes;
  217. static unsigned long nr_page_allocs;
  218. static unsigned long nr_page_frees;
  219. static unsigned long nr_page_fails;
  220. static unsigned long nr_page_nomatch;
  221. static bool use_pfn;
  222. static bool live_page;
  223. static struct perf_session *kmem_session;
  224. #define MAX_MIGRATE_TYPES 6
  225. #define MAX_PAGE_ORDER 11
  226. static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
  227. struct page_stat {
  228. struct rb_node node;
  229. u64 page;
  230. u64 callsite;
  231. int order;
  232. unsigned gfp_flags;
  233. unsigned migrate_type;
  234. u64 alloc_bytes;
  235. u64 free_bytes;
  236. int nr_alloc;
  237. int nr_free;
  238. };
  239. static struct rb_root page_live_tree;
  240. static struct rb_root page_alloc_tree;
  241. static struct rb_root page_alloc_sorted;
  242. static struct rb_root page_caller_tree;
  243. static struct rb_root page_caller_sorted;
  244. struct alloc_func {
  245. u64 start;
  246. u64 end;
  247. char *name;
  248. };
  249. static int nr_alloc_funcs;
  250. static struct alloc_func *alloc_func_list;
  251. static int funcmp(const void *a, const void *b)
  252. {
  253. const struct alloc_func *fa = a;
  254. const struct alloc_func *fb = b;
  255. if (fa->start > fb->start)
  256. return 1;
  257. else
  258. return -1;
  259. }
  260. static int callcmp(const void *a, const void *b)
  261. {
  262. const struct alloc_func *fa = a;
  263. const struct alloc_func *fb = b;
  264. if (fb->start <= fa->start && fa->end < fb->end)
  265. return 0;
  266. if (fa->start > fb->start)
  267. return 1;
  268. else
  269. return -1;
  270. }
  271. static int build_alloc_func_list(void)
  272. {
  273. int ret;
  274. struct map *kernel_map;
  275. struct symbol *sym;
  276. struct rb_node *node;
  277. struct alloc_func *func;
  278. struct machine *machine = &kmem_session->machines.host;
  279. regex_t alloc_func_regex;
  280. static const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
  281. ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
  282. if (ret) {
  283. char err[BUFSIZ];
  284. regerror(ret, &alloc_func_regex, err, sizeof(err));
  285. pr_err("Invalid regex: %s\n%s", pattern, err);
  286. return -EINVAL;
  287. }
  288. kernel_map = machine__kernel_map(machine);
  289. if (map__load(kernel_map) < 0) {
  290. pr_err("cannot load kernel map\n");
  291. return -ENOENT;
  292. }
  293. map__for_each_symbol(kernel_map, sym, node) {
  294. if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
  295. continue;
  296. func = realloc(alloc_func_list,
  297. (nr_alloc_funcs + 1) * sizeof(*func));
  298. if (func == NULL)
  299. return -ENOMEM;
  300. pr_debug("alloc func: %s\n", sym->name);
  301. func[nr_alloc_funcs].start = sym->start;
  302. func[nr_alloc_funcs].end = sym->end;
  303. func[nr_alloc_funcs].name = sym->name;
  304. alloc_func_list = func;
  305. nr_alloc_funcs++;
  306. }
  307. qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
  308. regfree(&alloc_func_regex);
  309. return 0;
  310. }
  311. /*
  312. * Find first non-memory allocation function from callchain.
  313. * The allocation functions are in the 'alloc_func_list'.
  314. */
  315. static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)
  316. {
  317. struct addr_location al;
  318. struct machine *machine = &kmem_session->machines.host;
  319. struct callchain_cursor_node *node;
  320. if (alloc_func_list == NULL) {
  321. if (build_alloc_func_list() < 0)
  322. goto out;
  323. }
  324. al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  325. sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);
  326. callchain_cursor_commit(&callchain_cursor);
  327. while (true) {
  328. struct alloc_func key, *caller;
  329. u64 addr;
  330. node = callchain_cursor_current(&callchain_cursor);
  331. if (node == NULL)
  332. break;
  333. key.start = key.end = node->ip;
  334. caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
  335. sizeof(key), callcmp);
  336. if (!caller) {
  337. /* found */
  338. if (node->ms.map)
  339. addr = map__unmap_ip(node->ms.map, node->ip);
  340. else
  341. addr = node->ip;
  342. return addr;
  343. } else
  344. pr_debug3("skipping alloc function: %s\n", caller->name);
  345. callchain_cursor_advance(&callchain_cursor);
  346. }
  347. out:
  348. pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
  349. return sample->ip;
  350. }
  351. struct sort_dimension {
  352. const char name[20];
  353. sort_fn_t cmp;
  354. struct list_head list;
  355. };
  356. static LIST_HEAD(page_alloc_sort_input);
  357. static LIST_HEAD(page_caller_sort_input);
  358. static struct page_stat *
  359. __page_stat__findnew_page(struct page_stat *pstat, bool create)
  360. {
  361. struct rb_node **node = &page_live_tree.rb_node;
  362. struct rb_node *parent = NULL;
  363. struct page_stat *data;
  364. while (*node) {
  365. s64 cmp;
  366. parent = *node;
  367. data = rb_entry(*node, struct page_stat, node);
  368. cmp = data->page - pstat->page;
  369. if (cmp < 0)
  370. node = &parent->rb_left;
  371. else if (cmp > 0)
  372. node = &parent->rb_right;
  373. else
  374. return data;
  375. }
  376. if (!create)
  377. return NULL;
  378. data = zalloc(sizeof(*data));
  379. if (data != NULL) {
  380. data->page = pstat->page;
  381. data->order = pstat->order;
  382. data->gfp_flags = pstat->gfp_flags;
  383. data->migrate_type = pstat->migrate_type;
  384. rb_link_node(&data->node, parent, node);
  385. rb_insert_color(&data->node, &page_live_tree);
  386. }
  387. return data;
  388. }
  389. static struct page_stat *page_stat__find_page(struct page_stat *pstat)
  390. {
  391. return __page_stat__findnew_page(pstat, false);
  392. }
  393. static struct page_stat *page_stat__findnew_page(struct page_stat *pstat)
  394. {
  395. return __page_stat__findnew_page(pstat, true);
  396. }
  397. static struct page_stat *
  398. __page_stat__findnew_alloc(struct page_stat *pstat, bool create)
  399. {
  400. struct rb_node **node = &page_alloc_tree.rb_node;
  401. struct rb_node *parent = NULL;
  402. struct page_stat *data;
  403. struct sort_dimension *sort;
  404. while (*node) {
  405. int cmp = 0;
  406. parent = *node;
  407. data = rb_entry(*node, struct page_stat, node);
  408. list_for_each_entry(sort, &page_alloc_sort_input, list) {
  409. cmp = sort->cmp(pstat, data);
  410. if (cmp)
  411. break;
  412. }
  413. if (cmp < 0)
  414. node = &parent->rb_left;
  415. else if (cmp > 0)
  416. node = &parent->rb_right;
  417. else
  418. return data;
  419. }
  420. if (!create)
  421. return NULL;
  422. data = zalloc(sizeof(*data));
  423. if (data != NULL) {
  424. data->page = pstat->page;
  425. data->order = pstat->order;
  426. data->gfp_flags = pstat->gfp_flags;
  427. data->migrate_type = pstat->migrate_type;
  428. rb_link_node(&data->node, parent, node);
  429. rb_insert_color(&data->node, &page_alloc_tree);
  430. }
  431. return data;
  432. }
  433. static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
  434. {
  435. return __page_stat__findnew_alloc(pstat, false);
  436. }
  437. static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
  438. {
  439. return __page_stat__findnew_alloc(pstat, true);
  440. }
  441. static struct page_stat *
  442. __page_stat__findnew_caller(struct page_stat *pstat, bool create)
  443. {
  444. struct rb_node **node = &page_caller_tree.rb_node;
  445. struct rb_node *parent = NULL;
  446. struct page_stat *data;
  447. struct sort_dimension *sort;
  448. while (*node) {
  449. int cmp = 0;
  450. parent = *node;
  451. data = rb_entry(*node, struct page_stat, node);
  452. list_for_each_entry(sort, &page_caller_sort_input, list) {
  453. cmp = sort->cmp(pstat, data);
  454. if (cmp)
  455. break;
  456. }
  457. if (cmp < 0)
  458. node = &parent->rb_left;
  459. else if (cmp > 0)
  460. node = &parent->rb_right;
  461. else
  462. return data;
  463. }
  464. if (!create)
  465. return NULL;
  466. data = zalloc(sizeof(*data));
  467. if (data != NULL) {
  468. data->callsite = pstat->callsite;
  469. data->order = pstat->order;
  470. data->gfp_flags = pstat->gfp_flags;
  471. data->migrate_type = pstat->migrate_type;
  472. rb_link_node(&data->node, parent, node);
  473. rb_insert_color(&data->node, &page_caller_tree);
  474. }
  475. return data;
  476. }
  477. static struct page_stat *page_stat__find_caller(struct page_stat *pstat)
  478. {
  479. return __page_stat__findnew_caller(pstat, false);
  480. }
  481. static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat)
  482. {
  483. return __page_stat__findnew_caller(pstat, true);
  484. }
  485. static bool valid_page(u64 pfn_or_page)
  486. {
  487. if (use_pfn && pfn_or_page == -1UL)
  488. return false;
  489. if (!use_pfn && pfn_or_page == 0)
  490. return false;
  491. return true;
  492. }
  493. struct gfp_flag {
  494. unsigned int flags;
  495. char *compact_str;
  496. char *human_readable;
  497. };
  498. static struct gfp_flag *gfps;
  499. static int nr_gfps;
  500. static int gfpcmp(const void *a, const void *b)
  501. {
  502. const struct gfp_flag *fa = a;
  503. const struct gfp_flag *fb = b;
  504. return fa->flags - fb->flags;
  505. }
  506. /* see include/trace/events/mmflags.h */
  507. static const struct {
  508. const char *original;
  509. const char *compact;
  510. } gfp_compact_table[] = {
  511. { "GFP_TRANSHUGE", "THP" },
  512. { "GFP_TRANSHUGE_LIGHT", "THL" },
  513. { "GFP_HIGHUSER_MOVABLE", "HUM" },
  514. { "GFP_HIGHUSER", "HU" },
  515. { "GFP_USER", "U" },
  516. { "GFP_KERNEL_ACCOUNT", "KAC" },
  517. { "GFP_KERNEL", "K" },
  518. { "GFP_NOFS", "NF" },
  519. { "GFP_ATOMIC", "A" },
  520. { "GFP_NOIO", "NI" },
  521. { "GFP_NOWAIT", "NW" },
  522. { "GFP_DMA", "D" },
  523. { "__GFP_HIGHMEM", "HM" },
  524. { "GFP_DMA32", "D32" },
  525. { "__GFP_HIGH", "H" },
  526. { "__GFP_ATOMIC", "_A" },
  527. { "__GFP_IO", "I" },
  528. { "__GFP_FS", "F" },
  529. { "__GFP_NOWARN", "NWR" },
  530. { "__GFP_RETRY_MAYFAIL", "R" },
  531. { "__GFP_NOFAIL", "NF" },
  532. { "__GFP_NORETRY", "NR" },
  533. { "__GFP_COMP", "C" },
  534. { "__GFP_ZERO", "Z" },
  535. { "__GFP_NOMEMALLOC", "NMA" },
  536. { "__GFP_MEMALLOC", "MA" },
  537. { "__GFP_HARDWALL", "HW" },
  538. { "__GFP_THISNODE", "TN" },
  539. { "__GFP_RECLAIMABLE", "RC" },
  540. { "__GFP_MOVABLE", "M" },
  541. { "__GFP_ACCOUNT", "AC" },
  542. { "__GFP_WRITE", "WR" },
  543. { "__GFP_RECLAIM", "R" },
  544. { "__GFP_DIRECT_RECLAIM", "DR" },
  545. { "__GFP_KSWAPD_RECLAIM", "KR" },
  546. };
  547. static size_t max_gfp_len;
  548. static char *compact_gfp_flags(char *gfp_flags)
  549. {
  550. char *orig_flags = strdup(gfp_flags);
  551. char *new_flags = NULL;
  552. char *str, *pos = NULL;
  553. size_t len = 0;
  554. if (orig_flags == NULL)
  555. return NULL;
  556. str = strtok_r(orig_flags, "|", &pos);
  557. while (str) {
  558. size_t i;
  559. char *new;
  560. const char *cpt;
  561. for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
  562. if (strcmp(gfp_compact_table[i].original, str))
  563. continue;
  564. cpt = gfp_compact_table[i].compact;
  565. new = realloc(new_flags, len + strlen(cpt) + 2);
  566. if (new == NULL) {
  567. free(new_flags);
  568. free(orig_flags);
  569. return NULL;
  570. }
  571. new_flags = new;
  572. if (!len) {
  573. strcpy(new_flags, cpt);
  574. } else {
  575. strcat(new_flags, "|");
  576. strcat(new_flags, cpt);
  577. len++;
  578. }
  579. len += strlen(cpt);
  580. }
  581. str = strtok_r(NULL, "|", &pos);
  582. }
  583. if (max_gfp_len < len)
  584. max_gfp_len = len;
  585. free(orig_flags);
  586. return new_flags;
  587. }
  588. static char *compact_gfp_string(unsigned long gfp_flags)
  589. {
  590. struct gfp_flag key = {
  591. .flags = gfp_flags,
  592. };
  593. struct gfp_flag *gfp;
  594. gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp);
  595. if (gfp)
  596. return gfp->compact_str;
  597. return NULL;
  598. }
  599. static int parse_gfp_flags(struct evsel *evsel, struct perf_sample *sample,
  600. unsigned int gfp_flags)
  601. {
  602. struct tep_record record = {
  603. .cpu = sample->cpu,
  604. .data = sample->raw_data,
  605. .size = sample->raw_size,
  606. };
  607. struct trace_seq seq;
  608. char *str, *pos = NULL;
  609. if (nr_gfps) {
  610. struct gfp_flag key = {
  611. .flags = gfp_flags,
  612. };
  613. if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp))
  614. return 0;
  615. }
  616. trace_seq_init(&seq);
  617. tep_print_event(evsel->tp_format->tep,
  618. &seq, &record, "%s", TEP_PRINT_INFO);
  619. str = strtok_r(seq.buffer, " ", &pos);
  620. while (str) {
  621. if (!strncmp(str, "gfp_flags=", 10)) {
  622. struct gfp_flag *new;
  623. new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps));
  624. if (new == NULL)
  625. return -ENOMEM;
  626. gfps = new;
  627. new += nr_gfps++;
  628. new->flags = gfp_flags;
  629. new->human_readable = strdup(str + 10);
  630. new->compact_str = compact_gfp_flags(str + 10);
  631. if (!new->human_readable || !new->compact_str)
  632. return -ENOMEM;
  633. qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp);
  634. }
  635. str = strtok_r(NULL, " ", &pos);
  636. }
  637. trace_seq_destroy(&seq);
  638. return 0;
  639. }
  640. static int perf_evsel__process_page_alloc_event(struct evsel *evsel,
  641. struct perf_sample *sample)
  642. {
  643. u64 page;
  644. unsigned int order = perf_evsel__intval(evsel, sample, "order");
  645. unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
  646. unsigned int migrate_type = perf_evsel__intval(evsel, sample,
  647. "migratetype");
  648. u64 bytes = kmem_page_size << order;
  649. u64 callsite;
  650. struct page_stat *pstat;
  651. struct page_stat this = {
  652. .order = order,
  653. .gfp_flags = gfp_flags,
  654. .migrate_type = migrate_type,
  655. };
  656. if (use_pfn)
  657. page = perf_evsel__intval(evsel, sample, "pfn");
  658. else
  659. page = perf_evsel__intval(evsel, sample, "page");
  660. nr_page_allocs++;
  661. total_page_alloc_bytes += bytes;
  662. if (!valid_page(page)) {
  663. nr_page_fails++;
  664. total_page_fail_bytes += bytes;
  665. return 0;
  666. }
  667. if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
  668. return -1;
  669. callsite = find_callsite(evsel, sample);
  670. /*
  671. * This is to find the current page (with correct gfp flags and
  672. * migrate type) at free event.
  673. */
  674. this.page = page;
  675. pstat = page_stat__findnew_page(&this);
  676. if (pstat == NULL)
  677. return -ENOMEM;
  678. pstat->nr_alloc++;
  679. pstat->alloc_bytes += bytes;
  680. pstat->callsite = callsite;
  681. if (!live_page) {
  682. pstat = page_stat__findnew_alloc(&this);
  683. if (pstat == NULL)
  684. return -ENOMEM;
  685. pstat->nr_alloc++;
  686. pstat->alloc_bytes += bytes;
  687. pstat->callsite = callsite;
  688. }
  689. this.callsite = callsite;
  690. pstat = page_stat__findnew_caller(&this);
  691. if (pstat == NULL)
  692. return -ENOMEM;
  693. pstat->nr_alloc++;
  694. pstat->alloc_bytes += bytes;
  695. order_stats[order][migrate_type]++;
  696. return 0;
  697. }
  698. static int perf_evsel__process_page_free_event(struct evsel *evsel,
  699. struct perf_sample *sample)
  700. {
  701. u64 page;
  702. unsigned int order = perf_evsel__intval(evsel, sample, "order");
  703. u64 bytes = kmem_page_size << order;
  704. struct page_stat *pstat;
  705. struct page_stat this = {
  706. .order = order,
  707. };
  708. if (use_pfn)
  709. page = perf_evsel__intval(evsel, sample, "pfn");
  710. else
  711. page = perf_evsel__intval(evsel, sample, "page");
  712. nr_page_frees++;
  713. total_page_free_bytes += bytes;
  714. this.page = page;
  715. pstat = page_stat__find_page(&this);
  716. if (pstat == NULL) {
  717. pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
  718. page, order);
  719. nr_page_nomatch++;
  720. total_page_nomatch_bytes += bytes;
  721. return 0;
  722. }
  723. this.gfp_flags = pstat->gfp_flags;
  724. this.migrate_type = pstat->migrate_type;
  725. this.callsite = pstat->callsite;
  726. rb_erase(&pstat->node, &page_live_tree);
  727. free(pstat);
  728. if (live_page) {
  729. order_stats[this.order][this.migrate_type]--;
  730. } else {
  731. pstat = page_stat__find_alloc(&this);
  732. if (pstat == NULL)
  733. return -ENOMEM;
  734. pstat->nr_free++;
  735. pstat->free_bytes += bytes;
  736. }
  737. pstat = page_stat__find_caller(&this);
  738. if (pstat == NULL)
  739. return -ENOENT;
  740. pstat->nr_free++;
  741. pstat->free_bytes += bytes;
  742. if (live_page) {
  743. pstat->nr_alloc--;
  744. pstat->alloc_bytes -= bytes;
  745. if (pstat->nr_alloc == 0) {
  746. rb_erase(&pstat->node, &page_caller_tree);
  747. free(pstat);
  748. }
  749. }
  750. return 0;
  751. }
  752. static bool perf_kmem__skip_sample(struct perf_sample *sample)
  753. {
  754. /* skip sample based on time? */
  755. if (perf_time__skip_sample(&ptime, sample->time))
  756. return true;
  757. return false;
  758. }
  759. typedef int (*tracepoint_handler)(struct evsel *evsel,
  760. struct perf_sample *sample);
  761. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  762. union perf_event *event,
  763. struct perf_sample *sample,
  764. struct evsel *evsel,
  765. struct machine *machine)
  766. {
  767. int err = 0;
  768. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  769. sample->tid);
  770. if (thread == NULL) {
  771. pr_debug("problem processing %d event, skipping it.\n",
  772. event->header.type);
  773. return -1;
  774. }
  775. if (perf_kmem__skip_sample(sample))
  776. return 0;
  777. dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
  778. if (evsel->handler != NULL) {
  779. tracepoint_handler f = evsel->handler;
  780. err = f(evsel, sample);
  781. }
  782. thread__put(thread);
  783. return err;
  784. }
  785. static struct perf_tool perf_kmem = {
  786. .sample = process_sample_event,
  787. .comm = perf_event__process_comm,
  788. .mmap = perf_event__process_mmap,
  789. .mmap2 = perf_event__process_mmap2,
  790. .namespaces = perf_event__process_namespaces,
  791. .ordered_events = true,
  792. };
  793. static double fragmentation(unsigned long n_req, unsigned long n_alloc)
  794. {
  795. if (n_alloc == 0)
  796. return 0.0;
  797. else
  798. return 100.0 - (100.0 * n_req / n_alloc);
  799. }
  800. static void __print_slab_result(struct rb_root *root,
  801. struct perf_session *session,
  802. int n_lines, int is_caller)
  803. {
  804. struct rb_node *next;
  805. struct machine *machine = &session->machines.host;
  806. printf("%.105s\n", graph_dotted_line);
  807. printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
  808. printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
  809. printf("%.105s\n", graph_dotted_line);
  810. next = rb_first(root);
  811. while (next && n_lines--) {
  812. struct alloc_stat *data = rb_entry(next, struct alloc_stat,
  813. node);
  814. struct symbol *sym = NULL;
  815. struct map *map;
  816. char buf[BUFSIZ];
  817. u64 addr;
  818. if (is_caller) {
  819. addr = data->call_site;
  820. if (!raw_ip)
  821. sym = machine__find_kernel_symbol(machine, addr, &map);
  822. } else
  823. addr = data->ptr;
  824. if (sym != NULL)
  825. snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
  826. addr - map->unmap_ip(map, sym->start));
  827. else
  828. snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
  829. printf(" %-34s |", buf);
  830. printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
  831. (unsigned long long)data->bytes_alloc,
  832. (unsigned long)data->bytes_alloc / data->hit,
  833. (unsigned long long)data->bytes_req,
  834. (unsigned long)data->bytes_req / data->hit,
  835. (unsigned long)data->hit,
  836. (unsigned long)data->pingpong,
  837. fragmentation(data->bytes_req, data->bytes_alloc));
  838. next = rb_next(next);
  839. }
  840. if (n_lines == -1)
  841. printf(" ... | ... | ... | ... | ... | ... \n");
  842. printf("%.105s\n", graph_dotted_line);
  843. }
  844. static const char * const migrate_type_str[] = {
  845. "UNMOVABL",
  846. "RECLAIM",
  847. "MOVABLE",
  848. "RESERVED",
  849. "CMA/ISLT",
  850. "UNKNOWN",
  851. };
  852. static void __print_page_alloc_result(struct perf_session *session, int n_lines)
  853. {
  854. struct rb_node *next = rb_first(&page_alloc_sorted);
  855. struct machine *machine = &session->machines.host;
  856. const char *format;
  857. int gfp_len = max(strlen("GFP flags"), max_gfp_len);
  858. printf("\n%.105s\n", graph_dotted_line);
  859. printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
  860. use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total",
  861. gfp_len, "GFP flags");
  862. printf("%.105s\n", graph_dotted_line);
  863. if (use_pfn)
  864. format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
  865. else
  866. format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
  867. while (next && n_lines--) {
  868. struct page_stat *data;
  869. struct symbol *sym;
  870. struct map *map;
  871. char buf[32];
  872. char *caller = buf;
  873. data = rb_entry(next, struct page_stat, node);
  874. sym = machine__find_kernel_symbol(machine, data->callsite, &map);
  875. if (sym)
  876. caller = sym->name;
  877. else
  878. scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
  879. printf(format, (unsigned long long)data->page,
  880. (unsigned long long)data->alloc_bytes / 1024,
  881. data->nr_alloc, data->order,
  882. migrate_type_str[data->migrate_type],
  883. gfp_len, compact_gfp_string(data->gfp_flags), caller);
  884. next = rb_next(next);
  885. }
  886. if (n_lines == -1) {
  887. printf(" ... | ... | ... | ... | ... | %-*s | ...\n",
  888. gfp_len, "...");
  889. }
  890. printf("%.105s\n", graph_dotted_line);
  891. }
  892. static void __print_page_caller_result(struct perf_session *session, int n_lines)
  893. {
  894. struct rb_node *next = rb_first(&page_caller_sorted);
  895. struct machine *machine = &session->machines.host;
  896. int gfp_len = max(strlen("GFP flags"), max_gfp_len);
  897. printf("\n%.105s\n", graph_dotted_line);
  898. printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
  899. live_page ? "Live" : "Total", gfp_len, "GFP flags");
  900. printf("%.105s\n", graph_dotted_line);
  901. while (next && n_lines--) {
  902. struct page_stat *data;
  903. struct symbol *sym;
  904. struct map *map;
  905. char buf[32];
  906. char *caller = buf;
  907. data = rb_entry(next, struct page_stat, node);
  908. sym = machine__find_kernel_symbol(machine, data->callsite, &map);
  909. if (sym)
  910. caller = sym->name;
  911. else
  912. scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
  913. printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n",
  914. (unsigned long long)data->alloc_bytes / 1024,
  915. data->nr_alloc, data->order,
  916. migrate_type_str[data->migrate_type],
  917. gfp_len, compact_gfp_string(data->gfp_flags), caller);
  918. next = rb_next(next);
  919. }
  920. if (n_lines == -1) {
  921. printf(" ... | ... | ... | ... | %-*s | ...\n",
  922. gfp_len, "...");
  923. }
  924. printf("%.105s\n", graph_dotted_line);
  925. }
  926. static void print_gfp_flags(void)
  927. {
  928. int i;
  929. printf("#\n");
  930. printf("# GFP flags\n");
  931. printf("# ---------\n");
  932. for (i = 0; i < nr_gfps; i++) {
  933. printf("# %08x: %*s: %s\n", gfps[i].flags,
  934. (int) max_gfp_len, gfps[i].compact_str,
  935. gfps[i].human_readable);
  936. }
  937. }
  938. static void print_slab_summary(void)
  939. {
  940. printf("\nSUMMARY (SLAB allocator)");
  941. printf("\n========================\n");
  942. printf("Total bytes requested: %'lu\n", total_requested);
  943. printf("Total bytes allocated: %'lu\n", total_allocated);
  944. printf("Total bytes freed: %'lu\n", total_freed);
  945. if (total_allocated > total_freed) {
  946. printf("Net total bytes allocated: %'lu\n",
  947. total_allocated - total_freed);
  948. }
  949. printf("Total bytes wasted on internal fragmentation: %'lu\n",
  950. total_allocated - total_requested);
  951. printf("Internal fragmentation: %f%%\n",
  952. fragmentation(total_requested, total_allocated));
  953. printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
  954. }
  955. static void print_page_summary(void)
  956. {
  957. int o, m;
  958. u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
  959. u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
  960. printf("\nSUMMARY (page allocator)");
  961. printf("\n========================\n");
  962. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests",
  963. nr_page_allocs, total_page_alloc_bytes / 1024);
  964. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests",
  965. nr_page_frees, total_page_free_bytes / 1024);
  966. printf("\n");
  967. printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
  968. nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
  969. printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
  970. nr_page_allocs - nr_alloc_freed,
  971. (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
  972. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests",
  973. nr_page_nomatch, total_page_nomatch_bytes / 1024);
  974. printf("\n");
  975. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures",
  976. nr_page_fails, total_page_fail_bytes / 1024);
  977. printf("\n");
  978. printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable",
  979. "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
  980. printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line,
  981. graph_dotted_line, graph_dotted_line, graph_dotted_line,
  982. graph_dotted_line, graph_dotted_line);
  983. for (o = 0; o < MAX_PAGE_ORDER; o++) {
  984. printf("%5d", o);
  985. for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
  986. if (order_stats[o][m])
  987. printf(" %'12d", order_stats[o][m]);
  988. else
  989. printf(" %12c", '.');
  990. }
  991. printf("\n");
  992. }
  993. }
  994. static void print_slab_result(struct perf_session *session)
  995. {
  996. if (caller_flag)
  997. __print_slab_result(&root_caller_sorted, session, caller_lines, 1);
  998. if (alloc_flag)
  999. __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
  1000. print_slab_summary();
  1001. }
  1002. static void print_page_result(struct perf_session *session)
  1003. {
  1004. if (caller_flag || alloc_flag)
  1005. print_gfp_flags();
  1006. if (caller_flag)
  1007. __print_page_caller_result(session, caller_lines);
  1008. if (alloc_flag)
  1009. __print_page_alloc_result(session, alloc_lines);
  1010. print_page_summary();
  1011. }
  1012. static void print_result(struct perf_session *session)
  1013. {
  1014. if (kmem_slab)
  1015. print_slab_result(session);
  1016. if (kmem_page)
  1017. print_page_result(session);
  1018. }
  1019. static LIST_HEAD(slab_caller_sort);
  1020. static LIST_HEAD(slab_alloc_sort);
  1021. static LIST_HEAD(page_caller_sort);
  1022. static LIST_HEAD(page_alloc_sort);
  1023. static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
  1024. struct list_head *sort_list)
  1025. {
  1026. struct rb_node **new = &(root->rb_node);
  1027. struct rb_node *parent = NULL;
  1028. struct sort_dimension *sort;
  1029. while (*new) {
  1030. struct alloc_stat *this;
  1031. int cmp = 0;
  1032. this = rb_entry(*new, struct alloc_stat, node);
  1033. parent = *new;
  1034. list_for_each_entry(sort, sort_list, list) {
  1035. cmp = sort->cmp(data, this);
  1036. if (cmp)
  1037. break;
  1038. }
  1039. if (cmp > 0)
  1040. new = &((*new)->rb_left);
  1041. else
  1042. new = &((*new)->rb_right);
  1043. }
  1044. rb_link_node(&data->node, parent, new);
  1045. rb_insert_color(&data->node, root);
  1046. }
  1047. static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
  1048. struct list_head *sort_list)
  1049. {
  1050. struct rb_node *node;
  1051. struct alloc_stat *data;
  1052. for (;;) {
  1053. node = rb_first(root);
  1054. if (!node)
  1055. break;
  1056. rb_erase(node, root);
  1057. data = rb_entry(node, struct alloc_stat, node);
  1058. sort_slab_insert(root_sorted, data, sort_list);
  1059. }
  1060. }
  1061. static void sort_page_insert(struct rb_root *root, struct page_stat *data,
  1062. struct list_head *sort_list)
  1063. {
  1064. struct rb_node **new = &root->rb_node;
  1065. struct rb_node *parent = NULL;
  1066. struct sort_dimension *sort;
  1067. while (*new) {
  1068. struct page_stat *this;
  1069. int cmp = 0;
  1070. this = rb_entry(*new, struct page_stat, node);
  1071. parent = *new;
  1072. list_for_each_entry(sort, sort_list, list) {
  1073. cmp = sort->cmp(data, this);
  1074. if (cmp)
  1075. break;
  1076. }
  1077. if (cmp > 0)
  1078. new = &parent->rb_left;
  1079. else
  1080. new = &parent->rb_right;
  1081. }
  1082. rb_link_node(&data->node, parent, new);
  1083. rb_insert_color(&data->node, root);
  1084. }
  1085. static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted,
  1086. struct list_head *sort_list)
  1087. {
  1088. struct rb_node *node;
  1089. struct page_stat *data;
  1090. for (;;) {
  1091. node = rb_first(root);
  1092. if (!node)
  1093. break;
  1094. rb_erase(node, root);
  1095. data = rb_entry(node, struct page_stat, node);
  1096. sort_page_insert(root_sorted, data, sort_list);
  1097. }
  1098. }
  1099. static void sort_result(void)
  1100. {
  1101. if (kmem_slab) {
  1102. __sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
  1103. &slab_alloc_sort);
  1104. __sort_slab_result(&root_caller_stat, &root_caller_sorted,
  1105. &slab_caller_sort);
  1106. }
  1107. if (kmem_page) {
  1108. if (live_page)
  1109. __sort_page_result(&page_live_tree, &page_alloc_sorted,
  1110. &page_alloc_sort);
  1111. else
  1112. __sort_page_result(&page_alloc_tree, &page_alloc_sorted,
  1113. &page_alloc_sort);
  1114. __sort_page_result(&page_caller_tree, &page_caller_sorted,
  1115. &page_caller_sort);
  1116. }
  1117. }
  1118. static int __cmd_kmem(struct perf_session *session)
  1119. {
  1120. int err = -EINVAL;
  1121. struct evsel *evsel;
  1122. const struct evsel_str_handler kmem_tracepoints[] = {
  1123. /* slab allocator */
  1124. { "kmem:kmalloc", perf_evsel__process_alloc_event, },
  1125. { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
  1126. { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
  1127. { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
  1128. { "kmem:kfree", perf_evsel__process_free_event, },
  1129. { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
  1130. /* page allocator */
  1131. { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, },
  1132. { "kmem:mm_page_free", perf_evsel__process_page_free_event, },
  1133. };
  1134. if (!perf_session__has_traces(session, "kmem record"))
  1135. goto out;
  1136. if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
  1137. pr_err("Initializing perf session tracepoint handlers failed\n");
  1138. goto out;
  1139. }
  1140. evlist__for_each_entry(session->evlist, evsel) {
  1141. if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") &&
  1142. perf_evsel__field(evsel, "pfn")) {
  1143. use_pfn = true;
  1144. break;
  1145. }
  1146. }
  1147. setup_pager();
  1148. err = perf_session__process_events(session);
  1149. if (err != 0) {
  1150. pr_err("error during process events: %d\n", err);
  1151. goto out;
  1152. }
  1153. sort_result();
  1154. print_result(session);
  1155. out:
  1156. return err;
  1157. }
  1158. /* slab sort keys */
  1159. static int ptr_cmp(void *a, void *b)
  1160. {
  1161. struct alloc_stat *l = a;
  1162. struct alloc_stat *r = b;
  1163. if (l->ptr < r->ptr)
  1164. return -1;
  1165. else if (l->ptr > r->ptr)
  1166. return 1;
  1167. return 0;
  1168. }
  1169. static struct sort_dimension ptr_sort_dimension = {
  1170. .name = "ptr",
  1171. .cmp = ptr_cmp,
  1172. };
  1173. static int slab_callsite_cmp(void *a, void *b)
  1174. {
  1175. struct alloc_stat *l = a;
  1176. struct alloc_stat *r = b;
  1177. if (l->call_site < r->call_site)
  1178. return -1;
  1179. else if (l->call_site > r->call_site)
  1180. return 1;
  1181. return 0;
  1182. }
  1183. static struct sort_dimension callsite_sort_dimension = {
  1184. .name = "callsite",
  1185. .cmp = slab_callsite_cmp,
  1186. };
  1187. static int hit_cmp(void *a, void *b)
  1188. {
  1189. struct alloc_stat *l = a;
  1190. struct alloc_stat *r = b;
  1191. if (l->hit < r->hit)
  1192. return -1;
  1193. else if (l->hit > r->hit)
  1194. return 1;
  1195. return 0;
  1196. }
  1197. static struct sort_dimension hit_sort_dimension = {
  1198. .name = "hit",
  1199. .cmp = hit_cmp,
  1200. };
  1201. static int bytes_cmp(void *a, void *b)
  1202. {
  1203. struct alloc_stat *l = a;
  1204. struct alloc_stat *r = b;
  1205. if (l->bytes_alloc < r->bytes_alloc)
  1206. return -1;
  1207. else if (l->bytes_alloc > r->bytes_alloc)
  1208. return 1;
  1209. return 0;
  1210. }
  1211. static struct sort_dimension bytes_sort_dimension = {
  1212. .name = "bytes",
  1213. .cmp = bytes_cmp,
  1214. };
  1215. static int frag_cmp(void *a, void *b)
  1216. {
  1217. double x, y;
  1218. struct alloc_stat *l = a;
  1219. struct alloc_stat *r = b;
  1220. x = fragmentation(l->bytes_req, l->bytes_alloc);
  1221. y = fragmentation(r->bytes_req, r->bytes_alloc);
  1222. if (x < y)
  1223. return -1;
  1224. else if (x > y)
  1225. return 1;
  1226. return 0;
  1227. }
  1228. static struct sort_dimension frag_sort_dimension = {
  1229. .name = "frag",
  1230. .cmp = frag_cmp,
  1231. };
  1232. static int pingpong_cmp(void *a, void *b)
  1233. {
  1234. struct alloc_stat *l = a;
  1235. struct alloc_stat *r = b;
  1236. if (l->pingpong < r->pingpong)
  1237. return -1;
  1238. else if (l->pingpong > r->pingpong)
  1239. return 1;
  1240. return 0;
  1241. }
  1242. static struct sort_dimension pingpong_sort_dimension = {
  1243. .name = "pingpong",
  1244. .cmp = pingpong_cmp,
  1245. };
  1246. /* page sort keys */
  1247. static int page_cmp(void *a, void *b)
  1248. {
  1249. struct page_stat *l = a;
  1250. struct page_stat *r = b;
  1251. if (l->page < r->page)
  1252. return -1;
  1253. else if (l->page > r->page)
  1254. return 1;
  1255. return 0;
  1256. }
  1257. static struct sort_dimension page_sort_dimension = {
  1258. .name = "page",
  1259. .cmp = page_cmp,
  1260. };
  1261. static int page_callsite_cmp(void *a, void *b)
  1262. {
  1263. struct page_stat *l = a;
  1264. struct page_stat *r = b;
  1265. if (l->callsite < r->callsite)
  1266. return -1;
  1267. else if (l->callsite > r->callsite)
  1268. return 1;
  1269. return 0;
  1270. }
  1271. static struct sort_dimension page_callsite_sort_dimension = {
  1272. .name = "callsite",
  1273. .cmp = page_callsite_cmp,
  1274. };
  1275. static int page_hit_cmp(void *a, void *b)
  1276. {
  1277. struct page_stat *l = a;
  1278. struct page_stat *r = b;
  1279. if (l->nr_alloc < r->nr_alloc)
  1280. return -1;
  1281. else if (l->nr_alloc > r->nr_alloc)
  1282. return 1;
  1283. return 0;
  1284. }
  1285. static struct sort_dimension page_hit_sort_dimension = {
  1286. .name = "hit",
  1287. .cmp = page_hit_cmp,
  1288. };
  1289. static int page_bytes_cmp(void *a, void *b)
  1290. {
  1291. struct page_stat *l = a;
  1292. struct page_stat *r = b;
  1293. if (l->alloc_bytes < r->alloc_bytes)
  1294. return -1;
  1295. else if (l->alloc_bytes > r->alloc_bytes)
  1296. return 1;
  1297. return 0;
  1298. }
  1299. static struct sort_dimension page_bytes_sort_dimension = {
  1300. .name = "bytes",
  1301. .cmp = page_bytes_cmp,
  1302. };
  1303. static int page_order_cmp(void *a, void *b)
  1304. {
  1305. struct page_stat *l = a;
  1306. struct page_stat *r = b;
  1307. if (l->order < r->order)
  1308. return -1;
  1309. else if (l->order > r->order)
  1310. return 1;
  1311. return 0;
  1312. }
  1313. static struct sort_dimension page_order_sort_dimension = {
  1314. .name = "order",
  1315. .cmp = page_order_cmp,
  1316. };
  1317. static int migrate_type_cmp(void *a, void *b)
  1318. {
  1319. struct page_stat *l = a;
  1320. struct page_stat *r = b;
  1321. /* for internal use to find free'd page */
  1322. if (l->migrate_type == -1U)
  1323. return 0;
  1324. if (l->migrate_type < r->migrate_type)
  1325. return -1;
  1326. else if (l->migrate_type > r->migrate_type)
  1327. return 1;
  1328. return 0;
  1329. }
  1330. static struct sort_dimension migrate_type_sort_dimension = {
  1331. .name = "migtype",
  1332. .cmp = migrate_type_cmp,
  1333. };
  1334. static int gfp_flags_cmp(void *a, void *b)
  1335. {
  1336. struct page_stat *l = a;
  1337. struct page_stat *r = b;
  1338. /* for internal use to find free'd page */
  1339. if (l->gfp_flags == -1U)
  1340. return 0;
  1341. if (l->gfp_flags < r->gfp_flags)
  1342. return -1;
  1343. else if (l->gfp_flags > r->gfp_flags)
  1344. return 1;
  1345. return 0;
  1346. }
  1347. static struct sort_dimension gfp_flags_sort_dimension = {
  1348. .name = "gfp",
  1349. .cmp = gfp_flags_cmp,
  1350. };
  1351. static struct sort_dimension *slab_sorts[] = {
  1352. &ptr_sort_dimension,
  1353. &callsite_sort_dimension,
  1354. &hit_sort_dimension,
  1355. &bytes_sort_dimension,
  1356. &frag_sort_dimension,
  1357. &pingpong_sort_dimension,
  1358. };
  1359. static struct sort_dimension *page_sorts[] = {
  1360. &page_sort_dimension,
  1361. &page_callsite_sort_dimension,
  1362. &page_hit_sort_dimension,
  1363. &page_bytes_sort_dimension,
  1364. &page_order_sort_dimension,
  1365. &migrate_type_sort_dimension,
  1366. &gfp_flags_sort_dimension,
  1367. };
  1368. static int slab_sort_dimension__add(const char *tok, struct list_head *list)
  1369. {
  1370. struct sort_dimension *sort;
  1371. int i;
  1372. for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) {
  1373. if (!strcmp(slab_sorts[i]->name, tok)) {
  1374. sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i]));
  1375. if (!sort) {
  1376. pr_err("%s: memdup failed\n", __func__);
  1377. return -1;
  1378. }
  1379. list_add_tail(&sort->list, list);
  1380. return 0;
  1381. }
  1382. }
  1383. return -1;
  1384. }
  1385. static int page_sort_dimension__add(const char *tok, struct list_head *list)
  1386. {
  1387. struct sort_dimension *sort;
  1388. int i;
  1389. for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) {
  1390. if (!strcmp(page_sorts[i]->name, tok)) {
  1391. sort = memdup(page_sorts[i], sizeof(*page_sorts[i]));
  1392. if (!sort) {
  1393. pr_err("%s: memdup failed\n", __func__);
  1394. return -1;
  1395. }
  1396. list_add_tail(&sort->list, list);
  1397. return 0;
  1398. }
  1399. }
  1400. return -1;
  1401. }
  1402. static int setup_slab_sorting(struct list_head *sort_list, const char *arg)
  1403. {
  1404. char *tok;
  1405. char *str = strdup(arg);
  1406. char *pos = str;
  1407. if (!str) {
  1408. pr_err("%s: strdup failed\n", __func__);
  1409. return -1;
  1410. }
  1411. while (true) {
  1412. tok = strsep(&pos, ",");
  1413. if (!tok)
  1414. break;
  1415. if (slab_sort_dimension__add(tok, sort_list) < 0) {
  1416. pr_err("Unknown slab --sort key: '%s'", tok);
  1417. free(str);
  1418. return -1;
  1419. }
  1420. }
  1421. free(str);
  1422. return 0;
  1423. }
  1424. static int setup_page_sorting(struct list_head *sort_list, const char *arg)
  1425. {
  1426. char *tok;
  1427. char *str = strdup(arg);
  1428. char *pos = str;
  1429. if (!str) {
  1430. pr_err("%s: strdup failed\n", __func__);
  1431. return -1;
  1432. }
  1433. while (true) {
  1434. tok = strsep(&pos, ",");
  1435. if (!tok)
  1436. break;
  1437. if (page_sort_dimension__add(tok, sort_list) < 0) {
  1438. pr_err("Unknown page --sort key: '%s'", tok);
  1439. free(str);
  1440. return -1;
  1441. }
  1442. }
  1443. free(str);
  1444. return 0;
  1445. }
  1446. static int parse_sort_opt(const struct option *opt __maybe_unused,
  1447. const char *arg, int unset __maybe_unused)
  1448. {
  1449. if (!arg)
  1450. return -1;
  1451. if (kmem_page > kmem_slab ||
  1452. (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) {
  1453. if (caller_flag > alloc_flag)
  1454. return setup_page_sorting(&page_caller_sort, arg);
  1455. else
  1456. return setup_page_sorting(&page_alloc_sort, arg);
  1457. } else {
  1458. if (caller_flag > alloc_flag)
  1459. return setup_slab_sorting(&slab_caller_sort, arg);
  1460. else
  1461. return setup_slab_sorting(&slab_alloc_sort, arg);
  1462. }
  1463. return 0;
  1464. }
  1465. static int parse_caller_opt(const struct option *opt __maybe_unused,
  1466. const char *arg __maybe_unused,
  1467. int unset __maybe_unused)
  1468. {
  1469. caller_flag = (alloc_flag + 1);
  1470. return 0;
  1471. }
  1472. static int parse_alloc_opt(const struct option *opt __maybe_unused,
  1473. const char *arg __maybe_unused,
  1474. int unset __maybe_unused)
  1475. {
  1476. alloc_flag = (caller_flag + 1);
  1477. return 0;
  1478. }
  1479. static int parse_slab_opt(const struct option *opt __maybe_unused,
  1480. const char *arg __maybe_unused,
  1481. int unset __maybe_unused)
  1482. {
  1483. kmem_slab = (kmem_page + 1);
  1484. return 0;
  1485. }
  1486. static int parse_page_opt(const struct option *opt __maybe_unused,
  1487. const char *arg __maybe_unused,
  1488. int unset __maybe_unused)
  1489. {
  1490. kmem_page = (kmem_slab + 1);
  1491. return 0;
  1492. }
  1493. static int parse_line_opt(const struct option *opt __maybe_unused,
  1494. const char *arg, int unset __maybe_unused)
  1495. {
  1496. int lines;
  1497. if (!arg)
  1498. return -1;
  1499. lines = strtoul(arg, NULL, 10);
  1500. if (caller_flag > alloc_flag)
  1501. caller_lines = lines;
  1502. else
  1503. alloc_lines = lines;
  1504. return 0;
  1505. }
  1506. static int __cmd_record(int argc, const char **argv)
  1507. {
  1508. const char * const record_args[] = {
  1509. "record", "-a", "-R", "-c", "1",
  1510. };
  1511. const char * const slab_events[] = {
  1512. "-e", "kmem:kmalloc",
  1513. "-e", "kmem:kmalloc_node",
  1514. "-e", "kmem:kfree",
  1515. "-e", "kmem:kmem_cache_alloc",
  1516. "-e", "kmem:kmem_cache_alloc_node",
  1517. "-e", "kmem:kmem_cache_free",
  1518. };
  1519. const char * const page_events[] = {
  1520. "-e", "kmem:mm_page_alloc",
  1521. "-e", "kmem:mm_page_free",
  1522. };
  1523. unsigned int rec_argc, i, j;
  1524. const char **rec_argv;
  1525. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  1526. if (kmem_slab)
  1527. rec_argc += ARRAY_SIZE(slab_events);
  1528. if (kmem_page)
  1529. rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
  1530. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1531. if (rec_argv == NULL)
  1532. return -ENOMEM;
  1533. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1534. rec_argv[i] = strdup(record_args[i]);
  1535. if (kmem_slab) {
  1536. for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
  1537. rec_argv[i] = strdup(slab_events[j]);
  1538. }
  1539. if (kmem_page) {
  1540. rec_argv[i++] = strdup("-g");
  1541. for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
  1542. rec_argv[i] = strdup(page_events[j]);
  1543. }
  1544. for (j = 1; j < (unsigned int)argc; j++, i++)
  1545. rec_argv[i] = argv[j];
  1546. return cmd_record(i, rec_argv);
  1547. }
  1548. static int kmem_config(const char *var, const char *value, void *cb __maybe_unused)
  1549. {
  1550. if (!strcmp(var, "kmem.default")) {
  1551. if (!strcmp(value, "slab"))
  1552. kmem_default = KMEM_SLAB;
  1553. else if (!strcmp(value, "page"))
  1554. kmem_default = KMEM_PAGE;
  1555. else
  1556. pr_err("invalid default value ('slab' or 'page' required): %s\n",
  1557. value);
  1558. return 0;
  1559. }
  1560. return 0;
  1561. }
  1562. int cmd_kmem(int argc, const char **argv)
  1563. {
  1564. const char * const default_slab_sort = "frag,hit,bytes";
  1565. const char * const default_page_sort = "bytes,hit";
  1566. struct perf_data data = {
  1567. .mode = PERF_DATA_MODE_READ,
  1568. };
  1569. const struct option kmem_options[] = {
  1570. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1571. OPT_INCR('v', "verbose", &verbose,
  1572. "be more verbose (show symbol address, etc)"),
  1573. OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
  1574. "show per-callsite statistics", parse_caller_opt),
  1575. OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
  1576. "show per-allocation statistics", parse_alloc_opt),
  1577. OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
  1578. "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, "
  1579. "page, order, migtype, gfp", parse_sort_opt),
  1580. OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
  1581. OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
  1582. OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
  1583. OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
  1584. parse_slab_opt),
  1585. OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
  1586. parse_page_opt),
  1587. OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
  1588. OPT_STRING(0, "time", &time_str, "str",
  1589. "Time span of interest (start,stop)"),
  1590. OPT_END()
  1591. };
  1592. const char *const kmem_subcommands[] = { "record", "stat", NULL };
  1593. const char *kmem_usage[] = {
  1594. NULL,
  1595. NULL
  1596. };
  1597. struct perf_session *session;
  1598. static const char errmsg[] = "No %s allocation events found. Have you run 'perf kmem record --%s'?\n";
  1599. int ret = perf_config(kmem_config, NULL);
  1600. if (ret)
  1601. return ret;
  1602. argc = parse_options_subcommand(argc, argv, kmem_options,
  1603. kmem_subcommands, kmem_usage, 0);
  1604. if (!argc)
  1605. usage_with_options(kmem_usage, kmem_options);
  1606. if (kmem_slab == 0 && kmem_page == 0) {
  1607. if (kmem_default == KMEM_SLAB)
  1608. kmem_slab = 1;
  1609. else
  1610. kmem_page = 1;
  1611. }
  1612. if (!strncmp(argv[0], "rec", 3)) {
  1613. symbol__init(NULL);
  1614. return __cmd_record(argc, argv);
  1615. }
  1616. data.path = input_name;
  1617. kmem_session = session = perf_session__new(&data, false, &perf_kmem);
  1618. if (IS_ERR(session))
  1619. return PTR_ERR(session);
  1620. ret = -1;
  1621. if (kmem_slab) {
  1622. if (!perf_evlist__find_tracepoint_by_name(session->evlist,
  1623. "kmem:kmalloc")) {
  1624. pr_err(errmsg, "slab", "slab");
  1625. goto out_delete;
  1626. }
  1627. }
  1628. if (kmem_page) {
  1629. struct evsel *evsel;
  1630. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  1631. "kmem:mm_page_alloc");
  1632. if (evsel == NULL) {
  1633. pr_err(errmsg, "page", "page");
  1634. goto out_delete;
  1635. }
  1636. kmem_page_size = tep_get_page_size(evsel->tp_format->tep);
  1637. symbol_conf.use_callchain = true;
  1638. }
  1639. symbol__init(&session->header.env);
  1640. if (perf_time__parse_str(&ptime, time_str) != 0) {
  1641. pr_err("Invalid time string\n");
  1642. ret = -EINVAL;
  1643. goto out_delete;
  1644. }
  1645. if (!strcmp(argv[0], "stat")) {
  1646. setlocale(LC_ALL, "");
  1647. if (cpu__setup_cpunode_map())
  1648. goto out_delete;
  1649. if (list_empty(&slab_caller_sort))
  1650. setup_slab_sorting(&slab_caller_sort, default_slab_sort);
  1651. if (list_empty(&slab_alloc_sort))
  1652. setup_slab_sorting(&slab_alloc_sort, default_slab_sort);
  1653. if (list_empty(&page_caller_sort))
  1654. setup_page_sorting(&page_caller_sort, default_page_sort);
  1655. if (list_empty(&page_alloc_sort))
  1656. setup_page_sorting(&page_alloc_sort, default_page_sort);
  1657. if (kmem_page) {
  1658. setup_page_sorting(&page_alloc_sort_input,
  1659. "page,order,migtype,gfp");
  1660. setup_page_sorting(&page_caller_sort_input,
  1661. "callsite,order,migtype,gfp");
  1662. }
  1663. ret = __cmd_kmem(session);
  1664. } else
  1665. usage_with_options(kmem_usage, kmem_options);
  1666. out_delete:
  1667. perf_session__delete(session);
  1668. return ret;
  1669. }