Commit | Line | Data |
---|---|---|
ba77c9e1 LZ |
1 | #include "builtin.h" |
2 | #include "perf.h" | |
3 | ||
0f7d2f1b | 4 | #include "util/evlist.h" |
fcf65bf1 | 5 | #include "util/evsel.h" |
ba77c9e1 LZ |
6 | #include "util/util.h" |
7 | #include "util/cache.h" | |
8 | #include "util/symbol.h" | |
9 | #include "util/thread.h" | |
10 | #include "util/header.h" | |
94c744b6 | 11 | #include "util/session.h" |
45694aa7 | 12 | #include "util/tool.h" |
c9758cc4 | 13 | #include "util/callchain.h" |
ba77c9e1 LZ |
14 | |
15 | #include "util/parse-options.h" | |
16 | #include "util/trace-event.h" | |
f5fc1412 | 17 | #include "util/data.h" |
4b627957 | 18 | #include "util/cpumap.h" |
ba77c9e1 LZ |
19 | |
20 | #include "util/debug.h" | |
ba77c9e1 LZ |
21 | |
22 | #include <linux/rbtree.h> | |
8d9233f2 | 23 | #include <linux/string.h> |
77cfe388 | 24 | #include <locale.h> |
c9758cc4 | 25 | #include <regex.h> |
ba77c9e1 | 26 | |
0d68bc92 NK |
27 | static int kmem_slab; |
28 | static int kmem_page; | |
29 | ||
30 | static long kmem_page_size; | |
31 | ||
ba77c9e1 LZ |
32 | struct alloc_stat; |
33 | typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *); | |
34 | ||
ba77c9e1 LZ |
35 | static int alloc_flag; |
36 | static int caller_flag; | |
37 | ||
ba77c9e1 LZ |
38 | static int alloc_lines = -1; |
39 | static int caller_lines = -1; | |
40 | ||
7707b6b6 LZ |
41 | static bool raw_ip; |
42 | ||
ba77c9e1 | 43 | struct alloc_stat { |
079d3f65 LZ |
44 | u64 call_site; |
45 | u64 ptr; | |
ba77c9e1 LZ |
46 | u64 bytes_req; |
47 | u64 bytes_alloc; | |
48 | u32 hit; | |
079d3f65 LZ |
49 | u32 pingpong; |
50 | ||
51 | short alloc_cpu; | |
ba77c9e1 LZ |
52 | |
53 | struct rb_node node; | |
54 | }; | |
55 | ||
56 | static struct rb_root root_alloc_stat; | |
57 | static struct rb_root root_alloc_sorted; | |
58 | static struct rb_root root_caller_stat; | |
59 | static struct rb_root root_caller_sorted; | |
60 | ||
61 | static unsigned long total_requested, total_allocated; | |
7d0d3945 | 62 | static unsigned long nr_allocs, nr_cross_allocs; |
ba77c9e1 | 63 | |
2814eb05 ACM |
64 | static int insert_alloc_stat(unsigned long call_site, unsigned long ptr, |
65 | int bytes_req, int bytes_alloc, int cpu) | |
ba77c9e1 LZ |
66 | { |
67 | struct rb_node **node = &root_alloc_stat.rb_node; | |
68 | struct rb_node *parent = NULL; | |
69 | struct alloc_stat *data = NULL; | |
70 | ||
ba77c9e1 LZ |
71 | 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; | |
4efb5290 | 86 | data->bytes_alloc += bytes_alloc; |
ba77c9e1 LZ |
87 | } else { |
88 | data = malloc(sizeof(*data)); | |
2814eb05 ACM |
89 | if (!data) { |
90 | pr_err("%s: malloc failed\n", __func__); | |
91 | return -1; | |
92 | } | |
ba77c9e1 | 93 | data->ptr = ptr; |
079d3f65 | 94 | data->pingpong = 0; |
ba77c9e1 LZ |
95 | 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 | } | |
079d3f65 LZ |
102 | data->call_site = call_site; |
103 | data->alloc_cpu = cpu; | |
2814eb05 | 104 | return 0; |
ba77c9e1 LZ |
105 | } |
106 | ||
2814eb05 | 107 | static int insert_caller_stat(unsigned long call_site, |
ba77c9e1 LZ |
108 | 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 | ||
ba77c9e1 LZ |
114 | 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; | |
4efb5290 | 129 | data->bytes_alloc += bytes_alloc; |
ba77c9e1 LZ |
130 | } else { |
131 | data = malloc(sizeof(*data)); | |
2814eb05 ACM |
132 | if (!data) { |
133 | pr_err("%s: malloc failed\n", __func__); | |
134 | return -1; | |
135 | } | |
ba77c9e1 | 136 | data->call_site = call_site; |
079d3f65 | 137 | data->pingpong = 0; |
ba77c9e1 LZ |
138 | 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 | } | |
2814eb05 ACM |
145 | |
146 | return 0; | |
ba77c9e1 LZ |
147 | } |
148 | ||
2814eb05 | 149 | static int perf_evsel__process_alloc_event(struct perf_evsel *evsel, |
0f7d2f1b | 150 | struct perf_sample *sample) |
ba77c9e1 | 151 | { |
0f7d2f1b ACM |
152 | 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"); | |
156 | ||
157 | if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) || | |
2814eb05 ACM |
158 | insert_caller_stat(call_site, bytes_req, bytes_alloc)) |
159 | return -1; | |
ba77c9e1 LZ |
160 | |
161 | total_requested += bytes_req; | |
162 | total_allocated += bytes_alloc; | |
7d0d3945 | 163 | |
0f7d2f1b ACM |
164 | nr_allocs++; |
165 | return 0; | |
166 | } | |
167 | ||
168 | static 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) { | |
4b627957 | 174 | int node1 = cpu__get_node(sample->cpu), |
0f7d2f1b ACM |
175 | node2 = perf_evsel__intval(evsel, sample, "node"); |
176 | ||
7d0d3945 LZ |
177 | if (node1 != node2) |
178 | nr_cross_allocs++; | |
179 | } | |
0f7d2f1b ACM |
180 | |
181 | return ret; | |
ba77c9e1 LZ |
182 | } |
183 | ||
079d3f65 LZ |
184 | static int ptr_cmp(struct alloc_stat *, struct alloc_stat *); |
185 | static int callsite_cmp(struct alloc_stat *, struct alloc_stat *); | |
186 | ||
187 | static 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 | ||
2814eb05 ACM |
212 | static int perf_evsel__process_free_event(struct perf_evsel *evsel, |
213 | struct perf_sample *sample) | |
ba77c9e1 | 214 | { |
0f7d2f1b | 215 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"); |
079d3f65 LZ |
216 | struct alloc_stat *s_alloc, *s_caller; |
217 | ||
079d3f65 LZ |
218 | s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp); |
219 | if (!s_alloc) | |
2814eb05 | 220 | return 0; |
079d3f65 | 221 | |
22ad798c | 222 | if ((short)sample->cpu != s_alloc->alloc_cpu) { |
079d3f65 LZ |
223 | s_alloc->pingpong++; |
224 | ||
225 | s_caller = search_alloc_stat(0, s_alloc->call_site, | |
226 | &root_caller_stat, callsite_cmp); | |
2814eb05 ACM |
227 | if (!s_caller) |
228 | return -1; | |
079d3f65 LZ |
229 | s_caller->pingpong++; |
230 | } | |
231 | s_alloc->alloc_cpu = -1; | |
2814eb05 ACM |
232 | |
233 | return 0; | |
ba77c9e1 LZ |
234 | } |
235 | ||
0d68bc92 NK |
236 | static u64 total_page_alloc_bytes; |
237 | static u64 total_page_free_bytes; | |
238 | static u64 total_page_nomatch_bytes; | |
239 | static u64 total_page_fail_bytes; | |
240 | static unsigned long nr_page_allocs; | |
241 | static unsigned long nr_page_frees; | |
242 | static unsigned long nr_page_fails; | |
243 | static unsigned long nr_page_nomatch; | |
244 | ||
245 | static bool use_pfn; | |
c9758cc4 | 246 | static struct perf_session *kmem_session; |
0d68bc92 NK |
247 | |
248 | #define MAX_MIGRATE_TYPES 6 | |
249 | #define MAX_PAGE_ORDER 11 | |
250 | ||
251 | static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES]; | |
252 | ||
253 | struct page_stat { | |
254 | struct rb_node node; | |
255 | u64 page; | |
c9758cc4 | 256 | u64 callsite; |
0d68bc92 NK |
257 | 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 | ||
266 | static struct rb_root page_tree; | |
267 | static struct rb_root page_alloc_tree; | |
268 | static struct rb_root page_alloc_sorted; | |
c9758cc4 NK |
269 | static struct rb_root page_caller_tree; |
270 | static struct rb_root page_caller_sorted; | |
0d68bc92 | 271 | |
c9758cc4 NK |
272 | struct alloc_func { |
273 | u64 start; | |
274 | u64 end; | |
275 | char *name; | |
276 | }; | |
277 | ||
278 | static int nr_alloc_funcs; | |
279 | static struct alloc_func *alloc_func_list; | |
280 | ||
281 | static 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 | ||
292 | static 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 | ||
306 | static 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 | */ | |
360 | static 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 | ||
400 | out: | |
401 | pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip); | |
402 | return sample->ip; | |
403 | } | |
404 | ||
405 | static struct page_stat * | |
406 | __page_stat__findnew_page(u64 page, bool create) | |
0d68bc92 NK |
407 | { |
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 | ||
c9758cc4 NK |
441 | static struct page_stat *page_stat__find_page(u64 page) |
442 | { | |
443 | return __page_stat__findnew_page(page, false); | |
444 | } | |
445 | ||
446 | static struct page_stat *page_stat__findnew_page(u64 page) | |
447 | { | |
448 | return __page_stat__findnew_page(page, true); | |
449 | } | |
450 | ||
0d68bc92 NK |
451 | static 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 | ||
c9758cc4 NK |
472 | static struct page_stat * |
473 | __page_stat__findnew_alloc(struct page_stat *pstat, bool create) | |
0d68bc92 NK |
474 | { |
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 | ||
6b1a2752 | 485 | cmp = page_stat_cmp(data, pstat); |
0d68bc92 NK |
486 | 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) { | |
6b1a2752 DA |
499 | data->page = pstat->page; |
500 | data->order = pstat->order; | |
501 | data->gfp_flags = pstat->gfp_flags; | |
502 | data->migrate_type = pstat->migrate_type; | |
0d68bc92 NK |
503 | |
504 | rb_link_node(&data->node, parent, node); | |
505 | rb_insert_color(&data->node, &page_alloc_tree); | |
506 | } | |
507 | ||
508 | return data; | |
509 | } | |
510 | ||
c9758cc4 NK |
511 | static struct page_stat *page_stat__find_alloc(struct page_stat *pstat) |
512 | { | |
513 | return __page_stat__findnew_alloc(pstat, false); | |
514 | } | |
515 | ||
516 | static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat) | |
517 | { | |
518 | return __page_stat__findnew_alloc(pstat, true); | |
519 | } | |
520 | ||
521 | static 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 | ||
557 | static struct page_stat *page_stat__find_caller(u64 callsite) | |
558 | { | |
559 | return __page_stat__findnew_caller(callsite, false); | |
560 | } | |
561 | ||
562 | static struct page_stat *page_stat__findnew_caller(u64 callsite) | |
563 | { | |
564 | return __page_stat__findnew_caller(callsite, true); | |
565 | } | |
566 | ||
0d68bc92 NK |
567 | static 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 | ||
576 | static 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; | |
c9758cc4 | 585 | u64 callsite; |
6b1a2752 | 586 | struct page_stat *pstat; |
0d68bc92 NK |
587 | 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 | ||
c9758cc4 NK |
608 | callsite = find_callsite(evsel, sample); |
609 | ||
0d68bc92 NK |
610 | /* |
611 | * This is to find the current page (with correct gfp flags and | |
612 | * migrate type) at free event. | |
613 | */ | |
c9758cc4 | 614 | pstat = page_stat__findnew_page(page); |
6b1a2752 | 615 | if (pstat == NULL) |
0d68bc92 NK |
616 | return -ENOMEM; |
617 | ||
6b1a2752 DA |
618 | pstat->order = order; |
619 | pstat->gfp_flags = gfp_flags; | |
620 | pstat->migrate_type = migrate_type; | |
c9758cc4 | 621 | pstat->callsite = callsite; |
0d68bc92 NK |
622 | |
623 | this.page = page; | |
c9758cc4 | 624 | pstat = page_stat__findnew_alloc(&this); |
6b1a2752 | 625 | if (pstat == NULL) |
0d68bc92 NK |
626 | return -ENOMEM; |
627 | ||
c9758cc4 NK |
628 | pstat->nr_alloc++; |
629 | pstat->alloc_bytes += bytes; | |
630 | 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 | ||
6b1a2752 DA |
640 | pstat->nr_alloc++; |
641 | pstat->alloc_bytes += bytes; | |
0d68bc92 NK |
642 | |
643 | order_stats[order][migrate_type]++; | |
644 | ||
645 | return 0; | |
646 | } | |
647 | ||
648 | static 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; | |
6b1a2752 | 654 | struct page_stat *pstat; |
0d68bc92 NK |
655 | 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 | ||
c9758cc4 | 667 | pstat = page_stat__find_page(page); |
6b1a2752 | 668 | if (pstat == NULL) { |
0d68bc92 NK |
669 | 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; | |
6b1a2752 DA |
679 | this.gfp_flags = pstat->gfp_flags; |
680 | this.migrate_type = pstat->migrate_type; | |
c9758cc4 | 681 | this.callsite = pstat->callsite; |
0d68bc92 | 682 | |
6b1a2752 DA |
683 | rb_erase(&pstat->node, &page_tree); |
684 | free(pstat); | |
0d68bc92 | 685 | |
c9758cc4 NK |
686 | 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); | |
6b1a2752 | 694 | if (pstat == NULL) |
0d68bc92 NK |
695 | return -ENOENT; |
696 | ||
6b1a2752 DA |
697 | pstat->nr_free++; |
698 | pstat->free_bytes += bytes; | |
0d68bc92 NK |
699 | |
700 | return 0; | |
701 | } | |
702 | ||
0f7d2f1b ACM |
703 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
704 | struct perf_sample *sample); | |
ba77c9e1 | 705 | |
1d037ca1 | 706 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 707 | union perf_event *event, |
8115d60c | 708 | struct perf_sample *sample, |
fcf65bf1 | 709 | struct perf_evsel *evsel, |
743eb868 | 710 | struct machine *machine) |
ba77c9e1 | 711 | { |
ef89325f | 712 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
13ce34df | 713 | sample->tid); |
ba77c9e1 | 714 | |
ba77c9e1 LZ |
715 | if (thread == NULL) { |
716 | pr_debug("problem processing %d event, skipping it.\n", | |
717 | event->header.type); | |
718 | return -1; | |
719 | } | |
720 | ||
b9c5143a | 721 | dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); |
ba77c9e1 | 722 | |
744a9719 ACM |
723 | if (evsel->handler != NULL) { |
724 | tracepoint_handler f = evsel->handler; | |
0f7d2f1b ACM |
725 | return f(evsel, sample); |
726 | } | |
727 | ||
728 | return 0; | |
ba77c9e1 LZ |
729 | } |
730 | ||
fcf65bf1 ACM |
731 | static struct perf_tool perf_kmem = { |
732 | .sample = process_sample_event, | |
733 | .comm = perf_event__process_comm, | |
64c40908 NK |
734 | .mmap = perf_event__process_mmap, |
735 | .mmap2 = perf_event__process_mmap2, | |
0a8cb85c | 736 | .ordered_events = true, |
ba77c9e1 LZ |
737 | }; |
738 | ||
ba77c9e1 LZ |
739 | static 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 | ||
0d68bc92 NK |
747 | static void __print_slab_result(struct rb_root *root, |
748 | struct perf_session *session, | |
749 | int n_lines, int is_caller) | |
ba77c9e1 LZ |
750 | { |
751 | struct rb_node *next; | |
34ba5122 | 752 | struct machine *machine = &session->machines.host; |
ba77c9e1 | 753 | |
65f46e02 | 754 | printf("%.105s\n", graph_dotted_line); |
079d3f65 | 755 | printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); |
47103277 | 756 | printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); |
65f46e02 | 757 | printf("%.105s\n", graph_dotted_line); |
ba77c9e1 LZ |
758 | |
759 | next = rb_first(root); | |
760 | ||
761 | while (next && n_lines--) { | |
1b145ae5 ACM |
762 | struct alloc_stat *data = rb_entry(next, struct alloc_stat, |
763 | node); | |
764 | struct symbol *sym = NULL; | |
71cf8b8f | 765 | struct map *map; |
079d3f65 | 766 | char buf[BUFSIZ]; |
1b145ae5 ACM |
767 | u64 addr; |
768 | ||
769 | if (is_caller) { | |
770 | addr = data->call_site; | |
7707b6b6 | 771 | if (!raw_ip) |
5c0541d5 | 772 | sym = machine__find_kernel_function(machine, addr, &map, NULL); |
1b145ae5 ACM |
773 | } else |
774 | addr = data->ptr; | |
775 | ||
776 | if (sym != NULL) | |
9486aa38 | 777 | snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name, |
71cf8b8f | 778 | addr - map->unmap_ip(map, sym->start)); |
1b145ae5 | 779 | else |
9486aa38 | 780 | snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr); |
079d3f65 | 781 | printf(" %-34s |", buf); |
ba77c9e1 | 782 | |
65f46e02 | 783 | printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n", |
079d3f65 | 784 | (unsigned long long)data->bytes_alloc, |
ba77c9e1 LZ |
785 | (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, | |
079d3f65 | 789 | (unsigned long)data->pingpong, |
ba77c9e1 LZ |
790 | fragmentation(data->bytes_req, data->bytes_alloc)); |
791 | ||
792 | next = rb_next(next); | |
793 | } | |
794 | ||
795 | if (n_lines == -1) | |
65f46e02 | 796 | printf(" ... | ... | ... | ... | ... | ... \n"); |
ba77c9e1 | 797 | |
65f46e02 | 798 | printf("%.105s\n", graph_dotted_line); |
ba77c9e1 LZ |
799 | } |
800 | ||
0d68bc92 NK |
801 | static const char * const migrate_type_str[] = { |
802 | "UNMOVABL", | |
803 | "RECLAIM", | |
804 | "MOVABLE", | |
805 | "RESERVED", | |
806 | "CMA/ISLT", | |
807 | "UNKNOWN", | |
808 | }; | |
809 | ||
c9758cc4 | 810 | static void __print_page_alloc_result(struct perf_session *session, int n_lines) |
0d68bc92 | 811 | { |
c9758cc4 NK |
812 | struct rb_node *next = rb_first(&page_alloc_sorted); |
813 | struct machine *machine = &session->machines.host; | |
0d68bc92 NK |
814 | const char *format; |
815 | ||
c9758cc4 NK |
816 | printf("\n%.105s\n", graph_dotted_line); |
817 | printf(" %-16s | Total alloc (KB) | Hits | Order | Mig.type | GFP flags | Callsite\n", | |
0d68bc92 | 818 | use_pfn ? "PFN" : "Page"); |
c9758cc4 | 819 | printf("%.105s\n", graph_dotted_line); |
0d68bc92 NK |
820 | |
821 | if (use_pfn) | |
c9758cc4 | 822 | format = " %16llu | %'16llu | %'9d | %5d | %8s | %08lx | %s\n"; |
0d68bc92 | 823 | else |
c9758cc4 | 824 | format = " %016llx | %'16llu | %'9d | %5d | %8s | %08lx | %s\n"; |
0d68bc92 NK |
825 | |
826 | while (next && n_lines--) { | |
827 | struct page_stat *data; | |
c9758cc4 NK |
828 | struct symbol *sym; |
829 | struct map *map; | |
830 | char buf[32]; | |
831 | char *caller = buf; | |
0d68bc92 NK |
832 | |
833 | data = rb_entry(next, struct page_stat, node); | |
c9758cc4 NK |
834 | 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); | |
0d68bc92 NK |
840 | |
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], | |
c9758cc4 | 845 | (unsigned long)data->gfp_flags, caller); |
0d68bc92 NK |
846 | |
847 | next = rb_next(next); | |
848 | } | |
849 | ||
850 | if (n_lines == -1) | |
c9758cc4 | 851 | printf(" ... | ... | ... | ... | ... | ... | ...\n"); |
0d68bc92 | 852 | |
c9758cc4 NK |
853 | printf("%.105s\n", graph_dotted_line); |
854 | } | |
855 | ||
856 | static 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); | |
0d68bc92 NK |
893 | } |
894 | ||
895 | static void print_slab_summary(void) | |
ba77c9e1 | 896 | { |
0d68bc92 NK |
897 | printf("\nSUMMARY (SLAB allocator)"); |
898 | printf("\n========================\n"); | |
77cfe388 NK |
899 | 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", | |
ba77c9e1 LZ |
902 | total_allocated - total_requested); |
903 | printf("Internal fragmentation: %f%%\n", | |
904 | fragmentation(total_requested, total_allocated)); | |
77cfe388 | 905 | printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs); |
ba77c9e1 LZ |
906 | } |
907 | ||
0d68bc92 NK |
908 | static 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 | ||
953 | static void print_slab_result(struct perf_session *session) | |
ba77c9e1 LZ |
954 | { |
955 | if (caller_flag) | |
0d68bc92 NK |
956 | __print_slab_result(&root_caller_sorted, session, caller_lines, 1); |
957 | if (alloc_flag) | |
958 | __print_slab_result(&root_alloc_sorted, session, alloc_lines, 0); | |
959 | print_slab_summary(); | |
960 | } | |
961 | ||
962 | static void print_page_result(struct perf_session *session) | |
963 | { | |
c9758cc4 NK |
964 | if (caller_flag) |
965 | __print_page_caller_result(session, caller_lines); | |
ba77c9e1 | 966 | if (alloc_flag) |
c9758cc4 | 967 | __print_page_alloc_result(session, alloc_lines); |
0d68bc92 NK |
968 | print_page_summary(); |
969 | } | |
970 | ||
971 | static 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); | |
ba77c9e1 LZ |
977 | } |
978 | ||
29b3e152 LZ |
979 | struct sort_dimension { |
980 | const char name[20]; | |
981 | sort_fn_t cmp; | |
982 | struct list_head list; | |
983 | }; | |
984 | ||
985 | static LIST_HEAD(caller_sort); | |
986 | static LIST_HEAD(alloc_sort); | |
987 | ||
0d68bc92 NK |
988 | static void sort_slab_insert(struct rb_root *root, struct alloc_stat *data, |
989 | struct list_head *sort_list) | |
ba77c9e1 LZ |
990 | { |
991 | struct rb_node **new = &(root->rb_node); | |
992 | struct rb_node *parent = NULL; | |
29b3e152 | 993 | struct sort_dimension *sort; |
ba77c9e1 LZ |
994 | |
995 | while (*new) { | |
996 | struct alloc_stat *this; | |
29b3e152 | 997 | int cmp = 0; |
ba77c9e1 LZ |
998 | |
999 | this = rb_entry(*new, struct alloc_stat, node); | |
1000 | parent = *new; | |
1001 | ||
29b3e152 LZ |
1002 | list_for_each_entry(sort, sort_list, list) { |
1003 | cmp = sort->cmp(data, this); | |
1004 | if (cmp) | |
1005 | break; | |
1006 | } | |
ba77c9e1 LZ |
1007 | |
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 | ||
0d68bc92 NK |
1018 | static void __sort_slab_result(struct rb_root *root, struct rb_root *root_sorted, |
1019 | struct list_head *sort_list) | |
ba77c9e1 LZ |
1020 | { |
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); | |
0d68bc92 NK |
1031 | sort_slab_insert(root_sorted, data, sort_list); |
1032 | } | |
1033 | } | |
1034 | ||
1035 | static 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 | ||
1060 | static 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); | |
ba77c9e1 LZ |
1073 | } |
1074 | } | |
1075 | ||
1076 | static void sort_result(void) | |
1077 | { | |
0d68bc92 NK |
1078 | 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); | |
c9758cc4 | 1086 | __sort_page_result(&page_caller_tree, &page_caller_sorted); |
0d68bc92 | 1087 | } |
ba77c9e1 LZ |
1088 | } |
1089 | ||
2b2b2c68 | 1090 | static int __cmd_kmem(struct perf_session *session) |
ba77c9e1 | 1091 | { |
d549c769 | 1092 | int err = -EINVAL; |
0d68bc92 | 1093 | struct perf_evsel *evsel; |
0f7d2f1b | 1094 | const struct perf_evsel_str_handler kmem_tracepoints[] = { |
0d68bc92 | 1095 | /* slab allocator */ |
0f7d2f1b ACM |
1096 | { "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, }, | |
0d68bc92 NK |
1102 | /* page allocator */ |
1103 | { "kmem:mm_page_alloc", perf_evsel__process_page_alloc_event, }, | |
1104 | { "kmem:mm_page_free", perf_evsel__process_page_free_event, }, | |
0f7d2f1b | 1105 | }; |
4aa65636 | 1106 | |
d549c769 | 1107 | if (!perf_session__has_traces(session, "kmem record")) |
2b2b2c68 | 1108 | goto out; |
d549c769 | 1109 | |
0f7d2f1b ACM |
1110 | if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) { |
1111 | pr_err("Initializing perf session tracepoint handlers failed\n"); | |
2b2b2c68 | 1112 | goto out; |
0f7d2f1b ACM |
1113 | } |
1114 | ||
0d68bc92 NK |
1115 | 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 | ||
ba77c9e1 | 1123 | setup_pager(); |
b7b61cbe | 1124 | err = perf_session__process_events(session); |
0d68bc92 NK |
1125 | if (err != 0) { |
1126 | pr_err("error during process events: %d\n", err); | |
2b2b2c68 | 1127 | goto out; |
0d68bc92 | 1128 | } |
ba77c9e1 | 1129 | sort_result(); |
4aa65636 | 1130 | print_result(session); |
2b2b2c68 | 1131 | out: |
4aa65636 | 1132 | return err; |
ba77c9e1 LZ |
1133 | } |
1134 | ||
ba77c9e1 LZ |
1135 | static 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 | ||
29b3e152 LZ |
1144 | static struct sort_dimension ptr_sort_dimension = { |
1145 | .name = "ptr", | |
1146 | .cmp = ptr_cmp, | |
1147 | }; | |
1148 | ||
ba77c9e1 LZ |
1149 | static 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 | ||
29b3e152 LZ |
1158 | static struct sort_dimension callsite_sort_dimension = { |
1159 | .name = "callsite", | |
1160 | .cmp = callsite_cmp, | |
1161 | }; | |
1162 | ||
f3ced7cd PE |
1163 | static 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 | ||
29b3e152 LZ |
1172 | static struct sort_dimension hit_sort_dimension = { |
1173 | .name = "hit", | |
1174 | .cmp = hit_cmp, | |
1175 | }; | |
1176 | ||
ba77c9e1 LZ |
1177 | static 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 | ||
29b3e152 LZ |
1186 | static struct sort_dimension bytes_sort_dimension = { |
1187 | .name = "bytes", | |
1188 | .cmp = bytes_cmp, | |
1189 | }; | |
1190 | ||
f3ced7cd PE |
1191 | static 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 | ||
29b3e152 LZ |
1205 | static struct sort_dimension frag_sort_dimension = { |
1206 | .name = "frag", | |
1207 | .cmp = frag_cmp, | |
1208 | }; | |
1209 | ||
079d3f65 LZ |
1210 | static 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 | ||
1219 | static struct sort_dimension pingpong_sort_dimension = { | |
1220 | .name = "pingpong", | |
1221 | .cmp = pingpong_cmp, | |
1222 | }; | |
1223 | ||
29b3e152 LZ |
1224 | static 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, | |
079d3f65 | 1230 | &pingpong_sort_dimension, |
29b3e152 LZ |
1231 | }; |
1232 | ||
49e4ba54 | 1233 | #define NUM_AVAIL_SORTS ((int)ARRAY_SIZE(avail_sorts)) |
29b3e152 LZ |
1234 | |
1235 | static 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)) { | |
8d9233f2 | 1242 | sort = memdup(avail_sorts[i], sizeof(*avail_sorts[i])); |
2814eb05 | 1243 | if (!sort) { |
8d9233f2 | 1244 | pr_err("%s: memdup failed\n", __func__); |
2814eb05 ACM |
1245 | return -1; |
1246 | } | |
29b3e152 LZ |
1247 | list_add_tail(&sort->list, list); |
1248 | return 0; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | return -1; | |
1253 | } | |
1254 | ||
1255 | static int setup_sorting(struct list_head *sort_list, const char *arg) | |
1256 | { | |
1257 | char *tok; | |
1258 | char *str = strdup(arg); | |
405f8755 | 1259 | char *pos = str; |
29b3e152 | 1260 | |
2814eb05 ACM |
1261 | if (!str) { |
1262 | pr_err("%s: strdup failed\n", __func__); | |
1263 | return -1; | |
1264 | } | |
29b3e152 LZ |
1265 | |
1266 | while (true) { | |
405f8755 | 1267 | tok = strsep(&pos, ","); |
29b3e152 LZ |
1268 | if (!tok) |
1269 | break; | |
1270 | if (sort_dimension__add(tok, sort_list) < 0) { | |
1271 | error("Unknown --sort key: '%s'", tok); | |
1b22859d | 1272 | free(str); |
29b3e152 LZ |
1273 | return -1; |
1274 | } | |
1275 | } | |
1276 | ||
1277 | free(str); | |
1278 | return 0; | |
1279 | } | |
1280 | ||
1d037ca1 IT |
1281 | static int parse_sort_opt(const struct option *opt __maybe_unused, |
1282 | const char *arg, int unset __maybe_unused) | |
ba77c9e1 | 1283 | { |
ba77c9e1 LZ |
1284 | if (!arg) |
1285 | return -1; | |
1286 | ||
ba77c9e1 | 1287 | if (caller_flag > alloc_flag) |
29b3e152 | 1288 | return setup_sorting(&caller_sort, arg); |
ba77c9e1 | 1289 | else |
29b3e152 | 1290 | return setup_sorting(&alloc_sort, arg); |
ba77c9e1 LZ |
1291 | |
1292 | return 0; | |
1293 | } | |
1294 | ||
1d037ca1 IT |
1295 | static int parse_caller_opt(const struct option *opt __maybe_unused, |
1296 | const char *arg __maybe_unused, | |
1297 | int unset __maybe_unused) | |
ba77c9e1 | 1298 | { |
90b86a9f LZ |
1299 | caller_flag = (alloc_flag + 1); |
1300 | return 0; | |
1301 | } | |
ba77c9e1 | 1302 | |
1d037ca1 IT |
1303 | static int parse_alloc_opt(const struct option *opt __maybe_unused, |
1304 | const char *arg __maybe_unused, | |
1305 | int unset __maybe_unused) | |
90b86a9f LZ |
1306 | { |
1307 | alloc_flag = (caller_flag + 1); | |
ba77c9e1 LZ |
1308 | return 0; |
1309 | } | |
1310 | ||
0d68bc92 NK |
1311 | static 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 | ||
1319 | static 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 | ||
1d037ca1 IT |
1327 | static int parse_line_opt(const struct option *opt __maybe_unused, |
1328 | const char *arg, int unset __maybe_unused) | |
ba77c9e1 LZ |
1329 | { |
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 | ||
0433ffbe ACM |
1345 | static int __cmd_record(int argc, const char **argv) |
1346 | { | |
1347 | const char * const record_args[] = { | |
4a4d371a | 1348 | "record", "-a", "-R", "-c", "1", |
0d68bc92 NK |
1349 | }; |
1350 | const char * const slab_events[] = { | |
ba77c9e1 LZ |
1351 | "-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", | |
0433ffbe | 1357 | }; |
0d68bc92 NK |
1358 | const char * const page_events[] = { |
1359 | "-e", "kmem:mm_page_alloc", | |
1360 | "-e", "kmem:mm_page_free", | |
1361 | }; | |
ba77c9e1 LZ |
1362 | unsigned int rec_argc, i, j; |
1363 | const char **rec_argv; | |
1364 | ||
1365 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
0d68bc92 NK |
1366 | if (kmem_slab) |
1367 | rec_argc += ARRAY_SIZE(slab_events); | |
1368 | if (kmem_page) | |
c9758cc4 | 1369 | rec_argc += ARRAY_SIZE(page_events) + 1; /* for -g */ |
0d68bc92 | 1370 | |
ba77c9e1 LZ |
1371 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
1372 | ||
ce47dc56 CS |
1373 | if (rec_argv == NULL) |
1374 | return -ENOMEM; | |
1375 | ||
ba77c9e1 LZ |
1376 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
1377 | rec_argv[i] = strdup(record_args[i]); | |
1378 | ||
0d68bc92 NK |
1379 | 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) { | |
c9758cc4 NK |
1384 | rec_argv[i++] = strdup("-g"); |
1385 | ||
0d68bc92 NK |
1386 | for (j = 0; j < ARRAY_SIZE(page_events); j++, i++) |
1387 | rec_argv[i] = strdup(page_events[j]); | |
1388 | } | |
1389 | ||
ba77c9e1 LZ |
1390 | 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 | ||
1d037ca1 | 1396 | int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused) |
ba77c9e1 | 1397 | { |
0433ffbe | 1398 | const char * const default_sort_order = "frag,hit,bytes"; |
d1eeb77c | 1399 | struct perf_data_file file = { |
d1eeb77c YS |
1400 | .mode = PERF_DATA_MODE_READ, |
1401 | }; | |
0433ffbe ACM |
1402 | const struct option kmem_options[] = { |
1403 | OPT_STRING('i', "input", &input_name, "file", "input file name"), | |
bd72a33e NK |
1404 | OPT_INCR('v', "verbose", &verbose, |
1405 | "be more verbose (show symbol address, etc)"), | |
0433ffbe ACM |
1406 | 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"), | |
d1eeb77c | 1415 | OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), |
0d68bc92 NK |
1416 | 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), | |
0433ffbe ACM |
1420 | OPT_END() |
1421 | }; | |
3bca2354 RR |
1422 | const char *const kmem_subcommands[] = { "record", "stat", NULL }; |
1423 | const char *kmem_usage[] = { | |
1424 | NULL, | |
0433ffbe ACM |
1425 | NULL |
1426 | }; | |
2b2b2c68 | 1427 | struct perf_session *session; |
2b2b2c68 NK |
1428 | int ret = -1; |
1429 | ||
3bca2354 RR |
1430 | argc = parse_options_subcommand(argc, argv, kmem_options, |
1431 | kmem_subcommands, kmem_usage, 0); | |
ba77c9e1 | 1432 | |
90b86a9f | 1433 | if (!argc) |
ba77c9e1 LZ |
1434 | usage_with_options(kmem_usage, kmem_options); |
1435 | ||
0d68bc92 NK |
1436 | if (kmem_slab == 0 && kmem_page == 0) |
1437 | kmem_slab = 1; /* for backward compatibility */ | |
1438 | ||
90b86a9f | 1439 | if (!strncmp(argv[0], "rec", 3)) { |
0a7e6d1b | 1440 | symbol__init(NULL); |
90b86a9f | 1441 | return __cmd_record(argc, argv); |
2b2b2c68 NK |
1442 | } |
1443 | ||
28939e1a JO |
1444 | file.path = input_name; |
1445 | ||
c9758cc4 | 1446 | kmem_session = session = perf_session__new(&file, false, &perf_kmem); |
2b2b2c68 | 1447 | if (session == NULL) |
52e02834 | 1448 | return -1; |
2b2b2c68 | 1449 | |
0d68bc92 NK |
1450 | 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); | |
c9758cc4 | 1459 | symbol_conf.use_callchain = true; |
0d68bc92 NK |
1460 | } |
1461 | ||
0a7e6d1b | 1462 | symbol__init(&session->header.env); |
2b2b2c68 NK |
1463 | |
1464 | if (!strcmp(argv[0], "stat")) { | |
77cfe388 NK |
1465 | setlocale(LC_ALL, ""); |
1466 | ||
4b627957 | 1467 | if (cpu__setup_cpunode_map()) |
2b2b2c68 | 1468 | goto out_delete; |
90b86a9f LZ |
1469 | |
1470 | 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); | |
ba77c9e1 | 1474 | |
2b2b2c68 | 1475 | ret = __cmd_kmem(session); |
b00eca8c PE |
1476 | } else |
1477 | usage_with_options(kmem_usage, kmem_options); | |
7d0d3945 | 1478 | |
2b2b2c68 NK |
1479 | out_delete: |
1480 | perf_session__delete(session); | |
1481 | ||
1482 | return ret; | |
ba77c9e1 LZ |
1483 | } |
1484 |