]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
ba77c9e1 LZ |
2 | #include "builtin.h" |
3 | #include "perf.h" | |
4 | ||
0f7d2f1b | 5 | #include "util/evlist.h" |
fcf65bf1 | 6 | #include "util/evsel.h" |
41840d21 | 7 | #include "util/config.h" |
1101f69a | 8 | #include "util/map.h" |
ba77c9e1 LZ |
9 | #include "util/symbol.h" |
10 | #include "util/thread.h" | |
11 | #include "util/header.h" | |
94c744b6 | 12 | #include "util/session.h" |
45694aa7 | 13 | #include "util/tool.h" |
c9758cc4 | 14 | #include "util/callchain.h" |
2a865bd8 | 15 | #include "util/time-utils.h" |
ba77c9e1 | 16 | |
4b6ab94e | 17 | #include <subcmd/parse-options.h> |
ba77c9e1 | 18 | #include "util/trace-event.h" |
f5fc1412 | 19 | #include "util/data.h" |
4b627957 | 20 | #include "util/cpumap.h" |
ba77c9e1 LZ |
21 | |
22 | #include "util/debug.h" | |
6a9fa4e3 | 23 | #include "util/string2.h" |
ba77c9e1 | 24 | |
877a7a11 | 25 | #include <linux/kernel.h> |
ba77c9e1 | 26 | #include <linux/rbtree.h> |
8d9233f2 | 27 | #include <linux/string.h> |
7f7c536f | 28 | #include <linux/zalloc.h> |
a43783ae | 29 | #include <errno.h> |
fd20e811 | 30 | #include <inttypes.h> |
77cfe388 | 31 | #include <locale.h> |
c9758cc4 | 32 | #include <regex.h> |
ba77c9e1 | 33 | |
3052ba56 | 34 | #include <linux/ctype.h> |
3d689ed6 | 35 | |
0d68bc92 NK |
36 | static int kmem_slab; |
37 | static int kmem_page; | |
38 | ||
39 | static long kmem_page_size; | |
0c160d49 NK |
40 | static enum { |
41 | KMEM_SLAB, | |
42 | KMEM_PAGE, | |
43 | } kmem_default = KMEM_SLAB; /* for backward compatibility */ | |
0d68bc92 | 44 | |
ba77c9e1 | 45 | struct alloc_stat; |
fb4f313d | 46 | typedef int (*sort_fn_t)(void *, void *); |
ba77c9e1 | 47 | |
ba77c9e1 LZ |
48 | static int alloc_flag; |
49 | static int caller_flag; | |
50 | ||
ba77c9e1 LZ |
51 | static int alloc_lines = -1; |
52 | static int caller_lines = -1; | |
53 | ||
7707b6b6 LZ |
54 | static bool raw_ip; |
55 | ||
ba77c9e1 | 56 | struct alloc_stat { |
079d3f65 LZ |
57 | u64 call_site; |
58 | u64 ptr; | |
ba77c9e1 LZ |
59 | u64 bytes_req; |
60 | u64 bytes_alloc; | |
aa58e9af | 61 | u64 last_alloc; |
ba77c9e1 | 62 | u32 hit; |
079d3f65 LZ |
63 | u32 pingpong; |
64 | ||
65 | short alloc_cpu; | |
ba77c9e1 LZ |
66 | |
67 | struct rb_node node; | |
68 | }; | |
69 | ||
70 | static struct rb_root root_alloc_stat; | |
71 | static struct rb_root root_alloc_sorted; | |
72 | static struct rb_root root_caller_stat; | |
73 | static struct rb_root root_caller_sorted; | |
74 | ||
aa58e9af | 75 | static unsigned long total_requested, total_allocated, total_freed; |
7d0d3945 | 76 | static unsigned long nr_allocs, nr_cross_allocs; |
ba77c9e1 | 77 | |
2a865bd8 DA |
78 | /* filters for controlling start and stop of time of analysis */ |
79 | static struct perf_time_interval ptime; | |
80 | const char *time_str; | |
81 | ||
2814eb05 ACM |
82 | static int insert_alloc_stat(unsigned long call_site, unsigned long ptr, |
83 | int bytes_req, int bytes_alloc, int cpu) | |
ba77c9e1 LZ |
84 | { |
85 | struct rb_node **node = &root_alloc_stat.rb_node; | |
86 | struct rb_node *parent = NULL; | |
87 | struct alloc_stat *data = NULL; | |
88 | ||
ba77c9e1 LZ |
89 | while (*node) { |
90 | parent = *node; | |
91 | data = rb_entry(*node, struct alloc_stat, node); | |
92 | ||
93 | if (ptr > data->ptr) | |
94 | node = &(*node)->rb_right; | |
95 | else if (ptr < data->ptr) | |
96 | node = &(*node)->rb_left; | |
97 | else | |
98 | break; | |
99 | } | |
100 | ||
101 | if (data && data->ptr == ptr) { | |
102 | data->hit++; | |
103 | data->bytes_req += bytes_req; | |
4efb5290 | 104 | data->bytes_alloc += bytes_alloc; |
ba77c9e1 LZ |
105 | } else { |
106 | data = malloc(sizeof(*data)); | |
2814eb05 ACM |
107 | if (!data) { |
108 | pr_err("%s: malloc failed\n", __func__); | |
109 | return -1; | |
110 | } | |
ba77c9e1 | 111 | data->ptr = ptr; |
079d3f65 | 112 | data->pingpong = 0; |
ba77c9e1 LZ |
113 | data->hit = 1; |
114 | data->bytes_req = bytes_req; | |
115 | data->bytes_alloc = bytes_alloc; | |
116 | ||
117 | rb_link_node(&data->node, parent, node); | |
118 | rb_insert_color(&data->node, &root_alloc_stat); | |
119 | } | |
079d3f65 LZ |
120 | data->call_site = call_site; |
121 | data->alloc_cpu = cpu; | |
aa58e9af DA |
122 | data->last_alloc = bytes_alloc; |
123 | ||
2814eb05 | 124 | return 0; |
ba77c9e1 LZ |
125 | } |
126 | ||
2814eb05 | 127 | static int insert_caller_stat(unsigned long call_site, |
ba77c9e1 LZ |
128 | int bytes_req, int bytes_alloc) |
129 | { | |
130 | struct rb_node **node = &root_caller_stat.rb_node; | |
131 | struct rb_node *parent = NULL; | |
132 | struct alloc_stat *data = NULL; | |
133 | ||
ba77c9e1 LZ |
134 | while (*node) { |
135 | parent = *node; | |
136 | data = rb_entry(*node, struct alloc_stat, node); | |
137 | ||
138 | if (call_site > data->call_site) | |
139 | node = &(*node)->rb_right; | |
140 | else if (call_site < data->call_site) | |
141 | node = &(*node)->rb_left; | |
142 | else | |
143 | break; | |
144 | } | |
145 | ||
146 | if (data && data->call_site == call_site) { | |
147 | data->hit++; | |
148 | data->bytes_req += bytes_req; | |
4efb5290 | 149 | data->bytes_alloc += bytes_alloc; |
ba77c9e1 LZ |
150 | } else { |
151 | data = malloc(sizeof(*data)); | |
2814eb05 ACM |
152 | if (!data) { |
153 | pr_err("%s: malloc failed\n", __func__); | |
154 | return -1; | |
155 | } | |
ba77c9e1 | 156 | data->call_site = call_site; |
079d3f65 | 157 | data->pingpong = 0; |
ba77c9e1 LZ |
158 | data->hit = 1; |
159 | data->bytes_req = bytes_req; | |
160 | data->bytes_alloc = bytes_alloc; | |
161 | ||
162 | rb_link_node(&data->node, parent, node); | |
163 | rb_insert_color(&data->node, &root_caller_stat); | |
164 | } | |
2814eb05 ACM |
165 | |
166 | return 0; | |
ba77c9e1 LZ |
167 | } |
168 | ||
2814eb05 | 169 | static int perf_evsel__process_alloc_event(struct perf_evsel *evsel, |
0f7d2f1b | 170 | struct perf_sample *sample) |
ba77c9e1 | 171 | { |
0f7d2f1b ACM |
172 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"), |
173 | call_site = perf_evsel__intval(evsel, sample, "call_site"); | |
174 | int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"), | |
175 | bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc"); | |
176 | ||
177 | if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) || | |
2814eb05 ACM |
178 | insert_caller_stat(call_site, bytes_req, bytes_alloc)) |
179 | return -1; | |
ba77c9e1 LZ |
180 | |
181 | total_requested += bytes_req; | |
182 | total_allocated += bytes_alloc; | |
7d0d3945 | 183 | |
0f7d2f1b ACM |
184 | nr_allocs++; |
185 | return 0; | |
186 | } | |
187 | ||
188 | static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel, | |
189 | struct perf_sample *sample) | |
190 | { | |
191 | int ret = perf_evsel__process_alloc_event(evsel, sample); | |
192 | ||
193 | if (!ret) { | |
4b627957 | 194 | int node1 = cpu__get_node(sample->cpu), |
0f7d2f1b ACM |
195 | node2 = perf_evsel__intval(evsel, sample, "node"); |
196 | ||
7d0d3945 LZ |
197 | if (node1 != node2) |
198 | nr_cross_allocs++; | |
199 | } | |
0f7d2f1b ACM |
200 | |
201 | return ret; | |
ba77c9e1 LZ |
202 | } |
203 | ||
fb4f313d NK |
204 | static int ptr_cmp(void *, void *); |
205 | static int slab_callsite_cmp(void *, void *); | |
079d3f65 LZ |
206 | |
207 | static struct alloc_stat *search_alloc_stat(unsigned long ptr, | |
208 | unsigned long call_site, | |
209 | struct rb_root *root, | |
210 | sort_fn_t sort_fn) | |
211 | { | |
212 | struct rb_node *node = root->rb_node; | |
213 | struct alloc_stat key = { .ptr = ptr, .call_site = call_site }; | |
214 | ||
215 | while (node) { | |
216 | struct alloc_stat *data; | |
217 | int cmp; | |
218 | ||
219 | data = rb_entry(node, struct alloc_stat, node); | |
220 | ||
221 | cmp = sort_fn(&key, data); | |
222 | if (cmp < 0) | |
223 | node = node->rb_left; | |
224 | else if (cmp > 0) | |
225 | node = node->rb_right; | |
226 | else | |
227 | return data; | |
228 | } | |
229 | return NULL; | |
230 | } | |
231 | ||
2814eb05 ACM |
232 | static int perf_evsel__process_free_event(struct perf_evsel *evsel, |
233 | struct perf_sample *sample) | |
ba77c9e1 | 234 | { |
0f7d2f1b | 235 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"); |
079d3f65 LZ |
236 | struct alloc_stat *s_alloc, *s_caller; |
237 | ||
079d3f65 LZ |
238 | s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp); |
239 | if (!s_alloc) | |
2814eb05 | 240 | return 0; |
079d3f65 | 241 | |
aa58e9af DA |
242 | total_freed += s_alloc->last_alloc; |
243 | ||
22ad798c | 244 | if ((short)sample->cpu != s_alloc->alloc_cpu) { |
079d3f65 LZ |
245 | s_alloc->pingpong++; |
246 | ||
247 | s_caller = search_alloc_stat(0, s_alloc->call_site, | |
fb4f313d NK |
248 | &root_caller_stat, |
249 | slab_callsite_cmp); | |
2814eb05 ACM |
250 | if (!s_caller) |
251 | return -1; | |
079d3f65 LZ |
252 | s_caller->pingpong++; |
253 | } | |
254 | s_alloc->alloc_cpu = -1; | |
2814eb05 ACM |
255 | |
256 | return 0; | |
ba77c9e1 LZ |
257 | } |
258 | ||
0d68bc92 NK |
259 | static u64 total_page_alloc_bytes; |
260 | static u64 total_page_free_bytes; | |
261 | static u64 total_page_nomatch_bytes; | |
262 | static u64 total_page_fail_bytes; | |
263 | static unsigned long nr_page_allocs; | |
264 | static unsigned long nr_page_frees; | |
265 | static unsigned long nr_page_fails; | |
266 | static unsigned long nr_page_nomatch; | |
267 | ||
268 | static bool use_pfn; | |
2a7ef02c | 269 | static bool live_page; |
c9758cc4 | 270 | static struct perf_session *kmem_session; |
0d68bc92 NK |
271 | |
272 | #define MAX_MIGRATE_TYPES 6 | |
273 | #define MAX_PAGE_ORDER 11 | |
274 | ||
275 | static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES]; | |
276 | ||
277 | struct page_stat { | |
278 | struct rb_node node; | |
279 | u64 page; | |
c9758cc4 | 280 | u64 callsite; |
0d68bc92 NK |
281 | int order; |
282 | unsigned gfp_flags; | |
283 | unsigned migrate_type; | |
284 | u64 alloc_bytes; | |
285 | u64 free_bytes; | |
286 | int nr_alloc; | |
287 | int nr_free; | |
288 | }; | |
289 | ||
2a7ef02c | 290 | static struct rb_root page_live_tree; |
0d68bc92 NK |
291 | static struct rb_root page_alloc_tree; |
292 | static struct rb_root page_alloc_sorted; | |
c9758cc4 NK |
293 | static struct rb_root page_caller_tree; |
294 | static struct rb_root page_caller_sorted; | |
0d68bc92 | 295 | |
c9758cc4 NK |
296 | struct alloc_func { |
297 | u64 start; | |
298 | u64 end; | |
299 | char *name; | |
300 | }; | |
301 | ||
302 | static int nr_alloc_funcs; | |
303 | static struct alloc_func *alloc_func_list; | |
304 | ||
305 | static int funcmp(const void *a, const void *b) | |
306 | { | |
307 | const struct alloc_func *fa = a; | |
308 | const struct alloc_func *fb = b; | |
309 | ||
310 | if (fa->start > fb->start) | |
311 | return 1; | |
312 | else | |
313 | return -1; | |
314 | } | |
315 | ||
316 | static int callcmp(const void *a, const void *b) | |
317 | { | |
318 | const struct alloc_func *fa = a; | |
319 | const struct alloc_func *fb = b; | |
320 | ||
321 | if (fb->start <= fa->start && fa->end < fb->end) | |
322 | return 0; | |
323 | ||
324 | if (fa->start > fb->start) | |
325 | return 1; | |
326 | else | |
327 | return -1; | |
328 | } | |
329 | ||
330 | static int build_alloc_func_list(void) | |
331 | { | |
332 | int ret; | |
333 | struct map *kernel_map; | |
334 | struct symbol *sym; | |
335 | struct rb_node *node; | |
336 | struct alloc_func *func; | |
337 | struct machine *machine = &kmem_session->machines.host; | |
338 | regex_t alloc_func_regex; | |
49b8e2be | 339 | static const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?"; |
c9758cc4 NK |
340 | |
341 | ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED); | |
342 | if (ret) { | |
343 | char err[BUFSIZ]; | |
344 | ||
345 | regerror(ret, &alloc_func_regex, err, sizeof(err)); | |
346 | pr_err("Invalid regex: %s\n%s", pattern, err); | |
347 | return -EINVAL; | |
348 | } | |
349 | ||
a5e813c6 | 350 | kernel_map = machine__kernel_map(machine); |
be39db9f | 351 | if (map__load(kernel_map) < 0) { |
c9758cc4 NK |
352 | pr_err("cannot load kernel map\n"); |
353 | return -ENOENT; | |
354 | } | |
355 | ||
356 | map__for_each_symbol(kernel_map, sym, node) { | |
357 | if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0)) | |
358 | continue; | |
359 | ||
360 | func = realloc(alloc_func_list, | |
361 | (nr_alloc_funcs + 1) * sizeof(*func)); | |
362 | if (func == NULL) | |
363 | return -ENOMEM; | |
364 | ||
365 | pr_debug("alloc func: %s\n", sym->name); | |
366 | func[nr_alloc_funcs].start = sym->start; | |
367 | func[nr_alloc_funcs].end = sym->end; | |
368 | func[nr_alloc_funcs].name = sym->name; | |
369 | ||
370 | alloc_func_list = func; | |
371 | nr_alloc_funcs++; | |
372 | } | |
373 | ||
374 | qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp); | |
375 | ||
376 | regfree(&alloc_func_regex); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | /* | |
381 | * Find first non-memory allocation function from callchain. | |
382 | * The allocation functions are in the 'alloc_func_list'. | |
383 | */ | |
384 | static u64 find_callsite(struct perf_evsel *evsel, struct perf_sample *sample) | |
385 | { | |
386 | struct addr_location al; | |
387 | struct machine *machine = &kmem_session->machines.host; | |
388 | struct callchain_cursor_node *node; | |
389 | ||
390 | if (alloc_func_list == NULL) { | |
391 | if (build_alloc_func_list() < 0) | |
392 | goto out; | |
393 | } | |
394 | ||
395 | al.thread = machine__findnew_thread(machine, sample->pid, sample->tid); | |
91d7b2de | 396 | sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16); |
c9758cc4 NK |
397 | |
398 | callchain_cursor_commit(&callchain_cursor); | |
399 | while (true) { | |
400 | struct alloc_func key, *caller; | |
401 | u64 addr; | |
402 | ||
403 | node = callchain_cursor_current(&callchain_cursor); | |
404 | if (node == NULL) | |
405 | break; | |
406 | ||
407 | key.start = key.end = node->ip; | |
408 | caller = bsearch(&key, alloc_func_list, nr_alloc_funcs, | |
409 | sizeof(key), callcmp); | |
410 | if (!caller) { | |
411 | /* found */ | |
412 | if (node->map) | |
413 | addr = map__unmap_ip(node->map, node->ip); | |
414 | else | |
415 | addr = node->ip; | |
416 | ||
417 | return addr; | |
418 | } else | |
419 | pr_debug3("skipping alloc function: %s\n", caller->name); | |
420 | ||
421 | callchain_cursor_advance(&callchain_cursor); | |
422 | } | |
423 | ||
424 | out: | |
425 | pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip); | |
426 | return sample->ip; | |
427 | } | |
428 | ||
2a7ef02c NK |
429 | struct sort_dimension { |
430 | const char name[20]; | |
431 | sort_fn_t cmp; | |
432 | struct list_head list; | |
433 | }; | |
434 | ||
435 | static LIST_HEAD(page_alloc_sort_input); | |
436 | static LIST_HEAD(page_caller_sort_input); | |
437 | ||
c9758cc4 | 438 | static struct page_stat * |
2a7ef02c | 439 | __page_stat__findnew_page(struct page_stat *pstat, bool create) |
0d68bc92 | 440 | { |
2a7ef02c | 441 | struct rb_node **node = &page_live_tree.rb_node; |
0d68bc92 NK |
442 | struct rb_node *parent = NULL; |
443 | struct page_stat *data; | |
444 | ||
445 | while (*node) { | |
446 | s64 cmp; | |
447 | ||
448 | parent = *node; | |
449 | data = rb_entry(*node, struct page_stat, node); | |
450 | ||
2a7ef02c | 451 | cmp = data->page - pstat->page; |
0d68bc92 NK |
452 | if (cmp < 0) |
453 | node = &parent->rb_left; | |
454 | else if (cmp > 0) | |
455 | node = &parent->rb_right; | |
456 | else | |
457 | return data; | |
458 | } | |
459 | ||
460 | if (!create) | |
461 | return NULL; | |
462 | ||
463 | data = zalloc(sizeof(*data)); | |
464 | if (data != NULL) { | |
2a7ef02c NK |
465 | data->page = pstat->page; |
466 | data->order = pstat->order; | |
467 | data->gfp_flags = pstat->gfp_flags; | |
468 | data->migrate_type = pstat->migrate_type; | |
0d68bc92 NK |
469 | |
470 | rb_link_node(&data->node, parent, node); | |
2a7ef02c | 471 | rb_insert_color(&data->node, &page_live_tree); |
0d68bc92 NK |
472 | } |
473 | ||
474 | return data; | |
475 | } | |
476 | ||
2a7ef02c | 477 | static struct page_stat *page_stat__find_page(struct page_stat *pstat) |
c9758cc4 | 478 | { |
2a7ef02c | 479 | return __page_stat__findnew_page(pstat, false); |
c9758cc4 NK |
480 | } |
481 | ||
2a7ef02c | 482 | static struct page_stat *page_stat__findnew_page(struct page_stat *pstat) |
c9758cc4 | 483 | { |
2a7ef02c | 484 | return __page_stat__findnew_page(pstat, true); |
c9758cc4 NK |
485 | } |
486 | ||
c9758cc4 NK |
487 | static struct page_stat * |
488 | __page_stat__findnew_alloc(struct page_stat *pstat, bool create) | |
0d68bc92 NK |
489 | { |
490 | struct rb_node **node = &page_alloc_tree.rb_node; | |
491 | struct rb_node *parent = NULL; | |
492 | struct page_stat *data; | |
fb4f313d | 493 | struct sort_dimension *sort; |
0d68bc92 NK |
494 | |
495 | while (*node) { | |
fb4f313d | 496 | int cmp = 0; |
0d68bc92 NK |
497 | |
498 | parent = *node; | |
499 | data = rb_entry(*node, struct page_stat, node); | |
500 | ||
fb4f313d NK |
501 | list_for_each_entry(sort, &page_alloc_sort_input, list) { |
502 | cmp = sort->cmp(pstat, data); | |
503 | if (cmp) | |
504 | break; | |
505 | } | |
506 | ||
0d68bc92 NK |
507 | if (cmp < 0) |
508 | node = &parent->rb_left; | |
509 | else if (cmp > 0) | |
510 | node = &parent->rb_right; | |
511 | else | |
512 | return data; | |
513 | } | |
514 | ||
515 | if (!create) | |
516 | return NULL; | |
517 | ||
518 | data = zalloc(sizeof(*data)); | |
519 | if (data != NULL) { | |
6b1a2752 DA |
520 | data->page = pstat->page; |
521 | data->order = pstat->order; | |
522 | data->gfp_flags = pstat->gfp_flags; | |
523 | data->migrate_type = pstat->migrate_type; | |
0d68bc92 NK |
524 | |
525 | rb_link_node(&data->node, parent, node); | |
526 | rb_insert_color(&data->node, &page_alloc_tree); | |
527 | } | |
528 | ||
529 | return data; | |
530 | } | |
531 | ||
c9758cc4 NK |
532 | static struct page_stat *page_stat__find_alloc(struct page_stat *pstat) |
533 | { | |
534 | return __page_stat__findnew_alloc(pstat, false); | |
535 | } | |
536 | ||
537 | static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat) | |
538 | { | |
539 | return __page_stat__findnew_alloc(pstat, true); | |
540 | } | |
541 | ||
542 | static struct page_stat * | |
fb4f313d | 543 | __page_stat__findnew_caller(struct page_stat *pstat, bool create) |
c9758cc4 NK |
544 | { |
545 | struct rb_node **node = &page_caller_tree.rb_node; | |
546 | struct rb_node *parent = NULL; | |
547 | struct page_stat *data; | |
fb4f313d | 548 | struct sort_dimension *sort; |
c9758cc4 NK |
549 | |
550 | while (*node) { | |
fb4f313d | 551 | int cmp = 0; |
c9758cc4 NK |
552 | |
553 | parent = *node; | |
554 | data = rb_entry(*node, struct page_stat, node); | |
555 | ||
fb4f313d NK |
556 | list_for_each_entry(sort, &page_caller_sort_input, list) { |
557 | cmp = sort->cmp(pstat, data); | |
558 | if (cmp) | |
559 | break; | |
560 | } | |
561 | ||
c9758cc4 NK |
562 | if (cmp < 0) |
563 | node = &parent->rb_left; | |
564 | else if (cmp > 0) | |
565 | node = &parent->rb_right; | |
566 | else | |
567 | return data; | |
568 | } | |
569 | ||
570 | if (!create) | |
571 | return NULL; | |
572 | ||
573 | data = zalloc(sizeof(*data)); | |
574 | if (data != NULL) { | |
fb4f313d NK |
575 | data->callsite = pstat->callsite; |
576 | data->order = pstat->order; | |
577 | data->gfp_flags = pstat->gfp_flags; | |
578 | data->migrate_type = pstat->migrate_type; | |
c9758cc4 NK |
579 | |
580 | rb_link_node(&data->node, parent, node); | |
581 | rb_insert_color(&data->node, &page_caller_tree); | |
582 | } | |
583 | ||
584 | return data; | |
585 | } | |
586 | ||
fb4f313d | 587 | static struct page_stat *page_stat__find_caller(struct page_stat *pstat) |
c9758cc4 | 588 | { |
fb4f313d | 589 | return __page_stat__findnew_caller(pstat, false); |
c9758cc4 NK |
590 | } |
591 | ||
fb4f313d | 592 | static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat) |
c9758cc4 | 593 | { |
fb4f313d | 594 | return __page_stat__findnew_caller(pstat, true); |
c9758cc4 NK |
595 | } |
596 | ||
0d68bc92 NK |
597 | static bool valid_page(u64 pfn_or_page) |
598 | { | |
599 | if (use_pfn && pfn_or_page == -1UL) | |
600 | return false; | |
601 | if (!use_pfn && pfn_or_page == 0) | |
602 | return false; | |
603 | return true; | |
604 | } | |
605 | ||
0e111156 NK |
606 | struct gfp_flag { |
607 | unsigned int flags; | |
608 | char *compact_str; | |
609 | char *human_readable; | |
610 | }; | |
611 | ||
612 | static struct gfp_flag *gfps; | |
613 | static int nr_gfps; | |
614 | ||
615 | static int gfpcmp(const void *a, const void *b) | |
616 | { | |
617 | const struct gfp_flag *fa = a; | |
618 | const struct gfp_flag *fb = b; | |
619 | ||
620 | return fa->flags - fb->flags; | |
621 | } | |
622 | ||
420adbe9 | 623 | /* see include/trace/events/mmflags.h */ |
0e111156 NK |
624 | static const struct { |
625 | const char *original; | |
626 | const char *compact; | |
627 | } gfp_compact_table[] = { | |
628 | { "GFP_TRANSHUGE", "THP" }, | |
25160354 | 629 | { "GFP_TRANSHUGE_LIGHT", "THL" }, |
0e111156 NK |
630 | { "GFP_HIGHUSER_MOVABLE", "HUM" }, |
631 | { "GFP_HIGHUSER", "HU" }, | |
632 | { "GFP_USER", "U" }, | |
14e0a214 | 633 | { "GFP_KERNEL_ACCOUNT", "KAC" }, |
0e111156 NK |
634 | { "GFP_KERNEL", "K" }, |
635 | { "GFP_NOFS", "NF" }, | |
636 | { "GFP_ATOMIC", "A" }, | |
637 | { "GFP_NOIO", "NI" }, | |
0e111156 | 638 | { "GFP_NOWAIT", "NW" }, |
14e0a214 VB |
639 | { "GFP_DMA", "D" }, |
640 | { "__GFP_HIGHMEM", "HM" }, | |
641 | { "GFP_DMA32", "D32" }, | |
642 | { "__GFP_HIGH", "H" }, | |
643 | { "__GFP_ATOMIC", "_A" }, | |
644 | { "__GFP_IO", "I" }, | |
645 | { "__GFP_FS", "F" }, | |
14e0a214 | 646 | { "__GFP_NOWARN", "NWR" }, |
dcda9b04 | 647 | { "__GFP_RETRY_MAYFAIL", "R" }, |
14e0a214 VB |
648 | { "__GFP_NOFAIL", "NF" }, |
649 | { "__GFP_NORETRY", "NR" }, | |
650 | { "__GFP_COMP", "C" }, | |
651 | { "__GFP_ZERO", "Z" }, | |
652 | { "__GFP_NOMEMALLOC", "NMA" }, | |
653 | { "__GFP_MEMALLOC", "MA" }, | |
654 | { "__GFP_HARDWALL", "HW" }, | |
655 | { "__GFP_THISNODE", "TN" }, | |
656 | { "__GFP_RECLAIMABLE", "RC" }, | |
657 | { "__GFP_MOVABLE", "M" }, | |
658 | { "__GFP_ACCOUNT", "AC" }, | |
14e0a214 VB |
659 | { "__GFP_WRITE", "WR" }, |
660 | { "__GFP_RECLAIM", "R" }, | |
661 | { "__GFP_DIRECT_RECLAIM", "DR" }, | |
662 | { "__GFP_KSWAPD_RECLAIM", "KR" }, | |
0e111156 NK |
663 | }; |
664 | ||
665 | static size_t max_gfp_len; | |
666 | ||
667 | static char *compact_gfp_flags(char *gfp_flags) | |
668 | { | |
669 | char *orig_flags = strdup(gfp_flags); | |
670 | char *new_flags = NULL; | |
b2365122 | 671 | char *str, *pos = NULL; |
0e111156 NK |
672 | size_t len = 0; |
673 | ||
674 | if (orig_flags == NULL) | |
675 | return NULL; | |
676 | ||
677 | str = strtok_r(orig_flags, "|", &pos); | |
678 | while (str) { | |
679 | size_t i; | |
680 | char *new; | |
681 | const char *cpt; | |
682 | ||
683 | for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) { | |
684 | if (strcmp(gfp_compact_table[i].original, str)) | |
685 | continue; | |
686 | ||
687 | cpt = gfp_compact_table[i].compact; | |
688 | new = realloc(new_flags, len + strlen(cpt) + 2); | |
689 | if (new == NULL) { | |
690 | free(new_flags); | |
97122b7c | 691 | free(orig_flags); |
0e111156 NK |
692 | return NULL; |
693 | } | |
694 | ||
695 | new_flags = new; | |
696 | ||
697 | if (!len) { | |
698 | strcpy(new_flags, cpt); | |
699 | } else { | |
700 | strcat(new_flags, "|"); | |
701 | strcat(new_flags, cpt); | |
702 | len++; | |
703 | } | |
704 | ||
705 | len += strlen(cpt); | |
706 | } | |
707 | ||
708 | str = strtok_r(NULL, "|", &pos); | |
709 | } | |
710 | ||
711 | if (max_gfp_len < len) | |
712 | max_gfp_len = len; | |
713 | ||
714 | free(orig_flags); | |
715 | return new_flags; | |
716 | } | |
717 | ||
718 | static char *compact_gfp_string(unsigned long gfp_flags) | |
719 | { | |
720 | struct gfp_flag key = { | |
721 | .flags = gfp_flags, | |
722 | }; | |
723 | struct gfp_flag *gfp; | |
724 | ||
725 | gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp); | |
726 | if (gfp) | |
727 | return gfp->compact_str; | |
728 | ||
729 | return NULL; | |
730 | } | |
731 | ||
732 | static int parse_gfp_flags(struct perf_evsel *evsel, struct perf_sample *sample, | |
733 | unsigned int gfp_flags) | |
734 | { | |
cbc49b25 | 735 | struct tep_record record = { |
0e111156 NK |
736 | .cpu = sample->cpu, |
737 | .data = sample->raw_data, | |
738 | .size = sample->raw_size, | |
739 | }; | |
740 | struct trace_seq seq; | |
08a9b985 | 741 | char *str, *pos = NULL; |
0e111156 NK |
742 | |
743 | if (nr_gfps) { | |
744 | struct gfp_flag key = { | |
745 | .flags = gfp_flags, | |
746 | }; | |
747 | ||
748 | if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp)) | |
749 | return 0; | |
750 | } | |
751 | ||
752 | trace_seq_init(&seq); | |
4d5c58b1 | 753 | tep_event_info(&seq, evsel->tp_format, &record); |
0e111156 NK |
754 | |
755 | str = strtok_r(seq.buffer, " ", &pos); | |
756 | while (str) { | |
757 | if (!strncmp(str, "gfp_flags=", 10)) { | |
758 | struct gfp_flag *new; | |
759 | ||
760 | new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps)); | |
761 | if (new == NULL) | |
762 | return -ENOMEM; | |
763 | ||
764 | gfps = new; | |
765 | new += nr_gfps++; | |
766 | ||
767 | new->flags = gfp_flags; | |
768 | new->human_readable = strdup(str + 10); | |
769 | new->compact_str = compact_gfp_flags(str + 10); | |
770 | if (!new->human_readable || !new->compact_str) | |
771 | return -ENOMEM; | |
772 | ||
773 | qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp); | |
774 | } | |
775 | ||
776 | str = strtok_r(NULL, " ", &pos); | |
777 | } | |
778 | ||
779 | trace_seq_destroy(&seq); | |
780 | return 0; | |
781 | } | |
782 | ||
0d68bc92 NK |
783 | static int perf_evsel__process_page_alloc_event(struct perf_evsel *evsel, |
784 | struct perf_sample *sample) | |
785 | { | |
786 | u64 page; | |
787 | unsigned int order = perf_evsel__intval(evsel, sample, "order"); | |
788 | unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags"); | |
789 | unsigned int migrate_type = perf_evsel__intval(evsel, sample, | |
790 | "migratetype"); | |
791 | u64 bytes = kmem_page_size << order; | |
c9758cc4 | 792 | u64 callsite; |
6b1a2752 | 793 | struct page_stat *pstat; |
0d68bc92 NK |
794 | struct page_stat this = { |
795 | .order = order, | |
796 | .gfp_flags = gfp_flags, | |
797 | .migrate_type = migrate_type, | |
798 | }; | |
799 | ||
800 | if (use_pfn) | |
801 | page = perf_evsel__intval(evsel, sample, "pfn"); | |
802 | else | |
803 | page = perf_evsel__intval(evsel, sample, "page"); | |
804 | ||
805 | nr_page_allocs++; | |
806 | total_page_alloc_bytes += bytes; | |
807 | ||
808 | if (!valid_page(page)) { | |
809 | nr_page_fails++; | |
810 | total_page_fail_bytes += bytes; | |
811 | ||
812 | return 0; | |
813 | } | |
814 | ||
0e111156 NK |
815 | if (parse_gfp_flags(evsel, sample, gfp_flags) < 0) |
816 | return -1; | |
817 | ||
c9758cc4 NK |
818 | callsite = find_callsite(evsel, sample); |
819 | ||
0d68bc92 NK |
820 | /* |
821 | * This is to find the current page (with correct gfp flags and | |
822 | * migrate type) at free event. | |
823 | */ | |
0d68bc92 | 824 | this.page = page; |
2a7ef02c | 825 | pstat = page_stat__findnew_page(&this); |
6b1a2752 | 826 | if (pstat == NULL) |
0d68bc92 NK |
827 | return -ENOMEM; |
828 | ||
c9758cc4 NK |
829 | pstat->nr_alloc++; |
830 | pstat->alloc_bytes += bytes; | |
831 | pstat->callsite = callsite; | |
832 | ||
2a7ef02c NK |
833 | if (!live_page) { |
834 | pstat = page_stat__findnew_alloc(&this); | |
835 | if (pstat == NULL) | |
836 | return -ENOMEM; | |
837 | ||
838 | pstat->nr_alloc++; | |
839 | pstat->alloc_bytes += bytes; | |
840 | pstat->callsite = callsite; | |
841 | } | |
842 | ||
fb4f313d NK |
843 | this.callsite = callsite; |
844 | pstat = page_stat__findnew_caller(&this); | |
c9758cc4 NK |
845 | if (pstat == NULL) |
846 | return -ENOMEM; | |
847 | ||
6b1a2752 DA |
848 | pstat->nr_alloc++; |
849 | pstat->alloc_bytes += bytes; | |
0d68bc92 NK |
850 | |
851 | order_stats[order][migrate_type]++; | |
852 | ||
853 | return 0; | |
854 | } | |
855 | ||
856 | static int perf_evsel__process_page_free_event(struct perf_evsel *evsel, | |
857 | struct perf_sample *sample) | |
858 | { | |
859 | u64 page; | |
860 | unsigned int order = perf_evsel__intval(evsel, sample, "order"); | |
861 | u64 bytes = kmem_page_size << order; | |
6b1a2752 | 862 | struct page_stat *pstat; |
0d68bc92 NK |
863 | struct page_stat this = { |
864 | .order = order, | |
865 | }; | |
866 | ||
867 | if (use_pfn) | |
868 | page = perf_evsel__intval(evsel, sample, "pfn"); | |
869 | else | |
870 | page = perf_evsel__intval(evsel, sample, "page"); | |
871 | ||
872 | nr_page_frees++; | |
873 | total_page_free_bytes += bytes; | |
874 | ||
2a7ef02c NK |
875 | this.page = page; |
876 | pstat = page_stat__find_page(&this); | |
6b1a2752 | 877 | if (pstat == NULL) { |
0d68bc92 NK |
878 | pr_debug2("missing free at page %"PRIx64" (order: %d)\n", |
879 | page, order); | |
880 | ||
881 | nr_page_nomatch++; | |
882 | total_page_nomatch_bytes += bytes; | |
883 | ||
884 | return 0; | |
885 | } | |
886 | ||
6b1a2752 DA |
887 | this.gfp_flags = pstat->gfp_flags; |
888 | this.migrate_type = pstat->migrate_type; | |
c9758cc4 | 889 | this.callsite = pstat->callsite; |
0d68bc92 | 890 | |
2a7ef02c | 891 | rb_erase(&pstat->node, &page_live_tree); |
6b1a2752 | 892 | free(pstat); |
0d68bc92 | 893 | |
2a7ef02c NK |
894 | if (live_page) { |
895 | order_stats[this.order][this.migrate_type]--; | |
896 | } else { | |
897 | pstat = page_stat__find_alloc(&this); | |
898 | if (pstat == NULL) | |
899 | return -ENOMEM; | |
c9758cc4 | 900 | |
2a7ef02c NK |
901 | pstat->nr_free++; |
902 | pstat->free_bytes += bytes; | |
903 | } | |
c9758cc4 | 904 | |
fb4f313d | 905 | pstat = page_stat__find_caller(&this); |
6b1a2752 | 906 | if (pstat == NULL) |
0d68bc92 NK |
907 | return -ENOENT; |
908 | ||
6b1a2752 DA |
909 | pstat->nr_free++; |
910 | pstat->free_bytes += bytes; | |
0d68bc92 | 911 | |
2a7ef02c NK |
912 | if (live_page) { |
913 | pstat->nr_alloc--; | |
914 | pstat->alloc_bytes -= bytes; | |
915 | ||
916 | if (pstat->nr_alloc == 0) { | |
917 | rb_erase(&pstat->node, &page_caller_tree); | |
918 | free(pstat); | |
919 | } | |
920 | } | |
921 | ||
0d68bc92 NK |
922 | return 0; |
923 | } | |
924 | ||
2a865bd8 DA |
925 | static bool perf_kmem__skip_sample(struct perf_sample *sample) |
926 | { | |
927 | /* skip sample based on time? */ | |
928 | if (perf_time__skip_sample(&ptime, sample->time)) | |
929 | return true; | |
930 | ||
931 | return false; | |
932 | } | |
933 | ||
0f7d2f1b ACM |
934 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
935 | struct perf_sample *sample); | |
ba77c9e1 | 936 | |
1d037ca1 | 937 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 938 | union perf_event *event, |
8115d60c | 939 | struct perf_sample *sample, |
fcf65bf1 | 940 | struct perf_evsel *evsel, |
743eb868 | 941 | struct machine *machine) |
ba77c9e1 | 942 | { |
b91fc39f | 943 | int err = 0; |
ef89325f | 944 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
13ce34df | 945 | sample->tid); |
ba77c9e1 | 946 | |
ba77c9e1 LZ |
947 | if (thread == NULL) { |
948 | pr_debug("problem processing %d event, skipping it.\n", | |
949 | event->header.type); | |
950 | return -1; | |
951 | } | |
952 | ||
2a865bd8 DA |
953 | if (perf_kmem__skip_sample(sample)) |
954 | return 0; | |
955 | ||
b9c5143a | 956 | dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); |
ba77c9e1 | 957 | |
744a9719 ACM |
958 | if (evsel->handler != NULL) { |
959 | tracepoint_handler f = evsel->handler; | |
b91fc39f | 960 | err = f(evsel, sample); |
0f7d2f1b ACM |
961 | } |
962 | ||
b91fc39f ACM |
963 | thread__put(thread); |
964 | ||
965 | return err; | |
ba77c9e1 LZ |
966 | } |
967 | ||
fcf65bf1 ACM |
968 | static struct perf_tool perf_kmem = { |
969 | .sample = process_sample_event, | |
970 | .comm = perf_event__process_comm, | |
64c40908 NK |
971 | .mmap = perf_event__process_mmap, |
972 | .mmap2 = perf_event__process_mmap2, | |
f3b3614a | 973 | .namespaces = perf_event__process_namespaces, |
0a8cb85c | 974 | .ordered_events = true, |
ba77c9e1 LZ |
975 | }; |
976 | ||
ba77c9e1 LZ |
977 | static double fragmentation(unsigned long n_req, unsigned long n_alloc) |
978 | { | |
979 | if (n_alloc == 0) | |
980 | return 0.0; | |
981 | else | |
982 | return 100.0 - (100.0 * n_req / n_alloc); | |
983 | } | |
984 | ||
0d68bc92 NK |
985 | static void __print_slab_result(struct rb_root *root, |
986 | struct perf_session *session, | |
987 | int n_lines, int is_caller) | |
ba77c9e1 LZ |
988 | { |
989 | struct rb_node *next; | |
34ba5122 | 990 | struct machine *machine = &session->machines.host; |
ba77c9e1 | 991 | |
65f46e02 | 992 | printf("%.105s\n", graph_dotted_line); |
079d3f65 | 993 | printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); |
47103277 | 994 | printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); |
65f46e02 | 995 | printf("%.105s\n", graph_dotted_line); |
ba77c9e1 LZ |
996 | |
997 | next = rb_first(root); | |
998 | ||
999 | while (next && n_lines--) { | |
1b145ae5 ACM |
1000 | struct alloc_stat *data = rb_entry(next, struct alloc_stat, |
1001 | node); | |
1002 | struct symbol *sym = NULL; | |
71cf8b8f | 1003 | struct map *map; |
079d3f65 | 1004 | char buf[BUFSIZ]; |
1b145ae5 ACM |
1005 | u64 addr; |
1006 | ||
1007 | if (is_caller) { | |
1008 | addr = data->call_site; | |
7707b6b6 | 1009 | if (!raw_ip) |
107cad95 | 1010 | sym = machine__find_kernel_symbol(machine, addr, &map); |
1b145ae5 ACM |
1011 | } else |
1012 | addr = data->ptr; | |
1013 | ||
1014 | if (sym != NULL) | |
9486aa38 | 1015 | snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name, |
71cf8b8f | 1016 | addr - map->unmap_ip(map, sym->start)); |
1b145ae5 | 1017 | else |
9486aa38 | 1018 | snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr); |
079d3f65 | 1019 | printf(" %-34s |", buf); |
ba77c9e1 | 1020 | |
65f46e02 | 1021 | printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n", |
079d3f65 | 1022 | (unsigned long long)data->bytes_alloc, |
ba77c9e1 LZ |
1023 | (unsigned long)data->bytes_alloc / data->hit, |
1024 | (unsigned long long)data->bytes_req, | |
1025 | (unsigned long)data->bytes_req / data->hit, | |
1026 | (unsigned long)data->hit, | |
079d3f65 | 1027 | (unsigned long)data->pingpong, |
ba77c9e1 LZ |
1028 | fragmentation(data->bytes_req, data->bytes_alloc)); |
1029 | ||
1030 | next = rb_next(next); | |
1031 | } | |
1032 | ||
1033 | if (n_lines == -1) | |
65f46e02 | 1034 | printf(" ... | ... | ... | ... | ... | ... \n"); |
ba77c9e1 | 1035 | |
65f46e02 | 1036 | printf("%.105s\n", graph_dotted_line); |
ba77c9e1 LZ |
1037 | } |
1038 | ||
0d68bc92 NK |
1039 | static const char * const migrate_type_str[] = { |
1040 | "UNMOVABL", | |
1041 | "RECLAIM", | |
1042 | "MOVABLE", | |
1043 | "RESERVED", | |
1044 | "CMA/ISLT", | |
1045 | "UNKNOWN", | |
1046 | }; | |
1047 | ||
c9758cc4 | 1048 | static void __print_page_alloc_result(struct perf_session *session, int n_lines) |
0d68bc92 | 1049 | { |
c9758cc4 NK |
1050 | struct rb_node *next = rb_first(&page_alloc_sorted); |
1051 | struct machine *machine = &session->machines.host; | |
0d68bc92 | 1052 | const char *format; |
0e111156 | 1053 | int gfp_len = max(strlen("GFP flags"), max_gfp_len); |
0d68bc92 | 1054 | |
c9758cc4 | 1055 | printf("\n%.105s\n", graph_dotted_line); |
0e111156 NK |
1056 | printf(" %-16s | %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n", |
1057 | use_pfn ? "PFN" : "Page", live_page ? "Live" : "Total", | |
1058 | gfp_len, "GFP flags"); | |
c9758cc4 | 1059 | printf("%.105s\n", graph_dotted_line); |
0d68bc92 NK |
1060 | |
1061 | if (use_pfn) | |
0e111156 | 1062 | format = " %16llu | %'16llu | %'9d | %5d | %8s | %-*s | %s\n"; |
0d68bc92 | 1063 | else |
0e111156 | 1064 | format = " %016llx | %'16llu | %'9d | %5d | %8s | %-*s | %s\n"; |
0d68bc92 NK |
1065 | |
1066 | while (next && n_lines--) { | |
1067 | struct page_stat *data; | |
c9758cc4 NK |
1068 | struct symbol *sym; |
1069 | struct map *map; | |
1070 | char buf[32]; | |
1071 | char *caller = buf; | |
0d68bc92 NK |
1072 | |
1073 | data = rb_entry(next, struct page_stat, node); | |
107cad95 | 1074 | sym = machine__find_kernel_symbol(machine, data->callsite, &map); |
a7c3899c | 1075 | if (sym) |
c9758cc4 NK |
1076 | caller = sym->name; |
1077 | else | |
1078 | scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite); | |
0d68bc92 NK |
1079 | |
1080 | printf(format, (unsigned long long)data->page, | |
1081 | (unsigned long long)data->alloc_bytes / 1024, | |
1082 | data->nr_alloc, data->order, | |
1083 | migrate_type_str[data->migrate_type], | |
0e111156 | 1084 | gfp_len, compact_gfp_string(data->gfp_flags), caller); |
0d68bc92 NK |
1085 | |
1086 | next = rb_next(next); | |
1087 | } | |
1088 | ||
0e111156 NK |
1089 | if (n_lines == -1) { |
1090 | printf(" ... | ... | ... | ... | ... | %-*s | ...\n", | |
1091 | gfp_len, "..."); | |
1092 | } | |
0d68bc92 | 1093 | |
c9758cc4 NK |
1094 | printf("%.105s\n", graph_dotted_line); |
1095 | } | |
1096 | ||
1097 | static void __print_page_caller_result(struct perf_session *session, int n_lines) | |
1098 | { | |
1099 | struct rb_node *next = rb_first(&page_caller_sorted); | |
1100 | struct machine *machine = &session->machines.host; | |
0e111156 | 1101 | int gfp_len = max(strlen("GFP flags"), max_gfp_len); |
c9758cc4 NK |
1102 | |
1103 | printf("\n%.105s\n", graph_dotted_line); | |
0e111156 NK |
1104 | printf(" %5s alloc (KB) | Hits | Order | Mig.type | %-*s | Callsite\n", |
1105 | live_page ? "Live" : "Total", gfp_len, "GFP flags"); | |
c9758cc4 NK |
1106 | printf("%.105s\n", graph_dotted_line); |
1107 | ||
1108 | while (next && n_lines--) { | |
1109 | struct page_stat *data; | |
1110 | struct symbol *sym; | |
1111 | struct map *map; | |
1112 | char buf[32]; | |
1113 | char *caller = buf; | |
1114 | ||
1115 | data = rb_entry(next, struct page_stat, node); | |
107cad95 | 1116 | sym = machine__find_kernel_symbol(machine, data->callsite, &map); |
a7c3899c | 1117 | if (sym) |
c9758cc4 NK |
1118 | caller = sym->name; |
1119 | else | |
1120 | scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite); | |
1121 | ||
0e111156 | 1122 | printf(" %'16llu | %'9d | %5d | %8s | %-*s | %s\n", |
c9758cc4 NK |
1123 | (unsigned long long)data->alloc_bytes / 1024, |
1124 | data->nr_alloc, data->order, | |
1125 | migrate_type_str[data->migrate_type], | |
0e111156 | 1126 | gfp_len, compact_gfp_string(data->gfp_flags), caller); |
c9758cc4 NK |
1127 | |
1128 | next = rb_next(next); | |
1129 | } | |
1130 | ||
0e111156 NK |
1131 | if (n_lines == -1) { |
1132 | printf(" ... | ... | ... | ... | %-*s | ...\n", | |
1133 | gfp_len, "..."); | |
1134 | } | |
c9758cc4 NK |
1135 | |
1136 | printf("%.105s\n", graph_dotted_line); | |
0d68bc92 NK |
1137 | } |
1138 | ||
0e111156 NK |
1139 | static void print_gfp_flags(void) |
1140 | { | |
1141 | int i; | |
1142 | ||
1143 | printf("#\n"); | |
1144 | printf("# GFP flags\n"); | |
1145 | printf("# ---------\n"); | |
1146 | for (i = 0; i < nr_gfps; i++) { | |
1147 | printf("# %08x: %*s: %s\n", gfps[i].flags, | |
1148 | (int) max_gfp_len, gfps[i].compact_str, | |
1149 | gfps[i].human_readable); | |
1150 | } | |
1151 | } | |
1152 | ||
0d68bc92 | 1153 | static void print_slab_summary(void) |
ba77c9e1 | 1154 | { |
0d68bc92 NK |
1155 | printf("\nSUMMARY (SLAB allocator)"); |
1156 | printf("\n========================\n"); | |
77cfe388 NK |
1157 | printf("Total bytes requested: %'lu\n", total_requested); |
1158 | printf("Total bytes allocated: %'lu\n", total_allocated); | |
aa58e9af DA |
1159 | printf("Total bytes freed: %'lu\n", total_freed); |
1160 | if (total_allocated > total_freed) { | |
1161 | printf("Net total bytes allocated: %'lu\n", | |
1162 | total_allocated - total_freed); | |
1163 | } | |
77cfe388 | 1164 | printf("Total bytes wasted on internal fragmentation: %'lu\n", |
ba77c9e1 LZ |
1165 | total_allocated - total_requested); |
1166 | printf("Internal fragmentation: %f%%\n", | |
1167 | fragmentation(total_requested, total_allocated)); | |
77cfe388 | 1168 | printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs); |
ba77c9e1 LZ |
1169 | } |
1170 | ||
0d68bc92 NK |
1171 | static void print_page_summary(void) |
1172 | { | |
1173 | int o, m; | |
1174 | u64 nr_alloc_freed = nr_page_frees - nr_page_nomatch; | |
1175 | u64 total_alloc_freed_bytes = total_page_free_bytes - total_page_nomatch_bytes; | |
1176 | ||
1177 | printf("\nSUMMARY (page allocator)"); | |
1178 | printf("\n========================\n"); | |
1179 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation requests", | |
1180 | nr_page_allocs, total_page_alloc_bytes / 1024); | |
1181 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free requests", | |
1182 | nr_page_frees, total_page_free_bytes / 1024); | |
1183 | printf("\n"); | |
1184 | ||
6145c259 | 1185 | printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc+freed requests", |
0d68bc92 | 1186 | nr_alloc_freed, (total_alloc_freed_bytes) / 1024); |
6145c259 | 1187 | printf("%-30s: %'16"PRIu64" [ %'16"PRIu64" KB ]\n", "Total alloc-only requests", |
0d68bc92 NK |
1188 | nr_page_allocs - nr_alloc_freed, |
1189 | (total_page_alloc_bytes - total_alloc_freed_bytes) / 1024); | |
1190 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total free-only requests", | |
1191 | nr_page_nomatch, total_page_nomatch_bytes / 1024); | |
1192 | printf("\n"); | |
1193 | ||
1194 | printf("%-30s: %'16lu [ %'16"PRIu64" KB ]\n", "Total allocation failures", | |
1195 | nr_page_fails, total_page_fail_bytes / 1024); | |
1196 | printf("\n"); | |
1197 | ||
1198 | printf("%5s %12s %12s %12s %12s %12s\n", "Order", "Unmovable", | |
1199 | "Reclaimable", "Movable", "Reserved", "CMA/Isolated"); | |
1200 | printf("%.5s %.12s %.12s %.12s %.12s %.12s\n", graph_dotted_line, | |
1201 | graph_dotted_line, graph_dotted_line, graph_dotted_line, | |
1202 | graph_dotted_line, graph_dotted_line); | |
1203 | ||
1204 | for (o = 0; o < MAX_PAGE_ORDER; o++) { | |
1205 | printf("%5d", o); | |
1206 | for (m = 0; m < MAX_MIGRATE_TYPES - 1; m++) { | |
1207 | if (order_stats[o][m]) | |
1208 | printf(" %'12d", order_stats[o][m]); | |
1209 | else | |
1210 | printf(" %12c", '.'); | |
1211 | } | |
1212 | printf("\n"); | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | static void print_slab_result(struct perf_session *session) | |
ba77c9e1 LZ |
1217 | { |
1218 | if (caller_flag) | |
0d68bc92 NK |
1219 | __print_slab_result(&root_caller_sorted, session, caller_lines, 1); |
1220 | if (alloc_flag) | |
1221 | __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0); | |
1222 | print_slab_summary(); | |
1223 | } | |
1224 | ||
1225 | static void print_page_result(struct perf_session *session) | |
1226 | { | |
0e111156 NK |
1227 | if (caller_flag || alloc_flag) |
1228 | print_gfp_flags(); | |
c9758cc4 NK |
1229 | if (caller_flag) |
1230 | __print_page_caller_result(session, caller_lines); | |
ba77c9e1 | 1231 | if (alloc_flag) |
c9758cc4 | 1232 | __print_page_alloc_result(session, alloc_lines); |
0d68bc92 NK |
1233 | print_page_summary(); |
1234 | } | |
1235 | ||
1236 | static void print_result(struct perf_session *session) | |
1237 | { | |
1238 | if (kmem_slab) | |
1239 | print_slab_result(session); | |
1240 | if (kmem_page) | |
1241 | print_page_result(session); | |
ba77c9e1 LZ |
1242 | } |
1243 | ||
fb4f313d NK |
1244 | static LIST_HEAD(slab_caller_sort); |
1245 | static LIST_HEAD(slab_alloc_sort); | |
1246 | static LIST_HEAD(page_caller_sort); | |
1247 | static LIST_HEAD(page_alloc_sort); | |
29b3e152 | 1248 | |
0d68bc92 NK |
1249 | static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data, |
1250 | struct list_head *sort_list) | |
ba77c9e1 LZ |
1251 | { |
1252 | struct rb_node **new = &(root->rb_node); | |
1253 | struct rb_node *parent = NULL; | |
29b3e152 | 1254 | struct sort_dimension *sort; |
ba77c9e1 LZ |
1255 | |
1256 | while (*new) { | |
1257 | struct alloc_stat *this; | |
29b3e152 | 1258 | int cmp = 0; |
ba77c9e1 LZ |
1259 | |
1260 | this = rb_entry(*new, struct alloc_stat, node); | |
1261 | parent = *new; | |
1262 | ||
29b3e152 LZ |
1263 | list_for_each_entry(sort, sort_list, list) { |
1264 | cmp = sort->cmp(data, this); | |
1265 | if (cmp) | |
1266 | break; | |
1267 | } | |
ba77c9e1 LZ |
1268 | |
1269 | if (cmp > 0) | |
1270 | new = &((*new)->rb_left); | |
1271 | else | |
1272 | new = &((*new)->rb_right); | |
1273 | } | |
1274 | ||
1275 | rb_link_node(&data->node, parent, new); | |
1276 | rb_insert_color(&data->node, root); | |
1277 | } | |
1278 | ||
0d68bc92 NK |
1279 | static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted, |
1280 | struct list_head *sort_list) | |
ba77c9e1 LZ |
1281 | { |
1282 | struct rb_node *node; | |
1283 | struct alloc_stat *data; | |
1284 | ||
1285 | for (;;) { | |
1286 | node = rb_first(root); | |
1287 | if (!node) | |
1288 | break; | |
1289 | ||
1290 | rb_erase(node, root); | |
1291 | data = rb_entry(node, struct alloc_stat, node); | |
0d68bc92 NK |
1292 | sort_slab_insert(root_sorted, data, sort_list); |
1293 | } | |
1294 | } | |
1295 | ||
fb4f313d NK |
1296 | static void sort_page_insert(struct rb_root *root, struct page_stat *data, |
1297 | struct list_head *sort_list) | |
0d68bc92 NK |
1298 | { |
1299 | struct rb_node **new = &root->rb_node; | |
1300 | struct rb_node *parent = NULL; | |
fb4f313d | 1301 | struct sort_dimension *sort; |
0d68bc92 NK |
1302 | |
1303 | while (*new) { | |
1304 | struct page_stat *this; | |
1305 | int cmp = 0; | |
1306 | ||
1307 | this = rb_entry(*new, struct page_stat, node); | |
1308 | parent = *new; | |
1309 | ||
fb4f313d NK |
1310 | list_for_each_entry(sort, sort_list, list) { |
1311 | cmp = sort->cmp(data, this); | |
1312 | if (cmp) | |
1313 | break; | |
1314 | } | |
0d68bc92 NK |
1315 | |
1316 | if (cmp > 0) | |
1317 | new = &parent->rb_left; | |
1318 | else | |
1319 | new = &parent->rb_right; | |
1320 | } | |
1321 | ||
1322 | rb_link_node(&data->node, parent, new); | |
1323 | rb_insert_color(&data->node, root); | |
1324 | } | |
1325 | ||
fb4f313d NK |
1326 | static void __sort_page_result(struct rb_root *root, struct rb_root *root_sorted, |
1327 | struct list_head *sort_list) | |
0d68bc92 NK |
1328 | { |
1329 | struct rb_node *node; | |
1330 | struct page_stat *data; | |
1331 | ||
1332 | for (;;) { | |
1333 | node = rb_first(root); | |
1334 | if (!node) | |
1335 | break; | |
1336 | ||
1337 | rb_erase(node, root); | |
1338 | data = rb_entry(node, struct page_stat, node); | |
fb4f313d | 1339 | sort_page_insert(root_sorted, data, sort_list); |
ba77c9e1 LZ |
1340 | } |
1341 | } | |
1342 | ||
1343 | static void sort_result(void) | |
1344 | { | |
0d68bc92 NK |
1345 | if (kmem_slab) { |
1346 | __sort_slab_result(&root_alloc_stat, &root_alloc_sorted, | |
fb4f313d | 1347 | &slab_alloc_sort); |
0d68bc92 | 1348 | __sort_slab_result(&root_caller_stat, &root_caller_sorted, |
fb4f313d | 1349 | &slab_caller_sort); |
0d68bc92 NK |
1350 | } |
1351 | if (kmem_page) { | |
2a7ef02c NK |
1352 | if (live_page) |
1353 | __sort_page_result(&page_live_tree, &page_alloc_sorted, | |
1354 | &page_alloc_sort); | |
1355 | else | |
1356 | __sort_page_result(&page_alloc_tree, &page_alloc_sorted, | |
1357 | &page_alloc_sort); | |
1358 | ||
fb4f313d NK |
1359 | __sort_page_result(&page_caller_tree, &page_caller_sorted, |
1360 | &page_caller_sort); | |
0d68bc92 | 1361 | } |
ba77c9e1 LZ |
1362 | } |
1363 | ||
2b2b2c68 | 1364 | static int __cmd_kmem(struct perf_session *session) |
ba77c9e1 | 1365 | { |
d549c769 | 1366 | int err = -EINVAL; |
0d68bc92 | 1367 | struct perf_evsel *evsel; |
0f7d2f1b | 1368 | const struct perf_evsel_str_handler kmem_tracepoints[] = { |
0d68bc92 | 1369 | /* slab allocator */ |
0f7d2f1b ACM |
1370 | { "kmem:kmalloc", perf_evsel__process_alloc_event, }, |
1371 | { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, }, | |
1372 | { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, }, | |
1373 | { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, }, | |
1374 | { "kmem:kfree", perf_evsel__process_free_event, }, | |
1375 | { "kmem:kmem_cache_free", perf_evsel__process_free_event, }, | |
0d68bc92 NK |
1376 | /* page allocator */ |
1377 | { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, }, | |
1378 | { "kmem:mm_page_free", perf_evsel__process_page_free_event, }, | |
0f7d2f1b | 1379 | }; |
4aa65636 | 1380 | |
d549c769 | 1381 | if (!perf_session__has_traces(session, "kmem record")) |
2b2b2c68 | 1382 | goto out; |
d549c769 | 1383 | |
0f7d2f1b ACM |
1384 | if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) { |
1385 | pr_err("Initializing perf session tracepoint handlers failed\n"); | |
2b2b2c68 | 1386 | goto out; |
0f7d2f1b ACM |
1387 | } |
1388 | ||
e5cadb93 | 1389 | evlist__for_each_entry(session->evlist, evsel) { |
0d68bc92 NK |
1390 | if (!strcmp(perf_evsel__name(evsel), "kmem:mm_page_alloc") && |
1391 | perf_evsel__field(evsel, "pfn")) { | |
1392 | use_pfn = true; | |
1393 | break; | |
1394 | } | |
1395 | } | |
1396 | ||
ba77c9e1 | 1397 | setup_pager(); |
b7b61cbe | 1398 | err = perf_session__process_events(session); |
0d68bc92 NK |
1399 | if (err != 0) { |
1400 | pr_err("error during process events: %d\n", err); | |
2b2b2c68 | 1401 | goto out; |
0d68bc92 | 1402 | } |
ba77c9e1 | 1403 | sort_result(); |
4aa65636 | 1404 | print_result(session); |
2b2b2c68 | 1405 | out: |
4aa65636 | 1406 | return err; |
ba77c9e1 LZ |
1407 | } |
1408 | ||
fb4f313d NK |
1409 | /* slab sort keys */ |
1410 | static int ptr_cmp(void *a, void *b) | |
ba77c9e1 | 1411 | { |
fb4f313d NK |
1412 | struct alloc_stat *l = a; |
1413 | struct alloc_stat *r = b; | |
1414 | ||
ba77c9e1 LZ |
1415 | if (l->ptr < r->ptr) |
1416 | return -1; | |
1417 | else if (l->ptr > r->ptr) | |
1418 | return 1; | |
1419 | return 0; | |
1420 | } | |
1421 | ||
29b3e152 LZ |
1422 | static struct sort_dimension ptr_sort_dimension = { |
1423 | .name = "ptr", | |
1424 | .cmp = ptr_cmp, | |
1425 | }; | |
1426 | ||
fb4f313d | 1427 | static int slab_callsite_cmp(void *a, void *b) |
ba77c9e1 | 1428 | { |
fb4f313d NK |
1429 | struct alloc_stat *l = a; |
1430 | struct alloc_stat *r = b; | |
1431 | ||
ba77c9e1 LZ |
1432 | if (l->call_site < r->call_site) |
1433 | return -1; | |
1434 | else if (l->call_site > r->call_site) | |
1435 | return 1; | |
1436 | return 0; | |
1437 | } | |
1438 | ||
29b3e152 LZ |
1439 | static struct sort_dimension callsite_sort_dimension = { |
1440 | .name = "callsite", | |
fb4f313d | 1441 | .cmp = slab_callsite_cmp, |
29b3e152 LZ |
1442 | }; |
1443 | ||
fb4f313d | 1444 | static int hit_cmp(void *a, void *b) |
f3ced7cd | 1445 | { |
fb4f313d NK |
1446 | struct alloc_stat *l = a; |
1447 | struct alloc_stat *r = b; | |
1448 | ||
f3ced7cd PE |
1449 | if (l->hit < r->hit) |
1450 | return -1; | |
1451 | else if (l->hit > r->hit) | |
1452 | return 1; | |
1453 | return 0; | |
1454 | } | |
1455 | ||
29b3e152 LZ |
1456 | static struct sort_dimension hit_sort_dimension = { |
1457 | .name = "hit", | |
1458 | .cmp = hit_cmp, | |
1459 | }; | |
1460 | ||
fb4f313d | 1461 | static int bytes_cmp(void *a, void *b) |
ba77c9e1 | 1462 | { |
fb4f313d NK |
1463 | struct alloc_stat *l = a; |
1464 | struct alloc_stat *r = b; | |
1465 | ||
ba77c9e1 LZ |
1466 | if (l->bytes_alloc < r->bytes_alloc) |
1467 | return -1; | |
1468 | else if (l->bytes_alloc > r->bytes_alloc) | |
1469 | return 1; | |
1470 | return 0; | |
1471 | } | |
1472 | ||
29b3e152 LZ |
1473 | static struct sort_dimension bytes_sort_dimension = { |
1474 | .name = "bytes", | |
1475 | .cmp = bytes_cmp, | |
1476 | }; | |
1477 | ||
fb4f313d | 1478 | static int frag_cmp(void *a, void *b) |
f3ced7cd PE |
1479 | { |
1480 | double x, y; | |
fb4f313d NK |
1481 | struct alloc_stat *l = a; |
1482 | struct alloc_stat *r = b; | |
f3ced7cd PE |
1483 | |
1484 | x = fragmentation(l->bytes_req, l->bytes_alloc); | |
1485 | y = fragmentation(r->bytes_req, r->bytes_alloc); | |
1486 | ||
1487 | if (x < y) | |
1488 | return -1; | |
1489 | else if (x > y) | |
1490 | return 1; | |
1491 | return 0; | |
1492 | } | |
1493 | ||
29b3e152 LZ |
1494 | static struct sort_dimension frag_sort_dimension = { |
1495 | .name = "frag", | |
1496 | .cmp = frag_cmp, | |
1497 | }; | |
1498 | ||
fb4f313d | 1499 | static int pingpong_cmp(void *a, void *b) |
079d3f65 | 1500 | { |
fb4f313d NK |
1501 | struct alloc_stat *l = a; |
1502 | struct alloc_stat *r = b; | |
1503 | ||
079d3f65 LZ |
1504 | if (l->pingpong < r->pingpong) |
1505 | return -1; | |
1506 | else if (l->pingpong > r->pingpong) | |
1507 | return 1; | |
1508 | return 0; | |
1509 | } | |
1510 | ||
1511 | static struct sort_dimension pingpong_sort_dimension = { | |
1512 | .name = "pingpong", | |
1513 | .cmp = pingpong_cmp, | |
1514 | }; | |
1515 | ||
fb4f313d NK |
1516 | /* page sort keys */ |
1517 | static int page_cmp(void *a, void *b) | |
1518 | { | |
1519 | struct page_stat *l = a; | |
1520 | struct page_stat *r = b; | |
1521 | ||
1522 | if (l->page < r->page) | |
1523 | return -1; | |
1524 | else if (l->page > r->page) | |
1525 | return 1; | |
1526 | return 0; | |
1527 | } | |
1528 | ||
1529 | static struct sort_dimension page_sort_dimension = { | |
1530 | .name = "page", | |
1531 | .cmp = page_cmp, | |
1532 | }; | |
1533 | ||
1534 | static int page_callsite_cmp(void *a, void *b) | |
1535 | { | |
1536 | struct page_stat *l = a; | |
1537 | struct page_stat *r = b; | |
1538 | ||
1539 | if (l->callsite < r->callsite) | |
1540 | return -1; | |
1541 | else if (l->callsite > r->callsite) | |
1542 | return 1; | |
1543 | return 0; | |
1544 | } | |
1545 | ||
1546 | static struct sort_dimension page_callsite_sort_dimension = { | |
1547 | .name = "callsite", | |
1548 | .cmp = page_callsite_cmp, | |
1549 | }; | |
1550 | ||
1551 | static int page_hit_cmp(void *a, void *b) | |
1552 | { | |
1553 | struct page_stat *l = a; | |
1554 | struct page_stat *r = b; | |
1555 | ||
1556 | if (l->nr_alloc < r->nr_alloc) | |
1557 | return -1; | |
1558 | else if (l->nr_alloc > r->nr_alloc) | |
1559 | return 1; | |
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | static struct sort_dimension page_hit_sort_dimension = { | |
1564 | .name = "hit", | |
1565 | .cmp = page_hit_cmp, | |
1566 | }; | |
1567 | ||
1568 | static int page_bytes_cmp(void *a, void *b) | |
1569 | { | |
1570 | struct page_stat *l = a; | |
1571 | struct page_stat *r = b; | |
1572 | ||
1573 | if (l->alloc_bytes < r->alloc_bytes) | |
1574 | return -1; | |
1575 | else if (l->alloc_bytes > r->alloc_bytes) | |
1576 | return 1; | |
1577 | return 0; | |
1578 | } | |
1579 | ||
1580 | static struct sort_dimension page_bytes_sort_dimension = { | |
1581 | .name = "bytes", | |
1582 | .cmp = page_bytes_cmp, | |
1583 | }; | |
1584 | ||
1585 | static int page_order_cmp(void *a, void *b) | |
1586 | { | |
1587 | struct page_stat *l = a; | |
1588 | struct page_stat *r = b; | |
1589 | ||
1590 | if (l->order < r->order) | |
1591 | return -1; | |
1592 | else if (l->order > r->order) | |
1593 | return 1; | |
1594 | return 0; | |
1595 | } | |
1596 | ||
1597 | static struct sort_dimension page_order_sort_dimension = { | |
1598 | .name = "order", | |
1599 | .cmp = page_order_cmp, | |
1600 | }; | |
1601 | ||
1602 | static int migrate_type_cmp(void *a, void *b) | |
1603 | { | |
1604 | struct page_stat *l = a; | |
1605 | struct page_stat *r = b; | |
1606 | ||
1607 | /* for internal use to find free'd page */ | |
1608 | if (l->migrate_type == -1U) | |
1609 | return 0; | |
1610 | ||
1611 | if (l->migrate_type < r->migrate_type) | |
1612 | return -1; | |
1613 | else if (l->migrate_type > r->migrate_type) | |
1614 | return 1; | |
1615 | return 0; | |
1616 | } | |
1617 | ||
1618 | static struct sort_dimension migrate_type_sort_dimension = { | |
1619 | .name = "migtype", | |
1620 | .cmp = migrate_type_cmp, | |
1621 | }; | |
1622 | ||
1623 | static int gfp_flags_cmp(void *a, void *b) | |
1624 | { | |
1625 | struct page_stat *l = a; | |
1626 | struct page_stat *r = b; | |
1627 | ||
1628 | /* for internal use to find free'd page */ | |
1629 | if (l->gfp_flags == -1U) | |
1630 | return 0; | |
1631 | ||
1632 | if (l->gfp_flags < r->gfp_flags) | |
1633 | return -1; | |
1634 | else if (l->gfp_flags > r->gfp_flags) | |
1635 | return 1; | |
1636 | return 0; | |
1637 | } | |
1638 | ||
1639 | static struct sort_dimension gfp_flags_sort_dimension = { | |
1640 | .name = "gfp", | |
1641 | .cmp = gfp_flags_cmp, | |
1642 | }; | |
1643 | ||
1644 | static struct sort_dimension *slab_sorts[] = { | |
29b3e152 LZ |
1645 | &ptr_sort_dimension, |
1646 | &callsite_sort_dimension, | |
1647 | &hit_sort_dimension, | |
1648 | &bytes_sort_dimension, | |
1649 | &frag_sort_dimension, | |
079d3f65 | 1650 | &pingpong_sort_dimension, |
29b3e152 LZ |
1651 | }; |
1652 | ||
fb4f313d NK |
1653 | static struct sort_dimension *page_sorts[] = { |
1654 | &page_sort_dimension, | |
1655 | &page_callsite_sort_dimension, | |
1656 | &page_hit_sort_dimension, | |
1657 | &page_bytes_sort_dimension, | |
1658 | &page_order_sort_dimension, | |
1659 | &migrate_type_sort_dimension, | |
1660 | &gfp_flags_sort_dimension, | |
1661 | }; | |
29b3e152 | 1662 | |
fb4f313d | 1663 | static int slab_sort_dimension__add(const char *tok, struct list_head *list) |
29b3e152 LZ |
1664 | { |
1665 | struct sort_dimension *sort; | |
1666 | int i; | |
1667 | ||
fb4f313d NK |
1668 | for (i = 0; i < (int)ARRAY_SIZE(slab_sorts); i++) { |
1669 | if (!strcmp(slab_sorts[i]->name, tok)) { | |
1670 | sort = memdup(slab_sorts[i], sizeof(*slab_sorts[i])); | |
2814eb05 | 1671 | if (!sort) { |
8d9233f2 | 1672 | pr_err("%s: memdup failed\n", __func__); |
2814eb05 ACM |
1673 | return -1; |
1674 | } | |
29b3e152 LZ |
1675 | list_add_tail(&sort->list, list); |
1676 | return 0; | |
1677 | } | |
1678 | } | |
1679 | ||
1680 | return -1; | |
1681 | } | |
1682 | ||
fb4f313d NK |
1683 | static int page_sort_dimension__add(const char *tok, struct list_head *list) |
1684 | { | |
1685 | struct sort_dimension *sort; | |
1686 | int i; | |
1687 | ||
1688 | for (i = 0; i < (int)ARRAY_SIZE(page_sorts); i++) { | |
1689 | if (!strcmp(page_sorts[i]->name, tok)) { | |
1690 | sort = memdup(page_sorts[i], sizeof(*page_sorts[i])); | |
1691 | if (!sort) { | |
1692 | pr_err("%s: memdup failed\n", __func__); | |
1693 | return -1; | |
1694 | } | |
1695 | list_add_tail(&sort->list, list); | |
1696 | return 0; | |
1697 | } | |
1698 | } | |
1699 | ||
1700 | return -1; | |
1701 | } | |
1702 | ||
1703 | static int setup_slab_sorting(struct list_head *sort_list, const char *arg) | |
29b3e152 LZ |
1704 | { |
1705 | char *tok; | |
1706 | char *str = strdup(arg); | |
405f8755 | 1707 | char *pos = str; |
29b3e152 | 1708 | |
2814eb05 ACM |
1709 | if (!str) { |
1710 | pr_err("%s: strdup failed\n", __func__); | |
1711 | return -1; | |
1712 | } | |
29b3e152 LZ |
1713 | |
1714 | while (true) { | |
405f8755 | 1715 | tok = strsep(&pos, ","); |
29b3e152 LZ |
1716 | if (!tok) |
1717 | break; | |
fb4f313d | 1718 | if (slab_sort_dimension__add(tok, sort_list) < 0) { |
62d94b00 | 1719 | pr_err("Unknown slab --sort key: '%s'", tok); |
fb4f313d NK |
1720 | free(str); |
1721 | return -1; | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | free(str); | |
1726 | return 0; | |
1727 | } | |
1728 | ||
1729 | static int setup_page_sorting(struct list_head *sort_list, const char *arg) | |
1730 | { | |
1731 | char *tok; | |
1732 | char *str = strdup(arg); | |
1733 | char *pos = str; | |
1734 | ||
1735 | if (!str) { | |
1736 | pr_err("%s: strdup failed\n", __func__); | |
1737 | return -1; | |
1738 | } | |
1739 | ||
1740 | while (true) { | |
1741 | tok = strsep(&pos, ","); | |
1742 | if (!tok) | |
1743 | break; | |
1744 | if (page_sort_dimension__add(tok, sort_list) < 0) { | |
62d94b00 | 1745 | pr_err("Unknown page --sort key: '%s'", tok); |
1b22859d | 1746 | free(str); |
29b3e152 LZ |
1747 | return -1; |
1748 | } | |
1749 | } | |
1750 | ||
1751 | free(str); | |
1752 | return 0; | |
1753 | } | |
1754 | ||
1d037ca1 IT |
1755 | static int parse_sort_opt(const struct option *opt __maybe_unused, |
1756 | const char *arg, int unset __maybe_unused) | |
ba77c9e1 | 1757 | { |
ba77c9e1 LZ |
1758 | if (!arg) |
1759 | return -1; | |
1760 | ||
0c160d49 NK |
1761 | if (kmem_page > kmem_slab || |
1762 | (kmem_page == 0 && kmem_slab == 0 && kmem_default == KMEM_PAGE)) { | |
fb4f313d NK |
1763 | if (caller_flag > alloc_flag) |
1764 | return setup_page_sorting(&page_caller_sort, arg); | |
1765 | else | |
1766 | return setup_page_sorting(&page_alloc_sort, arg); | |
1767 | } else { | |
1768 | if (caller_flag > alloc_flag) | |
1769 | return setup_slab_sorting(&slab_caller_sort, arg); | |
1770 | else | |
1771 | return setup_slab_sorting(&slab_alloc_sort, arg); | |
1772 | } | |
ba77c9e1 LZ |
1773 | |
1774 | return 0; | |
1775 | } | |
1776 | ||
1d037ca1 IT |
1777 | static int parse_caller_opt(const struct option *opt __maybe_unused, |
1778 | const char *arg __maybe_unused, | |
1779 | int unset __maybe_unused) | |
ba77c9e1 | 1780 | { |
90b86a9f LZ |
1781 | caller_flag = (alloc_flag + 1); |
1782 | return 0; | |
1783 | } | |
ba77c9e1 | 1784 | |
1d037ca1 IT |
1785 | static int parse_alloc_opt(const struct option *opt __maybe_unused, |
1786 | const char *arg __maybe_unused, | |
1787 | int unset __maybe_unused) | |
90b86a9f LZ |
1788 | { |
1789 | alloc_flag = (caller_flag + 1); | |
ba77c9e1 LZ |
1790 | return 0; |
1791 | } | |
1792 | ||
0d68bc92 NK |
1793 | static int parse_slab_opt(const struct option *opt __maybe_unused, |
1794 | const char *arg __maybe_unused, | |
1795 | int unset __maybe_unused) | |
1796 | { | |
1797 | kmem_slab = (kmem_page + 1); | |
1798 | return 0; | |
1799 | } | |
1800 | ||
1801 | static int parse_page_opt(const struct option *opt __maybe_unused, | |
1802 | const char *arg __maybe_unused, | |
1803 | int unset __maybe_unused) | |
1804 | { | |
1805 | kmem_page = (kmem_slab + 1); | |
1806 | return 0; | |
1807 | } | |
1808 | ||
1d037ca1 IT |
1809 | static int parse_line_opt(const struct option *opt __maybe_unused, |
1810 | const char *arg, int unset __maybe_unused) | |
ba77c9e1 LZ |
1811 | { |
1812 | int lines; | |
1813 | ||
1814 | if (!arg) | |
1815 | return -1; | |
1816 | ||
1817 | lines = strtoul(arg, NULL, 10); | |
1818 | ||
1819 | if (caller_flag > alloc_flag) | |
1820 | caller_lines = lines; | |
1821 | else | |
1822 | alloc_lines = lines; | |
1823 | ||
1824 | return 0; | |
1825 | } | |
1826 | ||
0433ffbe ACM |
1827 | static int __cmd_record(int argc, const char **argv) |
1828 | { | |
1829 | const char * const record_args[] = { | |
4a4d371a | 1830 | "record", "-a", "-R", "-c", "1", |
0d68bc92 NK |
1831 | }; |
1832 | const char * const slab_events[] = { | |
ba77c9e1 LZ |
1833 | "-e", "kmem:kmalloc", |
1834 | "-e", "kmem:kmalloc_node", | |
1835 | "-e", "kmem:kfree", | |
1836 | "-e", "kmem:kmem_cache_alloc", | |
1837 | "-e", "kmem:kmem_cache_alloc_node", | |
1838 | "-e", "kmem:kmem_cache_free", | |
0433ffbe | 1839 | }; |
0d68bc92 NK |
1840 | const char * const page_events[] = { |
1841 | "-e", "kmem:mm_page_alloc", | |
1842 | "-e", "kmem:mm_page_free", | |
1843 | }; | |
ba77c9e1 LZ |
1844 | unsigned int rec_argc, i, j; |
1845 | const char **rec_argv; | |
1846 | ||
1847 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
0d68bc92 NK |
1848 | if (kmem_slab) |
1849 | rec_argc += ARRAY_SIZE(slab_events); | |
1850 | if (kmem_page) | |
c9758cc4 | 1851 | rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */ |
0d68bc92 | 1852 | |
ba77c9e1 LZ |
1853 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
1854 | ||
ce47dc56 CS |
1855 | if (rec_argv == NULL) |
1856 | return -ENOMEM; | |
1857 | ||
ba77c9e1 LZ |
1858 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
1859 | rec_argv[i] = strdup(record_args[i]); | |
1860 | ||
0d68bc92 NK |
1861 | if (kmem_slab) { |
1862 | for (j = 0; j < ARRAY_SIZE(slab_events); j++, i++) | |
1863 | rec_argv[i] = strdup(slab_events[j]); | |
1864 | } | |
1865 | if (kmem_page) { | |
c9758cc4 NK |
1866 | rec_argv[i++] = strdup("-g"); |
1867 | ||
0d68bc92 NK |
1868 | for (j = 0; j < ARRAY_SIZE(page_events); j++, i++) |
1869 | rec_argv[i] = strdup(page_events[j]); | |
1870 | } | |
1871 | ||
ba77c9e1 LZ |
1872 | for (j = 1; j < (unsigned int)argc; j++, i++) |
1873 | rec_argv[i] = argv[j]; | |
1874 | ||
b0ad8ea6 | 1875 | return cmd_record(i, rec_argv); |
ba77c9e1 LZ |
1876 | } |
1877 | ||
b8cbb349 | 1878 | static int kmem_config(const char *var, const char *value, void *cb __maybe_unused) |
0c160d49 NK |
1879 | { |
1880 | if (!strcmp(var, "kmem.default")) { | |
1881 | if (!strcmp(value, "slab")) | |
1882 | kmem_default = KMEM_SLAB; | |
1883 | else if (!strcmp(value, "page")) | |
1884 | kmem_default = KMEM_PAGE; | |
1885 | else | |
1886 | pr_err("invalid default value ('slab' or 'page' required): %s\n", | |
1887 | value); | |
1888 | return 0; | |
1889 | } | |
1890 | ||
b8cbb349 | 1891 | return 0; |
0c160d49 NK |
1892 | } |
1893 | ||
b0ad8ea6 | 1894 | int cmd_kmem(int argc, const char **argv) |
ba77c9e1 | 1895 | { |
fb4f313d NK |
1896 | const char * const default_slab_sort = "frag,hit,bytes"; |
1897 | const char * const default_page_sort = "bytes,hit"; | |
8ceb41d7 | 1898 | struct perf_data data = { |
d1eeb77c YS |
1899 | .mode = PERF_DATA_MODE_READ, |
1900 | }; | |
0433ffbe ACM |
1901 | const struct option kmem_options[] = { |
1902 | OPT_STRING('i', "input", &input_name, "file", "input file name"), | |
bd72a33e NK |
1903 | OPT_INCR('v', "verbose", &verbose, |
1904 | "be more verbose (show symbol address, etc)"), | |
0433ffbe ACM |
1905 | OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL, |
1906 | "show per-callsite statistics", parse_caller_opt), | |
1907 | OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL, | |
1908 | "show per-allocation statistics", parse_alloc_opt), | |
1909 | OPT_CALLBACK('s', "sort", NULL, "key[,key2...]", | |
fb4f313d NK |
1910 | "sort by keys: ptr, callsite, bytes, hit, pingpong, frag, " |
1911 | "page, order, migtype, gfp", parse_sort_opt), | |
0433ffbe ACM |
1912 | OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt), |
1913 | OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"), | |
8ceb41d7 | 1914 | OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"), |
0d68bc92 NK |
1915 | OPT_CALLBACK_NOOPT(0, "slab", NULL, NULL, "Analyze slab allocator", |
1916 | parse_slab_opt), | |
1917 | OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator", | |
1918 | parse_page_opt), | |
2a7ef02c | 1919 | OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"), |
2a865bd8 DA |
1920 | OPT_STRING(0, "time", &time_str, "str", |
1921 | "Time span of interest (start,stop)"), | |
0433ffbe ACM |
1922 | OPT_END() |
1923 | }; | |
3bca2354 RR |
1924 | const char *const kmem_subcommands[] = { "record", "stat", NULL }; |
1925 | const char *kmem_usage[] = { | |
1926 | NULL, | |
0433ffbe ACM |
1927 | NULL |
1928 | }; | |
2b2b2c68 | 1929 | struct perf_session *session; |
49b8e2be | 1930 | static const char errmsg[] = "No %s allocation events found. Have you run 'perf kmem record --%s'?\n"; |
ecc4c561 ACM |
1931 | int ret = perf_config(kmem_config, NULL); |
1932 | ||
1933 | if (ret) | |
1934 | return ret; | |
2b2b2c68 | 1935 | |
3bca2354 RR |
1936 | argc = parse_options_subcommand(argc, argv, kmem_options, |
1937 | kmem_subcommands, kmem_usage, 0); | |
ba77c9e1 | 1938 | |
90b86a9f | 1939 | if (!argc) |
ba77c9e1 LZ |
1940 | usage_with_options(kmem_usage, kmem_options); |
1941 | ||
0c160d49 NK |
1942 | if (kmem_slab == 0 && kmem_page == 0) { |
1943 | if (kmem_default == KMEM_SLAB) | |
1944 | kmem_slab = 1; | |
1945 | else | |
1946 | kmem_page = 1; | |
1947 | } | |
0d68bc92 | 1948 | |
90b86a9f | 1949 | if (!strncmp(argv[0], "rec", 3)) { |
0a7e6d1b | 1950 | symbol__init(NULL); |
90b86a9f | 1951 | return __cmd_record(argc, argv); |
2b2b2c68 NK |
1952 | } |
1953 | ||
2d4f2799 | 1954 | data.path = input_name; |
28939e1a | 1955 | |
8ceb41d7 | 1956 | kmem_session = session = perf_session__new(&data, false, &perf_kmem); |
2b2b2c68 | 1957 | if (session == NULL) |
52e02834 | 1958 | return -1; |
2b2b2c68 | 1959 | |
ecc4c561 ACM |
1960 | ret = -1; |
1961 | ||
a923e2c4 NK |
1962 | if (kmem_slab) { |
1963 | if (!perf_evlist__find_tracepoint_by_name(session->evlist, | |
1964 | "kmem:kmalloc")) { | |
1965 | pr_err(errmsg, "slab", "slab"); | |
249ca1a8 | 1966 | goto out_delete; |
a923e2c4 NK |
1967 | } |
1968 | } | |
1969 | ||
0d68bc92 | 1970 | if (kmem_page) { |
a923e2c4 | 1971 | struct perf_evsel *evsel; |
0d68bc92 | 1972 | |
a923e2c4 NK |
1973 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
1974 | "kmem:mm_page_alloc"); | |
1975 | if (evsel == NULL) { | |
1976 | pr_err(errmsg, "page", "page"); | |
249ca1a8 | 1977 | goto out_delete; |
0d68bc92 NK |
1978 | } |
1979 | ||
69769ce1 | 1980 | kmem_page_size = tep_get_page_size(evsel->tp_format->tep); |
c9758cc4 | 1981 | symbol_conf.use_callchain = true; |
0d68bc92 NK |
1982 | } |
1983 | ||
0a7e6d1b | 1984 | symbol__init(&session->header.env); |
2b2b2c68 | 1985 | |
2a865bd8 DA |
1986 | if (perf_time__parse_str(&ptime, time_str) != 0) { |
1987 | pr_err("Invalid time string\n"); | |
79f56ebe CJ |
1988 | ret = -EINVAL; |
1989 | goto out_delete; | |
2a865bd8 DA |
1990 | } |
1991 | ||
2b2b2c68 | 1992 | if (!strcmp(argv[0], "stat")) { |
77cfe388 NK |
1993 | setlocale(LC_ALL, ""); |
1994 | ||
4b627957 | 1995 | if (cpu__setup_cpunode_map()) |
2b2b2c68 | 1996 | goto out_delete; |
90b86a9f | 1997 | |
fb4f313d NK |
1998 | if (list_empty(&slab_caller_sort)) |
1999 | setup_slab_sorting(&slab_caller_sort, default_slab_sort); | |
2000 | if (list_empty(&slab_alloc_sort)) | |
2001 | setup_slab_sorting(&slab_alloc_sort, default_slab_sort); | |
2002 | if (list_empty(&page_caller_sort)) | |
2003 | setup_page_sorting(&page_caller_sort, default_page_sort); | |
2004 | if (list_empty(&page_alloc_sort)) | |
2005 | setup_page_sorting(&page_alloc_sort, default_page_sort); | |
2006 | ||
2007 | if (kmem_page) { | |
2008 | setup_page_sorting(&page_alloc_sort_input, | |
2009 | "page,order,migtype,gfp"); | |
2010 | setup_page_sorting(&page_caller_sort_input, | |
2011 | "callsite,order,migtype,gfp"); | |
2012 | } | |
2b2b2c68 | 2013 | ret = __cmd_kmem(session); |
b00eca8c PE |
2014 | } else |
2015 | usage_with_options(kmem_usage, kmem_options); | |
7d0d3945 | 2016 | |
2b2b2c68 NK |
2017 | out_delete: |
2018 | perf_session__delete(session); | |
2019 | ||
2020 | return ret; | |
ba77c9e1 LZ |
2021 | } |
2022 |