blob: 8f0b94563936c8a20d064e38504fc50f6079c3de [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>
Namhyung Kim77cfe382015-03-23 15:30:40 +090026#include <locale.h>
Namhyung Kimc9758cc2015-04-21 13:55:02 +090027#include <regex.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080028
Namhyung Kim0d68bc92015-04-06 14:36:10 +090029static int kmem_slab;
30static int kmem_page;
31
32static long kmem_page_size;
Namhyung Kim0c160d42015-04-21 13:55:06 +090033static enum {
34 KMEM_SLAB,
35 KMEM_PAGE,
36} kmem_default = KMEM_SLAB; /* for backward compatibility */
Namhyung Kim0d68bc92015-04-06 14:36:10 +090037
Li Zefanba77c9e2009-11-20 15:53:25 +080038struct alloc_stat;
Namhyung Kimfb4f3132015-04-21 13:55:03 +090039typedef int (*sort_fn_t)(void *, void *);
Li Zefanba77c9e2009-11-20 15:53:25 +080040
Li Zefanba77c9e2009-11-20 15:53:25 +080041static int alloc_flag;
42static int caller_flag;
43
Li Zefanba77c9e2009-11-20 15:53:25 +080044static int alloc_lines = -1;
45static int caller_lines = -1;
46
Li Zefan7707b6b2009-11-24 13:25:48 +080047static bool raw_ip;
48
Li Zefanba77c9e2009-11-20 15:53:25 +080049struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080050 u64 call_site;
51 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080052 u64 bytes_req;
53 u64 bytes_alloc;
David Ahernaa58e9a2016-11-25 14:42:13 -070054 u64 last_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080055 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080056 u32 pingpong;
57
58 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080059
60 struct rb_node node;
61};
62
63static struct rb_root root_alloc_stat;
64static struct rb_root root_alloc_sorted;
65static struct rb_root root_caller_stat;
66static struct rb_root root_caller_sorted;
67
David Ahernaa58e9a2016-11-25 14:42:13 -070068static unsigned long total_requested, total_allocated, total_freed;
Li Zefan7d0d3942009-11-24 13:26:31 +080069static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080070
David Ahern2a865bd2016-11-29 10:15:45 -070071/* filters for controlling start and stop of time of analysis */
72static struct perf_time_interval ptime;
73const char *time_str;
74
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030075static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
76 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +080077{
78 struct rb_node **node = &root_alloc_stat.rb_node;
79 struct rb_node *parent = NULL;
80 struct alloc_stat *data = NULL;
81
Li Zefanba77c9e2009-11-20 15:53:25 +080082 while (*node) {
83 parent = *node;
84 data = rb_entry(*node, struct alloc_stat, node);
85
86 if (ptr > data->ptr)
87 node = &(*node)->rb_right;
88 else if (ptr < data->ptr)
89 node = &(*node)->rb_left;
90 else
91 break;
92 }
93
94 if (data && data->ptr == ptr) {
95 data->hit++;
96 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +080097 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080098 } else {
99 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300100 if (!data) {
101 pr_err("%s: malloc failed\n", __func__);
102 return -1;
103 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800104 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +0800105 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800106 data->hit = 1;
107 data->bytes_req = bytes_req;
108 data->bytes_alloc = bytes_alloc;
109
110 rb_link_node(&data->node, parent, node);
111 rb_insert_color(&data->node, &root_alloc_stat);
112 }
Li Zefan079d3f62009-11-24 13:26:55 +0800113 data->call_site = call_site;
114 data->alloc_cpu = cpu;
David Ahernaa58e9a2016-11-25 14:42:13 -0700115 data->last_alloc = bytes_alloc;
116
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300117 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800118}
119
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300120static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800121 int bytes_req, int bytes_alloc)
122{
123 struct rb_node **node = &root_caller_stat.rb_node;
124 struct rb_node *parent = NULL;
125 struct alloc_stat *data = NULL;
126
Li Zefanba77c9e2009-11-20 15:53:25 +0800127 while (*node) {
128 parent = *node;
129 data = rb_entry(*node, struct alloc_stat, node);
130
131 if (call_site > data->call_site)
132 node = &(*node)->rb_right;
133 else if (call_site < data->call_site)
134 node = &(*node)->rb_left;
135 else
136 break;
137 }
138
139 if (data && data->call_site == call_site) {
140 data->hit++;
141 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800142 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800143 } else {
144 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300145 if (!data) {
146 pr_err("%s: malloc failed\n", __func__);
147 return -1;
148 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800149 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800150 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800151 data->hit = 1;
152 data->bytes_req = bytes_req;
153 data->bytes_alloc = bytes_alloc;
154
155 rb_link_node(&data->node, parent, node);
156 rb_insert_color(&data->node, &root_caller_stat);
157 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300158
159 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800160}
161
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300162static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300163 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800164{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300165 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
166 call_site = perf_evsel__intval(evsel, sample, "call_site");
167 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
168 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800169
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300170 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300171 insert_caller_stat(call_site, bytes_req, bytes_alloc))
172 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800173
174 total_requested += bytes_req;
175 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800176
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300177 nr_allocs++;
178 return 0;
179}
180
181static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
182 struct perf_sample *sample)
183{
184 int ret = perf_evsel__process_alloc_event(evsel, sample);
185
186 if (!ret) {
Don Zickus4b627952014-04-07 14:55:23 -0400187 int node1 = cpu__get_node(sample->cpu),
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300188 node2 = perf_evsel__intval(evsel, sample, "node");
189
Li Zefan7d0d3942009-11-24 13:26:31 +0800190 if (node1 != node2)
191 nr_cross_allocs++;
192 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300193
194 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800195}
196
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900197static int ptr_cmp(void *, void *);
198static int slab_callsite_cmp(void *, void *);
Li Zefan079d3f62009-11-24 13:26:55 +0800199
200static struct alloc_stat *search_alloc_stat(unsigned long ptr,
201 unsigned long call_site,
202 struct rb_root *root,
203 sort_fn_t sort_fn)
204{
205 struct rb_node *node = root->rb_node;
206 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
207
208 while (node) {
209 struct alloc_stat *data;
210 int cmp;
211
212 data = rb_entry(node, struct alloc_stat, node);
213
214 cmp = sort_fn(&key, data);
215 if (cmp < 0)
216 node = node->rb_left;
217 else if (cmp > 0)
218 node = node->rb_right;
219 else
220 return data;
221 }
222 return NULL;
223}
224
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300225static int perf_evsel__process_free_event(struct perf_evsel *evsel,
226 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800227{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300228 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800229 struct alloc_stat *s_alloc, *s_caller;
230
Li Zefan079d3f62009-11-24 13:26:55 +0800231 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
232 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300233 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800234
David Ahernaa58e9a2016-11-25 14:42:13 -0700235 total_freed += s_alloc->last_alloc;
236
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300237 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800238 s_alloc->pingpong++;
239
240 s_caller = search_alloc_stat(0, s_alloc->call_site,
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900241 &root_caller_stat,
242 slab_callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300243 if (!s_caller)
244 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800245 s_caller->pingpong++;
246 }
247 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300248
249 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800250}
251
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900252static u64 total_page_alloc_bytes;
253static u64 total_page_free_bytes;
254static u64 total_page_nomatch_bytes;
255static u64 total_page_fail_bytes;
256static unsigned long nr_page_allocs;
257static unsigned long nr_page_frees;
258static unsigned long nr_page_fails;
259static unsigned long nr_page_nomatch;
260
261static bool use_pfn;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900262static bool live_page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900263static struct perf_session *kmem_session;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900264
265#define MAX_MIGRATE_TYPES 6
266#define MAX_PAGE_ORDER 11
267
268static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
269
270struct page_stat {
271 struct rb_node node;
272 u64 page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900273 u64 callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900274 int order;
275 unsigned gfp_flags;
276 unsigned migrate_type;
277 u64 alloc_bytes;
278 u64 free_bytes;
279 int nr_alloc;
280 int nr_free;
281};
282
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900283static struct rb_root page_live_tree;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900284static struct rb_root page_alloc_tree;
285static struct rb_root page_alloc_sorted;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900286static struct rb_root page_caller_tree;
287static struct rb_root page_caller_sorted;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900288
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900289struct alloc_func {
290 u64 start;
291 u64 end;
292 char *name;
293};
294
295static int nr_alloc_funcs;
296static struct alloc_func *alloc_func_list;
297
298static int funcmp(const void *a, const void *b)
299{
300 const struct alloc_func *fa = a;
301 const struct alloc_func *fb = b;
302
303 if (fa->start > fb->start)
304 return 1;
305 else
306 return -1;
307}
308
309static int callcmp(const void *a, const void *b)
310{
311 const struct alloc_func *fa = a;
312 const struct alloc_func *fb = b;
313
314 if (fb->start <= fa->start && fa->end < fb->end)
315 return 0;
316
317 if (fa->start > fb->start)
318 return 1;
319 else
320 return -1;
321}
322
323static int build_alloc_func_list(void)
324{
325 int ret;
326 struct map *kernel_map;
327 struct symbol *sym;
328 struct rb_node *node;
329 struct alloc_func *func;
330 struct machine *machine = &kmem_session->machines.host;
331 regex_t alloc_func_regex;
332 const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
333
334 ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
335 if (ret) {
336 char err[BUFSIZ];
337
338 regerror(ret, &alloc_func_regex, err, sizeof(err));
339 pr_err("Invalid regex: %s\n%s", pattern, err);
340 return -EINVAL;
341 }
342
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300343 kernel_map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300344 if (map__load(kernel_map) < 0) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900345 pr_err("cannot load kernel map\n");
346 return -ENOENT;
347 }
348
349 map__for_each_symbol(kernel_map, sym, node) {
350 if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
351 continue;
352
353 func = realloc(alloc_func_list,
354 (nr_alloc_funcs + 1) * sizeof(*func));
355 if (func == NULL)
356 return -ENOMEM;
357
358 pr_debug("alloc func: %s\n", sym->name);
359 func[nr_alloc_funcs].start = sym->start;
360 func[nr_alloc_funcs].end = sym->end;
361 func[nr_alloc_funcs].name = sym->name;
362
363 alloc_func_list = func;
364 nr_alloc_funcs++;
365 }
366
367 qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
368
369 regfree(&alloc_func_regex);
370 return 0;
371}
372
373/*
374 * Find first non-memory allocation function from callchain.
375 * The allocation functions are in the 'alloc_func_list'.
376 */
377static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample)
378{
379 struct addr_location al;
380 struct machine *machine = &kmem_session->machines.host;
381 struct callchain_cursor_node *node;
382
383 if (alloc_func_list == NULL) {
384 if (build_alloc_func_list() < 0)
385 goto out;
386 }
387
388 al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300389 sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900390
391 callchain_cursor_commit(&callchain_cursor);
392 while (true) {
393 struct alloc_func key, *caller;
394 u64 addr;
395
396 node = callchain_cursor_current(&callchain_cursor);
397 if (node == NULL)
398 break;
399
400 key.start = key.end = node->ip;
401 caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
402 sizeof(key), callcmp);
403 if (!caller) {
404 /* found */
405 if (node->map)
406 addr = map__unmap_ip(node->map, node->ip);
407 else
408 addr = node->ip;
409
410 return addr;
411 } else
412 pr_debug3("skipping alloc function: %s\n", caller->name);
413
414 callchain_cursor_advance(&callchain_cursor);
415 }
416
417out:
418 pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
419 return sample->ip;
420}
421
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900422struct sort_dimension {
423 const char name[20];
424 sort_fn_t cmp;
425 struct list_head list;
426};
427
428static LIST_HEAD(page_alloc_sort_input);
429static LIST_HEAD(page_caller_sort_input);
430
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900431static struct page_stat *
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900432__page_stat__findnew_page(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900433{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900434 struct rb_node **node = &page_live_tree.rb_node;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900435 struct rb_node *parent = NULL;
436 struct page_stat *data;
437
438 while (*node) {
439 s64 cmp;
440
441 parent = *node;
442 data = rb_entry(*node, struct page_stat, node);
443
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900444 cmp = data->page - pstat->page;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900445 if (cmp < 0)
446 node = &parent->rb_left;
447 else if (cmp > 0)
448 node = &parent->rb_right;
449 else
450 return data;
451 }
452
453 if (!create)
454 return NULL;
455
456 data = zalloc(sizeof(*data));
457 if (data != NULL) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900458 data->page = pstat->page;
459 data->order = pstat->order;
460 data->gfp_flags = pstat->gfp_flags;
461 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900462
463 rb_link_node(&data->node, parent, node);
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900464 rb_insert_color(&data->node, &page_live_tree);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900465 }
466
467 return data;
468}
469
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900470static struct page_stat *page_stat__find_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900471{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900472 return __page_stat__findnew_page(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900473}
474
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900475static struct page_stat *page_stat__findnew_page(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900476{
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900477 return __page_stat__findnew_page(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900478}
479
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900480static struct page_stat *
481__page_stat__findnew_alloc(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900482{
483 struct rb_node **node = &page_alloc_tree.rb_node;
484 struct rb_node *parent = NULL;
485 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900486 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900487
488 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900489 int cmp = 0;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900490
491 parent = *node;
492 data = rb_entry(*node, struct page_stat, node);
493
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900494 list_for_each_entry(sort, &page_alloc_sort_input, list) {
495 cmp = sort->cmp(pstat, data);
496 if (cmp)
497 break;
498 }
499
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900500 if (cmp < 0)
501 node = &parent->rb_left;
502 else if (cmp > 0)
503 node = &parent->rb_right;
504 else
505 return data;
506 }
507
508 if (!create)
509 return NULL;
510
511 data = zalloc(sizeof(*data));
512 if (data != NULL) {
David Ahern6b1a2752015-04-14 13:49:33 -0400513 data->page = pstat->page;
514 data->order = pstat->order;
515 data->gfp_flags = pstat->gfp_flags;
516 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900517
518 rb_link_node(&data->node, parent, node);
519 rb_insert_color(&data->node, &page_alloc_tree);
520 }
521
522 return data;
523}
524
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900525static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
526{
527 return __page_stat__findnew_alloc(pstat, false);
528}
529
530static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
531{
532 return __page_stat__findnew_alloc(pstat, true);
533}
534
535static struct page_stat *
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900536__page_stat__findnew_caller(struct page_stat *pstat, bool create)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900537{
538 struct rb_node **node = &page_caller_tree.rb_node;
539 struct rb_node *parent = NULL;
540 struct page_stat *data;
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900541 struct sort_dimension *sort;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900542
543 while (*node) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900544 int cmp = 0;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900545
546 parent = *node;
547 data = rb_entry(*node, struct page_stat, node);
548
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900549 list_for_each_entry(sort, &page_caller_sort_input, list) {
550 cmp = sort->cmp(pstat, data);
551 if (cmp)
552 break;
553 }
554
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900555 if (cmp < 0)
556 node = &parent->rb_left;
557 else if (cmp > 0)
558 node = &parent->rb_right;
559 else
560 return data;
561 }
562
563 if (!create)
564 return NULL;
565
566 data = zalloc(sizeof(*data));
567 if (data != NULL) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900568 data->callsite = pstat->callsite;
569 data->order = pstat->order;
570 data->gfp_flags = pstat->gfp_flags;
571 data->migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900572
573 rb_link_node(&data->node, parent, node);
574 rb_insert_color(&data->node, &page_caller_tree);
575 }
576
577 return data;
578}
579
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900580static struct page_stat *page_stat__find_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900581{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900582 return __page_stat__findnew_caller(pstat, false);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900583}
584
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900585static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900586{
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900587 return __page_stat__findnew_caller(pstat, true);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900588}
589
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900590static bool valid_page(u64 pfn_or_page)
591{
592 if (use_pfn && pfn_or_page == -1UL)
593 return false;
594 if (!use_pfn && pfn_or_page == 0)
595 return false;
596 return true;
597}
598
Namhyung Kim0e111152015-04-21 13:55:05 +0900599struct gfp_flag {
600 unsigned int flags;
601 char *compact_str;
602 char *human_readable;
603};
604
605static struct gfp_flag *gfps;
606static int nr_gfps;
607
608static int gfpcmp(const void *a, const void *b)
609{
610 const struct gfp_flag *fa = a;
611 const struct gfp_flag *fb = b;
612
613 return fa->flags - fb->flags;
614}
615
Vlastimil Babka420adbe92016-03-15 14:55:52 -0700616/* see include/trace/events/mmflags.h */
Namhyung Kim0e111152015-04-21 13:55:05 +0900617static const struct {
618 const char *original;
619 const char *compact;
620} gfp_compact_table[] = {
621 { "GFP_TRANSHUGE", "THP" },
Vlastimil Babka25160352016-07-28 15:49:25 -0700622 { "GFP_TRANSHUGE_LIGHT", "THL" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900623 { "GFP_HIGHUSER_MOVABLE", "HUM" },
624 { "GFP_HIGHUSER", "HU" },
625 { "GFP_USER", "U" },
626 { "GFP_TEMPORARY", "TMP" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700627 { "GFP_KERNEL_ACCOUNT", "KAC" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900628 { "GFP_KERNEL", "K" },
629 { "GFP_NOFS", "NF" },
630 { "GFP_ATOMIC", "A" },
631 { "GFP_NOIO", "NI" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900632 { "GFP_NOWAIT", "NW" },
Vlastimil Babka14e0a212016-03-15 14:55:49 -0700633 { "GFP_DMA", "D" },
634 { "__GFP_HIGHMEM", "HM" },
635 { "GFP_DMA32", "D32" },
636 { "__GFP_HIGH", "H" },
637 { "__GFP_ATOMIC", "_A" },
638 { "__GFP_IO", "I" },
639 { "__GFP_FS", "F" },
640 { "__GFP_COLD", "CO" },
641 { "__GFP_NOWARN", "NWR" },
642 { "__GFP_REPEAT", "R" },
643 { "__GFP_NOFAIL", "NF" },
644 { "__GFP_NORETRY", "NR" },
645 { "__GFP_COMP", "C" },
646 { "__GFP_ZERO", "Z" },
647 { "__GFP_NOMEMALLOC", "NMA" },
648 { "__GFP_MEMALLOC", "MA" },
649 { "__GFP_HARDWALL", "HW" },
650 { "__GFP_THISNODE", "TN" },
651 { "__GFP_RECLAIMABLE", "RC" },
652 { "__GFP_MOVABLE", "M" },
653 { "__GFP_ACCOUNT", "AC" },
654 { "__GFP_NOTRACK", "NT" },
655 { "__GFP_WRITE", "WR" },
656 { "__GFP_RECLAIM", "R" },
657 { "__GFP_DIRECT_RECLAIM", "DR" },
658 { "__GFP_KSWAPD_RECLAIM", "KR" },
Namhyung Kim0e111152015-04-21 13:55:05 +0900659};
660
661static size_t max_gfp_len;
662
663static char *compact_gfp_flags(char *gfp_flags)
664{
665 char *orig_flags = strdup(gfp_flags);
666 char *new_flags = NULL;
Arnaldo Carvalho de Melob2365122015-05-29 09:48:13 -0300667 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900668 size_t len = 0;
669
670 if (orig_flags == NULL)
671 return NULL;
672
673 str = strtok_r(orig_flags, "|", &pos);
674 while (str) {
675 size_t i;
676 char *new;
677 const char *cpt;
678
679 for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
680 if (strcmp(gfp_compact_table[i].original, str))
681 continue;
682
683 cpt = gfp_compact_table[i].compact;
684 new = realloc(new_flags, len + strlen(cpt) + 2);
685 if (new == NULL) {
686 free(new_flags);
687 return NULL;
688 }
689
690 new_flags = new;
691
692 if (!len) {
693 strcpy(new_flags, cpt);
694 } else {
695 strcat(new_flags, "|");
696 strcat(new_flags, cpt);
697 len++;
698 }
699
700 len += strlen(cpt);
701 }
702
703 str = strtok_r(NULL, "|", &pos);
704 }
705
706 if (max_gfp_len < len)
707 max_gfp_len = len;
708
709 free(orig_flags);
710 return new_flags;
711}
712
713static char *compact_gfp_string(unsigned long gfp_flags)
714{
715 struct gfp_flag key = {
716 .flags = gfp_flags,
717 };
718 struct gfp_flag *gfp;
719
720 gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp);
721 if (gfp)
722 return gfp->compact_str;
723
724 return NULL;
725}
726
727static int parse_gfp_flags(struct perf_evsel *evsel, struct perf_sample *sample,
728 unsigned int gfp_flags)
729{
730 struct pevent_record record = {
731 .cpu = sample->cpu,
732 .data = sample->raw_data,
733 .size = sample->raw_size,
734 };
735 struct trace_seq seq;
Arnaldo Carvalho de Melo08a9b982015-05-11 11:41:17 -0300736 char *str, *pos = NULL;
Namhyung Kim0e111152015-04-21 13:55:05 +0900737
738 if (nr_gfps) {
739 struct gfp_flag key = {
740 .flags = gfp_flags,
741 };
742
743 if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp))
744 return 0;
745 }
746
747 trace_seq_init(&seq);
748 pevent_event_info(&seq, evsel->tp_format, &record);
749
750 str = strtok_r(seq.buffer, " ", &pos);
751 while (str) {
752 if (!strncmp(str, "gfp_flags=", 10)) {
753 struct gfp_flag *new;
754
755 new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps));
756 if (new == NULL)
757 return -ENOMEM;
758
759 gfps = new;
760 new += nr_gfps++;
761
762 new->flags = gfp_flags;
763 new->human_readable = strdup(str + 10);
764 new->compact_str = compact_gfp_flags(str + 10);
765 if (!new->human_readable || !new->compact_str)
766 return -ENOMEM;
767
768 qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp);
769 }
770
771 str = strtok_r(NULL, " ", &pos);
772 }
773
774 trace_seq_destroy(&seq);
775 return 0;
776}
777
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900778static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
779 struct perf_sample *sample)
780{
781 u64 page;
782 unsigned int order = perf_evsel__intval(evsel, sample, "order");
783 unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
784 unsigned int migrate_type = perf_evsel__intval(evsel, sample,
785 "migratetype");
786 u64 bytes = kmem_page_size << order;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900787 u64 callsite;
David Ahern6b1a2752015-04-14 13:49:33 -0400788 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900789 struct page_stat this = {
790 .order = order,
791 .gfp_flags = gfp_flags,
792 .migrate_type = migrate_type,
793 };
794
795 if (use_pfn)
796 page = perf_evsel__intval(evsel, sample, "pfn");
797 else
798 page = perf_evsel__intval(evsel, sample, "page");
799
800 nr_page_allocs++;
801 total_page_alloc_bytes += bytes;
802
803 if (!valid_page(page)) {
804 nr_page_fails++;
805 total_page_fail_bytes += bytes;
806
807 return 0;
808 }
809
Namhyung Kim0e111152015-04-21 13:55:05 +0900810 if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
811 return -1;
812
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900813 callsite = find_callsite(evsel, sample);
814
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900815 /*
816 * This is to find the current page (with correct gfp flags and
817 * migrate type) at free event.
818 */
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900819 this.page = page;
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900820 pstat = page_stat__findnew_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400821 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900822 return -ENOMEM;
823
David Ahern6b1a2752015-04-14 13:49:33 -0400824 pstat->nr_alloc++;
825 pstat->alloc_bytes += bytes;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900826 pstat->callsite = callsite;
827
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900828 if (!live_page) {
829 pstat = page_stat__findnew_alloc(&this);
830 if (pstat == NULL)
831 return -ENOMEM;
832
833 pstat->nr_alloc++;
834 pstat->alloc_bytes += bytes;
835 pstat->callsite = callsite;
836 }
837
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900838 this.callsite = callsite;
839 pstat = page_stat__findnew_caller(&this);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900840 if (pstat == NULL)
841 return -ENOMEM;
842
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900843 pstat->nr_alloc++;
844 pstat->alloc_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900845
846 order_stats[order][migrate_type]++;
847
848 return 0;
849}
850
851static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
852 struct perf_sample *sample)
853{
854 u64 page;
855 unsigned int order = perf_evsel__intval(evsel, sample, "order");
856 u64 bytes = kmem_page_size << order;
David Ahern6b1a2752015-04-14 13:49:33 -0400857 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900858 struct page_stat this = {
859 .order = order,
860 };
861
862 if (use_pfn)
863 page = perf_evsel__intval(evsel, sample, "pfn");
864 else
865 page = perf_evsel__intval(evsel, sample, "page");
866
867 nr_page_frees++;
868 total_page_free_bytes += bytes;
869
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900870 this.page = page;
871 pstat = page_stat__find_page(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400872 if (pstat == NULL) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900873 pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
874 page, order);
875
876 nr_page_nomatch++;
877 total_page_nomatch_bytes += bytes;
878
879 return 0;
880 }
881
David Ahern6b1a2752015-04-14 13:49:33 -0400882 this.gfp_flags = pstat->gfp_flags;
883 this.migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900884 this.callsite = pstat->callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900885
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900886 rb_erase(&pstat->node, &page_live_tree);
David Ahern6b1a2752015-04-14 13:49:33 -0400887 free(pstat);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900888
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900889 if (live_page) {
890 order_stats[this.order][this.migrate_type]--;
891 } else {
892 pstat = page_stat__find_alloc(&this);
893 if (pstat == NULL)
894 return -ENOMEM;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900895
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900896 pstat->nr_free++;
897 pstat->free_bytes += bytes;
898 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900899
Namhyung Kimfb4f3132015-04-21 13:55:03 +0900900 pstat = page_stat__find_caller(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400901 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900902 return -ENOENT;
903
David Ahern6b1a2752015-04-14 13:49:33 -0400904 pstat->nr_free++;
905 pstat->free_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900906
Namhyung Kim2a7ef022015-04-21 13:55:04 +0900907 if (live_page) {
908 pstat->nr_alloc--;
909 pstat->alloc_bytes -= bytes;
910
911 if (pstat->nr_alloc == 0) {
912 rb_erase(&pstat->node, &page_caller_tree);
913 free(pstat);
914 }
915 }
916
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900917 return 0;
918}
919
David Ahern2a865bd2016-11-29 10:15:45 -0700920static bool perf_kmem__skip_sample(struct perf_sample *sample)
921{
922 /* skip sample based on time? */
923 if (perf_time__skip_sample(&ptime, sample->time))
924 return true;
925
926 return false;
927}
928
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300929typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
930 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800931
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300932static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200933 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200934 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300935 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200936 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800937{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300938 int err = 0;
Adrian Hunteref893252013-08-27 11:23:06 +0300939 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900940 sample->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800941
Li Zefanba77c9e2009-11-20 15:53:25 +0800942 if (thread == NULL) {
943 pr_debug("problem processing %d event, skipping it.\n",
944 event->header.type);
945 return -1;
946 }
947
David Ahern2a865bd2016-11-29 10:15:45 -0700948 if (perf_kmem__skip_sample(sample))
949 return 0;
950
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200951 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800952
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300953 if (evsel->handler != NULL) {
954 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300955 err = f(evsel, sample);
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300956 }
957
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300958 thread__put(thread);
959
960 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +0800961}
962
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300963static struct perf_tool perf_kmem = {
964 .sample = process_sample_event,
965 .comm = perf_event__process_comm,
Namhyung Kim64c40902014-08-01 14:59:31 +0900966 .mmap = perf_event__process_mmap,
967 .mmap2 = perf_event__process_mmap2,
Hari Bathinif3b36142017-03-08 02:11:43 +0530968 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200969 .ordered_events = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800970};
971
Li Zefanba77c9e2009-11-20 15:53:25 +0800972static double fragmentation(unsigned long n_req, unsigned long n_alloc)
973{
974 if (n_alloc == 0)
975 return 0.0;
976 else
977 return 100.0 - (100.0 * n_req / n_alloc);
978}
979
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900980static void __print_slab_result(struct rb_root *root,
981 struct perf_session *session,
982 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800983{
984 struct rb_node *next;
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300985 struct machine *machine = &session->machines.host;
Li Zefanba77c9e2009-11-20 15:53:25 +0800986
Namhyung Kim65f46e02015-03-12 16:32:48 +0900987 printf("%.105s\n", graph_dotted_line);
Li Zefan079d3f62009-11-24 13:26:55 +0800988 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200989 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Namhyung Kim65f46e02015-03-12 16:32:48 +0900990 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800991
992 next = rb_first(root);
993
994 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200995 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
996 node);
997 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300998 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +0800999 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001000 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +08001001
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001002 if (is_caller) {
1003 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +08001004 if (!raw_ip)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001005 sym = machine__find_kernel_function(machine, addr, &map);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001006 } else
1007 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +08001008
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001009 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001010 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -03001011 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001012 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001013 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +08001014 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -02001015
Namhyung Kim65f46e02015-03-12 16:32:48 +09001016 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +08001017 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +08001018 (unsigned long)data->bytes_alloc / data->hit,
1019 (unsigned long long)data->bytes_req,
1020 (unsigned long)data->bytes_req / data->hit,
1021 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +08001022 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +08001023 fragmentation(data->bytes_req, data->bytes_alloc));
1024
1025 next = rb_next(next);
1026 }
1027
1028 if (n_lines == -1)
Namhyung Kim65f46e02015-03-12 16:32:48 +09001029 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +08001030
Namhyung Kim65f46e02015-03-12 16:32:48 +09001031 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +08001032}
1033
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001034static const char * const migrate_type_str[] = {
1035 "UNMOVABL",
1036 "RECLAIM",
1037 "MOVABLE",
1038 "RESERVED",
1039 "CMA/ISLT",
1040 "UNKNOWN",
1041};
1042
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001043static void __print_page_alloc_result(struct perf_session *session, int n_lines)
Li Zefanba77c9e2009-11-20 15:53:25 +08001044{
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001045 struct rb_node *next = rb_first(&page_alloc_sorted);
1046 struct machine *machine = &session->machines.host;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001047 const char *format;
Namhyung Kim0e111152015-04-21 13:55:05 +09001048 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001049
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001050 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001051 printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1052 use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total",
1053 gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001054 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001055
1056 if (use_pfn)
Namhyung Kim0e111152015-04-21 13:55:05 +09001057 format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001058 else
Namhyung Kim0e111152015-04-21 13:55:05 +09001059 format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001060
1061 while (next && n_lines--) {
1062 struct page_stat *data;
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001063 struct symbol *sym;
1064 struct map *map;
1065 char buf[32];
1066 char *caller = buf;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001067
1068 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001069 sym = machine__find_kernel_function(machine, data->callsite, &map);
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001070 if (sym)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001071 caller = sym->name;
1072 else
1073 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001074
1075 printf(format, (unsigned long long)data->page,
1076 (unsigned long long)data->alloc_bytes / 1024,
1077 data->nr_alloc, data->order,
1078 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001079 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001080
1081 next = rb_next(next);
1082 }
1083
Namhyung Kim0e111152015-04-21 13:55:05 +09001084 if (n_lines == -1) {
1085 printf(" ... | ... | ... | ... | ... | %-*s | ...\n",
1086 gfp_len, "...");
1087 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001088
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001089 printf("%.105s\n", graph_dotted_line);
1090}
1091
1092static void __print_page_caller_result(struct perf_session *session, int n_lines)
1093{
1094 struct rb_node *next = rb_first(&page_caller_sorted);
1095 struct machine *machine = &session->machines.host;
Namhyung Kim0e111152015-04-21 13:55:05 +09001096 int gfp_len = max(strlen("GFP flags"), max_gfp_len);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001097
1098 printf("\n%.105s\n", graph_dotted_line);
Namhyung Kim0e111152015-04-21 13:55:05 +09001099 printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n",
1100 live_page ? "Live" : "Total", gfp_len, "GFP flags");
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001101 printf("%.105s\n", graph_dotted_line);
1102
1103 while (next && n_lines--) {
1104 struct page_stat *data;
1105 struct symbol *sym;
1106 struct map *map;
1107 char buf[32];
1108 char *caller = buf;
1109
1110 data = rb_entry(next, struct page_stat, node);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001111 sym = machine__find_kernel_function(machine, data->callsite, &map);
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001112 if (sym)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001113 caller = sym->name;
1114 else
1115 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
1116
Namhyung Kim0e111152015-04-21 13:55:05 +09001117 printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n",
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001118 (unsigned long long)data->alloc_bytes / 1024,
1119 data->nr_alloc, data->order,
1120 migrate_type_str[data->migrate_type],
Namhyung Kim0e111152015-04-21 13:55:05 +09001121 gfp_len, compact_gfp_string(data->gfp_flags), caller);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001122
1123 next = rb_next(next);
1124 }
1125
Namhyung Kim0e111152015-04-21 13:55:05 +09001126 if (n_lines == -1) {
1127 printf(" ... | ... | ... | ... | %-*s | ...\n",
1128 gfp_len, "...");
1129 }
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001130
1131 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001132}
1133
Namhyung Kim0e111152015-04-21 13:55:05 +09001134static void print_gfp_flags(void)
1135{
1136 int i;
1137
1138 printf("#\n");
1139 printf("# GFP flags\n");
1140 printf("# ---------\n");
1141 for (i = 0; i < nr_gfps; i++) {
1142 printf("# %08x: %*s: %s\n", gfps[i].flags,
1143 (int) max_gfp_len, gfps[i].compact_str,
1144 gfps[i].human_readable);
1145 }
1146}
1147
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001148static void print_slab_summary(void)
1149{
1150 printf("\nSUMMARY (SLAB allocator)");
1151 printf("\n========================\n");
Namhyung Kim77cfe382015-03-23 15:30:40 +09001152 printf("Total bytes requested: %'lu\n", total_requested);
1153 printf("Total bytes allocated: %'lu\n", total_allocated);
David Ahernaa58e9a2016-11-25 14:42:13 -07001154 printf("Total bytes freed: %'lu\n", total_freed);
1155 if (total_allocated > total_freed) {
1156 printf("Net total bytes allocated: %'lu\n",
1157 total_allocated - total_freed);
1158 }
Namhyung Kim77cfe382015-03-23 15:30:40 +09001159 printf("Total bytes wasted on internal fragmentation: %'lu\n",
Li Zefanba77c9e2009-11-20 15:53:25 +08001160 total_allocated - total_requested);
1161 printf("Internal fragmentation: %f%%\n",
1162 fragmentation(total_requested, total_allocated));
Namhyung Kim77cfe382015-03-23 15:30:40 +09001163 printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +08001164}
1165
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001166static void print_page_summary(void)
1167{
1168 int o, m;
1169 u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
1170 u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
1171
1172 printf("\nSUMMARY (page allocator)");
1173 printf("\n========================\n");
1174 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests",
1175 nr_page_allocs, total_page_alloc_bytes / 1024);
1176 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests",
1177 nr_page_frees, total_page_free_bytes / 1024);
1178 printf("\n");
1179
Will Deacon6145c252015-04-23 14:40:37 +01001180 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001181 nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
Will Deacon6145c252015-04-23 14:40:37 +01001182 printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001183 nr_page_allocs - nr_alloc_freed,
1184 (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
1185 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests",
1186 nr_page_nomatch, total_page_nomatch_bytes / 1024);
1187 printf("\n");
1188
1189 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures",
1190 nr_page_fails, total_page_fail_bytes / 1024);
1191 printf("\n");
1192
1193 printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable",
1194 "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
1195 printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line,
1196 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1197 graph_dotted_line, graph_dotted_line);
1198
1199 for (o = 0; o < MAX_PAGE_ORDER; o++) {
1200 printf("%5d", o);
1201 for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
1202 if (order_stats[o][m])
1203 printf(" %'12d", order_stats[o][m]);
1204 else
1205 printf(" %12c", '.');
1206 }
1207 printf("\n");
1208 }
1209}
1210
1211static void print_slab_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001212{
1213 if (caller_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001214 __print_slab_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001215 if (alloc_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001216 __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
1217 print_slab_summary();
1218}
1219
1220static void print_page_result(struct perf_session *session)
1221{
Namhyung Kim0e111152015-04-21 13:55:05 +09001222 if (caller_flag || alloc_flag)
1223 print_gfp_flags();
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001224 if (caller_flag)
1225 __print_page_caller_result(session, caller_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001226 if (alloc_flag)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001227 __print_page_alloc_result(session, alloc_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001228 print_page_summary();
1229}
1230
1231static void print_result(struct perf_session *session)
1232{
1233 if (kmem_slab)
1234 print_slab_result(session);
1235 if (kmem_page)
1236 print_page_result(session);
Li Zefanba77c9e2009-11-20 15:53:25 +08001237}
1238
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001239static LIST_HEAD(slab_caller_sort);
1240static LIST_HEAD(slab_alloc_sort);
1241static LIST_HEAD(page_caller_sort);
1242static LIST_HEAD(page_alloc_sort);
Li Zefan29b3e152009-11-24 13:26:10 +08001243
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001244static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
1245 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001246{
1247 struct rb_node **new = &(root->rb_node);
1248 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +08001249 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +08001250
1251 while (*new) {
1252 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +08001253 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +08001254
1255 this = rb_entry(*new, struct alloc_stat, node);
1256 parent = *new;
1257
Li Zefan29b3e152009-11-24 13:26:10 +08001258 list_for_each_entry(sort, sort_list, list) {
1259 cmp = sort->cmp(data, this);
1260 if (cmp)
1261 break;
1262 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001263
1264 if (cmp > 0)
1265 new = &((*new)->rb_left);
1266 else
1267 new = &((*new)->rb_right);
1268 }
1269
1270 rb_link_node(&data->node, parent, new);
1271 rb_insert_color(&data->node, root);
1272}
1273
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001274static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
1275 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001276{
1277 struct rb_node *node;
1278 struct alloc_stat *data;
1279
1280 for (;;) {
1281 node = rb_first(root);
1282 if (!node)
1283 break;
1284
1285 rb_erase(node, root);
1286 data = rb_entry(node, struct alloc_stat, node);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001287 sort_slab_insert(root_sorted, data, sort_list);
1288 }
1289}
1290
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001291static void sort_page_insert(struct rb_root *root, struct page_stat *data,
1292 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001293{
1294 struct rb_node **new = &root->rb_node;
1295 struct rb_node *parent = NULL;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001296 struct sort_dimension *sort;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001297
1298 while (*new) {
1299 struct page_stat *this;
1300 int cmp = 0;
1301
1302 this = rb_entry(*new, struct page_stat, node);
1303 parent = *new;
1304
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001305 list_for_each_entry(sort, sort_list, list) {
1306 cmp = sort->cmp(data, this);
1307 if (cmp)
1308 break;
1309 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001310
1311 if (cmp > 0)
1312 new = &parent->rb_left;
1313 else
1314 new = &parent->rb_right;
1315 }
1316
1317 rb_link_node(&data->node, parent, new);
1318 rb_insert_color(&data->node, root);
1319}
1320
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001321static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted,
1322 struct list_head *sort_list)
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001323{
1324 struct rb_node *node;
1325 struct page_stat *data;
1326
1327 for (;;) {
1328 node = rb_first(root);
1329 if (!node)
1330 break;
1331
1332 rb_erase(node, root);
1333 data = rb_entry(node, struct page_stat, node);
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001334 sort_page_insert(root_sorted, data, sort_list);
Li Zefanba77c9e2009-11-20 15:53:25 +08001335 }
1336}
1337
1338static void sort_result(void)
1339{
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001340 if (kmem_slab) {
1341 __sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001342 &slab_alloc_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001343 __sort_slab_result(&root_caller_stat, &root_caller_sorted,
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001344 &slab_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001345 }
1346 if (kmem_page) {
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001347 if (live_page)
1348 __sort_page_result(&page_live_tree, &page_alloc_sorted,
1349 &page_alloc_sort);
1350 else
1351 __sort_page_result(&page_alloc_tree, &page_alloc_sorted,
1352 &page_alloc_sort);
1353
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001354 __sort_page_result(&page_caller_tree, &page_caller_sorted,
1355 &page_caller_sort);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001356 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001357}
1358
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001359static int __cmd_kmem(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001360{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001361 int err = -EINVAL;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001362 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001363 const struct perf_evsel_str_handler kmem_tracepoints[] = {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001364 /* slab allocator */
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001365 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
1366 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
1367 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
1368 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
1369 { "kmem:kfree", perf_evsel__process_free_event, },
1370 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001371 /* page allocator */
1372 { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, },
1373 { "kmem:mm_page_free", perf_evsel__process_page_free_event, },
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001374 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001375
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001376 if (!perf_session__has_traces(session, "kmem record"))
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001377 goto out;
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001378
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001379 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
1380 pr_err("Initializing perf session tracepoint handlers failed\n");
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001381 goto out;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001382 }
1383
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001384 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001385 if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") &&
1386 perf_evsel__field(evsel, "pfn")) {
1387 use_pfn = true;
1388 break;
1389 }
1390 }
1391
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001392 setup_pager();
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001393 err = perf_session__process_events(session);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001394 if (err != 0) {
1395 pr_err("error during process events: %d\n", err);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001396 goto out;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001397 }
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001398 sort_result();
1399 print_result(session);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001400out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001401 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +08001402}
1403
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001404/* slab sort keys */
1405static int ptr_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001406{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001407 struct alloc_stat *l = a;
1408 struct alloc_stat *r = b;
1409
Li Zefanba77c9e2009-11-20 15:53:25 +08001410 if (l->ptr < r->ptr)
1411 return -1;
1412 else if (l->ptr > r->ptr)
1413 return 1;
1414 return 0;
1415}
1416
Li Zefan29b3e152009-11-24 13:26:10 +08001417static struct sort_dimension ptr_sort_dimension = {
1418 .name = "ptr",
1419 .cmp = ptr_cmp,
1420};
1421
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001422static int slab_callsite_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001423{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001424 struct alloc_stat *l = a;
1425 struct alloc_stat *r = b;
1426
Li Zefanba77c9e2009-11-20 15:53:25 +08001427 if (l->call_site < r->call_site)
1428 return -1;
1429 else if (l->call_site > r->call_site)
1430 return 1;
1431 return 0;
1432}
1433
Li Zefan29b3e152009-11-24 13:26:10 +08001434static struct sort_dimension callsite_sort_dimension = {
1435 .name = "callsite",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001436 .cmp = slab_callsite_cmp,
Li Zefan29b3e152009-11-24 13:26:10 +08001437};
1438
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001439static int hit_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001440{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001441 struct alloc_stat *l = a;
1442 struct alloc_stat *r = b;
1443
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001444 if (l->hit < r->hit)
1445 return -1;
1446 else if (l->hit > r->hit)
1447 return 1;
1448 return 0;
1449}
1450
Li Zefan29b3e152009-11-24 13:26:10 +08001451static struct sort_dimension hit_sort_dimension = {
1452 .name = "hit",
1453 .cmp = hit_cmp,
1454};
1455
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001456static int bytes_cmp(void *a, void *b)
Li Zefanba77c9e2009-11-20 15:53:25 +08001457{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001458 struct alloc_stat *l = a;
1459 struct alloc_stat *r = b;
1460
Li Zefanba77c9e2009-11-20 15:53:25 +08001461 if (l->bytes_alloc < r->bytes_alloc)
1462 return -1;
1463 else if (l->bytes_alloc > r->bytes_alloc)
1464 return 1;
1465 return 0;
1466}
1467
Li Zefan29b3e152009-11-24 13:26:10 +08001468static struct sort_dimension bytes_sort_dimension = {
1469 .name = "bytes",
1470 .cmp = bytes_cmp,
1471};
1472
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001473static int frag_cmp(void *a, void *b)
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001474{
1475 double x, y;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001476 struct alloc_stat *l = a;
1477 struct alloc_stat *r = b;
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001478
1479 x = fragmentation(l->bytes_req, l->bytes_alloc);
1480 y = fragmentation(r->bytes_req, r->bytes_alloc);
1481
1482 if (x < y)
1483 return -1;
1484 else if (x > y)
1485 return 1;
1486 return 0;
1487}
1488
Li Zefan29b3e152009-11-24 13:26:10 +08001489static struct sort_dimension frag_sort_dimension = {
1490 .name = "frag",
1491 .cmp = frag_cmp,
1492};
1493
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001494static int pingpong_cmp(void *a, void *b)
Li Zefan079d3f62009-11-24 13:26:55 +08001495{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001496 struct alloc_stat *l = a;
1497 struct alloc_stat *r = b;
1498
Li Zefan079d3f62009-11-24 13:26:55 +08001499 if (l->pingpong < r->pingpong)
1500 return -1;
1501 else if (l->pingpong > r->pingpong)
1502 return 1;
1503 return 0;
1504}
1505
1506static struct sort_dimension pingpong_sort_dimension = {
1507 .name = "pingpong",
1508 .cmp = pingpong_cmp,
1509};
1510
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001511/* page sort keys */
1512static int page_cmp(void *a, void *b)
1513{
1514 struct page_stat *l = a;
1515 struct page_stat *r = b;
1516
1517 if (l->page < r->page)
1518 return -1;
1519 else if (l->page > r->page)
1520 return 1;
1521 return 0;
1522}
1523
1524static struct sort_dimension page_sort_dimension = {
1525 .name = "page",
1526 .cmp = page_cmp,
1527};
1528
1529static int page_callsite_cmp(void *a, void *b)
1530{
1531 struct page_stat *l = a;
1532 struct page_stat *r = b;
1533
1534 if (l->callsite < r->callsite)
1535 return -1;
1536 else if (l->callsite > r->callsite)
1537 return 1;
1538 return 0;
1539}
1540
1541static struct sort_dimension page_callsite_sort_dimension = {
1542 .name = "callsite",
1543 .cmp = page_callsite_cmp,
1544};
1545
1546static int page_hit_cmp(void *a, void *b)
1547{
1548 struct page_stat *l = a;
1549 struct page_stat *r = b;
1550
1551 if (l->nr_alloc < r->nr_alloc)
1552 return -1;
1553 else if (l->nr_alloc > r->nr_alloc)
1554 return 1;
1555 return 0;
1556}
1557
1558static struct sort_dimension page_hit_sort_dimension = {
1559 .name = "hit",
1560 .cmp = page_hit_cmp,
1561};
1562
1563static int page_bytes_cmp(void *a, void *b)
1564{
1565 struct page_stat *l = a;
1566 struct page_stat *r = b;
1567
1568 if (l->alloc_bytes < r->alloc_bytes)
1569 return -1;
1570 else if (l->alloc_bytes > r->alloc_bytes)
1571 return 1;
1572 return 0;
1573}
1574
1575static struct sort_dimension page_bytes_sort_dimension = {
1576 .name = "bytes",
1577 .cmp = page_bytes_cmp,
1578};
1579
1580static int page_order_cmp(void *a, void *b)
1581{
1582 struct page_stat *l = a;
1583 struct page_stat *r = b;
1584
1585 if (l->order < r->order)
1586 return -1;
1587 else if (l->order > r->order)
1588 return 1;
1589 return 0;
1590}
1591
1592static struct sort_dimension page_order_sort_dimension = {
1593 .name = "order",
1594 .cmp = page_order_cmp,
1595};
1596
1597static int migrate_type_cmp(void *a, void *b)
1598{
1599 struct page_stat *l = a;
1600 struct page_stat *r = b;
1601
1602 /* for internal use to find free'd page */
1603 if (l->migrate_type == -1U)
1604 return 0;
1605
1606 if (l->migrate_type < r->migrate_type)
1607 return -1;
1608 else if (l->migrate_type > r->migrate_type)
1609 return 1;
1610 return 0;
1611}
1612
1613static struct sort_dimension migrate_type_sort_dimension = {
1614 .name = "migtype",
1615 .cmp = migrate_type_cmp,
1616};
1617
1618static int gfp_flags_cmp(void *a, void *b)
1619{
1620 struct page_stat *l = a;
1621 struct page_stat *r = b;
1622
1623 /* for internal use to find free'd page */
1624 if (l->gfp_flags == -1U)
1625 return 0;
1626
1627 if (l->gfp_flags < r->gfp_flags)
1628 return -1;
1629 else if (l->gfp_flags > r->gfp_flags)
1630 return 1;
1631 return 0;
1632}
1633
1634static struct sort_dimension gfp_flags_sort_dimension = {
1635 .name = "gfp",
1636 .cmp = gfp_flags_cmp,
1637};
1638
1639static struct sort_dimension *slab_sorts[] = {
Li Zefan29b3e152009-11-24 13:26:10 +08001640 &ptr_sort_dimension,
1641 &callsite_sort_dimension,
1642 &hit_sort_dimension,
1643 &bytes_sort_dimension,
1644 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +08001645 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +08001646};
1647
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001648static struct sort_dimension *page_sorts[] = {
1649 &page_sort_dimension,
1650 &page_callsite_sort_dimension,
1651 &page_hit_sort_dimension,
1652 &page_bytes_sort_dimension,
1653 &page_order_sort_dimension,
1654 &migrate_type_sort_dimension,
1655 &gfp_flags_sort_dimension,
1656};
Li Zefan29b3e152009-11-24 13:26:10 +08001657
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001658static int slab_sort_dimension__add(const char *tok, struct list_head *list)
Li Zefan29b3e152009-11-24 13:26:10 +08001659{
1660 struct sort_dimension *sort;
1661 int i;
1662
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001663 for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) {
1664 if (!strcmp(slab_sorts[i]->name, tok)) {
1665 sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i]));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001666 if (!sort) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -03001667 pr_err("%s: memdup failed\n", __func__);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001668 return -1;
1669 }
Li Zefan29b3e152009-11-24 13:26:10 +08001670 list_add_tail(&sort->list, list);
1671 return 0;
1672 }
1673 }
1674
1675 return -1;
1676}
1677
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001678static int page_sort_dimension__add(const char *tok, struct list_head *list)
1679{
1680 struct sort_dimension *sort;
1681 int i;
1682
1683 for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) {
1684 if (!strcmp(page_sorts[i]->name, tok)) {
1685 sort = memdup(page_sorts[i], sizeof(*page_sorts[i]));
1686 if (!sort) {
1687 pr_err("%s: memdup failed\n", __func__);
1688 return -1;
1689 }
1690 list_add_tail(&sort->list, list);
1691 return 0;
1692 }
1693 }
1694
1695 return -1;
1696}
1697
1698static int setup_slab_sorting(struct list_head *sort_list, const char *arg)
Li Zefan29b3e152009-11-24 13:26:10 +08001699{
1700 char *tok;
1701 char *str = strdup(arg);
Namhyung Kim405f8752015-03-12 16:32:46 +09001702 char *pos = str;
Li Zefan29b3e152009-11-24 13:26:10 +08001703
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001704 if (!str) {
1705 pr_err("%s: strdup failed\n", __func__);
1706 return -1;
1707 }
Li Zefan29b3e152009-11-24 13:26:10 +08001708
1709 while (true) {
Namhyung Kim405f8752015-03-12 16:32:46 +09001710 tok = strsep(&pos, ",");
Li Zefan29b3e152009-11-24 13:26:10 +08001711 if (!tok)
1712 break;
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001713 if (slab_sort_dimension__add(tok, sort_list) < 0) {
1714 error("Unknown slab --sort key: '%s'", tok);
1715 free(str);
1716 return -1;
1717 }
1718 }
1719
1720 free(str);
1721 return 0;
1722}
1723
1724static int setup_page_sorting(struct list_head *sort_list, const char *arg)
1725{
1726 char *tok;
1727 char *str = strdup(arg);
1728 char *pos = str;
1729
1730 if (!str) {
1731 pr_err("%s: strdup failed\n", __func__);
1732 return -1;
1733 }
1734
1735 while (true) {
1736 tok = strsep(&pos, ",");
1737 if (!tok)
1738 break;
1739 if (page_sort_dimension__add(tok, sort_list) < 0) {
1740 error("Unknown page --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +09001741 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +08001742 return -1;
1743 }
1744 }
1745
1746 free(str);
1747 return 0;
1748}
1749
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001750static int parse_sort_opt(const struct option *opt __maybe_unused,
1751 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001752{
Li Zefanba77c9e2009-11-20 15:53:25 +08001753 if (!arg)
1754 return -1;
1755
Namhyung Kim0c160d42015-04-21 13:55:06 +09001756 if (kmem_page > kmem_slab ||
1757 (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) {
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001758 if (caller_flag > alloc_flag)
1759 return setup_page_sorting(&page_caller_sort, arg);
1760 else
1761 return setup_page_sorting(&page_alloc_sort, arg);
1762 } else {
1763 if (caller_flag > alloc_flag)
1764 return setup_slab_sorting(&slab_caller_sort, arg);
1765 else
1766 return setup_slab_sorting(&slab_alloc_sort, arg);
1767 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001768
1769 return 0;
1770}
1771
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001772static int parse_caller_opt(const struct option *opt __maybe_unused,
1773 const char *arg __maybe_unused,
1774 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001775{
Li Zefan90b86a92009-12-10 15:21:57 +08001776 caller_flag = (alloc_flag + 1);
1777 return 0;
1778}
Li Zefanba77c9e2009-11-20 15:53:25 +08001779
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001780static int parse_alloc_opt(const struct option *opt __maybe_unused,
1781 const char *arg __maybe_unused,
1782 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +08001783{
1784 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001785 return 0;
1786}
1787
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001788static int parse_slab_opt(const struct option *opt __maybe_unused,
1789 const char *arg __maybe_unused,
1790 int unset __maybe_unused)
1791{
1792 kmem_slab = (kmem_page + 1);
1793 return 0;
1794}
1795
1796static int parse_page_opt(const struct option *opt __maybe_unused,
1797 const char *arg __maybe_unused,
1798 int unset __maybe_unused)
1799{
1800 kmem_page = (kmem_slab + 1);
1801 return 0;
1802}
1803
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001804static int parse_line_opt(const struct option *opt __maybe_unused,
1805 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001806{
1807 int lines;
1808
1809 if (!arg)
1810 return -1;
1811
1812 lines = strtoul(arg, NULL, 10);
1813
1814 if (caller_flag > alloc_flag)
1815 caller_lines = lines;
1816 else
1817 alloc_lines = lines;
1818
1819 return 0;
1820}
1821
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001822static int __cmd_record(int argc, const char **argv)
1823{
1824 const char * const record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001825 "record", "-a", "-R", "-c", "1",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001826 };
1827 const char * const slab_events[] = {
Li Zefanba77c9e2009-11-20 15:53:25 +08001828 "-e", "kmem:kmalloc",
1829 "-e", "kmem:kmalloc_node",
1830 "-e", "kmem:kfree",
1831 "-e", "kmem:kmem_cache_alloc",
1832 "-e", "kmem:kmem_cache_alloc_node",
1833 "-e", "kmem:kmem_cache_free",
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001834 };
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001835 const char * const page_events[] = {
1836 "-e", "kmem:mm_page_alloc",
1837 "-e", "kmem:mm_page_free",
1838 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001839 unsigned int rec_argc, i, j;
1840 const char **rec_argv;
1841
1842 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001843 if (kmem_slab)
1844 rec_argc += ARRAY_SIZE(slab_events);
1845 if (kmem_page)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001846 rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001847
Li Zefanba77c9e2009-11-20 15:53:25 +08001848 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1849
Chris Samuelce47dc52010-11-13 13:35:06 +11001850 if (rec_argv == NULL)
1851 return -ENOMEM;
1852
Li Zefanba77c9e2009-11-20 15:53:25 +08001853 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1854 rec_argv[i] = strdup(record_args[i]);
1855
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001856 if (kmem_slab) {
1857 for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
1858 rec_argv[i] = strdup(slab_events[j]);
1859 }
1860 if (kmem_page) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001861 rec_argv[i++] = strdup("-g");
1862
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001863 for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
1864 rec_argv[i] = strdup(page_events[j]);
1865 }
1866
Li Zefanba77c9e2009-11-20 15:53:25 +08001867 for (j = 1; j < (unsigned int)argc; j++, i++)
1868 rec_argv[i] = argv[j];
1869
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001870 return cmd_record(i, rec_argv);
Li Zefanba77c9e2009-11-20 15:53:25 +08001871}
1872
Wang Nanb8cbb342016-02-26 09:31:51 +00001873static int kmem_config(const char *var, const char *value, void *cb __maybe_unused)
Namhyung Kim0c160d42015-04-21 13:55:06 +09001874{
1875 if (!strcmp(var, "kmem.default")) {
1876 if (!strcmp(value, "slab"))
1877 kmem_default = KMEM_SLAB;
1878 else if (!strcmp(value, "page"))
1879 kmem_default = KMEM_PAGE;
1880 else
1881 pr_err("invalid default value ('slab' or 'page' required): %s\n",
1882 value);
1883 return 0;
1884 }
1885
Wang Nanb8cbb342016-02-26 09:31:51 +00001886 return 0;
Namhyung Kim0c160d42015-04-21 13:55:06 +09001887}
1888
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001889int cmd_kmem(int argc, const char **argv)
Li Zefanba77c9e2009-11-20 15:53:25 +08001890{
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001891 const char * const default_slab_sort = "frag,hit,bytes";
1892 const char * const default_page_sort = "bytes,hit";
Yunlong Songd1eeb772015-04-02 21:47:12 +08001893 struct perf_data_file file = {
Yunlong Songd1eeb772015-04-02 21:47:12 +08001894 .mode = PERF_DATA_MODE_READ,
1895 };
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001896 const struct option kmem_options[] = {
1897 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Namhyung Kimbd72a332015-03-12 16:32:47 +09001898 OPT_INCR('v', "verbose", &verbose,
1899 "be more verbose (show symbol address, etc)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001900 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
1901 "show per-callsite statistics", parse_caller_opt),
1902 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
1903 "show per-allocation statistics", parse_alloc_opt),
1904 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001905 "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, "
1906 "page, order, migtype, gfp", parse_sort_opt),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001907 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
1908 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Yunlong Songd1eeb772015-04-02 21:47:12 +08001909 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001910 OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
1911 parse_slab_opt),
1912 OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
1913 parse_page_opt),
Namhyung Kim2a7ef022015-04-21 13:55:04 +09001914 OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
David Ahern2a865bd2016-11-29 10:15:45 -07001915 OPT_STRING(0, "time", &time_str, "str",
1916 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001917 OPT_END()
1918 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001919 const char *const kmem_subcommands[] = { "record", "stat", NULL };
1920 const char *kmem_usage[] = {
1921 NULL,
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001922 NULL
1923 };
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001924 struct perf_session *session;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001925 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 -03001926 int ret = perf_config(kmem_config, NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001927
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001928 if (ret)
1929 return ret;
1930
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001931 argc = parse_options_subcommand(argc, argv, kmem_options,
1932 kmem_subcommands, kmem_usage, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +08001933
Li Zefan90b86a92009-12-10 15:21:57 +08001934 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +08001935 usage_with_options(kmem_usage, kmem_options);
1936
Namhyung Kim0c160d42015-04-21 13:55:06 +09001937 if (kmem_slab == 0 && kmem_page == 0) {
1938 if (kmem_default == KMEM_SLAB)
1939 kmem_slab = 1;
1940 else
1941 kmem_page = 1;
1942 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001943
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001944 if (!strncmp(argv[0], "rec", 3)) {
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001945 symbol__init(NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001946 return __cmd_record(argc, argv);
1947 }
1948
Jiri Olsa28939e12015-04-06 14:36:08 +09001949 file.path = input_name;
1950
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001951 kmem_session = session = perf_session__new(&file, false, &perf_kmem);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001952 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001953 return -1;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001954
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001955 ret = -1;
1956
Namhyung Kima923e2c2015-05-05 23:52:52 +09001957 if (kmem_slab) {
1958 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
1959 "kmem:kmalloc")) {
1960 pr_err(errmsg, "slab", "slab");
Taeung Song249ca1a2015-06-30 17:15:21 +09001961 goto out_delete;
Namhyung Kima923e2c2015-05-05 23:52:52 +09001962 }
1963 }
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001964
Namhyung Kima923e2c2015-05-05 23:52:52 +09001965 if (kmem_page) {
1966 struct perf_evsel *evsel;
1967
1968 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
1969 "kmem:mm_page_alloc");
1970 if (evsel == NULL) {
1971 pr_err(errmsg, "page", "page");
Taeung Song249ca1a2015-06-30 17:15:21 +09001972 goto out_delete;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001973 }
1974
1975 kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001976 symbol_conf.use_callchain = true;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001977 }
1978
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001979 symbol__init(&session->header.env);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001980
David Ahern2a865bd2016-11-29 10:15:45 -07001981 if (perf_time__parse_str(&ptime, time_str) != 0) {
1982 pr_err("Invalid time string\n");
1983 return -EINVAL;
1984 }
1985
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001986 if (!strcmp(argv[0], "stat")) {
Namhyung Kim77cfe382015-03-23 15:30:40 +09001987 setlocale(LC_ALL, "");
1988
Don Zickus4b627952014-04-07 14:55:23 -04001989 if (cpu__setup_cpunode_map())
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001990 goto out_delete;
Li Zefanba77c9e2009-11-20 15:53:25 +08001991
Namhyung Kimfb4f3132015-04-21 13:55:03 +09001992 if (list_empty(&slab_caller_sort))
1993 setup_slab_sorting(&slab_caller_sort, default_slab_sort);
1994 if (list_empty(&slab_alloc_sort))
1995 setup_slab_sorting(&slab_alloc_sort, default_slab_sort);
1996 if (list_empty(&page_caller_sort))
1997 setup_page_sorting(&page_caller_sort, default_page_sort);
1998 if (list_empty(&page_alloc_sort))
1999 setup_page_sorting(&page_alloc_sort, default_page_sort);
Li Zefan7d0d3942009-11-24 13:26:31 +08002000
Namhyung Kimfb4f3132015-04-21 13:55:03 +09002001 if (kmem_page) {
2002 setup_page_sorting(&page_alloc_sort_input,
2003 "page,order,migtype,gfp");
2004 setup_page_sorting(&page_caller_sort_input,
2005 "callsite,order,migtype,gfp");
2006 }
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09002007 ret = __cmd_kmem(session);
Pekka Enbergb00eca82010-01-19 19:26:11 +02002008 } else
2009 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +08002010
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09002011out_delete:
2012 perf_session__delete(session);
2013
2014 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +08002015}
2016