builtin-kmem.c 46 KB

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