builtin-kmem.c 46 KB

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