builtin-kmem.c 47 KB

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