builtin-kmem.c 46 KB

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