blob: b5682beaad72517a02116f72b688ca076df9294c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Li Zefanba77c9e2009-11-20 15:53:25 +08002#include "builtin.h"
3#include "perf.h"
4
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03005#include "util/dso.h"
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03006#include "util/evlist.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03007#include "util/evsel.h"
Taeung Song41840d22016-06-23 17:55:17 +09008#include "util/config.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +01009#include "util/map.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080010#include "util/symbol.h"
11#include "util/thread.h"
12#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020013#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020014#include "util/tool.h"
Namhyung Kimc9758cc2015-04-21 13:55:02 +090015#include "util/callchain.h"
David Ahern2a865bd2016-11-29 10:15:45 -070016#include "util/time-utils.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080017
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030018#include <subcmd/pager.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060019#include <subcmd/parse-options.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080020#include "util/trace-event.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020021#include "util/data.h"
Don Zickus4b627952014-04-07 14:55:23 -040022#include "util/cpumap.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080023
24#include "util/debug.h"
Arnaldo Carvalho de Melo6a9fa4e2019-06-25 17:31:26 -030025#include "util/string2.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080026
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030027#include <linux/kernel.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080028#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -030029#include <linux/string.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030030#include <linux/zalloc.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030031#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030032#include <inttypes.h>
Namhyung Kim77cfe382015-03-23 15:30:40 +090033#include <locale.h>
Namhyung Kimc9758cc2015-04-21 13:55:02 +090034#include <regex.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080035
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030036#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030037
Namhyung Kim0d68bc92015-04-06 14:36:10 +090038static int kmem_slab;
39static int kmem_page;
40
41static long kmem_page_size;
Namhyung Kim0c160d42015-04-21 13:55:06 +090042static enum {
43 KMEM_SLAB,
44 KMEM_PAGE,
45} kmem_default = KMEM_SLAB; /* for backward compatibility */
Namhyung Kim0d68bc92015-04-06 14:36:10 +090046
Li Zefanba77c9e2009-11-20 15:53:25 +080047struct alloc_stat;
Namhyung Kimfb4f3132015-04-21 13:55:03 +090048typedef int (*sort_fn_t)(void *, void *);
Li Zefanba77c9e2009-11-20 15:53:25 +080049
Li Zefanba77c9e2009-11-20 15:53:25 +080050static int alloc_flag;
51static int caller_flag;
52
Li Zefanba77c9e2009-11-20 15:53:25 +080053static int alloc_lines = -1;
54static int caller_lines = -1;
55
Li Zefan7707b6b2009-11-24 13:25:48 +080056static bool raw_ip;
57
Li Zefanba77c9e2009-11-20 15:53:25 +080058struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080059 u64 call_site;
60 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080061 u64 bytes_req;
62 u64 bytes_alloc;
David Ahernaa58e9a2016-11-25 14:42:13 -070063 u64 last_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080064 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080065 u32 pingpong;
66
67 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080068
69 struct rb_node node;
70};
71
72static struct rb_root root_alloc_stat;
73static struct rb_root root_alloc_sorted;
74static struct rb_root root_caller_stat;
75static struct rb_root root_caller_sorted;
76
David Ahernaa58e9a2016-11-25 14:42:13 -070077static unsigned long total_requested, total_allocated, total_freed;
Li Zefan7d0d3942009-11-24 13:26:31 +080078static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080079
David Ahern2a865bd2016-11-29 10:15:45 -070080/* filters for controlling start and stop of time of analysis */
81static struct perf_time_interval ptime;
82const char *time_str;
83
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030084static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
85 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +080086{
87 struct rb_node **node = &root_alloc_stat.rb_node;
88 struct rb_node *parent = NULL;
89 struct alloc_stat *data = NULL;
90
Li Zefanba77c9e2009-11-20 15:53:25 +080091 while (*node) {
92 parent = *node;
93 data = rb_entry(*node, struct alloc_stat, node);
94
95 if (ptr > data->ptr)
96 node = &(*node)->rb_right;
97 else if (ptr < data->ptr)
98 node = &(*node)->rb_left;
99 else
100 break;
101 }
102
103 if (data && data->ptr == ptr) {
104 data->hit++;
105 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800106 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800107 } else {
108 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300109 if (!data) {
110 pr_err("%s: malloc failed\n", __func__);
111 return -1;
112 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800113 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +0800114 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800115 data->hit = 1;
116 data->bytes_req = bytes_req;
117 data->bytes_alloc = bytes_alloc;
118
119 rb_link_node(&data->node, parent, node);
120 rb_insert_color(&data->node, &root_alloc_stat);
121 }
Li Zefan079d3f62009-11-24 13:26:55 +0800122 data->call_site = call_site;
123 data->alloc_cpu = cpu;
David Ahernaa58e9a2016-11-25 14:42:13 -0700124 data->last_alloc = bytes_alloc;
125
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300126 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800127}
128
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300129static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800130 int bytes_req, int bytes_alloc)
131{
132 struct rb_node **node = &root_caller_stat.rb_node;
133 struct rb_node *parent = NULL;
134 struct alloc_stat *data = NULL;
135
Li Zefanba77c9e2009-11-20 15:53:25 +0800136 while (*node) {
137 parent = *node;
138 data = rb_entry(*node, struct alloc_stat, node);
139
140 if (call_site > data->call_site)
141 node = &(*node)->rb_right;
142 else if (call_site < data->call_site)
143 node = &(*node)->rb_left;
144 else
145 break;
146 }
147
148 if (data && data->call_site == call_site) {
149 data->hit++;
150 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800151 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800152 } else {
153 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300154 if (!data) {
155 pr_err("%s: malloc failed\n", __func__);
156 return -1;
157 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800158 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800159 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800160 data->hit = 1;
161 data->bytes_req = bytes_req;
162 data->bytes_alloc = bytes_alloc;
163
164 rb_link_node(&data->node, parent, node);
165 rb_insert_color(&data->node, &root_caller_stat);
166 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300167
168 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800169}
170
Jiri Olsa32dcd022019-07-21 13:23:51 +0200171static int perf_evsel__process_alloc_event(struct evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300172 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800173{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300174 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
175 call_site = perf_evsel__intval(evsel, sample, "call_site");
176 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
177 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800178
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300179 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300180 insert_caller_stat(call_site, bytes_req, bytes_alloc))
181 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800182
183 total_requested += bytes_req;
184 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800185
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300186 nr_allocs++;
187 return 0;
188}
189
Jiri Olsa32dcd022019-07-21 13:23:51 +0200190static int perf_evsel__process_alloc_node_event(struct evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300191 struct perf_sample *sample)
192{
193 int ret = perf_evsel__process_alloc_event(evsel, sample);
194
195 if (!ret) {
Don Zickus4b627952014-04-07 14:55:23 -0400196 int node1 = cpu__get_node(sample->cpu),
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300197 node2 = perf_evsel__intval(evsel, sample, "node");
198
Li Zefan7d0d3942009-11-24 13:26:31 +0800199 if (node1 != node2)
200 nr_cross_allocs++;
201 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300202
203 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800204}
205
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900206static int ptr_cmp(void *, void *);
207static int slab_callsite_cmp(void *, void *);
Li Zefan079d3f62009-11-24 13:26:55 +0800208
209static struct alloc_stat *search_alloc_stat(unsigned long ptr,
210 unsigned long call_site,
211 struct rb_root *root,
212 sort_fn_t sort_fn)
213{
214 struct rb_node *node = root->rb_node;
215 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
216
217 while (node) {
218 struct alloc_stat *data;
219 int cmp;
220
221 data = rb_entry(node, struct alloc_stat, node);
222
223 cmp = sort_fn(&key, data);
224 if (cmp < 0)
225 node = node->rb_left;
226 else if (cmp > 0)
227 node = node->rb_right;
228 else
229 return data;
230 }
231 return NULL;
232}
233
Jiri Olsa32dcd022019-07-21 13:23:51 +0200234static int perf_evsel__process_free_event(struct evsel *evsel,
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300235 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800236{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300237 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800238 struct alloc_stat *s_alloc, *s_caller;
239
Li Zefan079d3f62009-11-24 13:26:55 +0800240 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
241 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300242 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800243
David Ahernaa58e9a2016-11-25 14:42:13 -0700244 total_freed += s_alloc->last_alloc;
245
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300246 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800247 s_alloc->pingpong++;
248
249 s_caller = search_alloc_stat(0, s_alloc->call_site,
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900250 &root_caller_stat,
251 slab_callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300252 if (!s_caller)
253 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800254 s_caller->pingpong++;
255 }
256 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300257
258 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800259}
260
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900261static u64 total_page_alloc_bytes;
262static u64 total_page_free_bytes;
263static u64 total_page_nomatch_bytes;
264static u64 total_page_fail_bytes;
265static unsigned long nr_page_allocs;
266static unsigned long nr_page_frees;
267static unsigned long nr_page_fails;
268static unsigned long nr_page_nomatch;
269
270static bool use_pfn;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900271static bool live_page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900272static struct perf_session *kmem_session;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900273
274#define MAX_MIGRATE_TYPES 6
275#define MAX_PAGE_ORDER 11
276
277static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
278
279struct page_stat {
280 struct rb_node node;
281 u64 page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900282 u64 callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900283 int order;
284 unsigned gfp_flags;
285 unsigned migrate_type;
286 u64 alloc_bytes;
287 u64 free_bytes;
288 int nr_alloc;
289 int nr_free;
290};
291
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900292static struct rb_root page_live_tree;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900293static struct rb_root page_alloc_tree;
294static struct rb_root page_alloc_sorted;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900295static struct rb_root page_caller_tree;
296static struct rb_root page_caller_sorted;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900297
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900298struct alloc_func {
299 u64 start;
300 u64 end;
301 char *name;
302};
303
304static int nr_alloc_funcs;
305static struct alloc_func *alloc_func_list;
306
307static int funcmp(const void *a, const void *b)
308{
309 const struct alloc_func *fa = a;
310 const struct alloc_func *fb = b;
311
312 if (fa->start > fb->start)
313 return 1;
314 else
315 return -1;
316}
317
318static int callcmp(const void *a, const void *b)
319{
320 const struct alloc_func *fa = a;
321 const struct alloc_func *fb = b;
322
323 if (fb->start <= fa->start && fa->end < fb->end)
324 return 0;
325
326 if (fa->start > fb->start)
327 return 1;
328 else
329 return -1;
330}
331
332static int build_alloc_func_list(void)
333{
334 int ret;
335 struct map *kernel_map;
336 struct symbol *sym;
337 struct rb_node *node;
338 struct alloc_func *func;
339 struct machine *machine = &kmem_session->machines.host;
340 regex_t alloc_func_regex;
Rasmus Villemoes49b8e2b2018-11-03 00:06:23 +0100341 static const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900342
343 ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
344 if (ret) {
345 char err[BUFSIZ];
346
347 regerror(ret, &alloc_func_regex, err, sizeof(err));
348 pr_err("Invalid regex: %s\n%s", pattern, err);
349 return -EINVAL;
350 }
351
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300352 kernel_map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300353 if (map__load(kernel_map) < 0) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900354 pr_err("cannot load kernel map\n");
355 return -ENOENT;
356 }
357
358 map__for_each_symbol(kernel_map, sym, node) {
359 if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
360 continue;
361
362 func = realloc(alloc_func_list,
363 (nr_alloc_funcs + 1) * sizeof(*func));
364 if (func == NULL)
365 return -ENOMEM;
366
367 pr_debug("alloc func: %s\n", sym->name);
368 func[nr_alloc_funcs].start = sym->start;
369 func[nr_alloc_funcs].end = sym->end;
370 func[nr_alloc_funcs].name = sym->name;
371
372 alloc_func_list = func;
373 nr_alloc_funcs++;
374 }
375
376 qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
377
378 regfree(&alloc_func_regex);
379 return 0;
380}
381
382/*
383 * Find first non-memory allocation function from callchain.
384 * The allocation functions are in the 'alloc_func_list'.
385 */
Jiri Olsa32dcd022019-07-21 13:23:51 +0200386static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900387{
388 struct addr_location al;
389 struct machine *machine = &kmem_session->machines.host;
390 struct callchain_cursor_node *node;
391
392 if (alloc_func_list == NULL) {
393 if (build_alloc_func_list() < 0)
394 goto out;
395 }
396
397 al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300398 sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900399
400 callchain_cursor_commit(&callchain_cursor);
401 while (true) {
402 struct alloc_func key, *caller;
403 u64 addr;
404
405 node = callchain_cursor_current(&callchain_cursor);
406 if (node == NULL)
407 break;
408
409 key.start = key.end = node->ip;
410 caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
411 sizeof(key), callcmp);
412 if (!caller) {
413 /* found */
414 if (node->map)
415 addr = map__unmap_ip(node->map, node->ip);
416 else
417 addr = node->ip;
418
419 return addr;
420 } else
421 pr_debug3("skipping alloc function: %s\n", caller->name);
422
423 callchain_cursor_advance(&callchain_cursor);
424 }
425
426out:
427 pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
428 return sample->ip;
429}
430
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900431struct sort_dimension {
432 const char name[20];
433 sort_fn_t cmp;
434 struct list_head list;
435};
436
437static LIST_HEAD(page_alloc_sort_input);
438static LIST_HEAD(page_caller_sort_input);
439
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900440static struct page_stat *
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900441__page_stat__findnew_page(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900442{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900443 struct rb_node **node = &page_live_tree.rb_node;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900444 struct rb_node *parent = NULL;
445 struct page_stat *data;
446
447 while (*node) {
448 s64 cmp;
449
450 parent = *node;
451 data = rb_entry(*node, struct page_stat, node);
452
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900453 cmp = data->page - pstat->page;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900454 if (cmp < 0)
455 node = &parent->rb_left;
456 else if (cmp > 0)
457 node = &parent->rb_right;
458 else
459 return data;
460 }
461
462 if (!create)
463 return NULL;
464
465 data = zalloc(sizeof(*data));
466 if (data != NULL) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900467 data->page = pstat->page;
468 data->order = pstat->order;
469 data->gfp_flags = pstat->gfp_flags;
470 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900471
472 rb_link_node(&data->node, parent, node);
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900473 rb_insert_color(&data->node, &page_live_tree);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900474 }
475
476 return data;
477}
478
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900479static struct page_stat *page_stat__find_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900480{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900481 return __page_stat__findnew_page(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900482}
483
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900484static struct page_stat *page_stat__findnew_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900485{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900486 return __page_stat__findnew_page(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900487}
488
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900489static struct page_stat *
490__page_stat__findnew_alloc(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900491{
492 struct rb_node **node = &page_alloc_tree.rb_node;
493 struct rb_node *parent = NULL;
494 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900495 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900496
497 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900498 int cmp = 0;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900499
500 parent = *node;
501 data = rb_entry(*node, struct page_stat, node);
502
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900503 list_for_each_entry(sort, &page_alloc_sort_input, list) {
504 cmp = sort->cmp(pstat, data);
505 if (cmp)
506 break;
507 }
508
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900509 if (cmp < 0)
510 node = &parent->rb_left;
511 else if (cmp > 0)
512 node = &parent->rb_right;
513 else
514 return data;
515 }
516
517 if (!create)
518 return NULL;
519
520 data = zalloc(sizeof(*data));
521 if (data != NULL) {
David Ahern6b1a2752015-04-14 13:49:33 -0400522 data->page = pstat->page;
523 data->order = pstat->order;
524 data->gfp_flags = pstat->gfp_flags;
525 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900526
527 rb_link_node(&data->node, parent, node);
528 rb_insert_color(&data->node, &page_alloc_tree);
529 }
530
531 return data;
532}
533
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900534static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
535{
536 return __page_stat__findnew_alloc(pstat, false);
537}
538
539static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
540{
541 return __page_stat__findnew_alloc(pstat, true);
542}
543
544static struct page_stat *
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900545__page_stat__findnew_caller(struct page_stat *pstat, bool create)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900546{
547 struct rb_node **node = &page_caller_tree.rb_node;
548 struct rb_node *parent = NULL;
549 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900550 struct sort_dimension *sort;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900551
552 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900553 int cmp = 0;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900554
555 parent = *node;
556 data = rb_entry(*node, struct page_stat, node);
557
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900558 list_for_each_entry(sort, &page_caller_sort_input, list) {
559 cmp = sort->cmp(pstat, data);
560 if (cmp)
561 break;
562 }
563
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900564 if (cmp < 0)
565 node = &parent->rb_left;
566 else if (cmp > 0)
567 node = &parent->rb_right;
568 else
569 return data;
570 }
571
572 if (!create)
573 return NULL;
574
575 data = zalloc(sizeof(*data));
576 if (data != NULL) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900577 data->callsite = pstat->callsite;
578 data->order = pstat->order;
579 data->gfp_flags = pstat->gfp_flags;
580 data->migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900581
582 rb_link_node(&data->node, parent, node);
583 rb_insert_color(&data->node, &page_caller_tree);
584 }
585
586 return data;
587}
588
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900589static struct page_stat *page_stat__find_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900590{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900591 return __page_stat__findnew_caller(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900592}
593
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900594static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900595{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900596 return __page_stat__findnew_caller(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900597}
598
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900599static bool valid_page(u64 pfn_or_page)
600{
601 if (use_pfn && pfn_or_page == -1UL)
602 return false;
603 if (!use_pfn && pfn_or_page == 0)
604 return false;
605 return true;
606}
607
Namhyung Kim0e111152015-04-21 13:55:05 +0900608struct gfp_flag {
609 unsigned int flags;
610 char *compact_str;
611 char *human_readable;
612};
613
614static struct gfp_flag *gfps;
615static int nr_gfps;
616
617static int gfpcmp(const void *a, const void *b)
618{
619 const struct gfp_flag *fa = a;
620 const struct gfp_flag *fb = b;
621
622 return fa->flags - fb->flags;
623}
624
Vlastimil Babka420adbe92016-03-15 14:55:52 -0700625/* see include/trace/events/mmflags.h */
Namhyung Kim0e111152015-04-21 13:55:05 +0900626static const struct {
627 const char *original;
628 const char *compact;
629} gfp_compact_table[] = {
630 { "GFP_TRANSHUGE", "THP" },
Vlastimil Babka25160352016-07-28 15:49:25 -0700631 { "GFP_TRANSHUGE_LIGHT", "THL" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900632 { "GFP_HIGHUSER_MOVABLE", "HUM" },
633 { "GFP_HIGHUSER", "HU" },
634 { "GFP_USER", "U" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700635 { "GFP_KERNEL_ACCOUNT", "KAC" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900636 { "GFP_KERNEL", "K" },
637 { "GFP_NOFS", "NF" },
638 { "GFP_ATOMIC", "A" },
639 { "GFP_NOIO", "NI" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900640 { "GFP_NOWAIT", "NW" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700641 { "GFP_DMA", "D" },
642 { "__GFP_HIGHMEM", "HM" },
643 { "GFP_DMA32", "D32" },
644 { "__GFP_HIGH", "H" },
645 { "__GFP_ATOMIC", "_A" },
646 { "__GFP_IO", "I" },
647 { "__GFP_FS", "F" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700648 { "__GFP_NOWARN", "NWR" },
Michal Hockodcda9b02017-07-12 14:36:45 -0700649 { "__GFP_RETRY_MAYFAIL", "R" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700650 { "__GFP_NOFAIL", "NF" },
651 { "__GFP_NORETRY", "NR" },
652 { "__GFP_COMP", "C" },
653 { "__GFP_ZERO", "Z" },
654 { "__GFP_NOMEMALLOC", "NMA" },
655 { "__GFP_MEMALLOC", "MA" },
656 { "__GFP_HARDWALL", "HW" },
657 { "__GFP_THISNODE", "TN" },
658 { "__GFP_RECLAIMABLE", "RC" },
659 { "__GFP_MOVABLE", "M" },
660 { "__GFP_ACCOUNT", "AC" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700661 { "__GFP_WRITE", "WR" },
662 { "__GFP_RECLAIM", "R" },
663 { "__GFP_DIRECT_RECLAIM", "DR" },
664 { "__GFP_KSWAPD_RECLAIM", "KR" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900665};
666
667static size_t max_gfp_len;
668
669static char *compact_gfp_flags(char *gfp_flags)
670{
671 char *orig_flags = strdup(gfp_flags);
672 char *new_flags = NULL;
Arnaldo Carvalho de Melob2365122015-05-29 09:48:13 -0300673 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900674 size_t len = 0;
675
676 if (orig_flags == NULL)
677 return NULL;
678
679 str = strtok_r(orig_flags, "|", &pos);
680 while (str) {
681 size_t i;
682 char *new;
683 const char *cpt;
684
685 for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
686 if (strcmp(gfp_compact_table[i].original, str))
687 continue;
688
689 cpt = gfp_compact_table[i].compact;
690 new = realloc(new_flags, len + strlen(cpt) + 2);
691 if (new == NULL) {
692 free(new_flags);
693 return NULL;
694 }
695
696 new_flags = new;
697
698 if (!len) {
699 strcpy(new_flags, cpt);
700 } else {
701 strcat(new_flags, "|");
702 strcat(new_flags, cpt);
703 len++;
704 }
705
706 len += strlen(cpt);
707 }
708
709 str = strtok_r(NULL, "|", &pos);
710 }
711
712 if (max_gfp_len < len)
713 max_gfp_len = len;
714
715 free(orig_flags);
716 return new_flags;
717}
718
719static char *compact_gfp_string(unsigned long gfp_flags)
720{
721 struct gfp_flag key = {
722 .flags = gfp_flags,
723 };
724 struct gfp_flag *gfp;
725
726 gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp);
727 if (gfp)
728 return gfp->compact_str;
729
730 return NULL;
731}
732
Jiri Olsa32dcd022019-07-21 13:23:51 +0200733static int parse_gfp_flags(struct evsel *evsel, struct perf_sample *sample,
Namhyung Kim0e111152015-04-21 13:55:05 +0900734 unsigned int gfp_flags)
735{
Tzvetomir Stoyanov (VMware)cbc49b22018-08-08 14:02:47 -0400736 struct tep_record record = {
Namhyung Kim0e111152015-04-21 13:55:05 +0900737 .cpu = sample->cpu,
738 .data = sample->raw_data,
739 .size = sample->raw_size,
740 };
741 struct trace_seq seq;
Arnaldo Carvalho de Melo08a9b982015-05-11 11:41:17 -0300742 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900743
744 if (nr_gfps) {
745 struct gfp_flag key = {
746 .flags = gfp_flags,
747 };
748
749 if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp))
750 return 0;
751 }
752
753 trace_seq_init(&seq);
Tzvetomir Stoyanov38847db2019-08-05 16:43:13 -0400754 tep_print_event(evsel->tp_format->tep,
755 &seq, &record, "%s", TEP_PRINT_INFO);
Namhyung Kim0e111152015-04-21 13:55:05 +0900756
757 str = strtok_r(seq.buffer, " ", &pos);
758 while (str) {
759 if (!strncmp(str, "gfp_flags=", 10)) {
760 struct gfp_flag *new;
761
762 new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps));
763 if (new == NULL)
764 return -ENOMEM;
765
766 gfps = new;
767 new += nr_gfps++;
768
769 new->flags = gfp_flags;
770 new->human_readable = strdup(str + 10);
771 new->compact_str = compact_gfp_flags(str + 10);
772 if (!new->human_readable || !new->compact_str)
773 return -ENOMEM;
774
775 qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp);
776 }
777
778 str = strtok_r(NULL, " ", &pos);
779 }
780
781 trace_seq_destroy(&seq);
782 return 0;
783}
784
Jiri Olsa32dcd022019-07-21 13:23:51 +0200785static int perf_evsel__process_page_alloc_event(struct evsel *evsel,
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900786 struct perf_sample *sample)
787{
788 u64 page;
789 unsigned int order = perf_evsel__intval(evsel, sample, "order");
790 unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
791 unsigned int migrate_type = perf_evsel__intval(evsel, sample,
792 "migratetype");
793 u64 bytes = kmem_page_size << order;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900794 u64 callsite;
David Ahern6b1a2752015-04-14 13:49:33 -0400795 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900796 struct page_stat this = {
797 .order = order,
798 .gfp_flags = gfp_flags,
799 .migrate_type = migrate_type,
800 };
801
802 if (use_pfn)
803 page = perf_evsel__intval(evsel, sample, "pfn");
804 else
805 page = perf_evsel__intval(evsel, sample, "page");
806
807 nr_page_allocs++;
808 total_page_alloc_bytes += bytes;
809
810 if (!valid_page(page)) {
811 nr_page_fails++;
812 total_page_fail_bytes += bytes;
813
814 return 0;
815 }
816
Namhyung Kim0e111152015-04-21 13:55:05 +0900817 if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
818 return -1;
819
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900820 callsite = find_callsite(evsel, sample);
821
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900822 /*
823 * This is to find the current page (with correct gfp flags and
824 * migrate type) at free event.
825 */
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900826 this.page = page;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900827 pstat = page_stat__findnew_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400828 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900829 return -ENOMEM;
830
David Ahern6b1a2752015-04-14 13:49:33 -0400831 pstat->nr_alloc++;
832 pstat->alloc_bytes += bytes;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900833 pstat->callsite = callsite;
834
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900835 if (!live_page) {
836 pstat = page_stat__findnew_alloc(&this);
837 if (pstat == NULL)
838 return -ENOMEM;
839
840 pstat->nr_alloc++;
841 pstat->alloc_bytes += bytes;
842 pstat->callsite = callsite;
843 }
844
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900845 this.callsite = callsite;
846 pstat = page_stat__findnew_caller(&this);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900847 if (pstat == NULL)
848 return -ENOMEM;
849
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900850 pstat->nr_alloc++;
851 pstat->alloc_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900852
853 order_stats[order][migrate_type]++;
854
855 return 0;
856}
857
Jiri Olsa32dcd022019-07-21 13:23:51 +0200858static int perf_evsel__process_page_free_event(struct evsel *evsel,
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900859 struct perf_sample *sample)
860{
861 u64 page;
862 unsigned int order = perf_evsel__intval(evsel, sample, "order");
863 u64 bytes = kmem_page_size << order;
David Ahern6b1a2752015-04-14 13:49:33 -0400864 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900865 struct page_stat this = {
866 .order = order,
867 };
868
869 if (use_pfn)
870 page = perf_evsel__intval(evsel, sample, "pfn");
871 else
872 page = perf_evsel__intval(evsel, sample, "page");
873
874 nr_page_frees++;
875 total_page_free_bytes += bytes;
876
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900877 this.page = page;
878 pstat = page_stat__find_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400879 if (pstat == NULL) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900880 pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
881 page, order);
882
883 nr_page_nomatch++;
884 total_page_nomatch_bytes += bytes;
885
886 return 0;
887 }
888
David Ahern6b1a2752015-04-14 13:49:33 -0400889 this.gfp_flags = pstat->gfp_flags;
890 this.migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900891 this.callsite = pstat->callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900892
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900893 rb_erase(&pstat->node, &page_live_tree);
David Ahern6b1a2752015-04-14 13:49:33 -0400894 free(pstat);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900895
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900896 if (live_page) {
897 order_stats[this.order][this.migrate_type]--;
898 } else {
899 pstat = page_stat__find_alloc(&this);
900 if (pstat == NULL)
901 return -ENOMEM;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900902
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900903 pstat->nr_free++;
904 pstat->free_bytes += bytes;
905 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900906
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900907 pstat = page_stat__find_caller(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400908 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900909 return -ENOENT;
910
David Ahern6b1a2752015-04-14 13:49:33 -0400911 pstat->nr_free++;
912 pstat->free_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900913
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900914 if (live_page) {
915 pstat->nr_alloc--;
916 pstat->alloc_bytes -= bytes;
917
918 if (pstat->nr_alloc == 0) {
919 rb_erase(&pstat->node, &page_caller_tree);
920 free(pstat);
921 }
922 }
923
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900924 return 0;
925}
926
David Ahern2a865bd2016-11-29 10:15:45 -0700927static bool perf_kmem__skip_sample(struct perf_sample *sample)
928{
929 /* skip sample based on time? */
930 if (perf_time__skip_sample(&ptime, sample->time))
931 return true;
932
933 return false;
934}
935
Jiri Olsa32dcd022019-07-21 13:23:51 +0200936typedef int (*tracepoint_handler)(struct evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300937 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800938
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300939static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200940 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200941 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200942 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200943 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800944{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300945 int err = 0;
Adrian Hunteref893252013-08-27 11:23:06 +0300946 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900947 sample->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800948
Li Zefanba77c9e2009-11-20 15:53:25 +0800949 if (thread == NULL) {
950 pr_debug("problem processing %d event, skipping it.\n",
951 event->header.type);
952 return -1;
953 }
954
David Ahern2a865bd2016-11-29 10:15:45 -0700955 if (perf_kmem__skip_sample(sample))
956 return 0;
957
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200958 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800959
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300960 if (evsel->handler != NULL) {
961 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300962 err = f(evsel, sample);
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300963 }
964
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300965 thread__put(thread);
966
967 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800968}
969
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300970static struct perf_tool perf_kmem = {
971 .sample = process_sample_event,
972 .comm = perf_event__process_comm,
Namhyung Kim64c40902014-08-01 14:59:31 +0900973 .mmap = perf_event__process_mmap,
974 .mmap2 = perf_event__process_mmap2,
Hari Bathinif3b36142017-03-08 02:11:43 +0530975 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200976 .ordered_events = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800977};
978
Li Zefanba77c9e2009-11-20 15:53:25 +0800979static double fragmentation(unsigned long n_req, unsigned long n_alloc)
980{
981 if (n_alloc == 0)
982 return 0.0;
983 else
984 return 100.0 - (100.0 * n_req / n_alloc);
985}
986
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900987static void __print_slab_result(struct rb_root *root,
988 struct perf_session *session,
989 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800990{
991 struct rb_node *next;
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300992 struct machine *machine = &session->machines.host;
Li Zefanba77c9e2009-11-20 15:53:25 +0800993
Namhyung Kim65f46e02015-03-12 16:32:48 +0900994 printf("%.105s\n", graph_dotted_line);
Li Zefan079d3f62009-11-24 13:26:55 +0800995 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200996 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Namhyung Kim65f46e02015-03-12 16:32:48 +0900997 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800998
999 next = rb_first(root);
1000
1001 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001002 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
1003 node);
1004 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -03001005 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +08001006 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001007 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +08001008
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001009 if (is_caller) {
1010 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +08001011 if (!raw_ip)
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03001012 sym = machine__find_kernel_symbol(machine, addr, &map);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001013 } else
1014 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +08001015
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001016 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001017 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -03001018 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001019 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001020 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +08001021 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001022
Namhyung Kim65f46e02015-03-12 16:32:48 +09001023 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +08001024 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +08001025 (unsigned long)data->bytes_alloc / data->hit,
1026 (unsigned long long)data->bytes_req,
1027 (unsigned long)data->bytes_req / data->hit,
1028 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +08001029 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +08001030 fragmentation(data->bytes_req, data->bytes_alloc));
1031
1032 next = rb_next(next);
1033 }
1034
1035 if (n_lines == -1)
Namhyung Kim65f46e02015-03-12 16:32:48 +09001036 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +08001037
Namhyung Kim65f46e02015-03-12 16:32:48 +09001038 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +08001039}
1040
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001041static const char * const migrate_type_str[] = {
1042 "UNMOVABL",
1043 "RECLAIM",
1044 "MOVABLE",
1045 "RESERVED",
1046 "CMA/ISLT",
1047 "UNKNOWN",
1048};
1049
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001050static void __print_page_alloc_result(struct perf_session *session, int n_lines)
Li Zefanba77c9e2009-11-20 15:53:25 +08001051{
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001052 struct rb_node *next = rb_first(&page_alloc_sorted);
1053 struct machine *machine = &session->machines.host;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001054 const char *format;
Namhyung Kim0e111152015-04-21 13:55:05 +09001055 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001056
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001057 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001058 printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1059 use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total",
1060 gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001061 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001062
1063 if (use_pfn)
Namhyung Kim0e111152015-04-21 13:55:05 +09001064 format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001065 else
Namhyung Kim0e111152015-04-21 13:55:05 +09001066 format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001067
1068 while (next && n_lines--) {
1069 struct page_stat *data;
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001070 struct symbol *sym;
1071 struct map *map;
1072 char buf[32];
1073 char *caller = buf;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001074
1075 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03001076 sym = machine__find_kernel_symbol(machine, data->callsite, &map);
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001077 if (sym)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001078 caller = sym->name;
1079 else
1080 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001081
1082 printf(format, (unsigned long long)data->page,
1083 (unsigned long long)data->alloc_bytes / 1024,
1084 data->nr_alloc, data->order,
1085 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001086 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001087
1088 next = rb_next(next);
1089 }
1090
Namhyung Kim0e111152015-04-21 13:55:05 +09001091 if (n_lines == -1) {
1092 printf(" ... | ... | ... | ... | ... | %-*s | ...\n",
1093 gfp_len, "...");
1094 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001095
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001096 printf("%.105s\n", graph_dotted_line);
1097}
1098
1099static void __print_page_caller_result(struct perf_session *session, int n_lines)
1100{
1101 struct rb_node *next = rb_first(&page_caller_sorted);
1102 struct machine *machine = &session->machines.host;
Namhyung Kim0e111152015-04-21 13:55:05 +09001103 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001104
1105 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001106 printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1107 live_page ? "Live" : "Total", gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001108 printf("%.105s\n", graph_dotted_line);
1109
1110 while (next && n_lines--) {
1111 struct page_stat *data;
1112 struct symbol *sym;
1113 struct map *map;
1114 char buf[32];
1115 char *caller = buf;
1116
1117 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03001118 sym = machine__find_kernel_symbol(machine, data->callsite, &map);
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001119 if (sym)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001120 caller = sym->name;
1121 else
1122 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
1123
Namhyung Kim0e111152015-04-21 13:55:05 +09001124 printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n",
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001125 (unsigned long long)data->alloc_bytes / 1024,
1126 data->nr_alloc, data->order,
1127 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001128 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001129
1130 next = rb_next(next);
1131 }
1132
Namhyung Kim0e111152015-04-21 13:55:05 +09001133 if (n_lines == -1) {
1134 printf(" ... | ... | ... | ... | %-*s | ...\n",
1135 gfp_len, "...");
1136 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001137
1138 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001139}
1140
Namhyung Kim0e111152015-04-21 13:55:05 +09001141static void print_gfp_flags(void)
1142{
1143 int i;
1144
1145 printf("#\n");
1146 printf("# GFP flags\n");
1147 printf("# ---------\n");
1148 for (i = 0; i < nr_gfps; i++) {
1149 printf("# %08x: %*s: %s\n", gfps[i].flags,
1150 (int) max_gfp_len, gfps[i].compact_str,
1151 gfps[i].human_readable);
1152 }
1153}
1154
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001155static void print_slab_summary(void)
1156{
1157 printf("\nSUMMARY (SLAB allocator)");
1158 printf("\n========================\n");
Namhyung Kim77cfe382015-03-23 15:30:40 +09001159 printf("Total bytes requested: %'lu\n", total_requested);
1160 printf("Total bytes allocated: %'lu\n", total_allocated);
David Ahernaa58e9a2016-11-25 14:42:13 -07001161 printf("Total bytes freed: %'lu\n", total_freed);
1162 if (total_allocated > total_freed) {
1163 printf("Net total bytes allocated: %'lu\n",
1164 total_allocated - total_freed);
1165 }
Namhyung Kim77cfe382015-03-23 15:30:40 +09001166 printf("Total bytes wasted on internal fragmentation: %'lu\n",
Li Zefanba77c9e2009-11-20 15:53:25 +08001167 total_allocated - total_requested);
1168 printf("Internal fragmentation: %f%%\n",
1169 fragmentation(total_requested, total_allocated));
Namhyung Kim77cfe382015-03-23 15:30:40 +09001170 printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +08001171}
1172
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001173static void print_page_summary(void)
1174{
1175 int o, m;
1176 u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
1177 u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
1178
1179 printf("\nSUMMARY (page allocator)");
1180 printf("\n========================\n");
1181 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests",
1182 nr_page_allocs, total_page_alloc_bytes / 1024);
1183 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests",
1184 nr_page_frees, total_page_free_bytes / 1024);
1185 printf("\n");
1186
Will Deacon6145c252015-04-23 14:40:37 +01001187 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001188 nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
Will Deacon6145c252015-04-23 14:40:37 +01001189 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001190 nr_page_allocs - nr_alloc_freed,
1191 (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
1192 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests",
1193 nr_page_nomatch, total_page_nomatch_bytes / 1024);
1194 printf("\n");
1195
1196 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures",
1197 nr_page_fails, total_page_fail_bytes / 1024);
1198 printf("\n");
1199
1200 printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable",
1201 "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
1202 printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line,
1203 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1204 graph_dotted_line, graph_dotted_line);
1205
1206 for (o = 0; o < MAX_PAGE_ORDER; o++) {
1207 printf("%5d", o);
1208 for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
1209 if (order_stats[o][m])
1210 printf(" %'12d", order_stats[o][m]);
1211 else
1212 printf(" %12c", '.');
1213 }
1214 printf("\n");
1215 }
1216}
1217
1218static void print_slab_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001219{
1220 if (caller_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001221 __print_slab_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001222 if (alloc_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001223 __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
1224 print_slab_summary();
1225}
1226
1227static void print_page_result(struct perf_session *session)
1228{
Namhyung Kim0e111152015-04-21 13:55:05 +09001229 if (caller_flag || alloc_flag)
1230 print_gfp_flags();
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001231 if (caller_flag)
1232 __print_page_caller_result(session, caller_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001233 if (alloc_flag)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001234 __print_page_alloc_result(session, alloc_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001235 print_page_summary();
1236}
1237
1238static void print_result(struct perf_session *session)
1239{
1240 if (kmem_slab)
1241 print_slab_result(session);
1242 if (kmem_page)
1243 print_page_result(session);
Li Zefanba77c9e2009-11-20 15:53:25 +08001244}
1245
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001246static LIST_HEAD(slab_caller_sort);
1247static LIST_HEAD(slab_alloc_sort);
1248static LIST_HEAD(page_caller_sort);
1249static LIST_HEAD(page_alloc_sort);
Li Zefan29b3e152009-11-24 13:26:10 +08001250
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001251static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
1252 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001253{
1254 struct rb_node **new = &(root->rb_node);
1255 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +08001256 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +08001257
1258 while (*new) {
1259 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +08001260 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +08001261
1262 this = rb_entry(*new, struct alloc_stat, node);
1263 parent = *new;
1264
Li Zefan29b3e152009-11-24 13:26:10 +08001265 list_for_each_entry(sort, sort_list, list) {
1266 cmp = sort->cmp(data, this);
1267 if (cmp)
1268 break;
1269 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001270
1271 if (cmp > 0)
1272 new = &((*new)->rb_left);
1273 else
1274 new = &((*new)->rb_right);
1275 }
1276
1277 rb_link_node(&data->node, parent, new);
1278 rb_insert_color(&data->node, root);
1279}
1280
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001281static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
1282 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001283{
1284 struct rb_node *node;
1285 struct alloc_stat *data;
1286
1287 for (;;) {
1288 node = rb_first(root);
1289 if (!node)
1290 break;
1291
1292 rb_erase(node, root);
1293 data = rb_entry(node, struct alloc_stat, node);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001294 sort_slab_insert(root_sorted, data, sort_list);
1295 }
1296}
1297
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001298static void sort_page_insert(struct rb_root *root, struct page_stat *data,
1299 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001300{
1301 struct rb_node **new = &root->rb_node;
1302 struct rb_node *parent = NULL;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001303 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001304
1305 while (*new) {
1306 struct page_stat *this;
1307 int cmp = 0;
1308
1309 this = rb_entry(*new, struct page_stat, node);
1310 parent = *new;
1311
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001312 list_for_each_entry(sort, sort_list, list) {
1313 cmp = sort->cmp(data, this);
1314 if (cmp)
1315 break;
1316 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001317
1318 if (cmp > 0)
1319 new = &parent->rb_left;
1320 else
1321 new = &parent->rb_right;
1322 }
1323
1324 rb_link_node(&data->node, parent, new);
1325 rb_insert_color(&data->node, root);
1326}
1327
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001328static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted,
1329 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001330{
1331 struct rb_node *node;
1332 struct page_stat *data;
1333
1334 for (;;) {
1335 node = rb_first(root);
1336 if (!node)
1337 break;
1338
1339 rb_erase(node, root);
1340 data = rb_entry(node, struct page_stat, node);
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001341 sort_page_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +08001342 }
1343}
1344
1345static void sort_result(void)
1346{
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001347 if (kmem_slab) {
1348 __sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001349 &slab_alloc_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001350 __sort_slab_result(&root_caller_stat, &root_caller_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001351 &slab_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001352 }
1353 if (kmem_page) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001354 if (live_page)
1355 __sort_page_result(&page_live_tree, &page_alloc_sorted,
1356 &page_alloc_sort);
1357 else
1358 __sort_page_result(&page_alloc_tree, &page_alloc_sorted,
1359 &page_alloc_sort);
1360
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001361 __sort_page_result(&page_caller_tree, &page_caller_sorted,
1362 &page_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001363 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001364}
1365
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001366static int __cmd_kmem(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001367{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001368 int err = -EINVAL;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001369 struct evsel *evsel;
1370 const struct evsel_str_handler kmem_tracepoints[] = {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001371 /* slab allocator */
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001372 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
1373 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
1374 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
1375 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
1376 { "kmem:kfree", perf_evsel__process_free_event, },
1377 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001378 /* page allocator */
1379 { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, },
1380 { "kmem:mm_page_free", perf_evsel__process_page_free_event, },
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001381 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001382
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001383 if (!perf_session__has_traces(session, "kmem record"))
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001384 goto out;
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001385
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001386 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
1387 pr_err("Initializing perf session tracepoint handlers failed\n");
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001388 goto out;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001389 }
1390
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001391 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001392 if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") &&
1393 perf_evsel__field(evsel, "pfn")) {
1394 use_pfn = true;
1395 break;
1396 }
1397 }
1398
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001399 setup_pager();
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001400 err = perf_session__process_events(session);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001401 if (err != 0) {
1402 pr_err("error during process events: %d\n", err);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001403 goto out;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001404 }
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001405 sort_result();
1406 print_result(session);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001407out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001408 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +08001409}
1410
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001411/* slab sort keys */
1412static int ptr_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001413{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001414 struct alloc_stat *l = a;
1415 struct alloc_stat *r = b;
1416
Li Zefanba77c9e2009-11-20 15:53:25 +08001417 if (l->ptr < r->ptr)
1418 return -1;
1419 else if (l->ptr > r->ptr)
1420 return 1;
1421 return 0;
1422}
1423
Li Zefan29b3e152009-11-24 13:26:10 +08001424static struct sort_dimension ptr_sort_dimension = {
1425 .name = "ptr",
1426 .cmp = ptr_cmp,
1427};
1428
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001429static int slab_callsite_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001430{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001431 struct alloc_stat *l = a;
1432 struct alloc_stat *r = b;
1433
Li Zefanba77c9e2009-11-20 15:53:25 +08001434 if (l->call_site < r->call_site)
1435 return -1;
1436 else if (l->call_site > r->call_site)
1437 return 1;
1438 return 0;
1439}
1440
Li Zefan29b3e152009-11-24 13:26:10 +08001441static struct sort_dimension callsite_sort_dimension = {
1442 .name = "callsite",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001443 .cmp = slab_callsite_cmp,
Li Zefan29b3e152009-11-24 13:26:10 +08001444};
1445
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001446static int hit_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001447{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001448 struct alloc_stat *l = a;
1449 struct alloc_stat *r = b;
1450
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001451 if (l->hit < r->hit)
1452 return -1;
1453 else if (l->hit > r->hit)
1454 return 1;
1455 return 0;
1456}
1457
Li Zefan29b3e152009-11-24 13:26:10 +08001458static struct sort_dimension hit_sort_dimension = {
1459 .name = "hit",
1460 .cmp = hit_cmp,
1461};
1462
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001463static int bytes_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001464{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001465 struct alloc_stat *l = a;
1466 struct alloc_stat *r = b;
1467
Li Zefanba77c9e2009-11-20 15:53:25 +08001468 if (l->bytes_alloc < r->bytes_alloc)
1469 return -1;
1470 else if (l->bytes_alloc > r->bytes_alloc)
1471 return 1;
1472 return 0;
1473}
1474
Li Zefan29b3e152009-11-24 13:26:10 +08001475static struct sort_dimension bytes_sort_dimension = {
1476 .name = "bytes",
1477 .cmp = bytes_cmp,
1478};
1479
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001480static int frag_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001481{
1482 double x, y;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001483 struct alloc_stat *l = a;
1484 struct alloc_stat *r = b;
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001485
1486 x = fragmentation(l->bytes_req, l->bytes_alloc);
1487 y = fragmentation(r->bytes_req, r->bytes_alloc);
1488
1489 if (x < y)
1490 return -1;
1491 else if (x > y)
1492 return 1;
1493 return 0;
1494}
1495
Li Zefan29b3e152009-11-24 13:26:10 +08001496static struct sort_dimension frag_sort_dimension = {
1497 .name = "frag",
1498 .cmp = frag_cmp,
1499};
1500
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001501static int pingpong_cmp(void *a, void *b)
Li Zefan079d3f62009-11-24 13:26:55 +08001502{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001503 struct alloc_stat *l = a;
1504 struct alloc_stat *r = b;
1505
Li Zefan079d3f62009-11-24 13:26:55 +08001506 if (l->pingpong < r->pingpong)
1507 return -1;
1508 else if (l->pingpong > r->pingpong)
1509 return 1;
1510 return 0;
1511}
1512
1513static struct sort_dimension pingpong_sort_dimension = {
1514 .name = "pingpong",
1515 .cmp = pingpong_cmp,
1516};
1517
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001518/* page sort keys */
1519static int page_cmp(void *a, void *b)
1520{
1521 struct page_stat *l = a;
1522 struct page_stat *r = b;
1523
1524 if (l->page < r->page)
1525 return -1;
1526 else if (l->page > r->page)
1527 return 1;
1528 return 0;
1529}
1530
1531static struct sort_dimension page_sort_dimension = {
1532 .name = "page",
1533 .cmp = page_cmp,
1534};
1535
1536static int page_callsite_cmp(void *a, void *b)
1537{
1538 struct page_stat *l = a;
1539 struct page_stat *r = b;
1540
1541 if (l->callsite < r->callsite)
1542 return -1;
1543 else if (l->callsite > r->callsite)
1544 return 1;
1545 return 0;
1546}
1547
1548static struct sort_dimension page_callsite_sort_dimension = {
1549 .name = "callsite",
1550 .cmp = page_callsite_cmp,
1551};
1552
1553static int page_hit_cmp(void *a, void *b)
1554{
1555 struct page_stat *l = a;
1556 struct page_stat *r = b;
1557
1558 if (l->nr_alloc < r->nr_alloc)
1559 return -1;
1560 else if (l->nr_alloc > r->nr_alloc)
1561 return 1;
1562 return 0;
1563}
1564
1565static struct sort_dimension page_hit_sort_dimension = {
1566 .name = "hit",
1567 .cmp = page_hit_cmp,
1568};
1569
1570static int page_bytes_cmp(void *a, void *b)
1571{
1572 struct page_stat *l = a;
1573 struct page_stat *r = b;
1574
1575 if (l->alloc_bytes < r->alloc_bytes)
1576 return -1;
1577 else if (l->alloc_bytes > r->alloc_bytes)
1578 return 1;
1579 return 0;
1580}
1581
1582static struct sort_dimension page_bytes_sort_dimension = {
1583 .name = "bytes",
1584 .cmp = page_bytes_cmp,
1585};
1586
1587static int page_order_cmp(void *a, void *b)
1588{
1589 struct page_stat *l = a;
1590 struct page_stat *r = b;
1591
1592 if (l->order < r->order)
1593 return -1;
1594 else if (l->order > r->order)
1595 return 1;
1596 return 0;
1597}
1598
1599static struct sort_dimension page_order_sort_dimension = {
1600 .name = "order",
1601 .cmp = page_order_cmp,
1602};
1603
1604static int migrate_type_cmp(void *a, void *b)
1605{
1606 struct page_stat *l = a;
1607 struct page_stat *r = b;
1608
1609 /* for internal use to find free'd page */
1610 if (l->migrate_type == -1U)
1611 return 0;
1612
1613 if (l->migrate_type < r->migrate_type)
1614 return -1;
1615 else if (l->migrate_type > r->migrate_type)
1616 return 1;
1617 return 0;
1618}
1619
1620static struct sort_dimension migrate_type_sort_dimension = {
1621 .name = "migtype",
1622 .cmp = migrate_type_cmp,
1623};
1624
1625static int gfp_flags_cmp(void *a, void *b)
1626{
1627 struct page_stat *l = a;
1628 struct page_stat *r = b;
1629
1630 /* for internal use to find free'd page */
1631 if (l->gfp_flags == -1U)
1632 return 0;
1633
1634 if (l->gfp_flags < r->gfp_flags)
1635 return -1;
1636 else if (l->gfp_flags > r->gfp_flags)
1637 return 1;
1638 return 0;
1639}
1640
1641static struct sort_dimension gfp_flags_sort_dimension = {
1642 .name = "gfp",
1643 .cmp = gfp_flags_cmp,
1644};
1645
1646static struct sort_dimension *slab_sorts[] = {
Li Zefan29b3e152009-11-24 13:26:10 +08001647 &ptr_sort_dimension,
1648 &callsite_sort_dimension,
1649 &hit_sort_dimension,
1650 &bytes_sort_dimension,
1651 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +08001652 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +08001653};
1654
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001655static struct sort_dimension *page_sorts[] = {
1656 &page_sort_dimension,
1657 &page_callsite_sort_dimension,
1658 &page_hit_sort_dimension,
1659 &page_bytes_sort_dimension,
1660 &page_order_sort_dimension,
1661 &migrate_type_sort_dimension,
1662 &gfp_flags_sort_dimension,
1663};
Li Zefan29b3e152009-11-24 13:26:10 +08001664
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001665static int slab_sort_dimension__add(const char *tok, struct list_head *list)
Li Zefan29b3e152009-11-24 13:26:10 +08001666{
1667 struct sort_dimension *sort;
1668 int i;
1669
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001670 for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) {
1671 if (!strcmp(slab_sorts[i]->name, tok)) {
1672 sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i]));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001673 if (!sort) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -03001674 pr_err("%s: memdup failed\n", __func__);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001675 return -1;
1676 }
Li Zefan29b3e152009-11-24 13:26:10 +08001677 list_add_tail(&sort->list, list);
1678 return 0;
1679 }
1680 }
1681
1682 return -1;
1683}
1684
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001685static int page_sort_dimension__add(const char *tok, struct list_head *list)
1686{
1687 struct sort_dimension *sort;
1688 int i;
1689
1690 for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) {
1691 if (!strcmp(page_sorts[i]->name, tok)) {
1692 sort = memdup(page_sorts[i], sizeof(*page_sorts[i]));
1693 if (!sort) {
1694 pr_err("%s: memdup failed\n", __func__);
1695 return -1;
1696 }
1697 list_add_tail(&sort->list, list);
1698 return 0;
1699 }
1700 }
1701
1702 return -1;
1703}
1704
1705static int setup_slab_sorting(struct list_head *sort_list, const char *arg)
Li Zefan29b3e152009-11-24 13:26:10 +08001706{
1707 char *tok;
1708 char *str = strdup(arg);
Namhyung Kim405f8752015-03-12 16:32:46 +09001709 char *pos = str;
Li Zefan29b3e152009-11-24 13:26:10 +08001710
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001711 if (!str) {
1712 pr_err("%s: strdup failed\n", __func__);
1713 return -1;
1714 }
Li Zefan29b3e152009-11-24 13:26:10 +08001715
1716 while (true) {
Namhyung Kim405f8752015-03-12 16:32:46 +09001717 tok = strsep(&pos, ",");
Li Zefan29b3e152009-11-24 13:26:10 +08001718 if (!tok)
1719 break;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001720 if (slab_sort_dimension__add(tok, sort_list) < 0) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001721 pr_err("Unknown slab --sort key: '%s'", tok);
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001722 free(str);
1723 return -1;
1724 }
1725 }
1726
1727 free(str);
1728 return 0;
1729}
1730
1731static int setup_page_sorting(struct list_head *sort_list, const char *arg)
1732{
1733 char *tok;
1734 char *str = strdup(arg);
1735 char *pos = str;
1736
1737 if (!str) {
1738 pr_err("%s: strdup failed\n", __func__);
1739 return -1;
1740 }
1741
1742 while (true) {
1743 tok = strsep(&pos, ",");
1744 if (!tok)
1745 break;
1746 if (page_sort_dimension__add(tok, sort_list) < 0) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03001747 pr_err("Unknown page --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +09001748 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +08001749 return -1;
1750 }
1751 }
1752
1753 free(str);
1754 return 0;
1755}
1756
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001757static int parse_sort_opt(const struct option *opt __maybe_unused,
1758 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001759{
Li Zefanba77c9e2009-11-20 15:53:25 +08001760 if (!arg)
1761 return -1;
1762
Namhyung Kim0c160d42015-04-21 13:55:06 +09001763 if (kmem_page > kmem_slab ||
1764 (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001765 if (caller_flag > alloc_flag)
1766 return setup_page_sorting(&page_caller_sort, arg);
1767 else
1768 return setup_page_sorting(&page_alloc_sort, arg);
1769 } else {
1770 if (caller_flag > alloc_flag)
1771 return setup_slab_sorting(&slab_caller_sort, arg);
1772 else
1773 return setup_slab_sorting(&slab_alloc_sort, arg);
1774 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001775
1776 return 0;
1777}
1778
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001779static int parse_caller_opt(const struct option *opt __maybe_unused,
1780 const char *arg __maybe_unused,
1781 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001782{
Li Zefan90b86a92009-12-10 15:21:57 +08001783 caller_flag = (alloc_flag + 1);
1784 return 0;
1785}
Li Zefanba77c9e2009-11-20 15:53:25 +08001786
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001787static int parse_alloc_opt(const struct option *opt __maybe_unused,
1788 const char *arg __maybe_unused,
1789 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +08001790{
1791 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001792 return 0;
1793}
1794
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001795static int parse_slab_opt(const struct option *opt __maybe_unused,
1796 const char *arg __maybe_unused,
1797 int unset __maybe_unused)
1798{
1799 kmem_slab = (kmem_page + 1);
1800 return 0;
1801}
1802
1803static int parse_page_opt(const struct option *opt __maybe_unused,
1804 const char *arg __maybe_unused,
1805 int unset __maybe_unused)
1806{
1807 kmem_page = (kmem_slab + 1);
1808 return 0;
1809}
1810
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001811static int parse_line_opt(const struct option *opt __maybe_unused,
1812 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001813{
1814 int lines;
1815
1816 if (!arg)
1817 return -1;
1818
1819 lines = strtoul(arg, NULL, 10);
1820
1821 if (caller_flag > alloc_flag)
1822 caller_lines = lines;
1823 else
1824 alloc_lines = lines;
1825
1826 return 0;
1827}
1828
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001829static int __cmd_record(int argc, const char **argv)
1830{
1831 const char * const record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001832 "record", "-a", "-R", "-c", "1",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001833 };
1834 const char * const slab_events[] = {
Li Zefanba77c9e2009-11-20 15:53:25 +08001835 "-e", "kmem:kmalloc",
1836 "-e", "kmem:kmalloc_node",
1837 "-e", "kmem:kfree",
1838 "-e", "kmem:kmem_cache_alloc",
1839 "-e", "kmem:kmem_cache_alloc_node",
1840 "-e", "kmem:kmem_cache_free",
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001841 };
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001842 const char * const page_events[] = {
1843 "-e", "kmem:mm_page_alloc",
1844 "-e", "kmem:mm_page_free",
1845 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001846 unsigned int rec_argc, i, j;
1847 const char **rec_argv;
1848
1849 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001850 if (kmem_slab)
1851 rec_argc += ARRAY_SIZE(slab_events);
1852 if (kmem_page)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001853 rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001854
Li Zefanba77c9e2009-11-20 15:53:25 +08001855 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1856
Chris Samuelce47dc52010-11-13 13:35:06 +11001857 if (rec_argv == NULL)
1858 return -ENOMEM;
1859
Li Zefanba77c9e2009-11-20 15:53:25 +08001860 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1861 rec_argv[i] = strdup(record_args[i]);
1862
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001863 if (kmem_slab) {
1864 for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
1865 rec_argv[i] = strdup(slab_events[j]);
1866 }
1867 if (kmem_page) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001868 rec_argv[i++] = strdup("-g");
1869
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001870 for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
1871 rec_argv[i] = strdup(page_events[j]);
1872 }
1873
Li Zefanba77c9e2009-11-20 15:53:25 +08001874 for (j = 1; j < (unsigned int)argc; j++, i++)
1875 rec_argv[i] = argv[j];
1876
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001877 return cmd_record(i, rec_argv);
Li Zefanba77c9e2009-11-20 15:53:25 +08001878}
1879
Wang Nanb8cbb342016-02-26 09:31:51 +00001880static int kmem_config(const char *var, const char *value, void *cb __maybe_unused)
Namhyung Kim0c160d42015-04-21 13:55:06 +09001881{
1882 if (!strcmp(var, "kmem.default")) {
1883 if (!strcmp(value, "slab"))
1884 kmem_default = KMEM_SLAB;
1885 else if (!strcmp(value, "page"))
1886 kmem_default = KMEM_PAGE;
1887 else
1888 pr_err("invalid default value ('slab' or 'page' required): %s\n",
1889 value);
1890 return 0;
1891 }
1892
Wang Nanb8cbb342016-02-26 09:31:51 +00001893 return 0;
Namhyung Kim0c160d42015-04-21 13:55:06 +09001894}
1895
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001896int cmd_kmem(int argc, const char **argv)
Li Zefanba77c9e2009-11-20 15:53:25 +08001897{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001898 const char * const default_slab_sort = "frag,hit,bytes";
1899 const char * const default_page_sort = "bytes,hit";
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001900 struct perf_data data = {
Yunlong Songd1eeb772015-04-02 21:47:12 +08001901 .mode = PERF_DATA_MODE_READ,
1902 };
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001903 const struct option kmem_options[] = {
1904 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Namhyung Kimbd72a332015-03-12 16:32:47 +09001905 OPT_INCR('v', "verbose", &verbose,
1906 "be more verbose (show symbol address, etc)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001907 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
1908 "show per-callsite statistics", parse_caller_opt),
1909 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
1910 "show per-allocation statistics", parse_alloc_opt),
1911 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001912 "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, "
1913 "page, order, migtype, gfp", parse_sort_opt),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001914 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
1915 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001916 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001917 OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
1918 parse_slab_opt),
1919 OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
1920 parse_page_opt),
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001921 OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
David Ahern2a865bd2016-11-29 10:15:45 -07001922 OPT_STRING(0, "time", &time_str, "str",
1923 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001924 OPT_END()
1925 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001926 const char *const kmem_subcommands[] = { "record", "stat", NULL };
1927 const char *kmem_usage[] = {
1928 NULL,
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001929 NULL
1930 };
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001931 struct perf_session *session;
Rasmus Villemoes49b8e2b2018-11-03 00:06:23 +01001932 static 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 -03001933 int ret = perf_config(kmem_config, NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001934
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001935 if (ret)
1936 return ret;
1937
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001938 argc = parse_options_subcommand(argc, argv, kmem_options,
1939 kmem_subcommands, kmem_usage, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +08001940
Li Zefan90b86a92009-12-10 15:21:57 +08001941 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +08001942 usage_with_options(kmem_usage, kmem_options);
1943
Namhyung Kim0c160d42015-04-21 13:55:06 +09001944 if (kmem_slab == 0 && kmem_page == 0) {
1945 if (kmem_default == KMEM_SLAB)
1946 kmem_slab = 1;
1947 else
1948 kmem_page = 1;
1949 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001950
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001951 if (!strncmp(argv[0], "rec", 3)) {
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001952 symbol__init(NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001953 return __cmd_record(argc, argv);
1954 }
1955
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001956 data.path = input_name;
Jiri Olsa28939e12015-04-06 14:36:08 +09001957
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001958 kmem_session = session = perf_session__new(&data, false, &perf_kmem);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001959 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001960 return -1;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001961
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001962 ret = -1;
1963
Namhyung Kima923e2c2015-05-05 23:52:52 +09001964 if (kmem_slab) {
1965 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
1966 "kmem:kmalloc")) {
1967 pr_err(errmsg, "slab", "slab");
Taeung Song249ca1a2015-06-30 17:15:21 +09001968 goto out_delete;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001969 }
1970 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001971
Namhyung Kima923e2c2015-05-05 23:52:52 +09001972 if (kmem_page) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001973 struct evsel *evsel;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001974
1975 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
1976 "kmem:mm_page_alloc");
1977 if (evsel == NULL) {
1978 pr_err(errmsg, "page", "page");
Taeung Song249ca1a2015-06-30 17:15:21 +09001979 goto out_delete;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001980 }
1981
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -04001982 kmem_page_size = tep_get_page_size(evsel->tp_format->tep);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001983 symbol_conf.use_callchain = true;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001984 }
1985
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001986 symbol__init(&session->header.env);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001987
David Ahern2a865bd2016-11-29 10:15:45 -07001988 if (perf_time__parse_str(&ptime, time_str) != 0) {
1989 pr_err("Invalid time string\n");
Christophe JAILLET79f56eb2017-09-16 08:09:36 +02001990 ret = -EINVAL;
1991 goto out_delete;
David Ahern2a865bd2016-11-29 10:15:45 -07001992 }
1993
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001994 if (!strcmp(argv[0], "stat")) {
Namhyung Kim77cfe382015-03-23 15:30:40 +09001995 setlocale(LC_ALL, "");
1996
Don Zickus4b627952014-04-07 14:55:23 -04001997 if (cpu__setup_cpunode_map())
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001998 goto out_delete;
Li Zefanba77c9e2009-11-20 15:53:25 +08001999
Namhyung Kimfb4f3132015-04-21 13:55:03 +09002000 if (list_empty(&slab_caller_sort))
2001 setup_slab_sorting(&slab_caller_sort, default_slab_sort);
2002 if (list_empty(&slab_alloc_sort))
2003 setup_slab_sorting(&slab_alloc_sort, default_slab_sort);
2004 if (list_empty(&page_caller_sort))
2005 setup_page_sorting(&page_caller_sort, default_page_sort);
2006 if (list_empty(&page_alloc_sort))
2007 setup_page_sorting(&page_alloc_sort, default_page_sort);
Li Zefan7d0d3942009-11-24 13:26:31 +08002008
Namhyung Kimfb4f3132015-04-21 13:55:03 +09002009 if (kmem_page) {
2010 setup_page_sorting(&page_alloc_sort_input,
2011 "page,order,migtype,gfp");
2012 setup_page_sorting(&page_caller_sort_input,
2013 "callsite,order,migtype,gfp");
2014 }
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09002015 ret = __cmd_kmem(session);
Pekka Enbergb00eca82010-01-19 19:26:11 +02002016 } else
2017 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +08002018
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09002019out_delete:
2020 perf_session__delete(session);
2021
2022 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +08002023}
2024