builtin-kmem.c 44 KB

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