blob: 3649eec6807f3a0766dff4b670f3357e33044c98 [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"
7#include "util/cache.h"
8#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Namhyung Kimc9758cc2015-04-21 13:55:02 +090013#include "util/callchain.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080014
15#include "util/parse-options.h"
16#include "util/trace-event.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020017#include "util/data.h"
Don Zickus4b627952014-04-07 14:55:23 -040018#include "util/cpumap.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080019
20#include "util/debug.h"
Li Zefanba77c9e2009-11-20 15:53:25 +080021
22#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -030023#include <linux/string.h>
Namhyung Kim77cfe382015-03-23 15:30:40 +090024#include <locale.h>
Namhyung Kimc9758cc2015-04-21 13:55:02 +090025#include <regex.h>
Li Zefanba77c9e2009-11-20 15:53:25 +080026
Namhyung Kim0d68bc92015-04-06 14:36:10 +090027static int kmem_slab;
28static int kmem_page;
29
30static long kmem_page_size;
31
Li Zefanba77c9e2009-11-20 15:53:25 +080032struct alloc_stat;
33typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
34
Li Zefanba77c9e2009-11-20 15:53:25 +080035static int alloc_flag;
36static int caller_flag;
37
Li Zefanba77c9e2009-11-20 15:53:25 +080038static int alloc_lines = -1;
39static int caller_lines = -1;
40
Li Zefan7707b6b2009-11-24 13:25:48 +080041static bool raw_ip;
42
Li Zefanba77c9e2009-11-20 15:53:25 +080043struct alloc_stat {
Li Zefan079d3f62009-11-24 13:26:55 +080044 u64 call_site;
45 u64 ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +080046 u64 bytes_req;
47 u64 bytes_alloc;
48 u32 hit;
Li Zefan079d3f62009-11-24 13:26:55 +080049 u32 pingpong;
50
51 short alloc_cpu;
Li Zefanba77c9e2009-11-20 15:53:25 +080052
53 struct rb_node node;
54};
55
56static struct rb_root root_alloc_stat;
57static struct rb_root root_alloc_sorted;
58static struct rb_root root_caller_stat;
59static struct rb_root root_caller_sorted;
60
61static unsigned long total_requested, total_allocated;
Li Zefan7d0d3942009-11-24 13:26:31 +080062static unsigned long nr_allocs, nr_cross_allocs;
Li Zefanba77c9e2009-11-20 15:53:25 +080063
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030064static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
65 int bytes_req, int bytes_alloc, int cpu)
Li Zefanba77c9e2009-11-20 15:53:25 +080066{
67 struct rb_node **node = &root_alloc_stat.rb_node;
68 struct rb_node *parent = NULL;
69 struct alloc_stat *data = NULL;
70
Li Zefanba77c9e2009-11-20 15:53:25 +080071 while (*node) {
72 parent = *node;
73 data = rb_entry(*node, struct alloc_stat, node);
74
75 if (ptr > data->ptr)
76 node = &(*node)->rb_right;
77 else if (ptr < data->ptr)
78 node = &(*node)->rb_left;
79 else
80 break;
81 }
82
83 if (data && data->ptr == ptr) {
84 data->hit++;
85 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +080086 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +080087 } else {
88 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -030089 if (!data) {
90 pr_err("%s: malloc failed\n", __func__);
91 return -1;
92 }
Li Zefanba77c9e2009-11-20 15:53:25 +080093 data->ptr = ptr;
Li Zefan079d3f62009-11-24 13:26:55 +080094 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +080095 data->hit = 1;
96 data->bytes_req = bytes_req;
97 data->bytes_alloc = bytes_alloc;
98
99 rb_link_node(&data->node, parent, node);
100 rb_insert_color(&data->node, &root_alloc_stat);
101 }
Li Zefan079d3f62009-11-24 13:26:55 +0800102 data->call_site = call_site;
103 data->alloc_cpu = cpu;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300104 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800105}
106
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300107static int insert_caller_stat(unsigned long call_site,
Li Zefanba77c9e2009-11-20 15:53:25 +0800108 int bytes_req, int bytes_alloc)
109{
110 struct rb_node **node = &root_caller_stat.rb_node;
111 struct rb_node *parent = NULL;
112 struct alloc_stat *data = NULL;
113
Li Zefanba77c9e2009-11-20 15:53:25 +0800114 while (*node) {
115 parent = *node;
116 data = rb_entry(*node, struct alloc_stat, node);
117
118 if (call_site > data->call_site)
119 node = &(*node)->rb_right;
120 else if (call_site < data->call_site)
121 node = &(*node)->rb_left;
122 else
123 break;
124 }
125
126 if (data && data->call_site == call_site) {
127 data->hit++;
128 data->bytes_req += bytes_req;
Wenji Huang4efb5292009-12-21 17:52:55 +0800129 data->bytes_alloc += bytes_alloc;
Li Zefanba77c9e2009-11-20 15:53:25 +0800130 } else {
131 data = malloc(sizeof(*data));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300132 if (!data) {
133 pr_err("%s: malloc failed\n", __func__);
134 return -1;
135 }
Li Zefanba77c9e2009-11-20 15:53:25 +0800136 data->call_site = call_site;
Li Zefan079d3f62009-11-24 13:26:55 +0800137 data->pingpong = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800138 data->hit = 1;
139 data->bytes_req = bytes_req;
140 data->bytes_alloc = bytes_alloc;
141
142 rb_link_node(&data->node, parent, node);
143 rb_insert_color(&data->node, &root_caller_stat);
144 }
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300145
146 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800147}
148
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300149static int perf_evsel__process_alloc_event(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300150 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800151{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300152 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
153 call_site = perf_evsel__intval(evsel, sample, "call_site");
154 int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
155 bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
Li Zefanba77c9e2009-11-20 15:53:25 +0800156
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300157 if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300158 insert_caller_stat(call_site, bytes_req, bytes_alloc))
159 return -1;
Li Zefanba77c9e2009-11-20 15:53:25 +0800160
161 total_requested += bytes_req;
162 total_allocated += bytes_alloc;
Li Zefan7d0d3942009-11-24 13:26:31 +0800163
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300164 nr_allocs++;
165 return 0;
166}
167
168static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel,
169 struct perf_sample *sample)
170{
171 int ret = perf_evsel__process_alloc_event(evsel, sample);
172
173 if (!ret) {
Don Zickus4b627952014-04-07 14:55:23 -0400174 int node1 = cpu__get_node(sample->cpu),
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300175 node2 = perf_evsel__intval(evsel, sample, "node");
176
Li Zefan7d0d3942009-11-24 13:26:31 +0800177 if (node1 != node2)
178 nr_cross_allocs++;
179 }
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300180
181 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +0800182}
183
Li Zefan079d3f62009-11-24 13:26:55 +0800184static int ptr_cmp(struct alloc_stat *, struct alloc_stat *);
185static int callsite_cmp(struct alloc_stat *, struct alloc_stat *);
186
187static struct alloc_stat *search_alloc_stat(unsigned long ptr,
188 unsigned long call_site,
189 struct rb_root *root,
190 sort_fn_t sort_fn)
191{
192 struct rb_node *node = root->rb_node;
193 struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
194
195 while (node) {
196 struct alloc_stat *data;
197 int cmp;
198
199 data = rb_entry(node, struct alloc_stat, node);
200
201 cmp = sort_fn(&key, data);
202 if (cmp < 0)
203 node = node->rb_left;
204 else if (cmp > 0)
205 node = node->rb_right;
206 else
207 return data;
208 }
209 return NULL;
210}
211
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300212static int perf_evsel__process_free_event(struct perf_evsel *evsel,
213 struct perf_sample *sample)
Li Zefanba77c9e2009-11-20 15:53:25 +0800214{
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300215 unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
Li Zefan079d3f62009-11-24 13:26:55 +0800216 struct alloc_stat *s_alloc, *s_caller;
217
Li Zefan079d3f62009-11-24 13:26:55 +0800218 s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
219 if (!s_alloc)
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300220 return 0;
Li Zefan079d3f62009-11-24 13:26:55 +0800221
Arnaldo Carvalho de Melo22ad7982012-08-07 10:56:43 -0300222 if ((short)sample->cpu != s_alloc->alloc_cpu) {
Li Zefan079d3f62009-11-24 13:26:55 +0800223 s_alloc->pingpong++;
224
225 s_caller = search_alloc_stat(0, s_alloc->call_site,
226 &root_caller_stat, callsite_cmp);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300227 if (!s_caller)
228 return -1;
Li Zefan079d3f62009-11-24 13:26:55 +0800229 s_caller->pingpong++;
230 }
231 s_alloc->alloc_cpu = -1;
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -0300232
233 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800234}
235
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900236static u64 total_page_alloc_bytes;
237static u64 total_page_free_bytes;
238static u64 total_page_nomatch_bytes;
239static u64 total_page_fail_bytes;
240static unsigned long nr_page_allocs;
241static unsigned long nr_page_frees;
242static unsigned long nr_page_fails;
243static unsigned long nr_page_nomatch;
244
245static bool use_pfn;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900246static struct perf_session *kmem_session;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900247
248#define MAX_MIGRATE_TYPES 6
249#define MAX_PAGE_ORDER 11
250
251static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
252
253struct page_stat {
254 struct rb_node node;
255 u64 page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900256 u64 callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900257 int order;
258 unsigned gfp_flags;
259 unsigned migrate_type;
260 u64 alloc_bytes;
261 u64 free_bytes;
262 int nr_alloc;
263 int nr_free;
264};
265
266static struct rb_root page_tree;
267static struct rb_root page_alloc_tree;
268static struct rb_root page_alloc_sorted;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900269static struct rb_root page_caller_tree;
270static struct rb_root page_caller_sorted;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900271
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900272struct alloc_func {
273 u64 start;
274 u64 end;
275 char *name;
276};
277
278static int nr_alloc_funcs;
279static struct alloc_func *alloc_func_list;
280
281static int funcmp(const void *a, const void *b)
282{
283 const struct alloc_func *fa = a;
284 const struct alloc_func *fb = b;
285
286 if (fa->start > fb->start)
287 return 1;
288 else
289 return -1;
290}
291
292static int callcmp(const void *a, const void *b)
293{
294 const struct alloc_func *fa = a;
295 const struct alloc_func *fb = b;
296
297 if (fb->start <= fa->start && fa->end < fb->end)
298 return 0;
299
300 if (fa->start > fb->start)
301 return 1;
302 else
303 return -1;
304}
305
306static int build_alloc_func_list(void)
307{
308 int ret;
309 struct map *kernel_map;
310 struct symbol *sym;
311 struct rb_node *node;
312 struct alloc_func *func;
313 struct machine *machine = &kmem_session->machines.host;
314 regex_t alloc_func_regex;
315 const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
316
317 ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
318 if (ret) {
319 char err[BUFSIZ];
320
321 regerror(ret, &alloc_func_regex, err, sizeof(err));
322 pr_err("Invalid regex: %s\n%s", pattern, err);
323 return -EINVAL;
324 }
325
326 kernel_map = machine->vmlinux_maps[MAP__FUNCTION];
327 if (map__load(kernel_map, NULL) < 0) {
328 pr_err("cannot load kernel map\n");
329 return -ENOENT;
330 }
331
332 map__for_each_symbol(kernel_map, sym, node) {
333 if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
334 continue;
335
336 func = realloc(alloc_func_list,
337 (nr_alloc_funcs + 1) * sizeof(*func));
338 if (func == NULL)
339 return -ENOMEM;
340
341 pr_debug("alloc func: %s\n", sym->name);
342 func[nr_alloc_funcs].start = sym->start;
343 func[nr_alloc_funcs].end = sym->end;
344 func[nr_alloc_funcs].name = sym->name;
345
346 alloc_func_list = func;
347 nr_alloc_funcs++;
348 }
349
350 qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
351
352 regfree(&alloc_func_regex);
353 return 0;
354}
355
356/*
357 * Find first non-memory allocation function from callchain.
358 * The allocation functions are in the 'alloc_func_list'.
359 */
360static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample)
361{
362 struct addr_location al;
363 struct machine *machine = &kmem_session->machines.host;
364 struct callchain_cursor_node *node;
365
366 if (alloc_func_list == NULL) {
367 if (build_alloc_func_list() < 0)
368 goto out;
369 }
370
371 al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
372 sample__resolve_callchain(sample, NULL, evsel, &al, 16);
373
374 callchain_cursor_commit(&callchain_cursor);
375 while (true) {
376 struct alloc_func key, *caller;
377 u64 addr;
378
379 node = callchain_cursor_current(&callchain_cursor);
380 if (node == NULL)
381 break;
382
383 key.start = key.end = node->ip;
384 caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
385 sizeof(key), callcmp);
386 if (!caller) {
387 /* found */
388 if (node->map)
389 addr = map__unmap_ip(node->map, node->ip);
390 else
391 addr = node->ip;
392
393 return addr;
394 } else
395 pr_debug3("skipping alloc function: %s\n", caller->name);
396
397 callchain_cursor_advance(&callchain_cursor);
398 }
399
400out:
401 pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
402 return sample->ip;
403}
404
405static struct page_stat *
406__page_stat__findnew_page(u64 page, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900407{
408 struct rb_node **node = &page_tree.rb_node;
409 struct rb_node *parent = NULL;
410 struct page_stat *data;
411
412 while (*node) {
413 s64 cmp;
414
415 parent = *node;
416 data = rb_entry(*node, struct page_stat, node);
417
418 cmp = data->page - page;
419 if (cmp < 0)
420 node = &parent->rb_left;
421 else if (cmp > 0)
422 node = &parent->rb_right;
423 else
424 return data;
425 }
426
427 if (!create)
428 return NULL;
429
430 data = zalloc(sizeof(*data));
431 if (data != NULL) {
432 data->page = page;
433
434 rb_link_node(&data->node, parent, node);
435 rb_insert_color(&data->node, &page_tree);
436 }
437
438 return data;
439}
440
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900441static struct page_stat *page_stat__find_page(u64 page)
442{
443 return __page_stat__findnew_page(page, false);
444}
445
446static struct page_stat *page_stat__findnew_page(u64 page)
447{
448 return __page_stat__findnew_page(page, true);
449}
450
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900451static int page_stat_cmp(struct page_stat *a, struct page_stat *b)
452{
453 if (a->page > b->page)
454 return -1;
455 if (a->page < b->page)
456 return 1;
457 if (a->order > b->order)
458 return -1;
459 if (a->order < b->order)
460 return 1;
461 if (a->migrate_type > b->migrate_type)
462 return -1;
463 if (a->migrate_type < b->migrate_type)
464 return 1;
465 if (a->gfp_flags > b->gfp_flags)
466 return -1;
467 if (a->gfp_flags < b->gfp_flags)
468 return 1;
469 return 0;
470}
471
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900472static struct page_stat *
473__page_stat__findnew_alloc(struct page_stat *pstat, bool create)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900474{
475 struct rb_node **node = &page_alloc_tree.rb_node;
476 struct rb_node *parent = NULL;
477 struct page_stat *data;
478
479 while (*node) {
480 s64 cmp;
481
482 parent = *node;
483 data = rb_entry(*node, struct page_stat, node);
484
David Ahern6b1a2752015-04-14 13:49:33 -0400485 cmp = page_stat_cmp(data, pstat);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900486 if (cmp < 0)
487 node = &parent->rb_left;
488 else if (cmp > 0)
489 node = &parent->rb_right;
490 else
491 return data;
492 }
493
494 if (!create)
495 return NULL;
496
497 data = zalloc(sizeof(*data));
498 if (data != NULL) {
David Ahern6b1a2752015-04-14 13:49:33 -0400499 data->page = pstat->page;
500 data->order = pstat->order;
501 data->gfp_flags = pstat->gfp_flags;
502 data->migrate_type = pstat->migrate_type;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900503
504 rb_link_node(&data->node, parent, node);
505 rb_insert_color(&data->node, &page_alloc_tree);
506 }
507
508 return data;
509}
510
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900511static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
512{
513 return __page_stat__findnew_alloc(pstat, false);
514}
515
516static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
517{
518 return __page_stat__findnew_alloc(pstat, true);
519}
520
521static struct page_stat *
522__page_stat__findnew_caller(u64 callsite, bool create)
523{
524 struct rb_node **node = &page_caller_tree.rb_node;
525 struct rb_node *parent = NULL;
526 struct page_stat *data;
527
528 while (*node) {
529 s64 cmp;
530
531 parent = *node;
532 data = rb_entry(*node, struct page_stat, node);
533
534 cmp = data->callsite - callsite;
535 if (cmp < 0)
536 node = &parent->rb_left;
537 else if (cmp > 0)
538 node = &parent->rb_right;
539 else
540 return data;
541 }
542
543 if (!create)
544 return NULL;
545
546 data = zalloc(sizeof(*data));
547 if (data != NULL) {
548 data->callsite = callsite;
549
550 rb_link_node(&data->node, parent, node);
551 rb_insert_color(&data->node, &page_caller_tree);
552 }
553
554 return data;
555}
556
557static struct page_stat *page_stat__find_caller(u64 callsite)
558{
559 return __page_stat__findnew_caller(callsite, false);
560}
561
562static struct page_stat *page_stat__findnew_caller(u64 callsite)
563{
564 return __page_stat__findnew_caller(callsite, true);
565}
566
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900567static bool valid_page(u64 pfn_or_page)
568{
569 if (use_pfn && pfn_or_page == -1UL)
570 return false;
571 if (!use_pfn && pfn_or_page == 0)
572 return false;
573 return true;
574}
575
576static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel,
577 struct perf_sample *sample)
578{
579 u64 page;
580 unsigned int order = perf_evsel__intval(evsel, sample, "order");
581 unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
582 unsigned int migrate_type = perf_evsel__intval(evsel, sample,
583 "migratetype");
584 u64 bytes = kmem_page_size << order;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900585 u64 callsite;
David Ahern6b1a2752015-04-14 13:49:33 -0400586 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900587 struct page_stat this = {
588 .order = order,
589 .gfp_flags = gfp_flags,
590 .migrate_type = migrate_type,
591 };
592
593 if (use_pfn)
594 page = perf_evsel__intval(evsel, sample, "pfn");
595 else
596 page = perf_evsel__intval(evsel, sample, "page");
597
598 nr_page_allocs++;
599 total_page_alloc_bytes += bytes;
600
601 if (!valid_page(page)) {
602 nr_page_fails++;
603 total_page_fail_bytes += bytes;
604
605 return 0;
606 }
607
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900608 callsite = find_callsite(evsel, sample);
609
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900610 /*
611 * This is to find the current page (with correct gfp flags and
612 * migrate type) at free event.
613 */
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900614 pstat = page_stat__findnew_page(page);
David Ahern6b1a2752015-04-14 13:49:33 -0400615 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900616 return -ENOMEM;
617
David Ahern6b1a2752015-04-14 13:49:33 -0400618 pstat->order = order;
619 pstat->gfp_flags = gfp_flags;
620 pstat->migrate_type = migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900621 pstat->callsite = callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900622
623 this.page = page;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900624 pstat = page_stat__findnew_alloc(&this);
David Ahern6b1a2752015-04-14 13:49:33 -0400625 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900626 return -ENOMEM;
627
David Ahern6b1a2752015-04-14 13:49:33 -0400628 pstat->nr_alloc++;
629 pstat->alloc_bytes += bytes;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900630 pstat->callsite = callsite;
631
632 pstat = page_stat__findnew_caller(callsite);
633 if (pstat == NULL)
634 return -ENOMEM;
635
636 pstat->order = order;
637 pstat->gfp_flags = gfp_flags;
638 pstat->migrate_type = migrate_type;
639
640 pstat->nr_alloc++;
641 pstat->alloc_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900642
643 order_stats[order][migrate_type]++;
644
645 return 0;
646}
647
648static int perf_evsel__process_page_free_event(struct perf_evsel *evsel,
649 struct perf_sample *sample)
650{
651 u64 page;
652 unsigned int order = perf_evsel__intval(evsel, sample, "order");
653 u64 bytes = kmem_page_size << order;
David Ahern6b1a2752015-04-14 13:49:33 -0400654 struct page_stat *pstat;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900655 struct page_stat this = {
656 .order = order,
657 };
658
659 if (use_pfn)
660 page = perf_evsel__intval(evsel, sample, "pfn");
661 else
662 page = perf_evsel__intval(evsel, sample, "page");
663
664 nr_page_frees++;
665 total_page_free_bytes += bytes;
666
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900667 pstat = page_stat__find_page(page);
David Ahern6b1a2752015-04-14 13:49:33 -0400668 if (pstat == NULL) {
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900669 pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
670 page, order);
671
672 nr_page_nomatch++;
673 total_page_nomatch_bytes += bytes;
674
675 return 0;
676 }
677
678 this.page = page;
David Ahern6b1a2752015-04-14 13:49:33 -0400679 this.gfp_flags = pstat->gfp_flags;
680 this.migrate_type = pstat->migrate_type;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900681 this.callsite = pstat->callsite;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900682
David Ahern6b1a2752015-04-14 13:49:33 -0400683 rb_erase(&pstat->node, &page_tree);
684 free(pstat);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900685
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900686 pstat = page_stat__find_alloc(&this);
687 if (pstat == NULL)
688 return -ENOENT;
689
690 pstat->nr_free++;
691 pstat->free_bytes += bytes;
692
693 pstat = page_stat__find_caller(this.callsite);
David Ahern6b1a2752015-04-14 13:49:33 -0400694 if (pstat == NULL)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900695 return -ENOENT;
696
David Ahern6b1a2752015-04-14 13:49:33 -0400697 pstat->nr_free++;
698 pstat->free_bytes += bytes;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900699
700 return 0;
701}
702
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300703typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
704 struct perf_sample *sample);
Li Zefanba77c9e2009-11-20 15:53:25 +0800705
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300706static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200707 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200708 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300709 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200710 struct machine *machine)
Li Zefanba77c9e2009-11-20 15:53:25 +0800711{
Adrian Hunteref893252013-08-27 11:23:06 +0300712 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900713 sample->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800714
Li Zefanba77c9e2009-11-20 15:53:25 +0800715 if (thread == NULL) {
716 pr_debug("problem processing %d event, skipping it.\n",
717 event->header.type);
718 return -1;
719 }
720
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200721 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Li Zefanba77c9e2009-11-20 15:53:25 +0800722
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300723 if (evsel->handler != NULL) {
724 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -0300725 return f(evsel, sample);
726 }
727
728 return 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800729}
730
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300731static struct perf_tool perf_kmem = {
732 .sample = process_sample_event,
733 .comm = perf_event__process_comm,
Namhyung Kim64c40902014-08-01 14:59:31 +0900734 .mmap = perf_event__process_mmap,
735 .mmap2 = perf_event__process_mmap2,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200736 .ordered_events = true,
Li Zefanba77c9e2009-11-20 15:53:25 +0800737};
738
Li Zefanba77c9e2009-11-20 15:53:25 +0800739static double fragmentation(unsigned long n_req, unsigned long n_alloc)
740{
741 if (n_alloc == 0)
742 return 0.0;
743 else
744 return 100.0 - (100.0 * n_req / n_alloc);
745}
746
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900747static void __print_slab_result(struct rb_root *root,
748 struct perf_session *session,
749 int n_lines, int is_caller)
Li Zefanba77c9e2009-11-20 15:53:25 +0800750{
751 struct rb_node *next;
Arnaldo Carvalho de Melo34ba5122012-12-19 09:04:24 -0300752 struct machine *machine = &session->machines.host;
Li Zefanba77c9e2009-11-20 15:53:25 +0800753
Namhyung Kim65f46e02015-03-12 16:32:48 +0900754 printf("%.105s\n", graph_dotted_line);
Li Zefan079d3f62009-11-24 13:26:55 +0800755 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
Pekka Enberg47103272010-01-19 19:23:23 +0200756 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
Namhyung Kim65f46e02015-03-12 16:32:48 +0900757 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800758
759 next = rb_first(root);
760
761 while (next && n_lines--) {
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200762 struct alloc_stat *data = rb_entry(next, struct alloc_stat,
763 node);
764 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300765 struct map *map;
Li Zefan079d3f62009-11-24 13:26:55 +0800766 char buf[BUFSIZ];
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200767 u64 addr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800768
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200769 if (is_caller) {
770 addr = data->call_site;
Li Zefan7707b6b2009-11-24 13:25:48 +0800771 if (!raw_ip)
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -0300772 sym = machine__find_kernel_function(machine, addr, &map, NULL);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200773 } else
774 addr = data->ptr;
Li Zefanba77c9e2009-11-20 15:53:25 +0800775
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200776 if (sym != NULL)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200777 snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
Arnaldo Carvalho de Melo71cf8b82010-04-01 21:24:38 -0300778 addr - map->unmap_ip(map, sym->start));
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200779 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200780 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
Li Zefan079d3f62009-11-24 13:26:55 +0800781 printf(" %-34s |", buf);
Arnaldo Carvalho de Melo1b145ae2009-11-23 17:51:09 -0200782
Namhyung Kim65f46e02015-03-12 16:32:48 +0900783 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
Li Zefan079d3f62009-11-24 13:26:55 +0800784 (unsigned long long)data->bytes_alloc,
Li Zefanba77c9e2009-11-20 15:53:25 +0800785 (unsigned long)data->bytes_alloc / data->hit,
786 (unsigned long long)data->bytes_req,
787 (unsigned long)data->bytes_req / data->hit,
788 (unsigned long)data->hit,
Li Zefan079d3f62009-11-24 13:26:55 +0800789 (unsigned long)data->pingpong,
Li Zefanba77c9e2009-11-20 15:53:25 +0800790 fragmentation(data->bytes_req, data->bytes_alloc));
791
792 next = rb_next(next);
793 }
794
795 if (n_lines == -1)
Namhyung Kim65f46e02015-03-12 16:32:48 +0900796 printf(" ... | ... | ... | ... | ... | ... \n");
Li Zefanba77c9e2009-11-20 15:53:25 +0800797
Namhyung Kim65f46e02015-03-12 16:32:48 +0900798 printf("%.105s\n", graph_dotted_line);
Li Zefanba77c9e2009-11-20 15:53:25 +0800799}
800
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900801static const char * const migrate_type_str[] = {
802 "UNMOVABL",
803 "RECLAIM",
804 "MOVABLE",
805 "RESERVED",
806 "CMA/ISLT",
807 "UNKNOWN",
808};
809
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900810static void __print_page_alloc_result(struct perf_session *session, int n_lines)
Li Zefanba77c9e2009-11-20 15:53:25 +0800811{
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900812 struct rb_node *next = rb_first(&page_alloc_sorted);
813 struct machine *machine = &session->machines.host;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900814 const char *format;
815
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900816 printf("\n%.105s\n", graph_dotted_line);
817 printf(" %-16s | Total alloc (KB) | Hits | Order | Mig.type | GFP flags | Callsite\n",
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900818 use_pfn ? "PFN" : "Page");
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900819 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900820
821 if (use_pfn)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900822 format = " %16llu | %'16llu | %'9d | %5d | %8s | %08lx | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900823 else
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900824 format = " %016llx | %'16llu | %'9d | %5d | %8s | %08lx | %s\n";
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900825
826 while (next && n_lines--) {
827 struct page_stat *data;
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900828 struct symbol *sym;
829 struct map *map;
830 char buf[32];
831 char *caller = buf;
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900832
833 data = rb_entry(next, struct page_stat, node);
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900834 sym = machine__find_kernel_function(machine, data->callsite,
835 &map, NULL);
836 if (sym && sym->name)
837 caller = sym->name;
838 else
839 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900840
841 printf(format, (unsigned long long)data->page,
842 (unsigned long long)data->alloc_bytes / 1024,
843 data->nr_alloc, data->order,
844 migrate_type_str[data->migrate_type],
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900845 (unsigned long)data->gfp_flags, caller);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900846
847 next = rb_next(next);
848 }
849
850 if (n_lines == -1)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900851 printf(" ... | ... | ... | ... | ... | ... | ...\n");
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900852
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900853 printf("%.105s\n", graph_dotted_line);
854}
855
856static void __print_page_caller_result(struct perf_session *session, int n_lines)
857{
858 struct rb_node *next = rb_first(&page_caller_sorted);
859 struct machine *machine = &session->machines.host;
860
861 printf("\n%.105s\n", graph_dotted_line);
862 printf(" Total alloc (KB) | Hits | Order | Mig.type | GFP flags | Callsite\n");
863 printf("%.105s\n", graph_dotted_line);
864
865 while (next && n_lines--) {
866 struct page_stat *data;
867 struct symbol *sym;
868 struct map *map;
869 char buf[32];
870 char *caller = buf;
871
872 data = rb_entry(next, struct page_stat, node);
873 sym = machine__find_kernel_function(machine, data->callsite,
874 &map, NULL);
875 if (sym && sym->name)
876 caller = sym->name;
877 else
878 scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
879
880 printf(" %'16llu | %'9d | %5d | %8s | %08lx | %s\n",
881 (unsigned long long)data->alloc_bytes / 1024,
882 data->nr_alloc, data->order,
883 migrate_type_str[data->migrate_type],
884 (unsigned long)data->gfp_flags, caller);
885
886 next = rb_next(next);
887 }
888
889 if (n_lines == -1)
890 printf(" ... | ... | ... | ... | ... | ...\n");
891
892 printf("%.105s\n", graph_dotted_line);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900893}
894
895static void print_slab_summary(void)
896{
897 printf("\nSUMMARY (SLAB allocator)");
898 printf("\n========================\n");
Namhyung Kim77cfe382015-03-23 15:30:40 +0900899 printf("Total bytes requested: %'lu\n", total_requested);
900 printf("Total bytes allocated: %'lu\n", total_allocated);
901 printf("Total bytes wasted on internal fragmentation: %'lu\n",
Li Zefanba77c9e2009-11-20 15:53:25 +0800902 total_allocated - total_requested);
903 printf("Internal fragmentation: %f%%\n",
904 fragmentation(total_requested, total_allocated));
Namhyung Kim77cfe382015-03-23 15:30:40 +0900905 printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
Li Zefanba77c9e2009-11-20 15:53:25 +0800906}
907
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900908static void print_page_summary(void)
909{
910 int o, m;
911 u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch;
912 u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes;
913
914 printf("\nSUMMARY (page allocator)");
915 printf("\n========================\n");
916 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests",
917 nr_page_allocs, total_page_alloc_bytes / 1024);
918 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests",
919 nr_page_frees, total_page_free_bytes / 1024);
920 printf("\n");
921
922 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests",
923 nr_alloc_freed, (total_alloc_freed_bytes) / 1024);
924 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total alloc-only requests",
925 nr_page_allocs - nr_alloc_freed,
926 (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024);
927 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests",
928 nr_page_nomatch, total_page_nomatch_bytes / 1024);
929 printf("\n");
930
931 printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures",
932 nr_page_fails, total_page_fail_bytes / 1024);
933 printf("\n");
934
935 printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable",
936 "Reclaimable", "Movable", "Reserved", "CMA/Isolated");
937 printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line,
938 graph_dotted_line, graph_dotted_line, graph_dotted_line,
939 graph_dotted_line, graph_dotted_line);
940
941 for (o = 0; o < MAX_PAGE_ORDER; o++) {
942 printf("%5d", o);
943 for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) {
944 if (order_stats[o][m])
945 printf(" %'12d", order_stats[o][m]);
946 else
947 printf(" %12c", '.');
948 }
949 printf("\n");
950 }
951}
952
953static void print_slab_result(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +0800954{
955 if (caller_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900956 __print_slab_result(&root_caller_sorted, session, caller_lines, 1);
Li Zefanba77c9e2009-11-20 15:53:25 +0800957 if (alloc_flag)
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900958 __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0);
959 print_slab_summary();
960}
961
962static void print_page_result(struct perf_session *session)
963{
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900964 if (caller_flag)
965 __print_page_caller_result(session, caller_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900966 if (alloc_flag)
Namhyung Kimc9758cc2015-04-21 13:55:02 +0900967 __print_page_alloc_result(session, alloc_lines);
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900968 print_page_summary();
969}
970
971static void print_result(struct perf_session *session)
972{
973 if (kmem_slab)
974 print_slab_result(session);
975 if (kmem_page)
976 print_page_result(session);
Li Zefanba77c9e2009-11-20 15:53:25 +0800977}
978
Li Zefan29b3e152009-11-24 13:26:10 +0800979struct sort_dimension {
980 const char name[20];
981 sort_fn_t cmp;
982 struct list_head list;
983};
984
985static LIST_HEAD(caller_sort);
986static LIST_HEAD(alloc_sort);
987
Namhyung Kim0d68bc92015-04-06 14:36:10 +0900988static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data,
989 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +0800990{
991 struct rb_node **new = &(root->rb_node);
992 struct rb_node *parent = NULL;
Li Zefan29b3e152009-11-24 13:26:10 +0800993 struct sort_dimension *sort;
Li Zefanba77c9e2009-11-20 15:53:25 +0800994
995 while (*new) {
996 struct alloc_stat *this;
Li Zefan29b3e152009-11-24 13:26:10 +0800997 int cmp = 0;
Li Zefanba77c9e2009-11-20 15:53:25 +0800998
999 this = rb_entry(*new, struct alloc_stat, node);
1000 parent = *new;
1001
Li Zefan29b3e152009-11-24 13:26:10 +08001002 list_for_each_entry(sort, sort_list, list) {
1003 cmp = sort->cmp(data, this);
1004 if (cmp)
1005 break;
1006 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001007
1008 if (cmp > 0)
1009 new = &((*new)->rb_left);
1010 else
1011 new = &((*new)->rb_right);
1012 }
1013
1014 rb_link_node(&data->node, parent, new);
1015 rb_insert_color(&data->node, root);
1016}
1017
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001018static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted,
1019 struct list_head *sort_list)
Li Zefanba77c9e2009-11-20 15:53:25 +08001020{
1021 struct rb_node *node;
1022 struct alloc_stat *data;
1023
1024 for (;;) {
1025 node = rb_first(root);
1026 if (!node)
1027 break;
1028
1029 rb_erase(node, root);
1030 data = rb_entry(node, struct alloc_stat, node);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001031 sort_slab_insert(root_sorted, data, sort_list);
1032 }
1033}
1034
1035static void sort_page_insert(struct rb_root *root, struct page_stat *data)
1036{
1037 struct rb_node **new = &root->rb_node;
1038 struct rb_node *parent = NULL;
1039
1040 while (*new) {
1041 struct page_stat *this;
1042 int cmp = 0;
1043
1044 this = rb_entry(*new, struct page_stat, node);
1045 parent = *new;
1046
1047 /* TODO: support more sort key */
1048 cmp = data->alloc_bytes - this->alloc_bytes;
1049
1050 if (cmp > 0)
1051 new = &parent->rb_left;
1052 else
1053 new = &parent->rb_right;
1054 }
1055
1056 rb_link_node(&data->node, parent, new);
1057 rb_insert_color(&data->node, root);
1058}
1059
1060static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted)
1061{
1062 struct rb_node *node;
1063 struct page_stat *data;
1064
1065 for (;;) {
1066 node = rb_first(root);
1067 if (!node)
1068 break;
1069
1070 rb_erase(node, root);
1071 data = rb_entry(node, struct page_stat, node);
1072 sort_page_insert(root_sorted, data);
Li Zefanba77c9e2009-11-20 15:53:25 +08001073 }
1074}
1075
1076static void sort_result(void)
1077{
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001078 if (kmem_slab) {
1079 __sort_slab_result(&root_alloc_stat, &root_alloc_sorted,
1080 &alloc_sort);
1081 __sort_slab_result(&root_caller_stat, &root_caller_sorted,
1082 &caller_sort);
1083 }
1084 if (kmem_page) {
1085 __sort_page_result(&page_alloc_tree, &page_alloc_sorted);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001086 __sort_page_result(&page_caller_tree, &page_caller_sorted);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001087 }
Li Zefanba77c9e2009-11-20 15:53:25 +08001088}
1089
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001090static int __cmd_kmem(struct perf_session *session)
Li Zefanba77c9e2009-11-20 15:53:25 +08001091{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001092 int err = -EINVAL;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001093 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001094 const struct perf_evsel_str_handler kmem_tracepoints[] = {
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001095 /* slab allocator */
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001096 { "kmem:kmalloc", perf_evsel__process_alloc_event, },
1097 { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, },
1098 { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, },
1099 { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, },
1100 { "kmem:kfree", perf_evsel__process_free_event, },
1101 { "kmem:kmem_cache_free", perf_evsel__process_free_event, },
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001102 /* page allocator */
1103 { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, },
1104 { "kmem:mm_page_free", perf_evsel__process_page_free_event, },
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001105 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001106
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001107 if (!perf_session__has_traces(session, "kmem record"))
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001108 goto out;
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001109
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001110 if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) {
1111 pr_err("Initializing perf session tracepoint handlers failed\n");
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001112 goto out;
Arnaldo Carvalho de Melo0f7d2f12012-09-24 10:46:54 -03001113 }
1114
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001115 evlist__for_each(session->evlist, evsel) {
1116 if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") &&
1117 perf_evsel__field(evsel, "pfn")) {
1118 use_pfn = true;
1119 break;
1120 }
1121 }
1122
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001123 setup_pager();
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001124 err = perf_session__process_events(session);
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001125 if (err != 0) {
1126 pr_err("error during process events: %d\n", err);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001127 goto out;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001128 }
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001129 sort_result();
1130 print_result(session);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001131out:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001132 return err;
Li Zefanba77c9e2009-11-20 15:53:25 +08001133}
1134
Li Zefanba77c9e2009-11-20 15:53:25 +08001135static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r)
1136{
1137 if (l->ptr < r->ptr)
1138 return -1;
1139 else if (l->ptr > r->ptr)
1140 return 1;
1141 return 0;
1142}
1143
Li Zefan29b3e152009-11-24 13:26:10 +08001144static struct sort_dimension ptr_sort_dimension = {
1145 .name = "ptr",
1146 .cmp = ptr_cmp,
1147};
1148
Li Zefanba77c9e2009-11-20 15:53:25 +08001149static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r)
1150{
1151 if (l->call_site < r->call_site)
1152 return -1;
1153 else if (l->call_site > r->call_site)
1154 return 1;
1155 return 0;
1156}
1157
Li Zefan29b3e152009-11-24 13:26:10 +08001158static struct sort_dimension callsite_sort_dimension = {
1159 .name = "callsite",
1160 .cmp = callsite_cmp,
1161};
1162
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001163static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r)
1164{
1165 if (l->hit < r->hit)
1166 return -1;
1167 else if (l->hit > r->hit)
1168 return 1;
1169 return 0;
1170}
1171
Li Zefan29b3e152009-11-24 13:26:10 +08001172static struct sort_dimension hit_sort_dimension = {
1173 .name = "hit",
1174 .cmp = hit_cmp,
1175};
1176
Li Zefanba77c9e2009-11-20 15:53:25 +08001177static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r)
1178{
1179 if (l->bytes_alloc < r->bytes_alloc)
1180 return -1;
1181 else if (l->bytes_alloc > r->bytes_alloc)
1182 return 1;
1183 return 0;
1184}
1185
Li Zefan29b3e152009-11-24 13:26:10 +08001186static struct sort_dimension bytes_sort_dimension = {
1187 .name = "bytes",
1188 .cmp = bytes_cmp,
1189};
1190
Pekka Enbergf3ced7c2009-11-22 11:58:00 +02001191static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r)
1192{
1193 double x, y;
1194
1195 x = fragmentation(l->bytes_req, l->bytes_alloc);
1196 y = fragmentation(r->bytes_req, r->bytes_alloc);
1197
1198 if (x < y)
1199 return -1;
1200 else if (x > y)
1201 return 1;
1202 return 0;
1203}
1204
Li Zefan29b3e152009-11-24 13:26:10 +08001205static struct sort_dimension frag_sort_dimension = {
1206 .name = "frag",
1207 .cmp = frag_cmp,
1208};
1209
Li Zefan079d3f62009-11-24 13:26:55 +08001210static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r)
1211{
1212 if (l->pingpong < r->pingpong)
1213 return -1;
1214 else if (l->pingpong > r->pingpong)
1215 return 1;
1216 return 0;
1217}
1218
1219static struct sort_dimension pingpong_sort_dimension = {
1220 .name = "pingpong",
1221 .cmp = pingpong_cmp,
1222};
1223
Li Zefan29b3e152009-11-24 13:26:10 +08001224static struct sort_dimension *avail_sorts[] = {
1225 &ptr_sort_dimension,
1226 &callsite_sort_dimension,
1227 &hit_sort_dimension,
1228 &bytes_sort_dimension,
1229 &frag_sort_dimension,
Li Zefan079d3f62009-11-24 13:26:55 +08001230 &pingpong_sort_dimension,
Li Zefan29b3e152009-11-24 13:26:10 +08001231};
1232
Sasha Levin49e4ba52012-12-20 14:11:16 -05001233#define NUM_AVAIL_SORTS ((int)ARRAY_SIZE(avail_sorts))
Li Zefan29b3e152009-11-24 13:26:10 +08001234
1235static int sort_dimension__add(const char *tok, struct list_head *list)
1236{
1237 struct sort_dimension *sort;
1238 int i;
1239
1240 for (i = 0; i < NUM_AVAIL_SORTS; i++) {
1241 if (!strcmp(avail_sorts[i]->name, tok)) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -03001242 sort = memdup(avail_sorts[i], sizeof(*avail_sorts[i]));
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001243 if (!sort) {
Arnaldo Carvalho de Melo8d9233f2013-01-24 22:24:57 -03001244 pr_err("%s: memdup failed\n", __func__);
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001245 return -1;
1246 }
Li Zefan29b3e152009-11-24 13:26:10 +08001247 list_add_tail(&sort->list, list);
1248 return 0;
1249 }
1250 }
1251
1252 return -1;
1253}
1254
1255static int setup_sorting(struct list_head *sort_list, const char *arg)
1256{
1257 char *tok;
1258 char *str = strdup(arg);
Namhyung Kim405f8752015-03-12 16:32:46 +09001259 char *pos = str;
Li Zefan29b3e152009-11-24 13:26:10 +08001260
Arnaldo Carvalho de Melo2814eb02012-09-08 22:53:06 -03001261 if (!str) {
1262 pr_err("%s: strdup failed\n", __func__);
1263 return -1;
1264 }
Li Zefan29b3e152009-11-24 13:26:10 +08001265
1266 while (true) {
Namhyung Kim405f8752015-03-12 16:32:46 +09001267 tok = strsep(&pos, ",");
Li Zefan29b3e152009-11-24 13:26:10 +08001268 if (!tok)
1269 break;
1270 if (sort_dimension__add(tok, sort_list) < 0) {
1271 error("Unknown --sort key: '%s'", tok);
Namhyung Kim1b228592012-01-08 02:25:29 +09001272 free(str);
Li Zefan29b3e152009-11-24 13:26:10 +08001273 return -1;
1274 }
1275 }
1276
1277 free(str);
1278 return 0;
1279}
1280
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001281static int parse_sort_opt(const struct option *opt __maybe_unused,
1282 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001283{
Li Zefanba77c9e2009-11-20 15:53:25 +08001284 if (!arg)
1285 return -1;
1286
Li Zefanba77c9e2009-11-20 15:53:25 +08001287 if (caller_flag > alloc_flag)
Li Zefan29b3e152009-11-24 13:26:10 +08001288 return setup_sorting(&caller_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +08001289 else
Li Zefan29b3e152009-11-24 13:26:10 +08001290 return setup_sorting(&alloc_sort, arg);
Li Zefanba77c9e2009-11-20 15:53:25 +08001291
1292 return 0;
1293}
1294
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001295static int parse_caller_opt(const struct option *opt __maybe_unused,
1296 const char *arg __maybe_unused,
1297 int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001298{
Li Zefan90b86a92009-12-10 15:21:57 +08001299 caller_flag = (alloc_flag + 1);
1300 return 0;
1301}
Li Zefanba77c9e2009-11-20 15:53:25 +08001302
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001303static int parse_alloc_opt(const struct option *opt __maybe_unused,
1304 const char *arg __maybe_unused,
1305 int unset __maybe_unused)
Li Zefan90b86a92009-12-10 15:21:57 +08001306{
1307 alloc_flag = (caller_flag + 1);
Li Zefanba77c9e2009-11-20 15:53:25 +08001308 return 0;
1309}
1310
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001311static int parse_slab_opt(const struct option *opt __maybe_unused,
1312 const char *arg __maybe_unused,
1313 int unset __maybe_unused)
1314{
1315 kmem_slab = (kmem_page + 1);
1316 return 0;
1317}
1318
1319static int parse_page_opt(const struct option *opt __maybe_unused,
1320 const char *arg __maybe_unused,
1321 int unset __maybe_unused)
1322{
1323 kmem_page = (kmem_slab + 1);
1324 return 0;
1325}
1326
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001327static int parse_line_opt(const struct option *opt __maybe_unused,
1328 const char *arg, int unset __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001329{
1330 int lines;
1331
1332 if (!arg)
1333 return -1;
1334
1335 lines = strtoul(arg, NULL, 10);
1336
1337 if (caller_flag > alloc_flag)
1338 caller_lines = lines;
1339 else
1340 alloc_lines = lines;
1341
1342 return 0;
1343}
1344
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001345static int __cmd_record(int argc, const char **argv)
1346{
1347 const char * const record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001348 "record", "-a", "-R", "-c", "1",
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001349 };
1350 const char * const slab_events[] = {
Li Zefanba77c9e2009-11-20 15:53:25 +08001351 "-e", "kmem:kmalloc",
1352 "-e", "kmem:kmalloc_node",
1353 "-e", "kmem:kfree",
1354 "-e", "kmem:kmem_cache_alloc",
1355 "-e", "kmem:kmem_cache_alloc_node",
1356 "-e", "kmem:kmem_cache_free",
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001357 };
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001358 const char * const page_events[] = {
1359 "-e", "kmem:mm_page_alloc",
1360 "-e", "kmem:mm_page_free",
1361 };
Li Zefanba77c9e2009-11-20 15:53:25 +08001362 unsigned int rec_argc, i, j;
1363 const char **rec_argv;
1364
1365 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001366 if (kmem_slab)
1367 rec_argc += ARRAY_SIZE(slab_events);
1368 if (kmem_page)
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001369 rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001370
Li Zefanba77c9e2009-11-20 15:53:25 +08001371 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1372
Chris Samuelce47dc52010-11-13 13:35:06 +11001373 if (rec_argv == NULL)
1374 return -ENOMEM;
1375
Li Zefanba77c9e2009-11-20 15:53:25 +08001376 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1377 rec_argv[i] = strdup(record_args[i]);
1378
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001379 if (kmem_slab) {
1380 for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++)
1381 rec_argv[i] = strdup(slab_events[j]);
1382 }
1383 if (kmem_page) {
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001384 rec_argv[i++] = strdup("-g");
1385
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001386 for (j = 0; j < ARRAY_SIZE(page_events); j++, i++)
1387 rec_argv[i] = strdup(page_events[j]);
1388 }
1389
Li Zefanba77c9e2009-11-20 15:53:25 +08001390 for (j = 1; j < (unsigned int)argc; j++, i++)
1391 rec_argv[i] = argv[j];
1392
1393 return cmd_record(i, rec_argv, NULL);
1394}
1395
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001396int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
Li Zefanba77c9e2009-11-20 15:53:25 +08001397{
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001398 const char * const default_sort_order = "frag,hit,bytes";
Yunlong Songd1eeb772015-04-02 21:47:12 +08001399 struct perf_data_file file = {
Yunlong Songd1eeb772015-04-02 21:47:12 +08001400 .mode = PERF_DATA_MODE_READ,
1401 };
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001402 const struct option kmem_options[] = {
1403 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Namhyung Kimbd72a332015-03-12 16:32:47 +09001404 OPT_INCR('v', "verbose", &verbose,
1405 "be more verbose (show symbol address, etc)"),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001406 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
1407 "show per-callsite statistics", parse_caller_opt),
1408 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
1409 "show per-allocation statistics", parse_alloc_opt),
1410 OPT_CALLBACK('s', "sort", NULL, "key[,key2...]",
1411 "sort by keys: ptr, call_site, bytes, hit, pingpong, frag",
1412 parse_sort_opt),
1413 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
1414 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
Yunlong Songd1eeb772015-04-02 21:47:12 +08001415 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001416 OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator",
1417 parse_slab_opt),
1418 OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
1419 parse_page_opt),
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001420 OPT_END()
1421 };
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001422 const char *const kmem_subcommands[] = { "record", "stat", NULL };
1423 const char *kmem_usage[] = {
1424 NULL,
Arnaldo Carvalho de Melo0433ffb2012-10-01 15:20:58 -03001425 NULL
1426 };
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001427 struct perf_session *session;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001428 int ret = -1;
1429
Ramkumar Ramachandra3bca2352014-03-14 23:17:51 -04001430 argc = parse_options_subcommand(argc, argv, kmem_options,
1431 kmem_subcommands, kmem_usage, 0);
Li Zefanba77c9e2009-11-20 15:53:25 +08001432
Li Zefan90b86a92009-12-10 15:21:57 +08001433 if (!argc)
Li Zefanba77c9e2009-11-20 15:53:25 +08001434 usage_with_options(kmem_usage, kmem_options);
1435
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001436 if (kmem_slab == 0 && kmem_page == 0)
1437 kmem_slab = 1; /* for backward compatibility */
1438
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001439 if (!strncmp(argv[0], "rec", 3)) {
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001440 symbol__init(NULL);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001441 return __cmd_record(argc, argv);
1442 }
1443
Jiri Olsa28939e12015-04-06 14:36:08 +09001444 file.path = input_name;
1445
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001446 kmem_session = session = perf_session__new(&file, false, &perf_kmem);
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001447 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001448 return -1;
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001449
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001450 if (kmem_page) {
1451 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
1452
1453 if (evsel == NULL || evsel->tp_format == NULL) {
1454 pr_err("invalid event found.. aborting\n");
1455 return -1;
1456 }
1457
1458 kmem_page_size = pevent_get_page_size(evsel->tp_format->pevent);
Namhyung Kimc9758cc2015-04-21 13:55:02 +09001459 symbol_conf.use_callchain = true;
Namhyung Kim0d68bc92015-04-06 14:36:10 +09001460 }
1461
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001462 symbol__init(&session->header.env);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001463
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001464 if (!strcmp(argv[0], "stat")) {
Namhyung Kim77cfe382015-03-23 15:30:40 +09001465 setlocale(LC_ALL, "");
1466
Don Zickus4b627952014-04-07 14:55:23 -04001467 if (cpu__setup_cpunode_map())
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001468 goto out_delete;
Li Zefanba77c9e2009-11-20 15:53:25 +08001469
Li Zefan90b86a92009-12-10 15:21:57 +08001470 if (list_empty(&caller_sort))
1471 setup_sorting(&caller_sort, default_sort_order);
1472 if (list_empty(&alloc_sort))
1473 setup_sorting(&alloc_sort, default_sort_order);
Li Zefan7d0d3942009-11-24 13:26:31 +08001474
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001475 ret = __cmd_kmem(session);
Pekka Enbergb00eca82010-01-19 19:26:11 +02001476 } else
1477 usage_with_options(kmem_usage, kmem_options);
Li Zefan90b86a92009-12-10 15:21:57 +08001478
Namhyung Kim2b2b2c62014-08-12 15:40:38 +09001479out_delete:
1480 perf_session__delete(session);
1481
1482 return ret;
Li Zefanba77c9e2009-11-20 15:53:25 +08001483}
1484