builtin-kmem.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  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/config.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 <subcmd/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) < 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, &callchain_cursor, 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/mmflags.h */
  490. static const struct {
  491. const char *original;
  492. const char *compact;
  493. } gfp_compact_table[] = {
  494. { "GFP_TRANSHUGE", "THP" },
  495. { "GFP_TRANSHUGE_LIGHT", "THL" },
  496. { "GFP_HIGHUSER_MOVABLE", "HUM" },
  497. { "GFP_HIGHUSER", "HU" },
  498. { "GFP_USER", "U" },
  499. { "GFP_TEMPORARY", "TMP" },
  500. { "GFP_KERNEL_ACCOUNT", "KAC" },
  501. { "GFP_KERNEL", "K" },
  502. { "GFP_NOFS", "NF" },
  503. { "GFP_ATOMIC", "A" },
  504. { "GFP_NOIO", "NI" },
  505. { "GFP_NOWAIT", "NW" },
  506. { "GFP_DMA", "D" },
  507. { "__GFP_HIGHMEM", "HM" },
  508. { "GFP_DMA32", "D32" },
  509. { "__GFP_HIGH", "H" },
  510. { "__GFP_ATOMIC", "_A" },
  511. { "__GFP_IO", "I" },
  512. { "__GFP_FS", "F" },
  513. { "__GFP_COLD", "CO" },
  514. { "__GFP_NOWARN", "NWR" },
  515. { "__GFP_REPEAT", "R" },
  516. { "__GFP_NOFAIL", "NF" },
  517. { "__GFP_NORETRY", "NR" },
  518. { "__GFP_COMP", "C" },
  519. { "__GFP_ZERO", "Z" },
  520. { "__GFP_NOMEMALLOC", "NMA" },
  521. { "__GFP_MEMALLOC", "MA" },
  522. { "__GFP_HARDWALL", "HW" },
  523. { "__GFP_THISNODE", "TN" },
  524. { "__GFP_RECLAIMABLE", "RC" },
  525. { "__GFP_MOVABLE", "M" },
  526. { "__GFP_ACCOUNT", "AC" },
  527. { "__GFP_NOTRACK", "NT" },
  528. { "__GFP_WRITE", "WR" },
  529. { "__GFP_RECLAIM", "R" },
  530. { "__GFP_DIRECT_RECLAIM", "DR" },
  531. { "__GFP_KSWAPD_RECLAIM", "KR" },
  532. { "__GFP_OTHER_NODE", "ON" },
  533. };
  534. static size_t max_gfp_len;
  535. static char *compact_gfp_flags(char *gfp_flags)
  536. {
  537. char *orig_flags = strdup(gfp_flags);
  538. char *new_flags = NULL;
  539. char *str, *pos = NULL;
  540. size_t len = 0;
  541. if (orig_flags == NULL)
  542. return NULL;
  543. str = strtok_r(orig_flags, "|", &pos);
  544. while (str) {
  545. size_t i;
  546. char *new;
  547. const char *cpt;
  548. for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
  549. if (strcmp(gfp_compact_table[i].original, str))
  550. continue;
  551. cpt = gfp_compact_table[i].compact;
  552. new = realloc(new_flags, len + strlen(cpt) + 2);
  553. if (new == NULL) {
  554. free(new_flags);
  555. return NULL;
  556. }
  557. new_flags = new;
  558. if (!len) {
  559. strcpy(new_flags, cpt);
  560. } else {
  561. strcat(new_flags, "|");
  562. strcat(new_flags, cpt);
  563. len++;
  564. }
  565. len += strlen(cpt);
  566. }
  567. str = strtok_r(NULL, "|", &pos);
  568. }
  569. if (max_gfp_len < len)
  570. max_gfp_len = len;
  571. free(orig_flags);
  572. return new_flags;
  573. }
  574. static char *compact_gfp_string(unsigned long gfp_flags)
  575. {
  576. struct gfp_flag key = {
  577. .flags = gfp_flags,
  578. };
  579. struct gfp_flag *gfp;
  580. gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp);
  581. if (gfp)
  582. return gfp->compact_str;
  583. return NULL;
  584. }
  585. static int parse_gfp_flags(struct perf_evsel *evsel, struct perf_sample *sample,
  586. unsigned int gfp_flags)
  587. {
  588. struct pevent_record record = {
  589. .cpu = sample->cpu,
  590. .data = sample->raw_data,
  591. .size = sample->raw_size,
  592. };
  593. struct trace_seq seq;
  594. char *str, *pos = NULL;
  595. if (nr_gfps) {
  596. struct gfp_flag key = {
  597. .flags = gfp_flags,
  598. };
  599. if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp))
  600. return 0;
  601. }
  602. trace_seq_init(&seq);
  603. pevent_event_info(&seq, evsel->tp_format, &record);
  604. str = strtok_r(seq.buffer, " ", &pos);
  605. while (str) {
  606. if (!strncmp(str, "gfp_flags=", 10)) {
  607. struct gfp_flag *new;
  608. new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps));
  609. if (new == NULL)
  610. return -ENOMEM;
  611. gfps = new;
  612. new += nr_gfps++;
  613. new->flags = gfp_flags;
  614. new->human_readable = strdup(str + 10);
  615. new->compact_str = compact_gfp_flags(str + 10);
  616. if (!new->human_readable || !new->compact_str)
  617. return -ENOMEM;
  618. qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp);
  619. }
  620. str = strtok_r(NULL, " ", &pos);
  621. }
  622. trace_seq_destroy(&seq);
  623. return 0;
  624. }
  625. static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
  626. struct perf_sample *sample)
  627. {
  628. u64 page;
  629. unsigned int order = perf_evsel__intval(evsel, sample, "order");
  630. unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
  631. unsigned int migrate_type = perf_evsel__intval(evsel, sample,
  632. "migratetype");
  633. u64 bytes = kmem_page_size << order;
  634. u64 callsite;
  635. struct page_stat *pstat;
  636. struct page_stat this = {
  637. .order = order,
  638. .gfp_flags = gfp_flags,
  639. .migrate_type = migrate_type,
  640. };
  641. if (use_pfn)
  642. page = perf_evsel__intval(evsel, sample, "pfn");
  643. else
  644. page = perf_evsel__intval(evsel, sample, "page");
  645. nr_page_allocs++;
  646. total_page_alloc_bytes += bytes;
  647. if (!valid_page(page)) {
  648. nr_page_fails++;
  649. total_page_fail_bytes += bytes;
  650. return 0;
  651. }
  652. if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
  653. return -1;
  654. callsite = find_callsite(evsel, sample);
  655. /*
  656. * This is to find the current page (with correct gfp flags and
  657. * migrate type) at free event.
  658. */
  659. this.page = page;
  660. pstat = page_stat__findnew_page(&this);
  661. if (pstat == NULL)
  662. return -ENOMEM;
  663. pstat->nr_alloc++;
  664. pstat->alloc_bytes += bytes;
  665. pstat->callsite = callsite;
  666. if (!live_page) {
  667. pstat = page_stat__findnew_alloc(&this);
  668. if (pstat == NULL)
  669. return -ENOMEM;
  670. pstat->nr_alloc++;
  671. pstat->alloc_bytes += bytes;
  672. pstat->callsite = callsite;
  673. }
  674. this.callsite = callsite;
  675. pstat = page_stat__findnew_caller(&this);
  676. if (pstat == NULL)
  677. return -ENOMEM;
  678. pstat->nr_alloc++;
  679. pstat->alloc_bytes += bytes;
  680. order_stats[order][migrate_type]++;
  681. return 0;
  682. }
  683. static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
  684. struct perf_sample *sample)
  685. {
  686. u64 page;
  687. unsigned int order = perf_evsel__intval(evsel, sample, "order");
  688. u64 bytes = kmem_page_size << order;
  689. struct page_stat *pstat;
  690. struct page_stat this = {
  691. .order = order,
  692. };
  693. if (use_pfn)
  694. page = perf_evsel__intval(evsel, sample, "pfn");
  695. else
  696. page = perf_evsel__intval(evsel, sample, "page");
  697. nr_page_frees++;
  698. total_page_free_bytes += bytes;
  699. this.page = page;
  700. pstat = page_stat__find_page(&this);
  701. if (pstat == NULL) {
  702. pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
  703. page, order);
  704. nr_page_nomatch++;
  705. total_page_nomatch_bytes += bytes;
  706. return 0;
  707. }
  708. this.gfp_flags = pstat->gfp_flags;
  709. this.migrate_type = pstat->migrate_type;
  710. this.callsite = pstat->callsite;
  711. rb_erase(&pstat->node, &page_live_tree);
  712. free(pstat);
  713. if (live_page) {
  714. order_stats[this.order][this.migrate_type]--;
  715. } else {
  716. pstat = page_stat__find_alloc(&this);
  717. if (pstat == NULL)
  718. return -ENOMEM;
  719. pstat->nr_free++;
  720. pstat->free_bytes += bytes;
  721. }
  722. pstat = page_stat__find_caller(&this);
  723. if (pstat == NULL)
  724. return -ENOENT;
  725. pstat->nr_free++;
  726. pstat->free_bytes += bytes;
  727. if (live_page) {
  728. pstat->nr_alloc--;
  729. pstat->alloc_bytes -= bytes;
  730. if (pstat->nr_alloc == 0) {
  731. rb_erase(&pstat->node, &page_caller_tree);
  732. free(pstat);
  733. }
  734. }
  735. return 0;
  736. }
  737. typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
  738. struct perf_sample *sample);
  739. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  740. union perf_event *event,
  741. struct perf_sample *sample,
  742. struct perf_evsel *evsel,
  743. struct machine *machine)
  744. {
  745. int err = 0;
  746. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  747. sample->tid);
  748. if (thread == NULL) {
  749. pr_debug("problem processing %d event, skipping it.\n",
  750. event->header.type);
  751. return -1;
  752. }
  753. dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
  754. if (evsel->handler != NULL) {
  755. tracepoint_handler f = evsel->handler;
  756. err = f(evsel, sample);
  757. }
  758. thread__put(thread);
  759. return err;
  760. }
  761. static struct perf_tool perf_kmem = {
  762. .sample = process_sample_event,
  763. .comm = perf_event__process_comm,
  764. .mmap = perf_event__process_mmap,
  765. .mmap2 = perf_event__process_mmap2,
  766. .ordered_events = true,
  767. };
  768. static double fragmentation(unsigned long n_req, unsigned long n_alloc)
  769. {
  770. if (n_alloc == 0)
  771. return 0.0;
  772. else
  773. return 100.0 - (100.0 * n_req / n_alloc);
  774. }
  775. static void __print_slab_result(struct rb_root *root,
  776. struct perf_session *session,
  777. int n_lines, int is_caller)
  778. {
  779. struct rb_node *next;
  780. struct machine *machine = &session->machines.host;
  781. printf("%.105s\n", graph_dotted_line);
  782. printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
  783. printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
  784. printf("%.105s\n", graph_dotted_line);
  785. next = rb_first(root);
  786. while (next && n_lines--) {
  787. struct alloc_stat *data = rb_entry(next, struct alloc_stat,
  788. node);
  789. struct symbol *sym = NULL;
  790. struct map *map;
  791. char buf[BUFSIZ];
  792. u64 addr;
  793. if (is_caller) {
  794. addr = data->call_site;
  795. if (!raw_ip)
  796. sym = machine__find_kernel_function(machine, addr, &map);
  797. } else
  798. addr = data->ptr;
  799. if (sym != NULL)
  800. snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
  801. addr - map->unmap_ip(map, sym->start));
  802. else
  803. snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
  804. printf(" %-34s |", buf);
  805. printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
  806. (unsigned long long)data->bytes_alloc,
  807. (unsigned long)data->bytes_alloc / data->hit,
  808. (unsigned long long)data->bytes_req,
  809. (unsigned long)data->bytes_req / data->hit,
  810. (unsigned long)data->hit,
  811. (unsigned long)data->pingpong,
  812. fragmentation(data->bytes_req, data->bytes_alloc));
  813. next = rb_next(next);
  814. }
  815. if (n_lines == -1)
  816. printf(" ... | ... | ... | ... | ... | ... \n");
  817. printf("%.105s\n", graph_dotted_line);
  818. }
  819. static const char * const migrate_type_str[] = {
  820. "UNMOVABL",
  821. "RECLAIM",
  822. "MOVABLE",
  823. "RESERVED",
  824. "CMA/ISLT",
  825. "UNKNOWN",
  826. };
  827. static void __print_page_alloc_result(struct perf_session *session, int n_lines)
  828. {
  829. struct rb_node *next = rb_first(&page_alloc_sorted);
  830. struct machine *machine = &session->machines.host;
  831. const char *format;
  832. int gfp_len = max(strlen("GFP flags"), max_gfp_len);
  833. printf("\n%.105s\n", graph_dotted_line);
  834. printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
  835. use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total",
  836. gfp_len, "GFP flags");
  837. printf("%.105s\n", graph_dotted_line);
  838. if (use_pfn)
  839. format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
  840. else
  841. format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
  842. while (next && n_lines--) {
  843. struct page_stat *data;
  844. struct symbol *sym;
  845. struct map *map;
  846. char buf[32];
  847. char *caller = buf;
  848. data = rb_entry(next, struct page_stat, node);
  849. sym = machine__find_kernel_function(machine, data->callsite, &map);
  850. if (sym && sym->name)
  851. caller = sym->name;
  852. else
  853. scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
  854. printf(format, (unsigned long long)data->page,
  855. (unsigned long long)data->alloc_bytes / 1024,
  856. data->nr_alloc, data->order,
  857. migrate_type_str[data->migrate_type],
  858. gfp_len, compact_gfp_string(data->gfp_flags), caller);
  859. next = rb_next(next);
  860. }
  861. if (n_lines == -1) {
  862. printf(" ... | ... | ... | ... | ... | %-*s | ...\n",
  863. gfp_len, "...");
  864. }
  865. printf("%.105s\n", graph_dotted_line);
  866. }
  867. static void __print_page_caller_result(struct perf_session *session, int n_lines)
  868. {
  869. struct rb_node *next = rb_first(&page_caller_sorted);
  870. struct machine *machine = &session->machines.host;
  871. int gfp_len = max(strlen("GFP flags"), max_gfp_len);
  872. printf("\n%.105s\n", graph_dotted_line);
  873. printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
  874. live_page ? "Live" : "Total", gfp_len, "GFP flags");
  875. printf("%.105s\n", graph_dotted_line);
  876. while (next && n_lines--) {
  877. struct page_stat *data;
  878. struct symbol *sym;
  879. struct map *map;
  880. char buf[32];
  881. char *caller = buf;
  882. data = rb_entry(next, struct page_stat, node);
  883. sym = machine__find_kernel_function(machine, data->callsite, &map);
  884. if (sym && sym->name)
  885. caller = sym->name;
  886. else
  887. scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
  888. printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n",
  889. (unsigned long long)data->alloc_bytes / 1024,
  890. data->nr_alloc, data->order,
  891. migrate_type_str[data->migrate_type],
  892. gfp_len, compact_gfp_string(data->gfp_flags), caller);
  893. next = rb_next(next);
  894. }
  895. if (n_lines == -1) {
  896. printf(" ... | ... | ... | ... | %-*s | ...\n",
  897. gfp_len, "...");
  898. }
  899. printf("%.105s\n", graph_dotted_line);
  900. }
  901. static void print_gfp_flags(void)
  902. {
  903. int i;
  904. printf("#\n");
  905. printf("# GFP flags\n");
  906. printf("# ---------\n");
  907. for (i = 0; i < nr_gfps; i++) {
  908. printf("# %08x: %*s: %s\n", gfps[i].flags,
  909. (int) max_gfp_len, gfps[i].compact_str,
  910. gfps[i].human_readable);
  911. }
  912. }
  913. static void print_slab_summary(void)
  914. {
  915. printf("\nSUMMARY (SLAB allocator)");
  916. printf("\n========================\n");
  917. printf("Total bytes requested: %'lu\n", total_requested);
  918. printf("Total bytes allocated: %'lu\n", total_allocated);
  919. printf("Total bytes wasted on internal fragmentation: %'lu\n",
  920. total_allocated - total_requested);
  921. printf("Internal fragmentation: %f%%\n",
  922. fragmentation(total_requested, total_allocated));
  923. printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
  924. }
  925. static void print_page_summary(void)
  926. {
  927. int o, m;
  928. u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
  929. u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
  930. printf("\nSUMMARY (page allocator)");
  931. printf("\n========================\n");
  932. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests",
  933. nr_page_allocs, total_page_alloc_bytes / 1024);
  934. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests",
  935. nr_page_frees, total_page_free_bytes / 1024);
  936. printf("\n");
  937. printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
  938. nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
  939. printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
  940. nr_page_allocs - nr_alloc_freed,
  941. (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
  942. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests",
  943. nr_page_nomatch, total_page_nomatch_bytes / 1024);
  944. printf("\n");
  945. printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures",
  946. nr_page_fails, total_page_fail_bytes / 1024);
  947. printf("\n");
  948. printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable",
  949. "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
  950. printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line,
  951. graph_dotted_line, graph_dotted_line, graph_dotted_line,
  952. graph_dotted_line, graph_dotted_line);
  953. for (o = 0; o < MAX_PAGE_ORDER; o++) {
  954. printf("%5d", o);
  955. for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
  956. if (order_stats[o][m])
  957. printf(" %'12d", order_stats[o][m]);
  958. else
  959. printf(" %12c", '.');
  960. }
  961. printf("\n");
  962. }
  963. }
  964. static void print_slab_result(struct perf_session *session)
  965. {
  966. if (caller_flag)
  967. __print_slab_result(&root_caller_sorted, session, caller_lines, 1);
  968. if (alloc_flag)
  969. __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
  970. print_slab_summary();
  971. }
  972. static void print_page_result(struct perf_session *session)
  973. {
  974. if (caller_flag || alloc_flag)
  975. print_gfp_flags();
  976. if (caller_flag)
  977. __print_page_caller_result(session, caller_lines);
  978. if (alloc_flag)
  979. __print_page_alloc_result(session, alloc_lines);
  980. print_page_summary();
  981. }
  982. static void print_result(struct perf_session *session)
  983. {
  984. if (kmem_slab)
  985. print_slab_result(session);
  986. if (kmem_page)
  987. print_page_result(session);
  988. }
  989. static LIST_HEAD(slab_caller_sort);
  990. static LIST_HEAD(slab_alloc_sort);
  991. static LIST_HEAD(page_caller_sort);
  992. static LIST_HEAD(page_alloc_sort);
  993. static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
  994. struct list_head *sort_list)
  995. {
  996. struct rb_node **new = &(root->rb_node);
  997. struct rb_node *parent = NULL;
  998. struct sort_dimension *sort;
  999. while (*new) {
  1000. struct alloc_stat *this;
  1001. int cmp = 0;
  1002. this = rb_entry(*new, struct alloc_stat, node);
  1003. parent = *new;
  1004. list_for_each_entry(sort, sort_list, list) {
  1005. cmp = sort->cmp(data, this);
  1006. if (cmp)
  1007. break;
  1008. }
  1009. if (cmp > 0)
  1010. new = &((*new)->rb_left);
  1011. else
  1012. new = &((*new)->rb_right);
  1013. }
  1014. rb_link_node(&data->node, parent, new);
  1015. rb_insert_color(&data->node, root);
  1016. }
  1017. static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
  1018. struct list_head *sort_list)
  1019. {
  1020. struct rb_node *node;
  1021. struct alloc_stat *data;
  1022. for (;;) {
  1023. node = rb_first(root);
  1024. if (!node)
  1025. break;
  1026. rb_erase(node, root);
  1027. data = rb_entry(node, struct alloc_stat, node);
  1028. sort_slab_insert(root_sorted, data, sort_list);
  1029. }
  1030. }
  1031. static void sort_page_insert(struct rb_root *root, struct page_stat *data,
  1032. struct list_head *sort_list)
  1033. {
  1034. struct rb_node **new = &root->rb_node;
  1035. struct rb_node *parent = NULL;
  1036. struct sort_dimension *sort;
  1037. while (*new) {
  1038. struct page_stat *this;
  1039. int cmp = 0;
  1040. this = rb_entry(*new, struct page_stat, node);
  1041. parent = *new;
  1042. list_for_each_entry(sort, sort_list, list) {
  1043. cmp = sort->cmp(data, this);
  1044. if (cmp)
  1045. break;
  1046. }
  1047. if (cmp > 0)
  1048. new = &parent->rb_left;
  1049. else
  1050. new = &parent->rb_right;
  1051. }
  1052. rb_link_node(&data->node, parent, new);
  1053. rb_insert_color(&data->node, root);
  1054. }
  1055. static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted,
  1056. struct list_head *sort_list)
  1057. {
  1058. struct rb_node *node;
  1059. struct page_stat *data;
  1060. for (;;) {
  1061. node = rb_first(root);
  1062. if (!node)
  1063. break;
  1064. rb_erase(node, root);
  1065. data = rb_entry(node, struct page_stat, node);
  1066. sort_page_insert(root_sorted, data, sort_list);
  1067. }
  1068. }
  1069. static void sort_result(void)
  1070. {
  1071. if (kmem_slab) {
  1072. __sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
  1073. &slab_alloc_sort);
  1074. __sort_slab_result(&root_caller_stat, &root_caller_sorted,
  1075. &slab_caller_sort);
  1076. }
  1077. if (kmem_page) {
  1078. if (live_page)
  1079. __sort_page_result(&page_live_tree, &page_alloc_sorted,
  1080. &page_alloc_sort);
  1081. else
  1082. __sort_page_result(&page_alloc_tree, &page_alloc_sorted,
  1083. &page_alloc_sort);
  1084. __sort_page_result(&page_caller_tree, &page_caller_sorted,
  1085. &page_caller_sort);
  1086. }
  1087. }
  1088. static int __cmd_kmem(struct perf_session *session)
  1089. {
  1090. int err = -EINVAL;
  1091. struct perf_evsel *evsel;
  1092. const struct perf_evsel_str_handler kmem_tracepoints[] = {
  1093. /* slab allocator */
  1094. { "kmem:kmalloc", perf_evsel__process_alloc_event, },
  1095. { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
  1096. { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
  1097. { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
  1098. { "kmem:kfree", perf_evsel__process_free_event, },
  1099. { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
  1100. /* page allocator */
  1101. { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, },
  1102. { "kmem:mm_page_free", perf_evsel__process_page_free_event, },
  1103. };
  1104. if (!perf_session__has_traces(session, "kmem record"))
  1105. goto out;
  1106. if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
  1107. pr_err("Initializing perf session tracepoint handlers failed\n");
  1108. goto out;
  1109. }
  1110. evlist__for_each_entry(session->evlist, evsel) {
  1111. if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") &&
  1112. perf_evsel__field(evsel, "pfn")) {
  1113. use_pfn = true;
  1114. break;
  1115. }
  1116. }
  1117. setup_pager();
  1118. err = perf_session__process_events(session);
  1119. if (err != 0) {
  1120. pr_err("error during process events: %d\n", err);
  1121. goto out;
  1122. }
  1123. sort_result();
  1124. print_result(session);
  1125. out:
  1126. return err;
  1127. }
  1128. /* slab sort keys */
  1129. static int ptr_cmp(void *a, void *b)
  1130. {
  1131. struct alloc_stat *l = a;
  1132. struct alloc_stat *r = b;
  1133. if (l->ptr < r->ptr)
  1134. return -1;
  1135. else if (l->ptr > r->ptr)
  1136. return 1;
  1137. return 0;
  1138. }
  1139. static struct sort_dimension ptr_sort_dimension = {
  1140. .name = "ptr",
  1141. .cmp = ptr_cmp,
  1142. };
  1143. static int slab_callsite_cmp(void *a, void *b)
  1144. {
  1145. struct alloc_stat *l = a;
  1146. struct alloc_stat *r = b;
  1147. if (l->call_site < r->call_site)
  1148. return -1;
  1149. else if (l->call_site > r->call_site)
  1150. return 1;
  1151. return 0;
  1152. }
  1153. static struct sort_dimension callsite_sort_dimension = {
  1154. .name = "callsite",
  1155. .cmp = slab_callsite_cmp,
  1156. };
  1157. static int hit_cmp(void *a, void *b)
  1158. {
  1159. struct alloc_stat *l = a;
  1160. struct alloc_stat *r = b;
  1161. if (l->hit < r->hit)
  1162. return -1;
  1163. else if (l->hit > r->hit)
  1164. return 1;
  1165. return 0;
  1166. }
  1167. static struct sort_dimension hit_sort_dimension = {
  1168. .name = "hit",
  1169. .cmp = hit_cmp,
  1170. };
  1171. static int bytes_cmp(void *a, void *b)
  1172. {
  1173. struct alloc_stat *l = a;
  1174. struct alloc_stat *r = b;
  1175. if (l->bytes_alloc < r->bytes_alloc)
  1176. return -1;
  1177. else if (l->bytes_alloc > r->bytes_alloc)
  1178. return 1;
  1179. return 0;
  1180. }
  1181. static struct sort_dimension bytes_sort_dimension = {
  1182. .name = "bytes",
  1183. .cmp = bytes_cmp,
  1184. };
  1185. static int frag_cmp(void *a, void *b)
  1186. {
  1187. double x, y;
  1188. struct alloc_stat *l = a;
  1189. struct alloc_stat *r = b;
  1190. x = fragmentation(l->bytes_req, l->bytes_alloc);
  1191. y = fragmentation(r->bytes_req, r->bytes_alloc);
  1192. if (x < y)
  1193. return -1;
  1194. else if (x > y)
  1195. return 1;
  1196. return 0;
  1197. }
  1198. static struct sort_dimension frag_sort_dimension = {
  1199. .name = "frag",
  1200. .cmp = frag_cmp,
  1201. };
  1202. static int pingpong_cmp(void *a, void *b)
  1203. {
  1204. struct alloc_stat *l = a;
  1205. struct alloc_stat *r = b;
  1206. if (l->pingpong < r->pingpong)
  1207. return -1;
  1208. else if (l->pingpong > r->pingpong)
  1209. return 1;
  1210. return 0;
  1211. }
  1212. static struct sort_dimension pingpong_sort_dimension = {
  1213. .name = "pingpong",
  1214. .cmp = pingpong_cmp,
  1215. };
  1216. /* page sort keys */
  1217. static int page_cmp(void *a, void *b)
  1218. {
  1219. struct page_stat *l = a;
  1220. struct page_stat *r = b;
  1221. if (l->page < r->page)
  1222. return -1;
  1223. else if (l->page > r->page)
  1224. return 1;
  1225. return 0;
  1226. }
  1227. static struct sort_dimension page_sort_dimension = {
  1228. .name = "page",
  1229. .cmp = page_cmp,
  1230. };
  1231. static int page_callsite_cmp(void *a, void *b)
  1232. {
  1233. struct page_stat *l = a;
  1234. struct page_stat *r = b;
  1235. if (l->callsite < r->callsite)
  1236. return -1;
  1237. else if (l->callsite > r->callsite)
  1238. return 1;
  1239. return 0;
  1240. }
  1241. static struct sort_dimension page_callsite_sort_dimension = {
  1242. .name = "callsite",
  1243. .cmp = page_callsite_cmp,
  1244. };
  1245. static int page_hit_cmp(void *a, void *b)
  1246. {
  1247. struct page_stat *l = a;
  1248. struct page_stat *r = b;
  1249. if (l->nr_alloc < r->nr_alloc)
  1250. return -1;
  1251. else if (l->nr_alloc > r->nr_alloc)
  1252. return 1;
  1253. return 0;
  1254. }
  1255. static struct sort_dimension page_hit_sort_dimension = {
  1256. .name = "hit",
  1257. .cmp = page_hit_cmp,
  1258. };
  1259. static int page_bytes_cmp(void *a, void *b)
  1260. {
  1261. struct page_stat *l = a;
  1262. struct page_stat *r = b;
  1263. if (l->alloc_bytes < r->alloc_bytes)
  1264. return -1;
  1265. else if (l->alloc_bytes > r->alloc_bytes)
  1266. return 1;
  1267. return 0;
  1268. }
  1269. static struct sort_dimension page_bytes_sort_dimension = {
  1270. .name = "bytes",
  1271. .cmp = page_bytes_cmp,
  1272. };
  1273. static int page_order_cmp(void *a, void *b)
  1274. {
  1275. struct page_stat *l = a;
  1276. struct page_stat *r = b;
  1277. if (l->order < r->order)
  1278. return -1;
  1279. else if (l->order > r->order)
  1280. return 1;
  1281. return 0;
  1282. }
  1283. static struct sort_dimension page_order_sort_dimension = {
  1284. .name = "order",
  1285. .cmp = page_order_cmp,
  1286. };
  1287. static int migrate_type_cmp(void *a, void *b)
  1288. {
  1289. struct page_stat *l = a;
  1290. struct page_stat *r = b;
  1291. /* for internal use to find free'd page */
  1292. if (l->migrate_type == -1U)
  1293. return 0;
  1294. if (l->migrate_type < r->migrate_type)
  1295. return -1;
  1296. else if (l->migrate_type > r->migrate_type)
  1297. return 1;
  1298. return 0;
  1299. }
  1300. static struct sort_dimension migrate_type_sort_dimension = {
  1301. .name = "migtype",
  1302. .cmp = migrate_type_cmp,
  1303. };
  1304. static int gfp_flags_cmp(void *a, void *b)
  1305. {
  1306. struct page_stat *l = a;
  1307. struct page_stat *r = b;
  1308. /* for internal use to find free'd page */
  1309. if (l->gfp_flags == -1U)
  1310. return 0;
  1311. if (l->gfp_flags < r->gfp_flags)
  1312. return -1;
  1313. else if (l->gfp_flags > r->gfp_flags)
  1314. return 1;
  1315. return 0;
  1316. }
  1317. static struct sort_dimension gfp_flags_sort_dimension = {
  1318. .name = "gfp",
  1319. .cmp = gfp_flags_cmp,
  1320. };
  1321. static struct sort_dimension *slab_sorts[] = {
  1322. &ptr_sort_dimension,
  1323. &callsite_sort_dimension,
  1324. &hit_sort_dimension,
  1325. &bytes_sort_dimension,
  1326. &frag_sort_dimension,
  1327. &pingpong_sort_dimension,
  1328. };
  1329. static struct sort_dimension *page_sorts[] = {
  1330. &page_sort_dimension,
  1331. &page_callsite_sort_dimension,
  1332. &page_hit_sort_dimension,
  1333. &page_bytes_sort_dimension,
  1334. &page_order_sort_dimension,
  1335. &migrate_type_sort_dimension,
  1336. &gfp_flags_sort_dimension,
  1337. };
  1338. static int slab_sort_dimension__add(const char *tok, struct list_head *list)
  1339. {
  1340. struct sort_dimension *sort;
  1341. int i;
  1342. for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) {
  1343. if (!strcmp(slab_sorts[i]->name, tok)) {
  1344. sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i]));
  1345. if (!sort) {
  1346. pr_err("%s: memdup failed\n", __func__);
  1347. return -1;
  1348. }
  1349. list_add_tail(&sort->list, list);
  1350. return 0;
  1351. }
  1352. }
  1353. return -1;
  1354. }
  1355. static int page_sort_dimension__add(const char *tok, struct list_head *list)
  1356. {
  1357. struct sort_dimension *sort;
  1358. int i;
  1359. for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) {
  1360. if (!strcmp(page_sorts[i]->name, tok)) {
  1361. sort = memdup(page_sorts[i], sizeof(*page_sorts[i]));
  1362. if (!sort) {
  1363. pr_err("%s: memdup failed\n", __func__);
  1364. return -1;
  1365. }
  1366. list_add_tail(&sort->list, list);
  1367. return 0;
  1368. }
  1369. }
  1370. return -1;
  1371. }
  1372. static int setup_slab_sorting(struct list_head *sort_list, const char *arg)
  1373. {
  1374. char *tok;
  1375. char *str = strdup(arg);
  1376. char *pos = str;
  1377. if (!str) {
  1378. pr_err("%s: strdup failed\n", __func__);
  1379. return -1;
  1380. }
  1381. while (true) {
  1382. tok = strsep(&pos, ",");
  1383. if (!tok)
  1384. break;
  1385. if (slab_sort_dimension__add(tok, sort_list) < 0) {
  1386. error("Unknown slab --sort key: '%s'", tok);
  1387. free(str);
  1388. return -1;
  1389. }
  1390. }
  1391. free(str);
  1392. return 0;
  1393. }
  1394. static int setup_page_sorting(struct list_head *sort_list, const char *arg)
  1395. {
  1396. char *tok;
  1397. char *str = strdup(arg);
  1398. char *pos = str;
  1399. if (!str) {
  1400. pr_err("%s: strdup failed\n", __func__);
  1401. return -1;
  1402. }
  1403. while (true) {
  1404. tok = strsep(&pos, ",");
  1405. if (!tok)
  1406. break;
  1407. if (page_sort_dimension__add(tok, sort_list) < 0) {
  1408. error("Unknown page --sort key: '%s'", tok);
  1409. free(str);
  1410. return -1;
  1411. }
  1412. }
  1413. free(str);
  1414. return 0;
  1415. }
  1416. static int parse_sort_opt(const struct option *opt __maybe_unused,
  1417. const char *arg, int unset __maybe_unused)
  1418. {
  1419. if (!arg)
  1420. return -1;
  1421. if (kmem_page > kmem_slab ||
  1422. (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) {
  1423. if (caller_flag > alloc_flag)
  1424. return setup_page_sorting(&page_caller_sort, arg);
  1425. else
  1426. return setup_page_sorting(&page_alloc_sort, arg);
  1427. } else {
  1428. if (caller_flag > alloc_flag)
  1429. return setup_slab_sorting(&slab_caller_sort, arg);
  1430. else
  1431. return setup_slab_sorting(&slab_alloc_sort, arg);
  1432. }
  1433. return 0;
  1434. }
  1435. static int parse_caller_opt(const struct option *opt __maybe_unused,
  1436. const char *arg __maybe_unused,
  1437. int unset __maybe_unused)
  1438. {
  1439. caller_flag = (alloc_flag + 1);
  1440. return 0;
  1441. }
  1442. static int parse_alloc_opt(const struct option *opt __maybe_unused,
  1443. const char *arg __maybe_unused,
  1444. int unset __maybe_unused)
  1445. {
  1446. alloc_flag = (caller_flag + 1);
  1447. return 0;
  1448. }
  1449. static int parse_slab_opt(const struct option *opt __maybe_unused,
  1450. const char *arg __maybe_unused,
  1451. int unset __maybe_unused)
  1452. {
  1453. kmem_slab = (kmem_page + 1);
  1454. return 0;
  1455. }
  1456. static int parse_page_opt(const struct option *opt __maybe_unused,
  1457. const char *arg __maybe_unused,
  1458. int unset __maybe_unused)
  1459. {
  1460. kmem_page = (kmem_slab + 1);
  1461. return 0;
  1462. }
  1463. static int parse_line_opt(const struct option *opt __maybe_unused,
  1464. const char *arg, int unset __maybe_unused)
  1465. {
  1466. int lines;
  1467. if (!arg)
  1468. return -1;
  1469. lines = strtoul(arg, NULL, 10);
  1470. if (caller_flag > alloc_flag)
  1471. caller_lines = lines;
  1472. else
  1473. alloc_lines = lines;
  1474. return 0;
  1475. }
  1476. static int __cmd_record(int argc, const char **argv)
  1477. {
  1478. const char * const record_args[] = {
  1479. "record", "-a", "-R", "-c", "1",
  1480. };
  1481. const char * const slab_events[] = {
  1482. "-e", "kmem:kmalloc",
  1483. "-e", "kmem:kmalloc_node",
  1484. "-e", "kmem:kfree",
  1485. "-e", "kmem:kmem_cache_alloc",
  1486. "-e", "kmem:kmem_cache_alloc_node",
  1487. "-e", "kmem:kmem_cache_free",
  1488. };
  1489. const char * const page_events[] = {
  1490. "-e", "kmem:mm_page_alloc",
  1491. "-e", "kmem:mm_page_free",
  1492. };
  1493. unsigned int rec_argc, i, j;
  1494. const char **rec_argv;
  1495. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  1496. if (kmem_slab)
  1497. rec_argc += ARRAY_SIZE(slab_events);
  1498. if (kmem_page)
  1499. rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
  1500. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1501. if (rec_argv == NULL)
  1502. return -ENOMEM;
  1503. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1504. rec_argv[i] = strdup(record_args[i]);
  1505. if (kmem_slab) {
  1506. for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
  1507. rec_argv[i] = strdup(slab_events[j]);
  1508. }
  1509. if (kmem_page) {
  1510. rec_argv[i++] = strdup("-g");
  1511. for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
  1512. rec_argv[i] = strdup(page_events[j]);
  1513. }
  1514. for (j = 1; j < (unsigned int)argc; j++, i++)
  1515. rec_argv[i] = argv[j];
  1516. return cmd_record(i, rec_argv, NULL);
  1517. }
  1518. static int kmem_config(const char *var, const char *value, void *cb __maybe_unused)
  1519. {
  1520. if (!strcmp(var, "kmem.default")) {
  1521. if (!strcmp(value, "slab"))
  1522. kmem_default = KMEM_SLAB;
  1523. else if (!strcmp(value, "page"))
  1524. kmem_default = KMEM_PAGE;
  1525. else
  1526. pr_err("invalid default value ('slab' or 'page' required): %s\n",
  1527. value);
  1528. return 0;
  1529. }
  1530. return 0;
  1531. }
  1532. int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
  1533. {
  1534. const char * const default_slab_sort = "frag,hit,bytes";
  1535. const char * const default_page_sort = "bytes,hit";
  1536. struct perf_data_file file = {
  1537. .mode = PERF_DATA_MODE_READ,
  1538. };
  1539. const struct option kmem_options[] = {
  1540. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1541. OPT_INCR('v', "verbose", &verbose,
  1542. "be more verbose (show symbol address, etc)"),
  1543. OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
  1544. "show per-callsite statistics", parse_caller_opt),
  1545. OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
  1546. "show per-allocation statistics", parse_alloc_opt),
  1547. OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
  1548. "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, "
  1549. "page, order, migtype, gfp", parse_sort_opt),
  1550. OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
  1551. OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
  1552. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1553. OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
  1554. parse_slab_opt),
  1555. OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
  1556. parse_page_opt),
  1557. OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
  1558. OPT_END()
  1559. };
  1560. const char *const kmem_subcommands[] = { "record", "stat", NULL };
  1561. const char *kmem_usage[] = {
  1562. NULL,
  1563. NULL
  1564. };
  1565. struct perf_session *session;
  1566. int ret = -1;
  1567. const char errmsg[] = "No %s allocation events found. Have you run 'perf kmem record --%s'?\n";
  1568. perf_config(kmem_config, NULL);
  1569. argc = parse_options_subcommand(argc, argv, kmem_options,
  1570. kmem_subcommands, kmem_usage, 0);
  1571. if (!argc)
  1572. usage_with_options(kmem_usage, kmem_options);
  1573. if (kmem_slab == 0 && kmem_page == 0) {
  1574. if (kmem_default == KMEM_SLAB)
  1575. kmem_slab = 1;
  1576. else
  1577. kmem_page = 1;
  1578. }
  1579. if (!strncmp(argv[0], "rec", 3)) {
  1580. symbol__init(NULL);
  1581. return __cmd_record(argc, argv);
  1582. }
  1583. file.path = input_name;
  1584. kmem_session = session = perf_session__new(&file, false, &perf_kmem);
  1585. if (session == NULL)
  1586. return -1;
  1587. if (kmem_slab) {
  1588. if (!perf_evlist__find_tracepoint_by_name(session->evlist,
  1589. "kmem:kmalloc")) {
  1590. pr_err(errmsg, "slab", "slab");
  1591. goto out_delete;
  1592. }
  1593. }
  1594. if (kmem_page) {
  1595. struct perf_evsel *evsel;
  1596. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  1597. "kmem:mm_page_alloc");
  1598. if (evsel == NULL) {
  1599. pr_err(errmsg, "page", "page");
  1600. goto out_delete;
  1601. }
  1602. kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent);
  1603. symbol_conf.use_callchain = true;
  1604. }
  1605. symbol__init(&session->header.env);
  1606. if (!strcmp(argv[0], "stat")) {
  1607. setlocale(LC_ALL, "");
  1608. if (cpu__setup_cpunode_map())
  1609. goto out_delete;
  1610. if (list_empty(&slab_caller_sort))
  1611. setup_slab_sorting(&slab_caller_sort, default_slab_sort);
  1612. if (list_empty(&slab_alloc_sort))
  1613. setup_slab_sorting(&slab_alloc_sort, default_slab_sort);
  1614. if (list_empty(&page_caller_sort))
  1615. setup_page_sorting(&page_caller_sort, default_page_sort);
  1616. if (list_empty(&page_alloc_sort))
  1617. setup_page_sorting(&page_alloc_sort, default_page_sort);
  1618. if (kmem_page) {
  1619. setup_page_sorting(&page_alloc_sort_input,
  1620. "page,order,migtype,gfp");
  1621. setup_page_sorting(&page_caller_sort_input,
  1622. "callsite,order,migtype,gfp");
  1623. }
  1624. ret = __cmd_kmem(session);
  1625. } else
  1626. usage_with_options(kmem_usage, kmem_options);
  1627. out_delete:
  1628. perf_session__delete(session);
  1629. return ret;
  1630. }