blob: fa6bf1c39b65efe21e64b131ce5ef9850ee6a399 [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"
David Ahern2a865bd2016-11-29 10:15:45 -070014#include "util/time-utils.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080015
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060016#include <subcmd/parse-options.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080017#include "util/trace-event.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020018#include "util/data.h"
Don Zickus4b627952014-04-07 14:55:23 -040019#include "util/cpumap.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080020
21#include "util/debug.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080022
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030023#include <linux/kernel.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080024#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -030025#include <linux/string.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030026#include <inttypes.h>
Namhyung Kim77cfe382015-03-23 15:30:40 +090027#include <locale.h>
Namhyung Kimc9758cc2015-04-21 13:55:02 +090028#include <regex.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080029
Namhyung Kim0d68bc92015-04-06 14:36:10 +090030static int kmem_slab;
31static int kmem_page;
32
33static long kmem_page_size;
Namhyung Kim0c160d42015-04-21 13:55:06 +090034static enum {
35 KMEM_SLAB,
36 KMEM_PAGE,
37} kmem_default = KMEM_SLAB; /* for backward compatibility */
Namhyung Kim0d68bc92015-04-06 14:36:10 +090038
Li Zefanba77c9e2009-11-20 15:53:25 +080039struct alloc_stat;
Namhyung Kimfb4f3132015-04-21 13:55:03 +090040typedef int (*sort_fn_t)(void *, void *);
Li Zefanba77c9e2009-11-20 15:53:25 +080041
Li Zefanba77c9e2009-11-20 15:53:25 +080042static int alloc_flag;
43static int caller_flag;
44
Li Zefanba77c9e2009-11-20 15:53:25 +080045static int alloc_lines = -1;
46static int caller_lines = -1;
47
Li Zefan7707b6b2009-11-24 13:25:48 +080048static bool raw_ip;
49
Li Zefanba77c9e2009-11-20 15:53:25 +080050struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080051 u64 call_site;
52 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080053 u64 bytes_req;
54 u64 bytes_alloc;
David Ahernaa58e9a2016-11-25 14:42:13 -070055 u64 last_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080056 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080057 u32 pingpong;
58
59 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080060
61 struct rb_node node;
62};
63
64static struct rb_root root_alloc_stat;
65static struct rb_root root_alloc_sorted;
66static struct rb_root root_caller_stat;
67static struct rb_root root_caller_sorted;
68
David Ahernaa58e9a2016-11-25 14:42:13 -070069static unsigned long total_requested, total_allocated, total_freed;
Li Zefan7d0d3942009-11-24 13:26:31 +080070static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080071
David Ahern2a865bd2016-11-29 10:15:45 -070072/* filters for controlling start and stop of time of analysis */
73static struct perf_time_interval ptime;
74const char *time_str;
75
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030076static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
77 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +080078{
79 struct rb_node **node = &root_alloc_stat.rb_node;
80 struct rb_node *parent = NULL;
81 struct alloc_stat *data = NULL;
82
Li Zefanba77c9e2009-11-20 15:53:25 +080083 while (*node) {
84 parent = *node;
85 data = rb_entry(*node, struct alloc_stat, node);
86
87 if (ptr > data->ptr)
88 node = &(*node)->rb_right;
89 else if (ptr < data->ptr)
90 node = &(*node)->rb_left;
91 else
92 break;
93 }
94
95 if (data && data->ptr == ptr) {
96 data->hit++;
97 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +080098 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080099 } else {
100 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300101 if (!data) {
102 pr_err("%s: malloc failed\n", __func__);
103 return -1;
104 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800105 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +0800106 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800107 data->hit = 1;
108 data->bytes_req = bytes_req;
109 data->bytes_alloc = bytes_alloc;
110
111 rb_link_node(&data->node, parent, node);
112 rb_insert_color(&data->node, &root_alloc_stat);
113 }
Li Zefan079d3f62009-11-24 13:26:55 +0800114 data->call_site = call_site;
115 data->alloc_cpu = cpu;
David Ahernaa58e9a2016-11-25 14:42:13 -0700116 data->last_alloc = bytes_alloc;
117
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300118 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800119}
120
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300121static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800122 int bytes_req, int bytes_alloc)
123{
124 struct rb_node **node = &root_caller_stat.rb_node;
125 struct rb_node *parent = NULL;
126 struct alloc_stat *data = NULL;
127
Li Zefanba77c9e2009-11-20 15:53:25 +0800128 while (*node) {
129 parent = *node;
130 data = rb_entry(*node, struct alloc_stat, node);
131
132 if (call_site > data->call_site)
133 node = &(*node)->rb_right;
134 else if (call_site < data->call_site)
135 node = &(*node)->rb_left;
136 else
137 break;
138 }
139
140 if (data && data->call_site == call_site) {
141 data->hit++;
142 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800143 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800144 } else {
145 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300146 if (!data) {
147 pr_err("%s: malloc failed\n", __func__);
148 return -1;
149 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800150 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800151 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800152 data->hit = 1;
153 data->bytes_req = bytes_req;
154 data->bytes_alloc = bytes_alloc;
155
156 rb_link_node(&data->node, parent, node);
157 rb_insert_color(&data->node, &root_caller_stat);
158 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300159
160 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800161}
162
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300163static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300164 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800165{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300166 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
167 call_site = perf_evsel__intval(evsel, sample, "call_site");
168 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
169 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800170
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300171 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300172 insert_caller_stat(call_site, bytes_req, bytes_alloc))
173 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800174
175 total_requested += bytes_req;
176 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800177
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300178 nr_allocs++;
179 return 0;
180}
181
182static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
183 struct perf_sample *sample)
184{
185 int ret = perf_evsel__process_alloc_event(evsel, sample);
186
187 if (!ret) {
Don Zickus4b627952014-04-07 14:55:23 -0400188 int node1 = cpu__get_node(sample->cpu),
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300189 node2 = perf_evsel__intval(evsel, sample, "node");
190
Li Zefan7d0d3942009-11-24 13:26:31 +0800191 if (node1 != node2)
192 nr_cross_allocs++;
193 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300194
195 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800196}
197
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900198static int ptr_cmp(void *, void *);
199static int slab_callsite_cmp(void *, void *);
Li Zefan079d3f62009-11-24 13:26:55 +0800200
201static struct alloc_stat *search_alloc_stat(unsigned long ptr,
202 unsigned long call_site,
203 struct rb_root *root,
204 sort_fn_t sort_fn)
205{
206 struct rb_node *node = root->rb_node;
207 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
208
209 while (node) {
210 struct alloc_stat *data;
211 int cmp;
212
213 data = rb_entry(node, struct alloc_stat, node);
214
215 cmp = sort_fn(&key, data);
216 if (cmp < 0)
217 node = node->rb_left;
218 else if (cmp > 0)
219 node = node->rb_right;
220 else
221 return data;
222 }
223 return NULL;
224}
225
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300226static int perf_evsel__process_free_event(struct perf_evsel *evsel,
227 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800228{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300229 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800230 struct alloc_stat *s_alloc, *s_caller;
231
Li Zefan079d3f62009-11-24 13:26:55 +0800232 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
233 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300234 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800235
David Ahernaa58e9a2016-11-25 14:42:13 -0700236 total_freed += s_alloc->last_alloc;
237
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300238 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800239 s_alloc->pingpong++;
240
241 s_caller = search_alloc_stat(0, s_alloc->call_site,
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900242 &root_caller_stat,
243 slab_callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300244 if (!s_caller)
245 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800246 s_caller->pingpong++;
247 }
248 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300249
250 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800251}
252
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900253static u64 total_page_alloc_bytes;
254static u64 total_page_free_bytes;
255static u64 total_page_nomatch_bytes;
256static u64 total_page_fail_bytes;
257static unsigned long nr_page_allocs;
258static unsigned long nr_page_frees;
259static unsigned long nr_page_fails;
260static unsigned long nr_page_nomatch;
261
262static bool use_pfn;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900263static bool live_page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900264static struct perf_session *kmem_session;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900265
266#define MAX_MIGRATE_TYPES 6
267#define MAX_PAGE_ORDER 11
268
269static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
270
271struct page_stat {
272 struct rb_node node;
273 u64 page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900274 u64 callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900275 int order;
276 unsigned gfp_flags;
277 unsigned migrate_type;
278 u64 alloc_bytes;
279 u64 free_bytes;
280 int nr_alloc;
281 int nr_free;
282};
283
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900284static struct rb_root page_live_tree;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900285static struct rb_root page_alloc_tree;
286static struct rb_root page_alloc_sorted;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900287static struct rb_root page_caller_tree;
288static struct rb_root page_caller_sorted;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900289
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900290struct alloc_func {
291 u64 start;
292 u64 end;
293 char *name;
294};
295
296static int nr_alloc_funcs;
297static struct alloc_func *alloc_func_list;
298
299static int funcmp(const void *a, const void *b)
300{
301 const struct alloc_func *fa = a;
302 const struct alloc_func *fb = b;
303
304 if (fa->start > fb->start)
305 return 1;
306 else
307 return -1;
308}
309
310static int callcmp(const void *a, const void *b)
311{
312 const struct alloc_func *fa = a;
313 const struct alloc_func *fb = b;
314
315 if (fb->start <= fa->start && fa->end < fb->end)
316 return 0;
317
318 if (fa->start > fb->start)
319 return 1;
320 else
321 return -1;
322}
323
324static int build_alloc_func_list(void)
325{
326 int ret;
327 struct map *kernel_map;
328 struct symbol *sym;
329 struct rb_node *node;
330 struct alloc_func *func;
331 struct machine *machine = &kmem_session->machines.host;
332 regex_t alloc_func_regex;
333 const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
334
335 ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
336 if (ret) {
337 char err[BUFSIZ];
338
339 regerror(ret, &alloc_func_regex, err, sizeof(err));
340 pr_err("Invalid regex: %s\n%s", pattern, err);
341 return -EINVAL;
342 }
343
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300344 kernel_map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300345 if (map__load(kernel_map) < 0) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900346 pr_err("cannot load kernel map\n");
347 return -ENOENT;
348 }
349
350 map__for_each_symbol(kernel_map, sym, node) {
351 if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
352 continue;
353
354 func = realloc(alloc_func_list,
355 (nr_alloc_funcs + 1) * sizeof(*func));
356 if (func == NULL)
357 return -ENOMEM;
358
359 pr_debug("alloc func: %s\n", sym->name);
360 func[nr_alloc_funcs].start = sym->start;
361 func[nr_alloc_funcs].end = sym->end;
362 func[nr_alloc_funcs].name = sym->name;
363
364 alloc_func_list = func;
365 nr_alloc_funcs++;
366 }
367
368 qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
369
370 regfree(&alloc_func_regex);
371 return 0;
372}
373
374/*
375 * Find first non-memory allocation function from callchain.
376 * The allocation functions are in the 'alloc_func_list'.
377 */
378static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample)
379{
380 struct addr_location al;
381 struct machine *machine = &kmem_session->machines.host;
382 struct callchain_cursor_node *node;
383
384 if (alloc_func_list == NULL) {
385 if (build_alloc_func_list() < 0)
386 goto out;
387 }
388
389 al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300390 sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900391
392 callchain_cursor_commit(&callchain_cursor);
393 while (true) {
394 struct alloc_func key, *caller;
395 u64 addr;
396
397 node = callchain_cursor_current(&callchain_cursor);
398 if (node == NULL)
399 break;
400
401 key.start = key.end = node->ip;
402 caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
403 sizeof(key), callcmp);
404 if (!caller) {
405 /* found */
406 if (node->map)
407 addr = map__unmap_ip(node->map, node->ip);
408 else
409 addr = node->ip;
410
411 return addr;
412 } else
413 pr_debug3("skipping alloc function: %s\n", caller->name);
414
415 callchain_cursor_advance(&callchain_cursor);
416 }
417
418out:
419 pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
420 return sample->ip;
421}
422
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900423struct sort_dimension {
424 const char name[20];
425 sort_fn_t cmp;
426 struct list_head list;
427};
428
429static LIST_HEAD(page_alloc_sort_input);
430static LIST_HEAD(page_caller_sort_input);
431
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900432static struct page_stat *
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900433__page_stat__findnew_page(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900434{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900435 struct rb_node **node = &page_live_tree.rb_node;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900436 struct rb_node *parent = NULL;
437 struct page_stat *data;
438
439 while (*node) {
440 s64 cmp;
441
442 parent = *node;
443 data = rb_entry(*node, struct page_stat, node);
444
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900445 cmp = data->page - pstat->page;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900446 if (cmp < 0)
447 node = &parent->rb_left;
448 else if (cmp > 0)
449 node = &parent->rb_right;
450 else
451 return data;
452 }
453
454 if (!create)
455 return NULL;
456
457 data = zalloc(sizeof(*data));
458 if (data != NULL) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900459 data->page = pstat->page;
460 data->order = pstat->order;
461 data->gfp_flags = pstat->gfp_flags;
462 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900463
464 rb_link_node(&data->node, parent, node);
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900465 rb_insert_color(&data->node, &page_live_tree);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900466 }
467
468 return data;
469}
470
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900471static struct page_stat *page_stat__find_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900472{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900473 return __page_stat__findnew_page(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900474}
475
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900476static struct page_stat *page_stat__findnew_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900477{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900478 return __page_stat__findnew_page(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900479}
480
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900481static struct page_stat *
482__page_stat__findnew_alloc(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900483{
484 struct rb_node **node = &page_alloc_tree.rb_node;
485 struct rb_node *parent = NULL;
486 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900487 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900488
489 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900490 int cmp = 0;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900491
492 parent = *node;
493 data = rb_entry(*node, struct page_stat, node);
494
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900495 list_for_each_entry(sort, &page_alloc_sort_input, list) {
496 cmp = sort->cmp(pstat, data);
497 if (cmp)
498 break;
499 }
500
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900501 if (cmp < 0)
502 node = &parent->rb_left;
503 else if (cmp > 0)
504 node = &parent->rb_right;
505 else
506 return data;
507 }
508
509 if (!create)
510 return NULL;
511
512 data = zalloc(sizeof(*data));
513 if (data != NULL) {
David Ahern6b1a2752015-04-14 13:49:33 -0400514 data->page = pstat->page;
515 data->order = pstat->order;
516 data->gfp_flags = pstat->gfp_flags;
517 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900518
519 rb_link_node(&data->node, parent, node);
520 rb_insert_color(&data->node, &page_alloc_tree);
521 }
522
523 return data;
524}
525
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900526static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
527{
528 return __page_stat__findnew_alloc(pstat, false);
529}
530
531static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
532{
533 return __page_stat__findnew_alloc(pstat, true);
534}
535
536static struct page_stat *
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900537__page_stat__findnew_caller(struct page_stat *pstat, bool create)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900538{
539 struct rb_node **node = &page_caller_tree.rb_node;
540 struct rb_node *parent = NULL;
541 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900542 struct sort_dimension *sort;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900543
544 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900545 int cmp = 0;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900546
547 parent = *node;
548 data = rb_entry(*node, struct page_stat, node);
549
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900550 list_for_each_entry(sort, &page_caller_sort_input, list) {
551 cmp = sort->cmp(pstat, data);
552 if (cmp)
553 break;
554 }
555
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900556 if (cmp < 0)
557 node = &parent->rb_left;
558 else if (cmp > 0)
559 node = &parent->rb_right;
560 else
561 return data;
562 }
563
564 if (!create)
565 return NULL;
566
567 data = zalloc(sizeof(*data));
568 if (data != NULL) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900569 data->callsite = pstat->callsite;
570 data->order = pstat->order;
571 data->gfp_flags = pstat->gfp_flags;
572 data->migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900573
574 rb_link_node(&data->node, parent, node);
575 rb_insert_color(&data->node, &page_caller_tree);
576 }
577
578 return data;
579}
580
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900581static struct page_stat *page_stat__find_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900582{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900583 return __page_stat__findnew_caller(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900584}
585
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900586static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900587{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900588 return __page_stat__findnew_caller(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900589}
590
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900591static bool valid_page(u64 pfn_or_page)
592{
593 if (use_pfn && pfn_or_page == -1UL)
594 return false;
595 if (!use_pfn && pfn_or_page == 0)
596 return false;
597 return true;
598}
599
Namhyung Kim0e111152015-04-21 13:55:05 +0900600struct gfp_flag {
601 unsigned int flags;
602 char *compact_str;
603 char *human_readable;
604};
605
606static struct gfp_flag *gfps;
607static int nr_gfps;
608
609static int gfpcmp(const void *a, const void *b)
610{
611 const struct gfp_flag *fa = a;
612 const struct gfp_flag *fb = b;
613
614 return fa->flags - fb->flags;
615}
616
Vlastimil Babka420adbe92016-03-15 14:55:52 -0700617/* see include/trace/events/mmflags.h */
Namhyung Kim0e111152015-04-21 13:55:05 +0900618static const struct {
619 const char *original;
620 const char *compact;
621} gfp_compact_table[] = {
622 { "GFP_TRANSHUGE", "THP" },
Vlastimil Babka25160352016-07-28 15:49:25 -0700623 { "GFP_TRANSHUGE_LIGHT", "THL" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900624 { "GFP_HIGHUSER_MOVABLE", "HUM" },
625 { "GFP_HIGHUSER", "HU" },
626 { "GFP_USER", "U" },
627 { "GFP_TEMPORARY", "TMP" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700628 { "GFP_KERNEL_ACCOUNT", "KAC" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900629 { "GFP_KERNEL", "K" },
630 { "GFP_NOFS", "NF" },
631 { "GFP_ATOMIC", "A" },
632 { "GFP_NOIO", "NI" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900633 { "GFP_NOWAIT", "NW" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700634 { "GFP_DMA", "D" },
635 { "__GFP_HIGHMEM", "HM" },
636 { "GFP_DMA32", "D32" },
637 { "__GFP_HIGH", "H" },
638 { "__GFP_ATOMIC", "_A" },
639 { "__GFP_IO", "I" },
640 { "__GFP_FS", "F" },
641 { "__GFP_COLD", "CO" },
642 { "__GFP_NOWARN", "NWR" },
643 { "__GFP_REPEAT", "R" },
644 { "__GFP_NOFAIL", "NF" },
645 { "__GFP_NORETRY", "NR" },
646 { "__GFP_COMP", "C" },
647 { "__GFP_ZERO", "Z" },
648 { "__GFP_NOMEMALLOC", "NMA" },
649 { "__GFP_MEMALLOC", "MA" },
650 { "__GFP_HARDWALL", "HW" },
651 { "__GFP_THISNODE", "TN" },
652 { "__GFP_RECLAIMABLE", "RC" },
653 { "__GFP_MOVABLE", "M" },
654 { "__GFP_ACCOUNT", "AC" },
655 { "__GFP_NOTRACK", "NT" },
656 { "__GFP_WRITE", "WR" },
657 { "__GFP_RECLAIM", "R" },
658 { "__GFP_DIRECT_RECLAIM", "DR" },
659 { "__GFP_KSWAPD_RECLAIM", "KR" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900660};
661
662static size_t max_gfp_len;
663
664static char *compact_gfp_flags(char *gfp_flags)
665{
666 char *orig_flags = strdup(gfp_flags);
667 char *new_flags = NULL;
Arnaldo Carvalho de Melob2365122015-05-29 09:48:13 -0300668 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900669 size_t len = 0;
670
671 if (orig_flags == NULL)
672 return NULL;
673
674 str = strtok_r(orig_flags, "|", &pos);
675 while (str) {
676 size_t i;
677 char *new;
678 const char *cpt;
679
680 for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
681 if (strcmp(gfp_compact_table[i].original, str))
682 continue;
683
684 cpt = gfp_compact_table[i].compact;
685 new = realloc(new_flags, len + strlen(cpt) + 2);
686 if (new == NULL) {
687 free(new_flags);
688 return NULL;
689 }
690
691 new_flags = new;
692
693 if (!len) {
694 strcpy(new_flags, cpt);
695 } else {
696 strcat(new_flags, "|");
697 strcat(new_flags, cpt);
698 len++;
699 }
700
701 len += strlen(cpt);
702 }
703
704 str = strtok_r(NULL, "|", &pos);
705 }
706
707 if (max_gfp_len < len)
708 max_gfp_len = len;
709
710 free(orig_flags);
711 return new_flags;
712}
713
714static char *compact_gfp_string(unsigned long gfp_flags)
715{
716 struct gfp_flag key = {
717 .flags = gfp_flags,
718 };
719 struct gfp_flag *gfp;
720
721 gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp);
722 if (gfp)
723 return gfp->compact_str;
724
725 return NULL;
726}
727
728static int parse_gfp_flags(struct perf_evsel *evsel, struct perf_sample *sample,
729 unsigned int gfp_flags)
730{
731 struct pevent_record record = {
732 .cpu = sample->cpu,
733 .data = sample->raw_data,
734 .size = sample->raw_size,
735 };
736 struct trace_seq seq;
Arnaldo Carvalho de Melo08a9b982015-05-11 11:41:17 -0300737 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900738
739 if (nr_gfps) {
740 struct gfp_flag key = {
741 .flags = gfp_flags,
742 };
743
744 if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp))
745 return 0;
746 }
747
748 trace_seq_init(&seq);
749 pevent_event_info(&seq, evsel->tp_format, &record);
750
751 str = strtok_r(seq.buffer, " ", &pos);
752 while (str) {
753 if (!strncmp(str, "gfp_flags=", 10)) {
754 struct gfp_flag *new;
755
756 new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps));
757 if (new == NULL)
758 return -ENOMEM;
759
760 gfps = new;
761 new += nr_gfps++;
762
763 new->flags = gfp_flags;
764 new->human_readable = strdup(str + 10);
765 new->compact_str = compact_gfp_flags(str + 10);
766 if (!new->human_readable || !new->compact_str)
767 return -ENOMEM;
768
769 qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp);
770 }
771
772 str = strtok_r(NULL, " ", &pos);
773 }
774
775 trace_seq_destroy(&seq);
776 return 0;
777}
778
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900779static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
780 struct perf_sample *sample)
781{
782 u64 page;
783 unsigned int order = perf_evsel__intval(evsel, sample, "order");
784 unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
785 unsigned int migrate_type = perf_evsel__intval(evsel, sample,
786 "migratetype");
787 u64 bytes = kmem_page_size << order;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900788 u64 callsite;
David Ahern6b1a2752015-04-14 13:49:33 -0400789 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900790 struct page_stat this = {
791 .order = order,
792 .gfp_flags = gfp_flags,
793 .migrate_type = migrate_type,
794 };
795
796 if (use_pfn)
797 page = perf_evsel__intval(evsel, sample, "pfn");
798 else
799 page = perf_evsel__intval(evsel, sample, "page");
800
801 nr_page_allocs++;
802 total_page_alloc_bytes += bytes;
803
804 if (!valid_page(page)) {
805 nr_page_fails++;
806 total_page_fail_bytes += bytes;
807
808 return 0;
809 }
810
Namhyung Kim0e111152015-04-21 13:55:05 +0900811 if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
812 return -1;
813
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900814 callsite = find_callsite(evsel, sample);
815
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900816 /*
817 * This is to find the current page (with correct gfp flags and
818 * migrate type) at free event.
819 */
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900820 this.page = page;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900821 pstat = page_stat__findnew_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400822 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900823 return -ENOMEM;
824
David Ahern6b1a2752015-04-14 13:49:33 -0400825 pstat->nr_alloc++;
826 pstat->alloc_bytes += bytes;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900827 pstat->callsite = callsite;
828
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900829 if (!live_page) {
830 pstat = page_stat__findnew_alloc(&this);
831 if (pstat == NULL)
832 return -ENOMEM;
833
834 pstat->nr_alloc++;
835 pstat->alloc_bytes += bytes;
836 pstat->callsite = callsite;
837 }
838
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900839 this.callsite = callsite;
840 pstat = page_stat__findnew_caller(&this);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900841 if (pstat == NULL)
842 return -ENOMEM;
843
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900844 pstat->nr_alloc++;
845 pstat->alloc_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900846
847 order_stats[order][migrate_type]++;
848
849 return 0;
850}
851
852static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
853 struct perf_sample *sample)
854{
855 u64 page;
856 unsigned int order = perf_evsel__intval(evsel, sample, "order");
857 u64 bytes = kmem_page_size << order;
David Ahern6b1a2752015-04-14 13:49:33 -0400858 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900859 struct page_stat this = {
860 .order = order,
861 };
862
863 if (use_pfn)
864 page = perf_evsel__intval(evsel, sample, "pfn");
865 else
866 page = perf_evsel__intval(evsel, sample, "page");
867
868 nr_page_frees++;
869 total_page_free_bytes += bytes;
870
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900871 this.page = page;
872 pstat = page_stat__find_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400873 if (pstat == NULL) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900874 pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
875 page, order);
876
877 nr_page_nomatch++;
878 total_page_nomatch_bytes += bytes;
879
880 return 0;
881 }
882
David Ahern6b1a2752015-04-14 13:49:33 -0400883 this.gfp_flags = pstat->gfp_flags;
884 this.migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900885 this.callsite = pstat->callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900886
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900887 rb_erase(&pstat->node, &page_live_tree);
David Ahern6b1a2752015-04-14 13:49:33 -0400888 free(pstat);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900889
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900890 if (live_page) {
891 order_stats[this.order][this.migrate_type]--;
892 } else {
893 pstat = page_stat__find_alloc(&this);
894 if (pstat == NULL)
895 return -ENOMEM;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900896
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900897 pstat->nr_free++;
898 pstat->free_bytes += bytes;
899 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900900
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900901 pstat = page_stat__find_caller(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400902 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900903 return -ENOENT;
904
David Ahern6b1a2752015-04-14 13:49:33 -0400905 pstat->nr_free++;
906 pstat->free_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900907
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900908 if (live_page) {
909 pstat->nr_alloc--;
910 pstat->alloc_bytes -= bytes;
911
912 if (pstat->nr_alloc == 0) {
913 rb_erase(&pstat->node, &page_caller_tree);
914 free(pstat);
915 }
916 }
917
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900918 return 0;
919}
920
David Ahern2a865bd2016-11-29 10:15:45 -0700921static bool perf_kmem__skip_sample(struct perf_sample *sample)
922{
923 /* skip sample based on time? */
924 if (perf_time__skip_sample(&ptime, sample->time))
925 return true;
926
927 return false;
928}
929
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300930typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
931 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800932
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300933static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200934 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200935 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300936 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200937 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800938{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300939 int err = 0;
Adrian Hunteref893252013-08-27 11:23:06 +0300940 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900941 sample->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800942
Li Zefanba77c9e2009-11-20 15:53:25 +0800943 if (thread == NULL) {
944 pr_debug("problem processing %d event, skipping it.\n",
945 event->header.type);
946 return -1;
947 }
948
David Ahern2a865bd2016-11-29 10:15:45 -0700949 if (perf_kmem__skip_sample(sample))
950 return 0;
951
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200952 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800953
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300954 if (evsel->handler != NULL) {
955 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300956 err = f(evsel, sample);
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300957 }
958
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300959 thread__put(thread);
960
961 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800962}
963
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300964static struct perf_tool perf_kmem = {
965 .sample = process_sample_event,
966 .comm = perf_event__process_comm,
Namhyung Kim64c40902014-08-01 14:59:31 +0900967 .mmap = perf_event__process_mmap,
968 .mmap2 = perf_event__process_mmap2,
Hari Bathinif3b36142017-03-08 02:11:43 +0530969 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200970 .ordered_events = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800971};
972
Li Zefanba77c9e2009-11-20 15:53:25 +0800973static double fragmentation(unsigned long n_req, unsigned long n_alloc)
974{
975 if (n_alloc == 0)
976 return 0.0;
977 else
978 return 100.0 - (100.0 * n_req / n_alloc);
979}
980
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900981static void __print_slab_result(struct rb_root *root,
982 struct perf_session *session,
983 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800984{
985 struct rb_node *next;
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300986 struct machine *machine = &session->machines.host;
Li Zefanba77c9e2009-11-20 15:53:25 +0800987
Namhyung Kim65f46e02015-03-12 16:32:48 +0900988 printf("%.105s\n", graph_dotted_line);
Li Zefan079d3f62009-11-24 13:26:55 +0800989 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200990 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Namhyung Kim65f46e02015-03-12 16:32:48 +0900991 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800992
993 next = rb_first(root);
994
995 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200996 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
997 node);
998 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300999 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +08001000 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001001 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +08001002
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001003 if (is_caller) {
1004 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +08001005 if (!raw_ip)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001006 sym = machine__find_kernel_function(machine, addr, &map);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001007 } else
1008 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +08001009
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001010 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001011 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -03001012 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001013 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001014 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +08001015 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001016
Namhyung Kim65f46e02015-03-12 16:32:48 +09001017 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +08001018 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +08001019 (unsigned long)data->bytes_alloc / data->hit,
1020 (unsigned long long)data->bytes_req,
1021 (unsigned long)data->bytes_req / data->hit,
1022 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +08001023 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +08001024 fragmentation(data->bytes_req, data->bytes_alloc));
1025
1026 next = rb_next(next);
1027 }
1028
1029 if (n_lines == -1)
Namhyung Kim65f46e02015-03-12 16:32:48 +09001030 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +08001031
Namhyung Kim65f46e02015-03-12 16:32:48 +09001032 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +08001033}
1034
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001035static const char * const migrate_type_str[] = {
1036 "UNMOVABL",
1037 "RECLAIM",
1038 "MOVABLE",
1039 "RESERVED",
1040 "CMA/ISLT",
1041 "UNKNOWN",
1042};
1043
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001044static void __print_page_alloc_result(struct perf_session *session, int n_lines)
Li Zefanba77c9e2009-11-20 15:53:25 +08001045{
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001046 struct rb_node *next = rb_first(&page_alloc_sorted);
1047 struct machine *machine = &session->machines.host;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001048 const char *format;
Namhyung Kim0e111152015-04-21 13:55:05 +09001049 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001050
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001051 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001052 printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1053 use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total",
1054 gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001055 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001056
1057 if (use_pfn)
Namhyung Kim0e111152015-04-21 13:55:05 +09001058 format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001059 else
Namhyung Kim0e111152015-04-21 13:55:05 +09001060 format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001061
1062 while (next && n_lines--) {
1063 struct page_stat *data;
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001064 struct symbol *sym;
1065 struct map *map;
1066 char buf[32];
1067 char *caller = buf;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001068
1069 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001070 sym = machine__find_kernel_function(machine, data->callsite, &map);
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001071 if (sym)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001072 caller = sym->name;
1073 else
1074 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001075
1076 printf(format, (unsigned long long)data->page,
1077 (unsigned long long)data->alloc_bytes / 1024,
1078 data->nr_alloc, data->order,
1079 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001080 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001081
1082 next = rb_next(next);
1083 }
1084
Namhyung Kim0e111152015-04-21 13:55:05 +09001085 if (n_lines == -1) {
1086 printf(" ... | ... | ... | ... | ... | %-*s | ...\n",
1087 gfp_len, "...");
1088 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001089
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001090 printf("%.105s\n", graph_dotted_line);
1091}
1092
1093static void __print_page_caller_result(struct perf_session *session, int n_lines)
1094{
1095 struct rb_node *next = rb_first(&page_caller_sorted);
1096 struct machine *machine = &session->machines.host;
Namhyung Kim0e111152015-04-21 13:55:05 +09001097 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001098
1099 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001100 printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1101 live_page ? "Live" : "Total", gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001102 printf("%.105s\n", graph_dotted_line);
1103
1104 while (next && n_lines--) {
1105 struct page_stat *data;
1106 struct symbol *sym;
1107 struct map *map;
1108 char buf[32];
1109 char *caller = buf;
1110
1111 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001112 sym = machine__find_kernel_function(machine, data->callsite, &map);
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001113 if (sym)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001114 caller = sym->name;
1115 else
1116 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
1117
Namhyung Kim0e111152015-04-21 13:55:05 +09001118 printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n",
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001119 (unsigned long long)data->alloc_bytes / 1024,
1120 data->nr_alloc, data->order,
1121 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001122 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001123
1124 next = rb_next(next);
1125 }
1126
Namhyung Kim0e111152015-04-21 13:55:05 +09001127 if (n_lines == -1) {
1128 printf(" ... | ... | ... | ... | %-*s | ...\n",
1129 gfp_len, "...");
1130 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001131
1132 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001133}
1134
Namhyung Kim0e111152015-04-21 13:55:05 +09001135static void print_gfp_flags(void)
1136{
1137 int i;
1138
1139 printf("#\n");
1140 printf("# GFP flags\n");
1141 printf("# ---------\n");
1142 for (i = 0; i < nr_gfps; i++) {
1143 printf("# %08x: %*s: %s\n", gfps[i].flags,
1144 (int) max_gfp_len, gfps[i].compact_str,
1145 gfps[i].human_readable);
1146 }
1147}
1148
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001149static void print_slab_summary(void)
1150{
1151 printf("\nSUMMARY (SLAB allocator)");
1152 printf("\n========================\n");
Namhyung Kim77cfe382015-03-23 15:30:40 +09001153 printf("Total bytes requested: %'lu\n", total_requested);
1154 printf("Total bytes allocated: %'lu\n", total_allocated);
David Ahernaa58e9a2016-11-25 14:42:13 -07001155 printf("Total bytes freed: %'lu\n", total_freed);
1156 if (total_allocated > total_freed) {
1157 printf("Net total bytes allocated: %'lu\n",
1158 total_allocated - total_freed);
1159 }
Namhyung Kim77cfe382015-03-23 15:30:40 +09001160 printf("Total bytes wasted on internal fragmentation: %'lu\n",
Li Zefanba77c9e2009-11-20 15:53:25 +08001161 total_allocated - total_requested);
1162 printf("Internal fragmentation: %f%%\n",
1163 fragmentation(total_requested, total_allocated));
Namhyung Kim77cfe382015-03-23 15:30:40 +09001164 printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +08001165}
1166
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001167static void print_page_summary(void)
1168{
1169 int o, m;
1170 u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
1171 u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
1172
1173 printf("\nSUMMARY (page allocator)");
1174 printf("\n========================\n");
1175 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests",
1176 nr_page_allocs, total_page_alloc_bytes / 1024);
1177 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests",
1178 nr_page_frees, total_page_free_bytes / 1024);
1179 printf("\n");
1180
Will Deacon6145c252015-04-23 14:40:37 +01001181 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001182 nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
Will Deacon6145c252015-04-23 14:40:37 +01001183 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001184 nr_page_allocs - nr_alloc_freed,
1185 (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
1186 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests",
1187 nr_page_nomatch, total_page_nomatch_bytes / 1024);
1188 printf("\n");
1189
1190 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures",
1191 nr_page_fails, total_page_fail_bytes / 1024);
1192 printf("\n");
1193
1194 printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable",
1195 "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
1196 printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line,
1197 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1198 graph_dotted_line, graph_dotted_line);
1199
1200 for (o = 0; o < MAX_PAGE_ORDER; o++) {
1201 printf("%5d", o);
1202 for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
1203 if (order_stats[o][m])
1204 printf(" %'12d", order_stats[o][m]);
1205 else
1206 printf(" %12c", '.');
1207 }
1208 printf("\n");
1209 }
1210}
1211
1212static void print_slab_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001213{
1214 if (caller_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001215 __print_slab_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001216 if (alloc_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001217 __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
1218 print_slab_summary();
1219}
1220
1221static void print_page_result(struct perf_session *session)
1222{
Namhyung Kim0e111152015-04-21 13:55:05 +09001223 if (caller_flag || alloc_flag)
1224 print_gfp_flags();
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001225 if (caller_flag)
1226 __print_page_caller_result(session, caller_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001227 if (alloc_flag)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001228 __print_page_alloc_result(session, alloc_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001229 print_page_summary();
1230}
1231
1232static void print_result(struct perf_session *session)
1233{
1234 if (kmem_slab)
1235 print_slab_result(session);
1236 if (kmem_page)
1237 print_page_result(session);
Li Zefanba77c9e2009-11-20 15:53:25 +08001238}
1239
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001240static LIST_HEAD(slab_caller_sort);
1241static LIST_HEAD(slab_alloc_sort);
1242static LIST_HEAD(page_caller_sort);
1243static LIST_HEAD(page_alloc_sort);
Li Zefan29b3e152009-11-24 13:26:10 +08001244
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001245static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
1246 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001247{
1248 struct rb_node **new = &(root->rb_node);
1249 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +08001250 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +08001251
1252 while (*new) {
1253 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +08001254 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +08001255
1256 this = rb_entry(*new, struct alloc_stat, node);
1257 parent = *new;
1258
Li Zefan29b3e152009-11-24 13:26:10 +08001259 list_for_each_entry(sort, sort_list, list) {
1260 cmp = sort->cmp(data, this);
1261 if (cmp)
1262 break;
1263 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001264
1265 if (cmp > 0)
1266 new = &((*new)->rb_left);
1267 else
1268 new = &((*new)->rb_right);
1269 }
1270
1271 rb_link_node(&data->node, parent, new);
1272 rb_insert_color(&data->node, root);
1273}
1274
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001275static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
1276 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001277{
1278 struct rb_node *node;
1279 struct alloc_stat *data;
1280
1281 for (;;) {
1282 node = rb_first(root);
1283 if (!node)
1284 break;
1285
1286 rb_erase(node, root);
1287 data = rb_entry(node, struct alloc_stat, node);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001288 sort_slab_insert(root_sorted, data, sort_list);
1289 }
1290}
1291
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001292static void sort_page_insert(struct rb_root *root, struct page_stat *data,
1293 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001294{
1295 struct rb_node **new = &root->rb_node;
1296 struct rb_node *parent = NULL;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001297 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001298
1299 while (*new) {
1300 struct page_stat *this;
1301 int cmp = 0;
1302
1303 this = rb_entry(*new, struct page_stat, node);
1304 parent = *new;
1305
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001306 list_for_each_entry(sort, sort_list, list) {
1307 cmp = sort->cmp(data, this);
1308 if (cmp)
1309 break;
1310 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001311
1312 if (cmp > 0)
1313 new = &parent->rb_left;
1314 else
1315 new = &parent->rb_right;
1316 }
1317
1318 rb_link_node(&data->node, parent, new);
1319 rb_insert_color(&data->node, root);
1320}
1321
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001322static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted,
1323 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001324{
1325 struct rb_node *node;
1326 struct page_stat *data;
1327
1328 for (;;) {
1329 node = rb_first(root);
1330 if (!node)
1331 break;
1332
1333 rb_erase(node, root);
1334 data = rb_entry(node, struct page_stat, node);
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001335 sort_page_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +08001336 }
1337}
1338
1339static void sort_result(void)
1340{
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001341 if (kmem_slab) {
1342 __sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001343 &slab_alloc_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001344 __sort_slab_result(&root_caller_stat, &root_caller_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001345 &slab_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001346 }
1347 if (kmem_page) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001348 if (live_page)
1349 __sort_page_result(&page_live_tree, &page_alloc_sorted,
1350 &page_alloc_sort);
1351 else
1352 __sort_page_result(&page_alloc_tree, &page_alloc_sorted,
1353 &page_alloc_sort);
1354
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001355 __sort_page_result(&page_caller_tree, &page_caller_sorted,
1356 &page_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001357 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001358}
1359
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001360static int __cmd_kmem(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001361{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001362 int err = -EINVAL;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001363 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001364 const struct perf_evsel_str_handler kmem_tracepoints[] = {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001365 /* slab allocator */
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001366 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
1367 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
1368 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
1369 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
1370 { "kmem:kfree", perf_evsel__process_free_event, },
1371 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001372 /* page allocator */
1373 { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, },
1374 { "kmem:mm_page_free", perf_evsel__process_page_free_event, },
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001375 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001376
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001377 if (!perf_session__has_traces(session, "kmem record"))
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001378 goto out;
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001379
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001380 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
1381 pr_err("Initializing perf session tracepoint handlers failed\n");
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001382 goto out;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001383 }
1384
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001385 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001386 if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") &&
1387 perf_evsel__field(evsel, "pfn")) {
1388 use_pfn = true;
1389 break;
1390 }
1391 }
1392
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001393 setup_pager();
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001394 err = perf_session__process_events(session);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001395 if (err != 0) {
1396 pr_err("error during process events: %d\n", err);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001397 goto out;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001398 }
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001399 sort_result();
1400 print_result(session);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001401out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001402 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +08001403}
1404
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001405/* slab sort keys */
1406static int ptr_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001407{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001408 struct alloc_stat *l = a;
1409 struct alloc_stat *r = b;
1410
Li Zefanba77c9e2009-11-20 15:53:25 +08001411 if (l->ptr < r->ptr)
1412 return -1;
1413 else if (l->ptr > r->ptr)
1414 return 1;
1415 return 0;
1416}
1417
Li Zefan29b3e152009-11-24 13:26:10 +08001418static struct sort_dimension ptr_sort_dimension = {
1419 .name = "ptr",
1420 .cmp = ptr_cmp,
1421};
1422
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001423static int slab_callsite_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001424{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001425 struct alloc_stat *l = a;
1426 struct alloc_stat *r = b;
1427
Li Zefanba77c9e2009-11-20 15:53:25 +08001428 if (l->call_site < r->call_site)
1429 return -1;
1430 else if (l->call_site > r->call_site)
1431 return 1;
1432 return 0;
1433}
1434
Li Zefan29b3e152009-11-24 13:26:10 +08001435static struct sort_dimension callsite_sort_dimension = {
1436 .name = "callsite",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001437 .cmp = slab_callsite_cmp,
Li Zefan29b3e152009-11-24 13:26:10 +08001438};
1439
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001440static int hit_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001441{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001442 struct alloc_stat *l = a;
1443 struct alloc_stat *r = b;
1444
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001445 if (l->hit < r->hit)
1446 return -1;
1447 else if (l->hit > r->hit)
1448 return 1;
1449 return 0;
1450}
1451
Li Zefan29b3e152009-11-24 13:26:10 +08001452static struct sort_dimension hit_sort_dimension = {
1453 .name = "hit",
1454 .cmp = hit_cmp,
1455};
1456
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001457static int bytes_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001458{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001459 struct alloc_stat *l = a;
1460 struct alloc_stat *r = b;
1461
Li Zefanba77c9e2009-11-20 15:53:25 +08001462 if (l->bytes_alloc < r->bytes_alloc)
1463 return -1;
1464 else if (l->bytes_alloc > r->bytes_alloc)
1465 return 1;
1466 return 0;
1467}
1468
Li Zefan29b3e152009-11-24 13:26:10 +08001469static struct sort_dimension bytes_sort_dimension = {
1470 .name = "bytes",
1471 .cmp = bytes_cmp,
1472};
1473
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001474static int frag_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001475{
1476 double x, y;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001477 struct alloc_stat *l = a;
1478 struct alloc_stat *r = b;
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001479
1480 x = fragmentation(l->bytes_req, l->bytes_alloc);
1481 y = fragmentation(r->bytes_req, r->bytes_alloc);
1482
1483 if (x < y)
1484 return -1;
1485 else if (x > y)
1486 return 1;
1487 return 0;
1488}
1489
Li Zefan29b3e152009-11-24 13:26:10 +08001490static struct sort_dimension frag_sort_dimension = {
1491 .name = "frag",
1492 .cmp = frag_cmp,
1493};
1494
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001495static int pingpong_cmp(void *a, void *b)
Li Zefan079d3f62009-11-24 13:26:55 +08001496{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001497 struct alloc_stat *l = a;
1498 struct alloc_stat *r = b;
1499
Li Zefan079d3f62009-11-24 13:26:55 +08001500 if (l->pingpong < r->pingpong)
1501 return -1;
1502 else if (l->pingpong > r->pingpong)
1503 return 1;
1504 return 0;
1505}
1506
1507static struct sort_dimension pingpong_sort_dimension = {
1508 .name = "pingpong",
1509 .cmp = pingpong_cmp,
1510};
1511
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001512/* page sort keys */
1513static int page_cmp(void *a, void *b)
1514{
1515 struct page_stat *l = a;
1516 struct page_stat *r = b;
1517
1518 if (l->page < r->page)
1519 return -1;
1520 else if (l->page > r->page)
1521 return 1;
1522 return 0;
1523}
1524
1525static struct sort_dimension page_sort_dimension = {
1526 .name = "page",
1527 .cmp = page_cmp,
1528};
1529
1530static int page_callsite_cmp(void *a, void *b)
1531{
1532 struct page_stat *l = a;
1533 struct page_stat *r = b;
1534
1535 if (l->callsite < r->callsite)
1536 return -1;
1537 else if (l->callsite > r->callsite)
1538 return 1;
1539 return 0;
1540}
1541
1542static struct sort_dimension page_callsite_sort_dimension = {
1543 .name = "callsite",
1544 .cmp = page_callsite_cmp,
1545};
1546
1547static int page_hit_cmp(void *a, void *b)
1548{
1549 struct page_stat *l = a;
1550 struct page_stat *r = b;
1551
1552 if (l->nr_alloc < r->nr_alloc)
1553 return -1;
1554 else if (l->nr_alloc > r->nr_alloc)
1555 return 1;
1556 return 0;
1557}
1558
1559static struct sort_dimension page_hit_sort_dimension = {
1560 .name = "hit",
1561 .cmp = page_hit_cmp,
1562};
1563
1564static int page_bytes_cmp(void *a, void *b)
1565{
1566 struct page_stat *l = a;
1567 struct page_stat *r = b;
1568
1569 if (l->alloc_bytes < r->alloc_bytes)
1570 return -1;
1571 else if (l->alloc_bytes > r->alloc_bytes)
1572 return 1;
1573 return 0;
1574}
1575
1576static struct sort_dimension page_bytes_sort_dimension = {
1577 .name = "bytes",
1578 .cmp = page_bytes_cmp,
1579};
1580
1581static int page_order_cmp(void *a, void *b)
1582{
1583 struct page_stat *l = a;
1584 struct page_stat *r = b;
1585
1586 if (l->order < r->order)
1587 return -1;
1588 else if (l->order > r->order)
1589 return 1;
1590 return 0;
1591}
1592
1593static struct sort_dimension page_order_sort_dimension = {
1594 .name = "order",
1595 .cmp = page_order_cmp,
1596};
1597
1598static int migrate_type_cmp(void *a, void *b)
1599{
1600 struct page_stat *l = a;
1601 struct page_stat *r = b;
1602
1603 /* for internal use to find free'd page */
1604 if (l->migrate_type == -1U)
1605 return 0;
1606
1607 if (l->migrate_type < r->migrate_type)
1608 return -1;
1609 else if (l->migrate_type > r->migrate_type)
1610 return 1;
1611 return 0;
1612}
1613
1614static struct sort_dimension migrate_type_sort_dimension = {
1615 .name = "migtype",
1616 .cmp = migrate_type_cmp,
1617};
1618
1619static int gfp_flags_cmp(void *a, void *b)
1620{
1621 struct page_stat *l = a;
1622 struct page_stat *r = b;
1623
1624 /* for internal use to find free'd page */
1625 if (l->gfp_flags == -1U)
1626 return 0;
1627
1628 if (l->gfp_flags < r->gfp_flags)
1629 return -1;
1630 else if (l->gfp_flags > r->gfp_flags)
1631 return 1;
1632 return 0;
1633}
1634
1635static struct sort_dimension gfp_flags_sort_dimension = {
1636 .name = "gfp",
1637 .cmp = gfp_flags_cmp,
1638};
1639
1640static struct sort_dimension *slab_sorts[] = {
Li Zefan29b3e152009-11-24 13:26:10 +08001641 &ptr_sort_dimension,
1642 &callsite_sort_dimension,
1643 &hit_sort_dimension,
1644 &bytes_sort_dimension,
1645 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +08001646 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +08001647};
1648
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001649static struct sort_dimension *page_sorts[] = {
1650 &page_sort_dimension,
1651 &page_callsite_sort_dimension,
1652 &page_hit_sort_dimension,
1653 &page_bytes_sort_dimension,
1654 &page_order_sort_dimension,
1655 &migrate_type_sort_dimension,
1656 &gfp_flags_sort_dimension,
1657};
Li Zefan29b3e152009-11-24 13:26:10 +08001658
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001659static int slab_sort_dimension__add(const char *tok, struct list_head *list)
Li Zefan29b3e152009-11-24 13:26:10 +08001660{
1661 struct sort_dimension *sort;
1662 int i;
1663
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001664 for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) {
1665 if (!strcmp(slab_sorts[i]->name, tok)) {
1666 sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i]));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001667 if (!sort) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -03001668 pr_err("%s: memdup failed\n", __func__);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001669 return -1;
1670 }
Li Zefan29b3e152009-11-24 13:26:10 +08001671 list_add_tail(&sort->list, list);
1672 return 0;
1673 }
1674 }
1675
1676 return -1;
1677}
1678
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001679static int page_sort_dimension__add(const char *tok, struct list_head *list)
1680{
1681 struct sort_dimension *sort;
1682 int i;
1683
1684 for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) {
1685 if (!strcmp(page_sorts[i]->name, tok)) {
1686 sort = memdup(page_sorts[i], sizeof(*page_sorts[i]));
1687 if (!sort) {
1688 pr_err("%s: memdup failed\n", __func__);
1689 return -1;
1690 }
1691 list_add_tail(&sort->list, list);
1692 return 0;
1693 }
1694 }
1695
1696 return -1;
1697}
1698
1699static int setup_slab_sorting(struct list_head *sort_list, const char *arg)
Li Zefan29b3e152009-11-24 13:26:10 +08001700{
1701 char *tok;
1702 char *str = strdup(arg);
Namhyung Kim405f8752015-03-12 16:32:46 +09001703 char *pos = str;
Li Zefan29b3e152009-11-24 13:26:10 +08001704
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001705 if (!str) {
1706 pr_err("%s: strdup failed\n", __func__);
1707 return -1;
1708 }
Li Zefan29b3e152009-11-24 13:26:10 +08001709
1710 while (true) {
Namhyung Kim405f8752015-03-12 16:32:46 +09001711 tok = strsep(&pos, ",");
Li Zefan29b3e152009-11-24 13:26:10 +08001712 if (!tok)
1713 break;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001714 if (slab_sort_dimension__add(tok, sort_list) < 0) {
1715 error("Unknown slab --sort key: '%s'", tok);
1716 free(str);
1717 return -1;
1718 }
1719 }
1720
1721 free(str);
1722 return 0;
1723}
1724
1725static int setup_page_sorting(struct list_head *sort_list, const char *arg)
1726{
1727 char *tok;
1728 char *str = strdup(arg);
1729 char *pos = str;
1730
1731 if (!str) {
1732 pr_err("%s: strdup failed\n", __func__);
1733 return -1;
1734 }
1735
1736 while (true) {
1737 tok = strsep(&pos, ",");
1738 if (!tok)
1739 break;
1740 if (page_sort_dimension__add(tok, sort_list) < 0) {
1741 error("Unknown page --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +09001742 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +08001743 return -1;
1744 }
1745 }
1746
1747 free(str);
1748 return 0;
1749}
1750
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001751static int parse_sort_opt(const struct option *opt __maybe_unused,
1752 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001753{
Li Zefanba77c9e2009-11-20 15:53:25 +08001754 if (!arg)
1755 return -1;
1756
Namhyung Kim0c160d42015-04-21 13:55:06 +09001757 if (kmem_page > kmem_slab ||
1758 (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001759 if (caller_flag > alloc_flag)
1760 return setup_page_sorting(&page_caller_sort, arg);
1761 else
1762 return setup_page_sorting(&page_alloc_sort, arg);
1763 } else {
1764 if (caller_flag > alloc_flag)
1765 return setup_slab_sorting(&slab_caller_sort, arg);
1766 else
1767 return setup_slab_sorting(&slab_alloc_sort, arg);
1768 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001769
1770 return 0;
1771}
1772
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001773static int parse_caller_opt(const struct option *opt __maybe_unused,
1774 const char *arg __maybe_unused,
1775 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001776{
Li Zefan90b86a92009-12-10 15:21:57 +08001777 caller_flag = (alloc_flag + 1);
1778 return 0;
1779}
Li Zefanba77c9e2009-11-20 15:53:25 +08001780
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001781static int parse_alloc_opt(const struct option *opt __maybe_unused,
1782 const char *arg __maybe_unused,
1783 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +08001784{
1785 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001786 return 0;
1787}
1788
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001789static int parse_slab_opt(const struct option *opt __maybe_unused,
1790 const char *arg __maybe_unused,
1791 int unset __maybe_unused)
1792{
1793 kmem_slab = (kmem_page + 1);
1794 return 0;
1795}
1796
1797static int parse_page_opt(const struct option *opt __maybe_unused,
1798 const char *arg __maybe_unused,
1799 int unset __maybe_unused)
1800{
1801 kmem_page = (kmem_slab + 1);
1802 return 0;
1803}
1804
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001805static int parse_line_opt(const struct option *opt __maybe_unused,
1806 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001807{
1808 int lines;
1809
1810 if (!arg)
1811 return -1;
1812
1813 lines = strtoul(arg, NULL, 10);
1814
1815 if (caller_flag > alloc_flag)
1816 caller_lines = lines;
1817 else
1818 alloc_lines = lines;
1819
1820 return 0;
1821}
1822
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001823static int __cmd_record(int argc, const char **argv)
1824{
1825 const char * const record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001826 "record", "-a", "-R", "-c", "1",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001827 };
1828 const char * const slab_events[] = {
Li Zefanba77c9e2009-11-20 15:53:25 +08001829 "-e", "kmem:kmalloc",
1830 "-e", "kmem:kmalloc_node",
1831 "-e", "kmem:kfree",
1832 "-e", "kmem:kmem_cache_alloc",
1833 "-e", "kmem:kmem_cache_alloc_node",
1834 "-e", "kmem:kmem_cache_free",
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001835 };
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001836 const char * const page_events[] = {
1837 "-e", "kmem:mm_page_alloc",
1838 "-e", "kmem:mm_page_free",
1839 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001840 unsigned int rec_argc, i, j;
1841 const char **rec_argv;
1842
1843 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001844 if (kmem_slab)
1845 rec_argc += ARRAY_SIZE(slab_events);
1846 if (kmem_page)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001847 rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001848
Li Zefanba77c9e2009-11-20 15:53:25 +08001849 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1850
Chris Samuelce47dc52010-11-13 13:35:06 +11001851 if (rec_argv == NULL)
1852 return -ENOMEM;
1853
Li Zefanba77c9e2009-11-20 15:53:25 +08001854 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1855 rec_argv[i] = strdup(record_args[i]);
1856
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001857 if (kmem_slab) {
1858 for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
1859 rec_argv[i] = strdup(slab_events[j]);
1860 }
1861 if (kmem_page) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001862 rec_argv[i++] = strdup("-g");
1863
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001864 for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
1865 rec_argv[i] = strdup(page_events[j]);
1866 }
1867
Li Zefanba77c9e2009-11-20 15:53:25 +08001868 for (j = 1; j < (unsigned int)argc; j++, i++)
1869 rec_argv[i] = argv[j];
1870
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001871 return cmd_record(i, rec_argv);
Li Zefanba77c9e2009-11-20 15:53:25 +08001872}
1873
Wang Nanb8cbb342016-02-26 09:31:51 +00001874static int kmem_config(const char *var, const char *value, void *cb __maybe_unused)
Namhyung Kim0c160d42015-04-21 13:55:06 +09001875{
1876 if (!strcmp(var, "kmem.default")) {
1877 if (!strcmp(value, "slab"))
1878 kmem_default = KMEM_SLAB;
1879 else if (!strcmp(value, "page"))
1880 kmem_default = KMEM_PAGE;
1881 else
1882 pr_err("invalid default value ('slab' or 'page' required): %s\n",
1883 value);
1884 return 0;
1885 }
1886
Wang Nanb8cbb342016-02-26 09:31:51 +00001887 return 0;
Namhyung Kim0c160d42015-04-21 13:55:06 +09001888}
1889
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001890int cmd_kmem(int argc, const char **argv)
Li Zefanba77c9e2009-11-20 15:53:25 +08001891{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001892 const char * const default_slab_sort = "frag,hit,bytes";
1893 const char * const default_page_sort = "bytes,hit";
Yunlong Songd1eeb772015-04-02 21:47:12 +08001894 struct perf_data_file file = {
Yunlong Songd1eeb772015-04-02 21:47:12 +08001895 .mode = PERF_DATA_MODE_READ,
1896 };
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001897 const struct option kmem_options[] = {
1898 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Namhyung Kimbd72a332015-03-12 16:32:47 +09001899 OPT_INCR('v', "verbose", &verbose,
1900 "be more verbose (show symbol address, etc)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001901 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
1902 "show per-callsite statistics", parse_caller_opt),
1903 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
1904 "show per-allocation statistics", parse_alloc_opt),
1905 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001906 "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, "
1907 "page, order, migtype, gfp", parse_sort_opt),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001908 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
1909 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Yunlong Songd1eeb772015-04-02 21:47:12 +08001910 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001911 OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
1912 parse_slab_opt),
1913 OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
1914 parse_page_opt),
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001915 OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
David Ahern2a865bd2016-11-29 10:15:45 -07001916 OPT_STRING(0, "time", &time_str, "str",
1917 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001918 OPT_END()
1919 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001920 const char *const kmem_subcommands[] = { "record", "stat", NULL };
1921 const char *kmem_usage[] = {
1922 NULL,
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001923 NULL
1924 };
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001925 struct perf_session *session;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001926 const char errmsg[] = "No %s allocation events found. Have you run 'perf kmem record --%s'?\n";
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001927 int ret = perf_config(kmem_config, NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001928
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001929 if (ret)
1930 return ret;
1931
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001932 argc = parse_options_subcommand(argc, argv, kmem_options,
1933 kmem_subcommands, kmem_usage, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +08001934
Li Zefan90b86a92009-12-10 15:21:57 +08001935 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +08001936 usage_with_options(kmem_usage, kmem_options);
1937
Namhyung Kim0c160d42015-04-21 13:55:06 +09001938 if (kmem_slab == 0 && kmem_page == 0) {
1939 if (kmem_default == KMEM_SLAB)
1940 kmem_slab = 1;
1941 else
1942 kmem_page = 1;
1943 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001944
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001945 if (!strncmp(argv[0], "rec", 3)) {
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001946 symbol__init(NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001947 return __cmd_record(argc, argv);
1948 }
1949
Jiri Olsa28939e12015-04-06 14:36:08 +09001950 file.path = input_name;
1951
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001952 kmem_session = session = perf_session__new(&file, false, &perf_kmem);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001953 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001954 return -1;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001955
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001956 ret = -1;
1957
Namhyung Kima923e2c2015-05-05 23:52:52 +09001958 if (kmem_slab) {
1959 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
1960 "kmem:kmalloc")) {
1961 pr_err(errmsg, "slab", "slab");
Taeung Song249ca1a2015-06-30 17:15:21 +09001962 goto out_delete;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001963 }
1964 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001965
Namhyung Kima923e2c2015-05-05 23:52:52 +09001966 if (kmem_page) {
1967 struct perf_evsel *evsel;
1968
1969 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
1970 "kmem:mm_page_alloc");
1971 if (evsel == NULL) {
1972 pr_err(errmsg, "page", "page");
Taeung Song249ca1a2015-06-30 17:15:21 +09001973 goto out_delete;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001974 }
1975
1976 kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001977 symbol_conf.use_callchain = true;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001978 }
1979
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001980 symbol__init(&session->header.env);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001981
David Ahern2a865bd2016-11-29 10:15:45 -07001982 if (perf_time__parse_str(&ptime, time_str) != 0) {
1983 pr_err("Invalid time string\n");
1984 return -EINVAL;
1985 }
1986
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001987 if (!strcmp(argv[0], "stat")) {
Namhyung Kim77cfe382015-03-23 15:30:40 +09001988 setlocale(LC_ALL, "");
1989
Don Zickus4b627952014-04-07 14:55:23 -04001990 if (cpu__setup_cpunode_map())
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001991 goto out_delete;
Li Zefanba77c9e2009-11-20 15:53:25 +08001992
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001993 if (list_empty(&slab_caller_sort))
1994 setup_slab_sorting(&slab_caller_sort, default_slab_sort);
1995 if (list_empty(&slab_alloc_sort))
1996 setup_slab_sorting(&slab_alloc_sort, default_slab_sort);
1997 if (list_empty(&page_caller_sort))
1998 setup_page_sorting(&page_caller_sort, default_page_sort);
1999 if (list_empty(&page_alloc_sort))
2000 setup_page_sorting(&page_alloc_sort, default_page_sort);
Li Zefan7d0d3942009-11-24 13:26:31 +08002001
Namhyung Kimfb4f3132015-04-21 13:55:03 +09002002 if (kmem_page) {
2003 setup_page_sorting(&page_alloc_sort_input,
2004 "page,order,migtype,gfp");
2005 setup_page_sorting(&page_caller_sort_input,
2006 "callsite,order,migtype,gfp");
2007 }
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09002008 ret = __cmd_kmem(session);
Pekka Enbergb00eca82010-01-19 19:26:11 +02002009 } else
2010 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +08002011
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09002012out_delete:
2013 perf_session__delete(session);
2014
2015 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +08002016}
2017