builtin-kmem.c 46 KB

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