blob: 7fd6f1e1e29352a70479e0c015f55797dcffdd34 [file] [log] [blame]
Li Zefanba77c9e2009-11-20 15:53:25 +08001#include "builtin.h"
2#include "perf.h"
3
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03004#include "util/evlist.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03005#include "util/evsel.h"
Li Zefanba77c9e2009-11-20 15:53:25 +08006#include "util/util.h"
Taeung Song41840d22016-06-23 17:55:17 +09007#include "util/config.h"
Li Zefanba77c9e2009-11-20 15:53:25 +08008#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Namhyung Kimc9758cc2015-04-21 13:55:02 +090013#include "util/callchain.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080014
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060015#include <subcmd/parse-options.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080016#include "util/trace-event.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020017#include "util/data.h"
Don Zickus4b627952014-04-07 14:55:23 -040018#include "util/cpumap.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080019
20#include "util/debug.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080021
22#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -030023#include <linux/string.h>
Namhyung Kim77cfe382015-03-23 15:30:40 +090024#include <locale.h>
Namhyung Kimc9758cc2015-04-21 13:55:02 +090025#include <regex.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080026
Namhyung Kim0d68bc92015-04-06 14:36:10 +090027static int kmem_slab;
28static int kmem_page;
29
30static long kmem_page_size;
Namhyung Kim0c160d42015-04-21 13:55:06 +090031static enum {
32 KMEM_SLAB,
33 KMEM_PAGE,
34} kmem_default = KMEM_SLAB; /* for backward compatibility */
Namhyung Kim0d68bc92015-04-06 14:36:10 +090035
Li Zefanba77c9e2009-11-20 15:53:25 +080036struct alloc_stat;
Namhyung Kimfb4f3132015-04-21 13:55:03 +090037typedef int (*sort_fn_t)(void *, void *);
Li Zefanba77c9e2009-11-20 15:53:25 +080038
Li Zefanba77c9e2009-11-20 15:53:25 +080039static int alloc_flag;
40static int caller_flag;
41
Li Zefanba77c9e2009-11-20 15:53:25 +080042static int alloc_lines = -1;
43static int caller_lines = -1;
44
Li Zefan7707b6b2009-11-24 13:25:48 +080045static bool raw_ip;
46
Li Zefanba77c9e2009-11-20 15:53:25 +080047struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080048 u64 call_site;
49 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080050 u64 bytes_req;
51 u64 bytes_alloc;
David Ahernaa58e9a2016-11-25 14:42:13 -070052 u64 last_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080053 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080054 u32 pingpong;
55
56 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080057
58 struct rb_node node;
59};
60
61static struct rb_root root_alloc_stat;
62static struct rb_root root_alloc_sorted;
63static struct rb_root root_caller_stat;
64static struct rb_root root_caller_sorted;
65
David Ahernaa58e9a2016-11-25 14:42:13 -070066static unsigned long total_requested, total_allocated, total_freed;
Li Zefan7d0d3942009-11-24 13:26:31 +080067static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080068
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030069static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
70 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +080071{
72 struct rb_node **node = &root_alloc_stat.rb_node;
73 struct rb_node *parent = NULL;
74 struct alloc_stat *data = NULL;
75
Li Zefanba77c9e2009-11-20 15:53:25 +080076 while (*node) {
77 parent = *node;
78 data = rb_entry(*node, struct alloc_stat, node);
79
80 if (ptr > data->ptr)
81 node = &(*node)->rb_right;
82 else if (ptr < data->ptr)
83 node = &(*node)->rb_left;
84 else
85 break;
86 }
87
88 if (data && data->ptr == ptr) {
89 data->hit++;
90 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +080091 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080092 } else {
93 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030094 if (!data) {
95 pr_err("%s: malloc failed\n", __func__);
96 return -1;
97 }
Li Zefanba77c9e2009-11-20 15:53:25 +080098 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +080099 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800100 data->hit = 1;
101 data->bytes_req = bytes_req;
102 data->bytes_alloc = bytes_alloc;
103
104 rb_link_node(&data->node, parent, node);
105 rb_insert_color(&data->node, &root_alloc_stat);
106 }
Li Zefan079d3f62009-11-24 13:26:55 +0800107 data->call_site = call_site;
108 data->alloc_cpu = cpu;
David Ahernaa58e9a2016-11-25 14:42:13 -0700109 data->last_alloc = bytes_alloc;
110
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300111 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800112}
113
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300114static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800115 int bytes_req, int bytes_alloc)
116{
117 struct rb_node **node = &root_caller_stat.rb_node;
118 struct rb_node *parent = NULL;
119 struct alloc_stat *data = NULL;
120
Li Zefanba77c9e2009-11-20 15:53:25 +0800121 while (*node) {
122 parent = *node;
123 data = rb_entry(*node, struct alloc_stat, node);
124
125 if (call_site > data->call_site)
126 node = &(*node)->rb_right;
127 else if (call_site < data->call_site)
128 node = &(*node)->rb_left;
129 else
130 break;
131 }
132
133 if (data && data->call_site == call_site) {
134 data->hit++;
135 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800136 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800137 } else {
138 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300139 if (!data) {
140 pr_err("%s: malloc failed\n", __func__);
141 return -1;
142 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800143 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800144 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800145 data->hit = 1;
146 data->bytes_req = bytes_req;
147 data->bytes_alloc = bytes_alloc;
148
149 rb_link_node(&data->node, parent, node);
150 rb_insert_color(&data->node, &root_caller_stat);
151 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300152
153 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800154}
155
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300156static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300157 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800158{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300159 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
160 call_site = perf_evsel__intval(evsel, sample, "call_site");
161 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
162 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800163
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300164 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300165 insert_caller_stat(call_site, bytes_req, bytes_alloc))
166 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800167
168 total_requested += bytes_req;
169 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800170
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300171 nr_allocs++;
172 return 0;
173}
174
175static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
176 struct perf_sample *sample)
177{
178 int ret = perf_evsel__process_alloc_event(evsel, sample);
179
180 if (!ret) {
Don Zickus4b627952014-04-07 14:55:23 -0400181 int node1 = cpu__get_node(sample->cpu),
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300182 node2 = perf_evsel__intval(evsel, sample, "node");
183
Li Zefan7d0d3942009-11-24 13:26:31 +0800184 if (node1 != node2)
185 nr_cross_allocs++;
186 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300187
188 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800189}
190
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900191static int ptr_cmp(void *, void *);
192static int slab_callsite_cmp(void *, void *);
Li Zefan079d3f62009-11-24 13:26:55 +0800193
194static struct alloc_stat *search_alloc_stat(unsigned long ptr,
195 unsigned long call_site,
196 struct rb_root *root,
197 sort_fn_t sort_fn)
198{
199 struct rb_node *node = root->rb_node;
200 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
201
202 while (node) {
203 struct alloc_stat *data;
204 int cmp;
205
206 data = rb_entry(node, struct alloc_stat, node);
207
208 cmp = sort_fn(&key, data);
209 if (cmp < 0)
210 node = node->rb_left;
211 else if (cmp > 0)
212 node = node->rb_right;
213 else
214 return data;
215 }
216 return NULL;
217}
218
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300219static int perf_evsel__process_free_event(struct perf_evsel *evsel,
220 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800221{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300222 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800223 struct alloc_stat *s_alloc, *s_caller;
224
Li Zefan079d3f62009-11-24 13:26:55 +0800225 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
226 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300227 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800228
David Ahernaa58e9a2016-11-25 14:42:13 -0700229 total_freed += s_alloc->last_alloc;
230
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300231 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800232 s_alloc->pingpong++;
233
234 s_caller = search_alloc_stat(0, s_alloc->call_site,
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900235 &root_caller_stat,
236 slab_callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300237 if (!s_caller)
238 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800239 s_caller->pingpong++;
240 }
241 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300242
243 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800244}
245
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900246static u64 total_page_alloc_bytes;
247static u64 total_page_free_bytes;
248static u64 total_page_nomatch_bytes;
249static u64 total_page_fail_bytes;
250static unsigned long nr_page_allocs;
251static unsigned long nr_page_frees;
252static unsigned long nr_page_fails;
253static unsigned long nr_page_nomatch;
254
255static bool use_pfn;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900256static bool live_page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900257static struct perf_session *kmem_session;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900258
259#define MAX_MIGRATE_TYPES 6
260#define MAX_PAGE_ORDER 11
261
262static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
263
264struct page_stat {
265 struct rb_node node;
266 u64 page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900267 u64 callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900268 int order;
269 unsigned gfp_flags;
270 unsigned migrate_type;
271 u64 alloc_bytes;
272 u64 free_bytes;
273 int nr_alloc;
274 int nr_free;
275};
276
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900277static struct rb_root page_live_tree;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900278static struct rb_root page_alloc_tree;
279static struct rb_root page_alloc_sorted;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900280static struct rb_root page_caller_tree;
281static struct rb_root page_caller_sorted;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900282
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900283struct alloc_func {
284 u64 start;
285 u64 end;
286 char *name;
287};
288
289static int nr_alloc_funcs;
290static struct alloc_func *alloc_func_list;
291
292static int funcmp(const void *a, const void *b)
293{
294 const struct alloc_func *fa = a;
295 const struct alloc_func *fb = b;
296
297 if (fa->start > fb->start)
298 return 1;
299 else
300 return -1;
301}
302
303static int callcmp(const void *a, const void *b)
304{
305 const struct alloc_func *fa = a;
306 const struct alloc_func *fb = b;
307
308 if (fb->start <= fa->start && fa->end < fb->end)
309 return 0;
310
311 if (fa->start > fb->start)
312 return 1;
313 else
314 return -1;
315}
316
317static int build_alloc_func_list(void)
318{
319 int ret;
320 struct map *kernel_map;
321 struct symbol *sym;
322 struct rb_node *node;
323 struct alloc_func *func;
324 struct machine *machine = &kmem_session->machines.host;
325 regex_t alloc_func_regex;
326 const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
327
328 ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
329 if (ret) {
330 char err[BUFSIZ];
331
332 regerror(ret, &alloc_func_regex, err, sizeof(err));
333 pr_err("Invalid regex: %s\n%s", pattern, err);
334 return -EINVAL;
335 }
336
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300337 kernel_map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300338 if (map__load(kernel_map) < 0) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900339 pr_err("cannot load kernel map\n");
340 return -ENOENT;
341 }
342
343 map__for_each_symbol(kernel_map, sym, node) {
344 if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
345 continue;
346
347 func = realloc(alloc_func_list,
348 (nr_alloc_funcs + 1) * sizeof(*func));
349 if (func == NULL)
350 return -ENOMEM;
351
352 pr_debug("alloc func: %s\n", sym->name);
353 func[nr_alloc_funcs].start = sym->start;
354 func[nr_alloc_funcs].end = sym->end;
355 func[nr_alloc_funcs].name = sym->name;
356
357 alloc_func_list = func;
358 nr_alloc_funcs++;
359 }
360
361 qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
362
363 regfree(&alloc_func_regex);
364 return 0;
365}
366
367/*
368 * Find first non-memory allocation function from callchain.
369 * The allocation functions are in the 'alloc_func_list'.
370 */
371static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample)
372{
373 struct addr_location al;
374 struct machine *machine = &kmem_session->machines.host;
375 struct callchain_cursor_node *node;
376
377 if (alloc_func_list == NULL) {
378 if (build_alloc_func_list() < 0)
379 goto out;
380 }
381
382 al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300383 sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900384
385 callchain_cursor_commit(&callchain_cursor);
386 while (true) {
387 struct alloc_func key, *caller;
388 u64 addr;
389
390 node = callchain_cursor_current(&callchain_cursor);
391 if (node == NULL)
392 break;
393
394 key.start = key.end = node->ip;
395 caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
396 sizeof(key), callcmp);
397 if (!caller) {
398 /* found */
399 if (node->map)
400 addr = map__unmap_ip(node->map, node->ip);
401 else
402 addr = node->ip;
403
404 return addr;
405 } else
406 pr_debug3("skipping alloc function: %s\n", caller->name);
407
408 callchain_cursor_advance(&callchain_cursor);
409 }
410
411out:
412 pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
413 return sample->ip;
414}
415
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900416struct sort_dimension {
417 const char name[20];
418 sort_fn_t cmp;
419 struct list_head list;
420};
421
422static LIST_HEAD(page_alloc_sort_input);
423static LIST_HEAD(page_caller_sort_input);
424
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900425static struct page_stat *
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900426__page_stat__findnew_page(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900427{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900428 struct rb_node **node = &page_live_tree.rb_node;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900429 struct rb_node *parent = NULL;
430 struct page_stat *data;
431
432 while (*node) {
433 s64 cmp;
434
435 parent = *node;
436 data = rb_entry(*node, struct page_stat, node);
437
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900438 cmp = data->page - pstat->page;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900439 if (cmp < 0)
440 node = &parent->rb_left;
441 else if (cmp > 0)
442 node = &parent->rb_right;
443 else
444 return data;
445 }
446
447 if (!create)
448 return NULL;
449
450 data = zalloc(sizeof(*data));
451 if (data != NULL) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900452 data->page = pstat->page;
453 data->order = pstat->order;
454 data->gfp_flags = pstat->gfp_flags;
455 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900456
457 rb_link_node(&data->node, parent, node);
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900458 rb_insert_color(&data->node, &page_live_tree);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900459 }
460
461 return data;
462}
463
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900464static struct page_stat *page_stat__find_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900465{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900466 return __page_stat__findnew_page(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900467}
468
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900469static struct page_stat *page_stat__findnew_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900470{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900471 return __page_stat__findnew_page(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900472}
473
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900474static struct page_stat *
475__page_stat__findnew_alloc(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900476{
477 struct rb_node **node = &page_alloc_tree.rb_node;
478 struct rb_node *parent = NULL;
479 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900480 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900481
482 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900483 int cmp = 0;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900484
485 parent = *node;
486 data = rb_entry(*node, struct page_stat, node);
487
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900488 list_for_each_entry(sort, &page_alloc_sort_input, list) {
489 cmp = sort->cmp(pstat, data);
490 if (cmp)
491 break;
492 }
493
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900494 if (cmp < 0)
495 node = &parent->rb_left;
496 else if (cmp > 0)
497 node = &parent->rb_right;
498 else
499 return data;
500 }
501
502 if (!create)
503 return NULL;
504
505 data = zalloc(sizeof(*data));
506 if (data != NULL) {
David Ahern6b1a2752015-04-14 13:49:33 -0400507 data->page = pstat->page;
508 data->order = pstat->order;
509 data->gfp_flags = pstat->gfp_flags;
510 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900511
512 rb_link_node(&data->node, parent, node);
513 rb_insert_color(&data->node, &page_alloc_tree);
514 }
515
516 return data;
517}
518
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900519static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
520{
521 return __page_stat__findnew_alloc(pstat, false);
522}
523
524static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
525{
526 return __page_stat__findnew_alloc(pstat, true);
527}
528
529static struct page_stat *
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900530__page_stat__findnew_caller(struct page_stat *pstat, bool create)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900531{
532 struct rb_node **node = &page_caller_tree.rb_node;
533 struct rb_node *parent = NULL;
534 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900535 struct sort_dimension *sort;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900536
537 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900538 int cmp = 0;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900539
540 parent = *node;
541 data = rb_entry(*node, struct page_stat, node);
542
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900543 list_for_each_entry(sort, &page_caller_sort_input, list) {
544 cmp = sort->cmp(pstat, data);
545 if (cmp)
546 break;
547 }
548
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900549 if (cmp < 0)
550 node = &parent->rb_left;
551 else if (cmp > 0)
552 node = &parent->rb_right;
553 else
554 return data;
555 }
556
557 if (!create)
558 return NULL;
559
560 data = zalloc(sizeof(*data));
561 if (data != NULL) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900562 data->callsite = pstat->callsite;
563 data->order = pstat->order;
564 data->gfp_flags = pstat->gfp_flags;
565 data->migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900566
567 rb_link_node(&data->node, parent, node);
568 rb_insert_color(&data->node, &page_caller_tree);
569 }
570
571 return data;
572}
573
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900574static struct page_stat *page_stat__find_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900575{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900576 return __page_stat__findnew_caller(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900577}
578
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900579static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900580{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900581 return __page_stat__findnew_caller(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900582}
583
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900584static bool valid_page(u64 pfn_or_page)
585{
586 if (use_pfn && pfn_or_page == -1UL)
587 return false;
588 if (!use_pfn && pfn_or_page == 0)
589 return false;
590 return true;
591}
592
Namhyung Kim0e111152015-04-21 13:55:05 +0900593struct gfp_flag {
594 unsigned int flags;
595 char *compact_str;
596 char *human_readable;
597};
598
599static struct gfp_flag *gfps;
600static int nr_gfps;
601
602static int gfpcmp(const void *a, const void *b)
603{
604 const struct gfp_flag *fa = a;
605 const struct gfp_flag *fb = b;
606
607 return fa->flags - fb->flags;
608}
609
Vlastimil Babka420adbe92016-03-15 14:55:52 -0700610/* see include/trace/events/mmflags.h */
Namhyung Kim0e111152015-04-21 13:55:05 +0900611static const struct {
612 const char *original;
613 const char *compact;
614} gfp_compact_table[] = {
615 { "GFP_TRANSHUGE", "THP" },
Vlastimil Babka25160352016-07-28 15:49:25 -0700616 { "GFP_TRANSHUGE_LIGHT", "THL" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900617 { "GFP_HIGHUSER_MOVABLE", "HUM" },
618 { "GFP_HIGHUSER", "HU" },
619 { "GFP_USER", "U" },
620 { "GFP_TEMPORARY", "TMP" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700621 { "GFP_KERNEL_ACCOUNT", "KAC" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900622 { "GFP_KERNEL", "K" },
623 { "GFP_NOFS", "NF" },
624 { "GFP_ATOMIC", "A" },
625 { "GFP_NOIO", "NI" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900626 { "GFP_NOWAIT", "NW" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700627 { "GFP_DMA", "D" },
628 { "__GFP_HIGHMEM", "HM" },
629 { "GFP_DMA32", "D32" },
630 { "__GFP_HIGH", "H" },
631 { "__GFP_ATOMIC", "_A" },
632 { "__GFP_IO", "I" },
633 { "__GFP_FS", "F" },
634 { "__GFP_COLD", "CO" },
635 { "__GFP_NOWARN", "NWR" },
636 { "__GFP_REPEAT", "R" },
637 { "__GFP_NOFAIL", "NF" },
638 { "__GFP_NORETRY", "NR" },
639 { "__GFP_COMP", "C" },
640 { "__GFP_ZERO", "Z" },
641 { "__GFP_NOMEMALLOC", "NMA" },
642 { "__GFP_MEMALLOC", "MA" },
643 { "__GFP_HARDWALL", "HW" },
644 { "__GFP_THISNODE", "TN" },
645 { "__GFP_RECLAIMABLE", "RC" },
646 { "__GFP_MOVABLE", "M" },
647 { "__GFP_ACCOUNT", "AC" },
648 { "__GFP_NOTRACK", "NT" },
649 { "__GFP_WRITE", "WR" },
650 { "__GFP_RECLAIM", "R" },
651 { "__GFP_DIRECT_RECLAIM", "DR" },
652 { "__GFP_KSWAPD_RECLAIM", "KR" },
653 { "__GFP_OTHER_NODE", "ON" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900654};
655
656static size_t max_gfp_len;
657
658static char *compact_gfp_flags(char *gfp_flags)
659{
660 char *orig_flags = strdup(gfp_flags);
661 char *new_flags = NULL;
Arnaldo Carvalho de Melob2365122015-05-29 09:48:13 -0300662 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900663 size_t len = 0;
664
665 if (orig_flags == NULL)
666 return NULL;
667
668 str = strtok_r(orig_flags, "|", &pos);
669 while (str) {
670 size_t i;
671 char *new;
672 const char *cpt;
673
674 for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
675 if (strcmp(gfp_compact_table[i].original, str))
676 continue;
677
678 cpt = gfp_compact_table[i].compact;
679 new = realloc(new_flags, len + strlen(cpt) + 2);
680 if (new == NULL) {
681 free(new_flags);
682 return NULL;
683 }
684
685 new_flags = new;
686
687 if (!len) {
688 strcpy(new_flags, cpt);
689 } else {
690 strcat(new_flags, "|");
691 strcat(new_flags, cpt);
692 len++;
693 }
694
695 len += strlen(cpt);
696 }
697
698 str = strtok_r(NULL, "|", &pos);
699 }
700
701 if (max_gfp_len < len)
702 max_gfp_len = len;
703
704 free(orig_flags);
705 return new_flags;
706}
707
708static char *compact_gfp_string(unsigned long gfp_flags)
709{
710 struct gfp_flag key = {
711 .flags = gfp_flags,
712 };
713 struct gfp_flag *gfp;
714
715 gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp);
716 if (gfp)
717 return gfp->compact_str;
718
719 return NULL;
720}
721
722static int parse_gfp_flags(struct perf_evsel *evsel, struct perf_sample *sample,
723 unsigned int gfp_flags)
724{
725 struct pevent_record record = {
726 .cpu = sample->cpu,
727 .data = sample->raw_data,
728 .size = sample->raw_size,
729 };
730 struct trace_seq seq;
Arnaldo Carvalho de Melo08a9b982015-05-11 11:41:17 -0300731 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900732
733 if (nr_gfps) {
734 struct gfp_flag key = {
735 .flags = gfp_flags,
736 };
737
738 if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp))
739 return 0;
740 }
741
742 trace_seq_init(&seq);
743 pevent_event_info(&seq, evsel->tp_format, &record);
744
745 str = strtok_r(seq.buffer, " ", &pos);
746 while (str) {
747 if (!strncmp(str, "gfp_flags=", 10)) {
748 struct gfp_flag *new;
749
750 new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps));
751 if (new == NULL)
752 return -ENOMEM;
753
754 gfps = new;
755 new += nr_gfps++;
756
757 new->flags = gfp_flags;
758 new->human_readable = strdup(str + 10);
759 new->compact_str = compact_gfp_flags(str + 10);
760 if (!new->human_readable || !new->compact_str)
761 return -ENOMEM;
762
763 qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp);
764 }
765
766 str = strtok_r(NULL, " ", &pos);
767 }
768
769 trace_seq_destroy(&seq);
770 return 0;
771}
772
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900773static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
774 struct perf_sample *sample)
775{
776 u64 page;
777 unsigned int order = perf_evsel__intval(evsel, sample, "order");
778 unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
779 unsigned int migrate_type = perf_evsel__intval(evsel, sample,
780 "migratetype");
781 u64 bytes = kmem_page_size << order;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900782 u64 callsite;
David Ahern6b1a2752015-04-14 13:49:33 -0400783 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900784 struct page_stat this = {
785 .order = order,
786 .gfp_flags = gfp_flags,
787 .migrate_type = migrate_type,
788 };
789
790 if (use_pfn)
791 page = perf_evsel__intval(evsel, sample, "pfn");
792 else
793 page = perf_evsel__intval(evsel, sample, "page");
794
795 nr_page_allocs++;
796 total_page_alloc_bytes += bytes;
797
798 if (!valid_page(page)) {
799 nr_page_fails++;
800 total_page_fail_bytes += bytes;
801
802 return 0;
803 }
804
Namhyung Kim0e111152015-04-21 13:55:05 +0900805 if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
806 return -1;
807
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900808 callsite = find_callsite(evsel, sample);
809
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900810 /*
811 * This is to find the current page (with correct gfp flags and
812 * migrate type) at free event.
813 */
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900814 this.page = page;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900815 pstat = page_stat__findnew_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400816 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900817 return -ENOMEM;
818
David Ahern6b1a2752015-04-14 13:49:33 -0400819 pstat->nr_alloc++;
820 pstat->alloc_bytes += bytes;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900821 pstat->callsite = callsite;
822
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900823 if (!live_page) {
824 pstat = page_stat__findnew_alloc(&this);
825 if (pstat == NULL)
826 return -ENOMEM;
827
828 pstat->nr_alloc++;
829 pstat->alloc_bytes += bytes;
830 pstat->callsite = callsite;
831 }
832
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900833 this.callsite = callsite;
834 pstat = page_stat__findnew_caller(&this);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900835 if (pstat == NULL)
836 return -ENOMEM;
837
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900838 pstat->nr_alloc++;
839 pstat->alloc_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900840
841 order_stats[order][migrate_type]++;
842
843 return 0;
844}
845
846static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
847 struct perf_sample *sample)
848{
849 u64 page;
850 unsigned int order = perf_evsel__intval(evsel, sample, "order");
851 u64 bytes = kmem_page_size << order;
David Ahern6b1a2752015-04-14 13:49:33 -0400852 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900853 struct page_stat this = {
854 .order = order,
855 };
856
857 if (use_pfn)
858 page = perf_evsel__intval(evsel, sample, "pfn");
859 else
860 page = perf_evsel__intval(evsel, sample, "page");
861
862 nr_page_frees++;
863 total_page_free_bytes += bytes;
864
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900865 this.page = page;
866 pstat = page_stat__find_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400867 if (pstat == NULL) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900868 pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
869 page, order);
870
871 nr_page_nomatch++;
872 total_page_nomatch_bytes += bytes;
873
874 return 0;
875 }
876
David Ahern6b1a2752015-04-14 13:49:33 -0400877 this.gfp_flags = pstat->gfp_flags;
878 this.migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900879 this.callsite = pstat->callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900880
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900881 rb_erase(&pstat->node, &page_live_tree);
David Ahern6b1a2752015-04-14 13:49:33 -0400882 free(pstat);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900883
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900884 if (live_page) {
885 order_stats[this.order][this.migrate_type]--;
886 } else {
887 pstat = page_stat__find_alloc(&this);
888 if (pstat == NULL)
889 return -ENOMEM;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900890
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900891 pstat->nr_free++;
892 pstat->free_bytes += bytes;
893 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900894
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900895 pstat = page_stat__find_caller(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400896 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900897 return -ENOENT;
898
David Ahern6b1a2752015-04-14 13:49:33 -0400899 pstat->nr_free++;
900 pstat->free_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900901
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900902 if (live_page) {
903 pstat->nr_alloc--;
904 pstat->alloc_bytes -= bytes;
905
906 if (pstat->nr_alloc == 0) {
907 rb_erase(&pstat->node, &page_caller_tree);
908 free(pstat);
909 }
910 }
911
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900912 return 0;
913}
914
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300915typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
916 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800917
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300918static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200919 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200920 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300921 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200922 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800923{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300924 int err = 0;
Adrian Hunteref893252013-08-27 11:23:06 +0300925 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900926 sample->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800927
Li Zefanba77c9e2009-11-20 15:53:25 +0800928 if (thread == NULL) {
929 pr_debug("problem processing %d event, skipping it.\n",
930 event->header.type);
931 return -1;
932 }
933
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200934 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800935
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300936 if (evsel->handler != NULL) {
937 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300938 err = f(evsel, sample);
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300939 }
940
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300941 thread__put(thread);
942
943 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800944}
945
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300946static struct perf_tool perf_kmem = {
947 .sample = process_sample_event,
948 .comm = perf_event__process_comm,
Namhyung Kim64c40902014-08-01 14:59:31 +0900949 .mmap = perf_event__process_mmap,
950 .mmap2 = perf_event__process_mmap2,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200951 .ordered_events = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800952};
953
Li Zefanba77c9e2009-11-20 15:53:25 +0800954static double fragmentation(unsigned long n_req, unsigned long n_alloc)
955{
956 if (n_alloc == 0)
957 return 0.0;
958 else
959 return 100.0 - (100.0 * n_req / n_alloc);
960}
961
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900962static void __print_slab_result(struct rb_root *root,
963 struct perf_session *session,
964 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800965{
966 struct rb_node *next;
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300967 struct machine *machine = &session->machines.host;
Li Zefanba77c9e2009-11-20 15:53:25 +0800968
Namhyung Kim65f46e02015-03-12 16:32:48 +0900969 printf("%.105s\n", graph_dotted_line);
Li Zefan079d3f62009-11-24 13:26:55 +0800970 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200971 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Namhyung Kim65f46e02015-03-12 16:32:48 +0900972 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800973
974 next = rb_first(root);
975
976 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200977 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
978 node);
979 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300980 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +0800981 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200982 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800983
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200984 if (is_caller) {
985 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800986 if (!raw_ip)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300987 sym = machine__find_kernel_function(machine, addr, &map);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200988 } else
989 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800990
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200991 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200992 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300993 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200994 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200995 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +0800996 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200997
Namhyung Kim65f46e02015-03-12 16:32:48 +0900998 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +0800999 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +08001000 (unsigned long)data->bytes_alloc / data->hit,
1001 (unsigned long long)data->bytes_req,
1002 (unsigned long)data->bytes_req / data->hit,
1003 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +08001004 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +08001005 fragmentation(data->bytes_req, data->bytes_alloc));
1006
1007 next = rb_next(next);
1008 }
1009
1010 if (n_lines == -1)
Namhyung Kim65f46e02015-03-12 16:32:48 +09001011 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +08001012
Namhyung Kim65f46e02015-03-12 16:32:48 +09001013 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +08001014}
1015
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001016static const char * const migrate_type_str[] = {
1017 "UNMOVABL",
1018 "RECLAIM",
1019 "MOVABLE",
1020 "RESERVED",
1021 "CMA/ISLT",
1022 "UNKNOWN",
1023};
1024
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001025static void __print_page_alloc_result(struct perf_session *session, int n_lines)
Li Zefanba77c9e2009-11-20 15:53:25 +08001026{
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001027 struct rb_node *next = rb_first(&page_alloc_sorted);
1028 struct machine *machine = &session->machines.host;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001029 const char *format;
Namhyung Kim0e111152015-04-21 13:55:05 +09001030 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001031
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001032 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001033 printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1034 use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total",
1035 gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001036 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001037
1038 if (use_pfn)
Namhyung Kim0e111152015-04-21 13:55:05 +09001039 format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001040 else
Namhyung Kim0e111152015-04-21 13:55:05 +09001041 format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001042
1043 while (next && n_lines--) {
1044 struct page_stat *data;
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001045 struct symbol *sym;
1046 struct map *map;
1047 char buf[32];
1048 char *caller = buf;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001049
1050 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001051 sym = machine__find_kernel_function(machine, data->callsite, &map);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001052 if (sym && sym->name)
1053 caller = sym->name;
1054 else
1055 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001056
1057 printf(format, (unsigned long long)data->page,
1058 (unsigned long long)data->alloc_bytes / 1024,
1059 data->nr_alloc, data->order,
1060 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001061 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001062
1063 next = rb_next(next);
1064 }
1065
Namhyung Kim0e111152015-04-21 13:55:05 +09001066 if (n_lines == -1) {
1067 printf(" ... | ... | ... | ... | ... | %-*s | ...\n",
1068 gfp_len, "...");
1069 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001070
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001071 printf("%.105s\n", graph_dotted_line);
1072}
1073
1074static void __print_page_caller_result(struct perf_session *session, int n_lines)
1075{
1076 struct rb_node *next = rb_first(&page_caller_sorted);
1077 struct machine *machine = &session->machines.host;
Namhyung Kim0e111152015-04-21 13:55:05 +09001078 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001079
1080 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001081 printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1082 live_page ? "Live" : "Total", gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001083 printf("%.105s\n", graph_dotted_line);
1084
1085 while (next && n_lines--) {
1086 struct page_stat *data;
1087 struct symbol *sym;
1088 struct map *map;
1089 char buf[32];
1090 char *caller = buf;
1091
1092 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001093 sym = machine__find_kernel_function(machine, data->callsite, &map);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001094 if (sym && sym->name)
1095 caller = sym->name;
1096 else
1097 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
1098
Namhyung Kim0e111152015-04-21 13:55:05 +09001099 printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n",
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001100 (unsigned long long)data->alloc_bytes / 1024,
1101 data->nr_alloc, data->order,
1102 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001103 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001104
1105 next = rb_next(next);
1106 }
1107
Namhyung Kim0e111152015-04-21 13:55:05 +09001108 if (n_lines == -1) {
1109 printf(" ... | ... | ... | ... | %-*s | ...\n",
1110 gfp_len, "...");
1111 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001112
1113 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001114}
1115
Namhyung Kim0e111152015-04-21 13:55:05 +09001116static void print_gfp_flags(void)
1117{
1118 int i;
1119
1120 printf("#\n");
1121 printf("# GFP flags\n");
1122 printf("# ---------\n");
1123 for (i = 0; i < nr_gfps; i++) {
1124 printf("# %08x: %*s: %s\n", gfps[i].flags,
1125 (int) max_gfp_len, gfps[i].compact_str,
1126 gfps[i].human_readable);
1127 }
1128}
1129
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001130static void print_slab_summary(void)
1131{
1132 printf("\nSUMMARY (SLAB allocator)");
1133 printf("\n========================\n");
Namhyung Kim77cfe382015-03-23 15:30:40 +09001134 printf("Total bytes requested: %'lu\n", total_requested);
1135 printf("Total bytes allocated: %'lu\n", total_allocated);
David Ahernaa58e9a2016-11-25 14:42:13 -07001136 printf("Total bytes freed: %'lu\n", total_freed);
1137 if (total_allocated > total_freed) {
1138 printf("Net total bytes allocated: %'lu\n",
1139 total_allocated - total_freed);
1140 }
Namhyung Kim77cfe382015-03-23 15:30:40 +09001141 printf("Total bytes wasted on internal fragmentation: %'lu\n",
Li Zefanba77c9e2009-11-20 15:53:25 +08001142 total_allocated - total_requested);
1143 printf("Internal fragmentation: %f%%\n",
1144 fragmentation(total_requested, total_allocated));
Namhyung Kim77cfe382015-03-23 15:30:40 +09001145 printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +08001146}
1147
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001148static void print_page_summary(void)
1149{
1150 int o, m;
1151 u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
1152 u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
1153
1154 printf("\nSUMMARY (page allocator)");
1155 printf("\n========================\n");
1156 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests",
1157 nr_page_allocs, total_page_alloc_bytes / 1024);
1158 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests",
1159 nr_page_frees, total_page_free_bytes / 1024);
1160 printf("\n");
1161
Will Deacon6145c252015-04-23 14:40:37 +01001162 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001163 nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
Will Deacon6145c252015-04-23 14:40:37 +01001164 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001165 nr_page_allocs - nr_alloc_freed,
1166 (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
1167 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests",
1168 nr_page_nomatch, total_page_nomatch_bytes / 1024);
1169 printf("\n");
1170
1171 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures",
1172 nr_page_fails, total_page_fail_bytes / 1024);
1173 printf("\n");
1174
1175 printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable",
1176 "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
1177 printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line,
1178 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1179 graph_dotted_line, graph_dotted_line);
1180
1181 for (o = 0; o < MAX_PAGE_ORDER; o++) {
1182 printf("%5d", o);
1183 for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
1184 if (order_stats[o][m])
1185 printf(" %'12d", order_stats[o][m]);
1186 else
1187 printf(" %12c", '.');
1188 }
1189 printf("\n");
1190 }
1191}
1192
1193static void print_slab_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001194{
1195 if (caller_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001196 __print_slab_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001197 if (alloc_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001198 __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
1199 print_slab_summary();
1200}
1201
1202static void print_page_result(struct perf_session *session)
1203{
Namhyung Kim0e111152015-04-21 13:55:05 +09001204 if (caller_flag || alloc_flag)
1205 print_gfp_flags();
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001206 if (caller_flag)
1207 __print_page_caller_result(session, caller_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001208 if (alloc_flag)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001209 __print_page_alloc_result(session, alloc_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001210 print_page_summary();
1211}
1212
1213static void print_result(struct perf_session *session)
1214{
1215 if (kmem_slab)
1216 print_slab_result(session);
1217 if (kmem_page)
1218 print_page_result(session);
Li Zefanba77c9e2009-11-20 15:53:25 +08001219}
1220
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001221static LIST_HEAD(slab_caller_sort);
1222static LIST_HEAD(slab_alloc_sort);
1223static LIST_HEAD(page_caller_sort);
1224static LIST_HEAD(page_alloc_sort);
Li Zefan29b3e152009-11-24 13:26:10 +08001225
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001226static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
1227 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001228{
1229 struct rb_node **new = &(root->rb_node);
1230 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +08001231 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +08001232
1233 while (*new) {
1234 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +08001235 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +08001236
1237 this = rb_entry(*new, struct alloc_stat, node);
1238 parent = *new;
1239
Li Zefan29b3e152009-11-24 13:26:10 +08001240 list_for_each_entry(sort, sort_list, list) {
1241 cmp = sort->cmp(data, this);
1242 if (cmp)
1243 break;
1244 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001245
1246 if (cmp > 0)
1247 new = &((*new)->rb_left);
1248 else
1249 new = &((*new)->rb_right);
1250 }
1251
1252 rb_link_node(&data->node, parent, new);
1253 rb_insert_color(&data->node, root);
1254}
1255
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001256static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
1257 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001258{
1259 struct rb_node *node;
1260 struct alloc_stat *data;
1261
1262 for (;;) {
1263 node = rb_first(root);
1264 if (!node)
1265 break;
1266
1267 rb_erase(node, root);
1268 data = rb_entry(node, struct alloc_stat, node);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001269 sort_slab_insert(root_sorted, data, sort_list);
1270 }
1271}
1272
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001273static void sort_page_insert(struct rb_root *root, struct page_stat *data,
1274 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001275{
1276 struct rb_node **new = &root->rb_node;
1277 struct rb_node *parent = NULL;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001278 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001279
1280 while (*new) {
1281 struct page_stat *this;
1282 int cmp = 0;
1283
1284 this = rb_entry(*new, struct page_stat, node);
1285 parent = *new;
1286
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001287 list_for_each_entry(sort, sort_list, list) {
1288 cmp = sort->cmp(data, this);
1289 if (cmp)
1290 break;
1291 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001292
1293 if (cmp > 0)
1294 new = &parent->rb_left;
1295 else
1296 new = &parent->rb_right;
1297 }
1298
1299 rb_link_node(&data->node, parent, new);
1300 rb_insert_color(&data->node, root);
1301}
1302
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001303static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted,
1304 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001305{
1306 struct rb_node *node;
1307 struct page_stat *data;
1308
1309 for (;;) {
1310 node = rb_first(root);
1311 if (!node)
1312 break;
1313
1314 rb_erase(node, root);
1315 data = rb_entry(node, struct page_stat, node);
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001316 sort_page_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +08001317 }
1318}
1319
1320static void sort_result(void)
1321{
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001322 if (kmem_slab) {
1323 __sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001324 &slab_alloc_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001325 __sort_slab_result(&root_caller_stat, &root_caller_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001326 &slab_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001327 }
1328 if (kmem_page) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001329 if (live_page)
1330 __sort_page_result(&page_live_tree, &page_alloc_sorted,
1331 &page_alloc_sort);
1332 else
1333 __sort_page_result(&page_alloc_tree, &page_alloc_sorted,
1334 &page_alloc_sort);
1335
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001336 __sort_page_result(&page_caller_tree, &page_caller_sorted,
1337 &page_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001338 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001339}
1340
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001341static int __cmd_kmem(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001342{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001343 int err = -EINVAL;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001344 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001345 const struct perf_evsel_str_handler kmem_tracepoints[] = {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001346 /* slab allocator */
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001347 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
1348 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
1349 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
1350 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
1351 { "kmem:kfree", perf_evsel__process_free_event, },
1352 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001353 /* page allocator */
1354 { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, },
1355 { "kmem:mm_page_free", perf_evsel__process_page_free_event, },
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001356 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001357
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001358 if (!perf_session__has_traces(session, "kmem record"))
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001359 goto out;
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001360
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001361 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
1362 pr_err("Initializing perf session tracepoint handlers failed\n");
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001363 goto out;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001364 }
1365
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001366 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001367 if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") &&
1368 perf_evsel__field(evsel, "pfn")) {
1369 use_pfn = true;
1370 break;
1371 }
1372 }
1373
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001374 setup_pager();
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001375 err = perf_session__process_events(session);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001376 if (err != 0) {
1377 pr_err("error during process events: %d\n", err);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001378 goto out;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001379 }
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001380 sort_result();
1381 print_result(session);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001382out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001383 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +08001384}
1385
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001386/* slab sort keys */
1387static int ptr_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001388{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001389 struct alloc_stat *l = a;
1390 struct alloc_stat *r = b;
1391
Li Zefanba77c9e2009-11-20 15:53:25 +08001392 if (l->ptr < r->ptr)
1393 return -1;
1394 else if (l->ptr > r->ptr)
1395 return 1;
1396 return 0;
1397}
1398
Li Zefan29b3e152009-11-24 13:26:10 +08001399static struct sort_dimension ptr_sort_dimension = {
1400 .name = "ptr",
1401 .cmp = ptr_cmp,
1402};
1403
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001404static int slab_callsite_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001405{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001406 struct alloc_stat *l = a;
1407 struct alloc_stat *r = b;
1408
Li Zefanba77c9e2009-11-20 15:53:25 +08001409 if (l->call_site < r->call_site)
1410 return -1;
1411 else if (l->call_site > r->call_site)
1412 return 1;
1413 return 0;
1414}
1415
Li Zefan29b3e152009-11-24 13:26:10 +08001416static struct sort_dimension callsite_sort_dimension = {
1417 .name = "callsite",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001418 .cmp = slab_callsite_cmp,
Li Zefan29b3e152009-11-24 13:26:10 +08001419};
1420
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001421static int hit_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001422{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001423 struct alloc_stat *l = a;
1424 struct alloc_stat *r = b;
1425
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001426 if (l->hit < r->hit)
1427 return -1;
1428 else if (l->hit > r->hit)
1429 return 1;
1430 return 0;
1431}
1432
Li Zefan29b3e152009-11-24 13:26:10 +08001433static struct sort_dimension hit_sort_dimension = {
1434 .name = "hit",
1435 .cmp = hit_cmp,
1436};
1437
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001438static int bytes_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001439{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001440 struct alloc_stat *l = a;
1441 struct alloc_stat *r = b;
1442
Li Zefanba77c9e2009-11-20 15:53:25 +08001443 if (l->bytes_alloc < r->bytes_alloc)
1444 return -1;
1445 else if (l->bytes_alloc > r->bytes_alloc)
1446 return 1;
1447 return 0;
1448}
1449
Li Zefan29b3e152009-11-24 13:26:10 +08001450static struct sort_dimension bytes_sort_dimension = {
1451 .name = "bytes",
1452 .cmp = bytes_cmp,
1453};
1454
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001455static int frag_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001456{
1457 double x, y;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001458 struct alloc_stat *l = a;
1459 struct alloc_stat *r = b;
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001460
1461 x = fragmentation(l->bytes_req, l->bytes_alloc);
1462 y = fragmentation(r->bytes_req, r->bytes_alloc);
1463
1464 if (x < y)
1465 return -1;
1466 else if (x > y)
1467 return 1;
1468 return 0;
1469}
1470
Li Zefan29b3e152009-11-24 13:26:10 +08001471static struct sort_dimension frag_sort_dimension = {
1472 .name = "frag",
1473 .cmp = frag_cmp,
1474};
1475
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001476static int pingpong_cmp(void *a, void *b)
Li Zefan079d3f62009-11-24 13:26:55 +08001477{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001478 struct alloc_stat *l = a;
1479 struct alloc_stat *r = b;
1480
Li Zefan079d3f62009-11-24 13:26:55 +08001481 if (l->pingpong < r->pingpong)
1482 return -1;
1483 else if (l->pingpong > r->pingpong)
1484 return 1;
1485 return 0;
1486}
1487
1488static struct sort_dimension pingpong_sort_dimension = {
1489 .name = "pingpong",
1490 .cmp = pingpong_cmp,
1491};
1492
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001493/* page sort keys */
1494static int page_cmp(void *a, void *b)
1495{
1496 struct page_stat *l = a;
1497 struct page_stat *r = b;
1498
1499 if (l->page < r->page)
1500 return -1;
1501 else if (l->page > r->page)
1502 return 1;
1503 return 0;
1504}
1505
1506static struct sort_dimension page_sort_dimension = {
1507 .name = "page",
1508 .cmp = page_cmp,
1509};
1510
1511static int page_callsite_cmp(void *a, void *b)
1512{
1513 struct page_stat *l = a;
1514 struct page_stat *r = b;
1515
1516 if (l->callsite < r->callsite)
1517 return -1;
1518 else if (l->callsite > r->callsite)
1519 return 1;
1520 return 0;
1521}
1522
1523static struct sort_dimension page_callsite_sort_dimension = {
1524 .name = "callsite",
1525 .cmp = page_callsite_cmp,
1526};
1527
1528static int page_hit_cmp(void *a, void *b)
1529{
1530 struct page_stat *l = a;
1531 struct page_stat *r = b;
1532
1533 if (l->nr_alloc < r->nr_alloc)
1534 return -1;
1535 else if (l->nr_alloc > r->nr_alloc)
1536 return 1;
1537 return 0;
1538}
1539
1540static struct sort_dimension page_hit_sort_dimension = {
1541 .name = "hit",
1542 .cmp = page_hit_cmp,
1543};
1544
1545static int page_bytes_cmp(void *a, void *b)
1546{
1547 struct page_stat *l = a;
1548 struct page_stat *r = b;
1549
1550 if (l->alloc_bytes < r->alloc_bytes)
1551 return -1;
1552 else if (l->alloc_bytes > r->alloc_bytes)
1553 return 1;
1554 return 0;
1555}
1556
1557static struct sort_dimension page_bytes_sort_dimension = {
1558 .name = "bytes",
1559 .cmp = page_bytes_cmp,
1560};
1561
1562static int page_order_cmp(void *a, void *b)
1563{
1564 struct page_stat *l = a;
1565 struct page_stat *r = b;
1566
1567 if (l->order < r->order)
1568 return -1;
1569 else if (l->order > r->order)
1570 return 1;
1571 return 0;
1572}
1573
1574static struct sort_dimension page_order_sort_dimension = {
1575 .name = "order",
1576 .cmp = page_order_cmp,
1577};
1578
1579static int migrate_type_cmp(void *a, void *b)
1580{
1581 struct page_stat *l = a;
1582 struct page_stat *r = b;
1583
1584 /* for internal use to find free'd page */
1585 if (l->migrate_type == -1U)
1586 return 0;
1587
1588 if (l->migrate_type < r->migrate_type)
1589 return -1;
1590 else if (l->migrate_type > r->migrate_type)
1591 return 1;
1592 return 0;
1593}
1594
1595static struct sort_dimension migrate_type_sort_dimension = {
1596 .name = "migtype",
1597 .cmp = migrate_type_cmp,
1598};
1599
1600static int gfp_flags_cmp(void *a, void *b)
1601{
1602 struct page_stat *l = a;
1603 struct page_stat *r = b;
1604
1605 /* for internal use to find free'd page */
1606 if (l->gfp_flags == -1U)
1607 return 0;
1608
1609 if (l->gfp_flags < r->gfp_flags)
1610 return -1;
1611 else if (l->gfp_flags > r->gfp_flags)
1612 return 1;
1613 return 0;
1614}
1615
1616static struct sort_dimension gfp_flags_sort_dimension = {
1617 .name = "gfp",
1618 .cmp = gfp_flags_cmp,
1619};
1620
1621static struct sort_dimension *slab_sorts[] = {
Li Zefan29b3e152009-11-24 13:26:10 +08001622 &ptr_sort_dimension,
1623 &callsite_sort_dimension,
1624 &hit_sort_dimension,
1625 &bytes_sort_dimension,
1626 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +08001627 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +08001628};
1629
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001630static struct sort_dimension *page_sorts[] = {
1631 &page_sort_dimension,
1632 &page_callsite_sort_dimension,
1633 &page_hit_sort_dimension,
1634 &page_bytes_sort_dimension,
1635 &page_order_sort_dimension,
1636 &migrate_type_sort_dimension,
1637 &gfp_flags_sort_dimension,
1638};
Li Zefan29b3e152009-11-24 13:26:10 +08001639
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001640static int slab_sort_dimension__add(const char *tok, struct list_head *list)
Li Zefan29b3e152009-11-24 13:26:10 +08001641{
1642 struct sort_dimension *sort;
1643 int i;
1644
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001645 for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) {
1646 if (!strcmp(slab_sorts[i]->name, tok)) {
1647 sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i]));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001648 if (!sort) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -03001649 pr_err("%s: memdup failed\n", __func__);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001650 return -1;
1651 }
Li Zefan29b3e152009-11-24 13:26:10 +08001652 list_add_tail(&sort->list, list);
1653 return 0;
1654 }
1655 }
1656
1657 return -1;
1658}
1659
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001660static int page_sort_dimension__add(const char *tok, struct list_head *list)
1661{
1662 struct sort_dimension *sort;
1663 int i;
1664
1665 for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) {
1666 if (!strcmp(page_sorts[i]->name, tok)) {
1667 sort = memdup(page_sorts[i], sizeof(*page_sorts[i]));
1668 if (!sort) {
1669 pr_err("%s: memdup failed\n", __func__);
1670 return -1;
1671 }
1672 list_add_tail(&sort->list, list);
1673 return 0;
1674 }
1675 }
1676
1677 return -1;
1678}
1679
1680static int setup_slab_sorting(struct list_head *sort_list, const char *arg)
Li Zefan29b3e152009-11-24 13:26:10 +08001681{
1682 char *tok;
1683 char *str = strdup(arg);
Namhyung Kim405f8752015-03-12 16:32:46 +09001684 char *pos = str;
Li Zefan29b3e152009-11-24 13:26:10 +08001685
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001686 if (!str) {
1687 pr_err("%s: strdup failed\n", __func__);
1688 return -1;
1689 }
Li Zefan29b3e152009-11-24 13:26:10 +08001690
1691 while (true) {
Namhyung Kim405f8752015-03-12 16:32:46 +09001692 tok = strsep(&pos, ",");
Li Zefan29b3e152009-11-24 13:26:10 +08001693 if (!tok)
1694 break;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001695 if (slab_sort_dimension__add(tok, sort_list) < 0) {
1696 error("Unknown slab --sort key: '%s'", tok);
1697 free(str);
1698 return -1;
1699 }
1700 }
1701
1702 free(str);
1703 return 0;
1704}
1705
1706static int setup_page_sorting(struct list_head *sort_list, const char *arg)
1707{
1708 char *tok;
1709 char *str = strdup(arg);
1710 char *pos = str;
1711
1712 if (!str) {
1713 pr_err("%s: strdup failed\n", __func__);
1714 return -1;
1715 }
1716
1717 while (true) {
1718 tok = strsep(&pos, ",");
1719 if (!tok)
1720 break;
1721 if (page_sort_dimension__add(tok, sort_list) < 0) {
1722 error("Unknown page --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +09001723 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +08001724 return -1;
1725 }
1726 }
1727
1728 free(str);
1729 return 0;
1730}
1731
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001732static int parse_sort_opt(const struct option *opt __maybe_unused,
1733 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001734{
Li Zefanba77c9e2009-11-20 15:53:25 +08001735 if (!arg)
1736 return -1;
1737
Namhyung Kim0c160d42015-04-21 13:55:06 +09001738 if (kmem_page > kmem_slab ||
1739 (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001740 if (caller_flag > alloc_flag)
1741 return setup_page_sorting(&page_caller_sort, arg);
1742 else
1743 return setup_page_sorting(&page_alloc_sort, arg);
1744 } else {
1745 if (caller_flag > alloc_flag)
1746 return setup_slab_sorting(&slab_caller_sort, arg);
1747 else
1748 return setup_slab_sorting(&slab_alloc_sort, arg);
1749 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001750
1751 return 0;
1752}
1753
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001754static int parse_caller_opt(const struct option *opt __maybe_unused,
1755 const char *arg __maybe_unused,
1756 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001757{
Li Zefan90b86a92009-12-10 15:21:57 +08001758 caller_flag = (alloc_flag + 1);
1759 return 0;
1760}
Li Zefanba77c9e2009-11-20 15:53:25 +08001761
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001762static int parse_alloc_opt(const struct option *opt __maybe_unused,
1763 const char *arg __maybe_unused,
1764 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +08001765{
1766 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001767 return 0;
1768}
1769
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001770static int parse_slab_opt(const struct option *opt __maybe_unused,
1771 const char *arg __maybe_unused,
1772 int unset __maybe_unused)
1773{
1774 kmem_slab = (kmem_page + 1);
1775 return 0;
1776}
1777
1778static int parse_page_opt(const struct option *opt __maybe_unused,
1779 const char *arg __maybe_unused,
1780 int unset __maybe_unused)
1781{
1782 kmem_page = (kmem_slab + 1);
1783 return 0;
1784}
1785
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001786static int parse_line_opt(const struct option *opt __maybe_unused,
1787 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001788{
1789 int lines;
1790
1791 if (!arg)
1792 return -1;
1793
1794 lines = strtoul(arg, NULL, 10);
1795
1796 if (caller_flag > alloc_flag)
1797 caller_lines = lines;
1798 else
1799 alloc_lines = lines;
1800
1801 return 0;
1802}
1803
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001804static int __cmd_record(int argc, const char **argv)
1805{
1806 const char * const record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001807 "record", "-a", "-R", "-c", "1",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001808 };
1809 const char * const slab_events[] = {
Li Zefanba77c9e2009-11-20 15:53:25 +08001810 "-e", "kmem:kmalloc",
1811 "-e", "kmem:kmalloc_node",
1812 "-e", "kmem:kfree",
1813 "-e", "kmem:kmem_cache_alloc",
1814 "-e", "kmem:kmem_cache_alloc_node",
1815 "-e", "kmem:kmem_cache_free",
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001816 };
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001817 const char * const page_events[] = {
1818 "-e", "kmem:mm_page_alloc",
1819 "-e", "kmem:mm_page_free",
1820 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001821 unsigned int rec_argc, i, j;
1822 const char **rec_argv;
1823
1824 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001825 if (kmem_slab)
1826 rec_argc += ARRAY_SIZE(slab_events);
1827 if (kmem_page)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001828 rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001829
Li Zefanba77c9e2009-11-20 15:53:25 +08001830 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1831
Chris Samuelce47dc52010-11-13 13:35:06 +11001832 if (rec_argv == NULL)
1833 return -ENOMEM;
1834
Li Zefanba77c9e2009-11-20 15:53:25 +08001835 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1836 rec_argv[i] = strdup(record_args[i]);
1837
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001838 if (kmem_slab) {
1839 for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
1840 rec_argv[i] = strdup(slab_events[j]);
1841 }
1842 if (kmem_page) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001843 rec_argv[i++] = strdup("-g");
1844
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001845 for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
1846 rec_argv[i] = strdup(page_events[j]);
1847 }
1848
Li Zefanba77c9e2009-11-20 15:53:25 +08001849 for (j = 1; j < (unsigned int)argc; j++, i++)
1850 rec_argv[i] = argv[j];
1851
1852 return cmd_record(i, rec_argv, NULL);
1853}
1854
Wang Nanb8cbb342016-02-26 09:31:51 +00001855static int kmem_config(const char *var, const char *value, void *cb __maybe_unused)
Namhyung Kim0c160d42015-04-21 13:55:06 +09001856{
1857 if (!strcmp(var, "kmem.default")) {
1858 if (!strcmp(value, "slab"))
1859 kmem_default = KMEM_SLAB;
1860 else if (!strcmp(value, "page"))
1861 kmem_default = KMEM_PAGE;
1862 else
1863 pr_err("invalid default value ('slab' or 'page' required): %s\n",
1864 value);
1865 return 0;
1866 }
1867
Wang Nanb8cbb342016-02-26 09:31:51 +00001868 return 0;
Namhyung Kim0c160d42015-04-21 13:55:06 +09001869}
1870
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001871int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001872{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001873 const char * const default_slab_sort = "frag,hit,bytes";
1874 const char * const default_page_sort = "bytes,hit";
Yunlong Songd1eeb772015-04-02 21:47:12 +08001875 struct perf_data_file file = {
Yunlong Songd1eeb772015-04-02 21:47:12 +08001876 .mode = PERF_DATA_MODE_READ,
1877 };
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001878 const struct option kmem_options[] = {
1879 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Namhyung Kimbd72a332015-03-12 16:32:47 +09001880 OPT_INCR('v', "verbose", &verbose,
1881 "be more verbose (show symbol address, etc)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001882 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
1883 "show per-callsite statistics", parse_caller_opt),
1884 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
1885 "show per-allocation statistics", parse_alloc_opt),
1886 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001887 "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, "
1888 "page, order, migtype, gfp", parse_sort_opt),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001889 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
1890 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Yunlong Songd1eeb772015-04-02 21:47:12 +08001891 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001892 OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
1893 parse_slab_opt),
1894 OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
1895 parse_page_opt),
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001896 OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001897 OPT_END()
1898 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001899 const char *const kmem_subcommands[] = { "record", "stat", NULL };
1900 const char *kmem_usage[] = {
1901 NULL,
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001902 NULL
1903 };
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001904 struct perf_session *session;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001905 int ret = -1;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001906 const char errmsg[] = "No %s allocation events found. Have you run 'perf kmem record --%s'?\n";
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001907
Namhyung Kim0c160d42015-04-21 13:55:06 +09001908 perf_config(kmem_config, NULL);
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001909 argc = parse_options_subcommand(argc, argv, kmem_options,
1910 kmem_subcommands, kmem_usage, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +08001911
Li Zefan90b86a92009-12-10 15:21:57 +08001912 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +08001913 usage_with_options(kmem_usage, kmem_options);
1914
Namhyung Kim0c160d42015-04-21 13:55:06 +09001915 if (kmem_slab == 0 && kmem_page == 0) {
1916 if (kmem_default == KMEM_SLAB)
1917 kmem_slab = 1;
1918 else
1919 kmem_page = 1;
1920 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001921
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001922 if (!strncmp(argv[0], "rec", 3)) {
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001923 symbol__init(NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001924 return __cmd_record(argc, argv);
1925 }
1926
Jiri Olsa28939e12015-04-06 14:36:08 +09001927 file.path = input_name;
1928
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001929 kmem_session = session = perf_session__new(&file, false, &perf_kmem);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001930 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001931 return -1;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001932
Namhyung Kima923e2c2015-05-05 23:52:52 +09001933 if (kmem_slab) {
1934 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
1935 "kmem:kmalloc")) {
1936 pr_err(errmsg, "slab", "slab");
Taeung Song249ca1a2015-06-30 17:15:21 +09001937 goto out_delete;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001938 }
1939 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001940
Namhyung Kima923e2c2015-05-05 23:52:52 +09001941 if (kmem_page) {
1942 struct perf_evsel *evsel;
1943
1944 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
1945 "kmem:mm_page_alloc");
1946 if (evsel == NULL) {
1947 pr_err(errmsg, "page", "page");
Taeung Song249ca1a2015-06-30 17:15:21 +09001948 goto out_delete;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001949 }
1950
1951 kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001952 symbol_conf.use_callchain = true;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001953 }
1954
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001955 symbol__init(&session->header.env);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001956
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001957 if (!strcmp(argv[0], "stat")) {
Namhyung Kim77cfe382015-03-23 15:30:40 +09001958 setlocale(LC_ALL, "");
1959
Don Zickus4b627952014-04-07 14:55:23 -04001960 if (cpu__setup_cpunode_map())
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001961 goto out_delete;
Li Zefanba77c9e2009-11-20 15:53:25 +08001962
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001963 if (list_empty(&slab_caller_sort))
1964 setup_slab_sorting(&slab_caller_sort, default_slab_sort);
1965 if (list_empty(&slab_alloc_sort))
1966 setup_slab_sorting(&slab_alloc_sort, default_slab_sort);
1967 if (list_empty(&page_caller_sort))
1968 setup_page_sorting(&page_caller_sort, default_page_sort);
1969 if (list_empty(&page_alloc_sort))
1970 setup_page_sorting(&page_alloc_sort, default_page_sort);
Li Zefan7d0d3942009-11-24 13:26:31 +08001971
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001972 if (kmem_page) {
1973 setup_page_sorting(&page_alloc_sort_input,
1974 "page,order,migtype,gfp");
1975 setup_page_sorting(&page_caller_sort_input,
1976 "callsite,order,migtype,gfp");
1977 }
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001978 ret = __cmd_kmem(session);
Pekka Enbergb00eca82010-01-19 19:26:11 +02001979 } else
1980 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +08001981
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001982out_delete:
1983 perf_session__delete(session);
1984
1985 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +08001986}
1987