builtin-kmem.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "builtin.h"
  3. #include "perf.h"
  4. #include "util/evlist.h"
  5. #include "util/evsel.h"
  6. #include "util/util.h"
  7. #include "util/config.h"
  8. #include "util/symbol.h"
  9. #include "util/thread.h"
  10. #include "util/header.h"
  11. #include "util/session.h"
  12. #include "util/tool.h"
  13. #include "util/callchain.h"
  14. #include "util/time-utils.h"
  15. #include <subcmd/parse-options.h>
  16. #include "util/trace-event.h"
  17. #include "util/data.h"
  18. #include "util/cpumap.h"
  19. #include "util/debug.h"
  20. #include <linux/kernel.h>
  21. #include <linux/rbtree.h>
  22. #include <linux/string.h>
  23. #include <errno.h>
  24. #include <inttypes.h>
  25. #include <locale.h>
  26. #include <regex.h>
  27. #include "sane_ctype.h"
  28. static int kmem_slab;
  29. static int kmem_page;
  30. static long kmem_page_size;
  31. static enum {
  32. KMEM_SLAB,
  33. KMEM_PAGE,
  34. } kmem_default = KMEM_SLAB; /* for backward compatibility */
  35. struct alloc_stat;
  36. typedef int (*sort_fn_t)(void *, void *);
  37. static int alloc_flag;
  38. static int caller_flag;
  39. static int alloc_lines = -1;
  40. static int caller_lines = -1;
  41. static bool raw_ip;
  42. struct alloc_stat {
  43. u64 call_site;
  44. u64 ptr;
  45. u64 bytes_req;
  46. u64 bytes_alloc;
  47. u64 last_alloc;
  48. u32 hit;
  49. u32 pingpong;
  50. short alloc_cpu;
  51. struct rb_node node;
  52. };
  53. static struct rb_root root_alloc_stat;
  54. static struct rb_root root_alloc_sorted;
  55. static struct rb_root root_caller_stat;
  56. static struct rb_root root_caller_sorted;
  57. static unsigned long total_requested, total_allocated, total_freed;
  58. static unsigned long nr_allocs, nr_cross_allocs;
  59. /* filters for controlling start and stop of time of analysis */
  60. static struct perf_time_interval ptime;
  61. const char *time_str;
  62. static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
  63. int bytes_req, int bytes_alloc, int cpu)
  64. {
  65. struct rb_node **node = &root_alloc_stat.rb_node;
  66. struct rb_node *parent = NULL;
  67. struct alloc_stat *data = NULL;
  68. while (*node) {
  69. parent = *node;
  70. data = rb_entry(*node, struct alloc_stat, node);
  71. if (ptr > data->ptr)
  72. node = &(*node)->rb_right;
  73. else if (ptr < data->ptr)
  74. node = &(*node)->rb_left;
  75. else
  76. break;
  77. }
  78. if (data && data->ptr == ptr) {
  79. data->hit++;
  80. data->bytes_req += bytes_req;
  81. data->bytes_alloc += bytes_alloc;
  82. } else {
  83. data = malloc(sizeof(*data));
  84. if (!data) {
  85. pr_err("%s: malloc failed\n", __func__);
  86. return -1;
  87. }
  88. data->ptr = ptr;
  89. data->pingpong = 0;
  90. data->hit = 1;
  91. data->bytes_req = bytes_req;
  92. data->bytes_alloc = bytes_alloc;
  93. rb_link_node(&data->node, parent, node);
  94. rb_insert_color(&data->node, &root_alloc_stat);
  95. }
  96. data->call_site = call_site;
  97. data->alloc_cpu = cpu;
  98. data->last_alloc = bytes_alloc;
  99. return 0;
  100. }
  101. static int insert_caller_stat(unsigned long call_site,
  102. int bytes_req, int bytes_alloc)
  103. {
  104. struct rb_node **node = &root_caller_stat.rb_node;
  105. struct rb_node *parent = NULL;
  106. struct alloc_stat *data = NULL;
  107. while (*node) {
  108. parent = *node;
  109. data = rb_entry(*node, struct alloc_stat, node);
  110. if (call_site > data->call_site)
  111. node = &(*node)->rb_right;
  112. else if (call_site < data->call_site)
  113. node = &(*node)->rb_left;
  114. else
  115. break;
  116. }
  117. if (data && data->call_site == call_site) {
  118. data->hit++;
  119. data->bytes_req += bytes_req;
  120. data->bytes_alloc += bytes_alloc;
  121. } else {
  122. data = malloc(sizeof(*data));
  123. if (!data) {
  124. pr_err("%s: malloc failed\n", __func__);
  125. return -1;
  126. }
  127. data->call_site = call_site;
  128. data->pingpong = 0;
  129. data->hit = 1;
  130. data->bytes_req = bytes_req;
  131. data->bytes_alloc = bytes_alloc;
  132. rb_link_node(&data->node, parent, node);
  133. rb_insert_color(&data->node, &root_caller_stat);
  134. }
  135. return 0;
  136. }
  137. static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
  138. struct perf_sample *sample)
  139. {
  140. unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
  141. call_site = perf_evsel__intval(evsel, sample, "call_site");
  142. int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
  143. bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
  144. if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
  145. insert_caller_stat(call_site, bytes_req, bytes_alloc))
  146. return -1;
  147. total_requested += bytes_req;
  148. total_allocated += bytes_alloc;
  149. nr_allocs++;
  150. return 0;
  151. }
  152. static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
  153. struct perf_sample *sample)
  154. {
  155. int ret = perf_evsel__process_alloc_event(evsel, sample);
  156. if (!ret) {
  157. int node1 = cpu__get_node(sample->cpu),
  158. node2 = perf_evsel__intval(evsel, sample, "node");
  159. if (node1 != node2)
  160. nr_cross_allocs++;
  161. }
  162. return ret;
  163. }
  164. static int ptr_cmp(void *, void *);
  165. static int slab_callsite_cmp(void *, void *);
  166. static struct alloc_stat *search_alloc_stat(unsigned long ptr,
  167. unsigned long call_site,
  168. struct rb_root *root,
  169. sort_fn_t sort_fn)
  170. {
  171. struct rb_node *node = root->rb_node;
  172. struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
  173. while (node) {
  174. struct alloc_stat *data;
  175. int cmp;
  176. data = rb_entry(node, struct alloc_stat, node);
  177. cmp = sort_fn(&key, data);
  178. if (cmp < 0)
  179. node = node->rb_left;
  180. else if (cmp > 0)
  181. node = node->rb_right;
  182. else
  183. return data;
  184. }
  185. return NULL;
  186. }
  187. static int perf_evsel__process_free_event(struct perf_evsel *evsel,
  188. struct perf_sample *sample)
  189. {
  190. unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
  191. struct alloc_stat *s_alloc, *s_caller;
  192. s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
  193. if (!s_alloc)
  194. return 0;
  195. total_freed += s_alloc->last_alloc;
  196. if ((short)sample->cpu != s_alloc->alloc_cpu) {
  197. s_alloc->pingpong++;
  198. s_caller = search_alloc_stat(0, s_alloc->call_site,
  199. &root_caller_stat,
  200. slab_callsite_cmp);
  201. if (!s_caller)
  202. return -1;
  203. s_caller->pingpong++;
  204. }
  205. s_alloc->alloc_cpu = -1;
  206. return 0;
  207. }
  208. static u64 total_page_alloc_bytes;
  209. static u64 total_page_free_bytes;
  210. static u64 total_page_nomatch_bytes;
  211. static u64 total_page_fail_bytes;
  212. static unsigned long nr_page_allocs;
  213. static unsigned long nr_page_frees;
  214. static unsigned long nr_page_fails;
  215. static unsigned long nr_page_nomatch;
  216. static bool use_pfn;
  217. static bool live_page;
  218. static struct perf_session *kmem_session;
  219. #define MAX_MIGRATE_TYPES 6
  220. #define MAX_PAGE_ORDER 11
  221. static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
  222. struct page_stat {
  223. struct rb_node node;
  224. u64 page;
  225. u64 callsite;
  226. int order;
  227. unsigned gfp_flags;
  228. unsigned migrate_type;
  229. u64 alloc_bytes;
  230. u64 free_bytes;
  231. int nr_alloc;
  232. int nr_free;
  233. };
  234. static struct rb_root page_live_tree;
  235. static struct rb_root page_alloc_tree;
  236. static struct rb_root page_alloc_sorted;
  237. static struct rb_root page_caller_tree;
  238. static struct rb_root page_caller_sorted;
  239. struct alloc_func {
  240. u64 start;
  241. u64 end;
  242. char *name;
  243. };
  244. static int nr_alloc_funcs;
  245. static struct alloc_func *alloc_func_list;
  246. static int funcmp(const void *a, const void *b)
  247. {
  248. const struct alloc_func *fa = a;
  249. const struct alloc_func *fb = b;
  250. if (fa->start > fb->start)
  251. return 1;
  252. else
  253. return -1;
  254. }
  255. static int callcmp(const void *a, const void *b)
  256. {
  257. const struct alloc_func *fa = a;
  258. const struct alloc_func *fb = b;
  259. if (fb->start <= fa->start && fa->end < fb->end)
  260. return 0;
  261. if (fa->start > fb->start)
  262. return 1;
  263. else
  264. return -1;
  265. }
  266. static int build_alloc_func_list(void)
  267. {
  268. int ret;
  269. struct map *kernel_map;
  270. struct symbol *sym;
  271. struct rb_node *node;
  272. struct alloc_func *func;
  273. struct machine *machine = &kmem_session->machines.host;
  274. regex_t alloc_func_regex;
  275. const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
  276. ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
  277. if (ret) {
  278. char err[BUFSIZ];
  279. regerror(ret, &alloc_func_regex, err, sizeof(err));
  280. pr_err("Invalid regex: %s\n%s", pattern, err);
  281. return -EINVAL;
  282. }
  283. kernel_map = machine__kernel_map(machine);
  284. if (map__load(kernel_map) < 0) {
  285. pr_err("cannot load kernel map\n");
  286. return -ENOENT;
  287. }
  288. map__for_each_symbol(kernel_map, sym, node) {
  289. if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
  290. continue;
  291. func = realloc(alloc_func_list,
  292. (nr_alloc_funcs + 1) * sizeof(*func));
  293. if (func == NULL)
  294. return -ENOMEM;
  295. pr_debug("alloc func: %s\n", sym->name);
  296. func[nr_alloc_funcs].start = sym->start;
  297. func[nr_alloc_funcs].end = sym->end;
  298. func[nr_alloc_funcs].name = sym->name;
  299. alloc_func_list = func;
  300. nr_alloc_funcs++;
  301. }
  302. qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
  303. regfree(&alloc_func_regex);
  304. return 0;
  305. }
  306. /*
  307. * Find first non-memory allocation function from callchain.
  308. * The allocation functions are in the 'alloc_func_list'.
  309. */
  310. static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample)
  311. {
  312. struct addr_location al;
  313. struct machine *machine = &kmem_session->machines.host;
  314. struct callchain_cursor_node *node;
  315. if (alloc_func_list == NULL) {
  316. if (build_alloc_func_list() < 0)
  317. goto out;
  318. }
  319. al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  320. sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);
  321. callchain_cursor_commit(&callchain_cursor);
  322. while (true) {
  323. struct alloc_func key, *caller;
  324. u64 addr;
  325. node = callchain_cursor_current(&callchain_cursor);
  326. if (node == NULL)
  327. break;
  328. key.start = key.end = node->ip;
  329. caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
  330. sizeof(key), callcmp);
  331. if (!caller) {
  332. /* found */
  333. if (node->map)
  334. addr = map__unmap_ip(node->map, node->ip);
  335. else
  336. addr = node->ip;
  337. return addr;
  338. } else
  339. pr_debug3("skipping alloc function: %s\n", caller->name);
  340. callchain_cursor_advance(&callchain_cursor);
  341. }
  342. out:
  343. pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
  344. return sample->ip;
  345. }
  346. struct sort_dimension {
  347. const char name[20];
  348. sort_fn_t cmp;
  349. struct list_head list;
  350. };
  351. static LIST_HEAD(page_alloc_sort_input);
  352. static LIST_HEAD(page_caller_sort_input);
  353. static struct page_stat *
  354. __page_stat__findnew_page(struct page_stat *pstat, bool create)
  355. {
  356. struct rb_node **node = &page_live_tree.rb_node;
  357. struct rb_node *parent = NULL;
  358. struct page_stat *data;
  359. while (*node) {
  360. s64 cmp;
  361. parent = *node;
  362. data = rb_entry(*node, struct page_stat, node);
  363. cmp = data->page - pstat->page;
  364. if (cmp < 0)
  365. node = &parent->rb_left;
  366. else if (cmp > 0)
  367. node = &parent->rb_right;
  368. else
  369. return data;
  370. }
  371. if (!create)
  372. return NULL;
  373. data = zalloc(sizeof(*data));
  374. if (data != NULL) {
  375. data->page = pstat->page;
  376. data->order = pstat->order;
  377. data->gfp_flags = pstat->gfp_flags;
  378. data->migrate_type = pstat->migrate_type;
  379. rb_link_node(&data->node, parent, node);
  380. rb_insert_color(&data->node, &page_live_tree);
  381. }
  382. return data;
  383. }
  384. static struct page_stat *page_stat__find_page(struct page_stat *pstat)
  385. {
  386. return __page_stat__findnew_page(pstat, false);
  387. }
  388. static struct page_stat *page_stat__findnew_page(struct page_stat *pstat)
  389. {
  390. return __page_stat__findnew_page(pstat, true);
  391. }
  392. static struct page_stat *
  393. __page_stat__findnew_alloc(struct page_stat *pstat, bool create)
  394. {
  395. struct rb_node **node = &page_alloc_tree.rb_node;
  396. struct rb_node *parent = NULL;
  397. struct page_stat *data;
  398. struct sort_dimension *sort;
  399. while (*node) {
  400. int cmp = 0;
  401. parent = *node;
  402. data = rb_entry(*node, struct page_stat, node);
  403. list_for_each_entry(sort, &page_alloc_sort_input, list) {
  404. cmp = sort->cmp(pstat, data);
  405. if (cmp)
  406. break;
  407. }
  408. if (cmp < 0)
  409. node = &parent->rb_left;
  410. else if (cmp > 0)
  411. node = &parent->rb_right;
  412. else
  413. return data;
  414. }
  415. if (!create)
  416. return NULL;
  417. data = zalloc(sizeof(*data));
  418. if (data != NULL) {
  419. data->page = pstat->page;
  420. data->order = pstat->order;
  421. data->gfp_flags = pstat->gfp_flags;
  422. data->migrate_type = pstat->migrate_type;
  423. rb_link_node(&data->node, parent, node);
  424. rb_insert_color(&data->node, &page_alloc_tree);
  425. }
  426. return data;
  427. }
  428. static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
  429. {
  430. return __page_stat__findnew_alloc(pstat, false);
  431. }
  432. static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
  433. {
  434. return __page_stat__findnew_alloc(pstat, true);
  435. }
  436. static struct page_stat *
  437. __page_stat__findnew_caller(struct page_stat *pstat, bool create)
  438. {
  439. struct rb_node **node = &page_caller_tree.rb_node;
  440. struct rb_node *parent = NULL;
  441. struct page_stat *data;
  442. struct sort_dimension *sort;
  443. while (*node) {
  444. int cmp = 0;
  445. parent = *node;
  446. data = rb_entry(*node, struct page_stat, node);
  447. list_for_each_entry(sort, &page_caller_sort_input, list) {
  448. cmp = sort->cmp(pstat, data);
  449. if (cmp)
  450. break;
  451. }
  452. if (cmp < 0)
  453. node = &parent->rb_left;
  454. else if (cmp > 0)
  455. node = &parent->rb_right;
  456. else
  457. return data;
  458. }
  459. if (!create)
  460. return NULL;
  461. data = zalloc(sizeof(*data));
  462. if (data != NULL) {
  463. data->callsite = pstat->callsite;
  464. data->order = pstat->order;
  465. data->gfp_flags = pstat->gfp_flags;
  466. data->migrate_type = pstat->migrate_type;
  467. rb_link_node(&data->node, parent, node);
  468. rb_insert_color(&data->node, &page_caller_tree);
  469. }
  470. return data;
  471. }
  472. static struct page_stat *page_stat__find_caller(struct page_stat *pstat)
  473. {
  474. return __page_stat__findnew_caller(pstat, false);
  475. }
  476. static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat)
  477. {
  478. return __page_stat__findnew_caller(pstat, true);
  479. }
  480. static bool valid_page(u64 pfn_or_page)
  481. {
  482. if (use_pfn && pfn_or_page == -1UL)
  483. return false;
  484. if (!use_pfn && pfn_or_page == 0)
  485. return false;
  486. return true;
  487. }
  488. struct gfp_flag {
  489. unsigned int flags;
  490. char *compact_str;
  491. char *human_readable;
  492. };
  493. static struct gfp_flag *gfps;
  494. static int nr_gfps;
  495. static int gfpcmp(const void *a, const void *b)
  496. {
  497. const struct gfp_flag *fa = a;
  498. const struct gfp_flag *fb = b;
  499. return fa->flags - fb->flags;
  500. }
  501. /* see include/trace/events/mmflags.h */
  502. static const struct {
  503. const char *original;
  504. const char *compact;
  505. } gfp_compact_table[] = {
  506. { "GFP_TRANSHUGE", "THP" },
  507. { "GFP_TRANSHUGE_LIGHT", "THL" },
  508. { "GFP_HIGHUSER_MOVABLE", "HUM" },
  509. { "GFP_HIGHUSER", "HU" },
  510. { "GFP_USER", "U" },
  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_WRITE", "WR" },
  539. { "__GFP_RECLAIM", "R" },
  540. { "__GFP_DIRECT_RECLAIM", "DR" },
  541. { "__GFP_KSWAPD_RECLAIM", "KR" },
  542. };
  543. static size_t max_gfp_len;
  544. static char *compact_gfp_flags(char *gfp_flags)
  545. {
  546. char *orig_flags = strdup(gfp_flags);
  547. char *new_flags = NULL;
  548. char *str, *pos = NULL;
  549. size_t len = 0;
  550. if (orig_flags == NULL)
  551. return NULL;
  552. str = strtok_r(orig_flags, "|", &pos);
  553. while (str) {
  554. size_t i;
  555. char *new;
  556. const char *cpt;
  557. for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
  558. if (strcmp(gfp_compact_table[i].original, str))
  559. continue;
  560. cpt = gfp_compact_table[i].compact;
  561. new = realloc(new_flags, len + strlen(cpt) + 2);
  562. if (new == NULL) {
  563. free(new_flags);
  564. free(orig_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. }