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