forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin-kmem.c
2026 lines (1669 loc) · 45.8 KB
/
builtin-kmem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
#include "builtin.h"
#include "perf.h"
#include "util/dso.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/config.h"
#include "util/map.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"
#include "util/session.h"
#include "util/tool.h"
#include "util/callchain.h"
#include "util/time-utils.h"
#include <linux/err.h>
#include <subcmd/pager.h>
#include <subcmd/parse-options.h>
#include "util/trace-event.h"
#include "util/data.h"
#include "util/cpumap.h"
#include "util/debug.h"
#include "util/string2.h"
#include <linux/kernel.h>
#include <linux/rbtree.h>
#include <linux/string.h>
#include <linux/zalloc.h>
#include <errno.h>
#include <inttypes.h>
#include <locale.h>
#include <regex.h>
#include <linux/ctype.h>
static int kmem_slab;
static int kmem_page;
static long kmem_page_size;
static enum {
KMEM_SLAB,
KMEM_PAGE,
} kmem_default = KMEM_SLAB; /* for backward compatibility */
struct alloc_stat;
typedef int (*sort_fn_t)(void *, void *);
static int alloc_flag;
static int caller_flag;
static int alloc_lines = -1;
static int caller_lines = -1;
static bool raw_ip;
struct alloc_stat {
u64 call_site;
u64 ptr;
u64 bytes_req;
u64 bytes_alloc;
u64 last_alloc;
u32 hit;
u32 pingpong;
short alloc_cpu;
struct rb_node node;
};
static struct rb_root root_alloc_stat;
static struct rb_root root_alloc_sorted;
static struct rb_root root_caller_stat;
static struct rb_root root_caller_sorted;
static unsigned long total_requested, total_allocated, total_freed;
static unsigned long nr_allocs, nr_cross_allocs;
/* filters for controlling start and stop of time of analysis */
static struct perf_time_interval ptime;
const char *time_str;
static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
int bytes_req, int bytes_alloc, int cpu)
{
struct rb_node **node = &root_alloc_stat.rb_node;
struct rb_node *parent = NULL;
struct alloc_stat *data = NULL;
while (*node) {
parent = *node;
data = rb_entry(*node, struct alloc_stat, node);
if (ptr > data->ptr)
node = &(*node)->rb_right;
else if (ptr < data->ptr)
node = &(*node)->rb_left;
else
break;
}
if (data && data->ptr == ptr) {
data->hit++;
data->bytes_req += bytes_req;
data->bytes_alloc += bytes_alloc;
} else {
data = malloc(sizeof(*data));
if (!data) {
pr_err("%s: malloc failed\n", __func__);
return -1;
}
data->ptr = ptr;
data->pingpong = 0;
data->hit = 1;
data->bytes_req = bytes_req;
data->bytes_alloc = bytes_alloc;
rb_link_node(&data->node, parent, node);
rb_insert_color(&data->node, &root_alloc_stat);
}
data->call_site = call_site;
data->alloc_cpu = cpu;
data->last_alloc = bytes_alloc;
return 0;
}
static int insert_caller_stat(unsigned long call_site,
int bytes_req, int bytes_alloc)
{
struct rb_node **node = &root_caller_stat.rb_node;
struct rb_node *parent = NULL;
struct alloc_stat *data = NULL;
while (*node) {
parent = *node;
data = rb_entry(*node, struct alloc_stat, node);
if (call_site > data->call_site)
node = &(*node)->rb_right;
else if (call_site < data->call_site)
node = &(*node)->rb_left;
else
break;
}
if (data && data->call_site == call_site) {
data->hit++;
data->bytes_req += bytes_req;
data->bytes_alloc += bytes_alloc;
} else {
data = malloc(sizeof(*data));
if (!data) {
pr_err("%s: malloc failed\n", __func__);
return -1;
}
data->call_site = call_site;
data->pingpong = 0;
data->hit = 1;
data->bytes_req = bytes_req;
data->bytes_alloc = bytes_alloc;
rb_link_node(&data->node, parent, node);
rb_insert_color(&data->node, &root_caller_stat);
}
return 0;
}
static int perf_evsel__process_alloc_event(struct evsel *evsel,
struct perf_sample *sample)
{
unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"),
call_site = perf_evsel__intval(evsel, sample, "call_site");
int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"),
bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc");
if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) ||
insert_caller_stat(call_site, bytes_req, bytes_alloc))
return -1;
total_requested += bytes_req;
total_allocated += bytes_alloc;
nr_allocs++;
return 0;
}
static int perf_evsel__process_alloc_node_event(struct evsel *evsel,
struct perf_sample *sample)
{
int ret = perf_evsel__process_alloc_event(evsel, sample);
if (!ret) {
int node1 = cpu__get_node(sample->cpu),
node2 = perf_evsel__intval(evsel, sample, "node");
if (node1 != node2)
nr_cross_allocs++;
}
return ret;
}
static int ptr_cmp(void *, void *);
static int slab_callsite_cmp(void *, void *);
static struct alloc_stat *search_alloc_stat(unsigned long ptr,
unsigned long call_site,
struct rb_root *root,
sort_fn_t sort_fn)
{
struct rb_node *node = root->rb_node;
struct alloc_stat key = { .ptr = ptr, .call_site = call_site };
while (node) {
struct alloc_stat *data;
int cmp;
data = rb_entry(node, struct alloc_stat, node);
cmp = sort_fn(&key, data);
if (cmp < 0)
node = node->rb_left;
else if (cmp > 0)
node = node->rb_right;
else
return data;
}
return NULL;
}
static int perf_evsel__process_free_event(struct evsel *evsel,
struct perf_sample *sample)
{
unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr");
struct alloc_stat *s_alloc, *s_caller;
s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp);
if (!s_alloc)
return 0;
total_freed += s_alloc->last_alloc;
if ((short)sample->cpu != s_alloc->alloc_cpu) {
s_alloc->pingpong++;
s_caller = search_alloc_stat(0, s_alloc->call_site,
&root_caller_stat,
slab_callsite_cmp);
if (!s_caller)
return -1;
s_caller->pingpong++;
}
s_alloc->alloc_cpu = -1;
return 0;
}
static u64 total_page_alloc_bytes;
static u64 total_page_free_bytes;
static u64 total_page_nomatch_bytes;
static u64 total_page_fail_bytes;
static unsigned long nr_page_allocs;
static unsigned long nr_page_frees;
static unsigned long nr_page_fails;
static unsigned long nr_page_nomatch;
static bool use_pfn;
static bool live_page;
static struct perf_session *kmem_session;
#define MAX_MIGRATE_TYPES 6
#define MAX_PAGE_ORDER 11
static int order_stats[MAX_PAGE_ORDER][MAX_MIGRATE_TYPES];
struct page_stat {
struct rb_node node;
u64 page;
u64 callsite;
int order;
unsigned gfp_flags;
unsigned migrate_type;
u64 alloc_bytes;
u64 free_bytes;
int nr_alloc;
int nr_free;
};
static struct rb_root page_live_tree;
static struct rb_root page_alloc_tree;
static struct rb_root page_alloc_sorted;
static struct rb_root page_caller_tree;
static struct rb_root page_caller_sorted;
struct alloc_func {
u64 start;
u64 end;
char *name;
};
static int nr_alloc_funcs;
static struct alloc_func *alloc_func_list;
static int funcmp(const void *a, const void *b)
{
const struct alloc_func *fa = a;
const struct alloc_func *fb = b;
if (fa->start > fb->start)
return 1;
else
return -1;
}
static int callcmp(const void *a, const void *b)
{
const struct alloc_func *fa = a;
const struct alloc_func *fb = b;
if (fb->start <= fa->start && fa->end < fb->end)
return 0;
if (fa->start > fb->start)
return 1;
else
return -1;
}
static int build_alloc_func_list(void)
{
int ret;
struct map *kernel_map;
struct symbol *sym;
struct rb_node *node;
struct alloc_func *func;
struct machine *machine = &kmem_session->machines.host;
regex_t alloc_func_regex;
static const char pattern[] = "^_?_?(alloc|get_free|get_zeroed)_pages?";
ret = regcomp(&alloc_func_regex, pattern, REG_EXTENDED);
if (ret) {
char err[BUFSIZ];
regerror(ret, &alloc_func_regex, err, sizeof(err));
pr_err("Invalid regex: %s\n%s", pattern, err);
return -EINVAL;
}
kernel_map = machine__kernel_map(machine);
if (map__load(kernel_map) < 0) {
pr_err("cannot load kernel map\n");
return -ENOENT;
}
map__for_each_symbol(kernel_map, sym, node) {
if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
continue;
func = realloc(alloc_func_list,
(nr_alloc_funcs + 1) * sizeof(*func));
if (func == NULL)
return -ENOMEM;
pr_debug("alloc func: %s\n", sym->name);
func[nr_alloc_funcs].start = sym->start;
func[nr_alloc_funcs].end = sym->end;
func[nr_alloc_funcs].name = sym->name;
alloc_func_list = func;
nr_alloc_funcs++;
}
qsort(alloc_func_list, nr_alloc_funcs, sizeof(*func), funcmp);
regfree(&alloc_func_regex);
return 0;
}
/*
* Find first non-memory allocation function from callchain.
* The allocation functions are in the 'alloc_func_list'.
*/
static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)
{
struct addr_location al;
struct machine *machine = &kmem_session->machines.host;
struct callchain_cursor_node *node;
if (alloc_func_list == NULL) {
if (build_alloc_func_list() < 0)
goto out;
}
al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);
callchain_cursor_commit(&callchain_cursor);
while (true) {
struct alloc_func key, *caller;
u64 addr;
node = callchain_cursor_current(&callchain_cursor);
if (node == NULL)
break;
key.start = key.end = node->ip;
caller = bsearch(&key, alloc_func_list, nr_alloc_funcs,
sizeof(key), callcmp);
if (!caller) {
/* found */
if (node->ms.map)
addr = map__unmap_ip(node->ms.map, node->ip);
else
addr = node->ip;
return addr;
} else
pr_debug3("skipping alloc function: %s\n", caller->name);
callchain_cursor_advance(&callchain_cursor);
}
out:
pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
return sample->ip;
}
struct sort_dimension {
const char name[20];
sort_fn_t cmp;
struct list_head list;
};
static LIST_HEAD(page_alloc_sort_input);
static LIST_HEAD(page_caller_sort_input);
static struct page_stat *
__page_stat__findnew_page(struct page_stat *pstat, bool create)
{
struct rb_node **node = &page_live_tree.rb_node;
struct rb_node *parent = NULL;
struct page_stat *data;
while (*node) {
s64 cmp;
parent = *node;
data = rb_entry(*node, struct page_stat, node);
cmp = data->page - pstat->page;
if (cmp < 0)
node = &parent->rb_left;
else if (cmp > 0)
node = &parent->rb_right;
else
return data;
}
if (!create)
return NULL;
data = zalloc(sizeof(*data));
if (data != NULL) {
data->page = pstat->page;
data->order = pstat->order;
data->gfp_flags = pstat->gfp_flags;
data->migrate_type = pstat->migrate_type;
rb_link_node(&data->node, parent, node);
rb_insert_color(&data->node, &page_live_tree);
}
return data;
}
static struct page_stat *page_stat__find_page(struct page_stat *pstat)
{
return __page_stat__findnew_page(pstat, false);
}
static struct page_stat *page_stat__findnew_page(struct page_stat *pstat)
{
return __page_stat__findnew_page(pstat, true);
}
static struct page_stat *
__page_stat__findnew_alloc(struct page_stat *pstat, bool create)
{
struct rb_node **node = &page_alloc_tree.rb_node;
struct rb_node *parent = NULL;
struct page_stat *data;
struct sort_dimension *sort;
while (*node) {
int cmp = 0;
parent = *node;
data = rb_entry(*node, struct page_stat, node);
list_for_each_entry(sort, &page_alloc_sort_input, list) {
cmp = sort->cmp(pstat, data);
if (cmp)
break;
}
if (cmp < 0)
node = &parent->rb_left;
else if (cmp > 0)
node = &parent->rb_right;
else
return data;
}
if (!create)
return NULL;
data = zalloc(sizeof(*data));
if (data != NULL) {
data->page = pstat->page;
data->order = pstat->order;
data->gfp_flags = pstat->gfp_flags;
data->migrate_type = pstat->migrate_type;
rb_link_node(&data->node, parent, node);
rb_insert_color(&data->node, &page_alloc_tree);
}
return data;
}
static struct page_stat *page_stat__find_alloc(struct page_stat *pstat)
{
return __page_stat__findnew_alloc(pstat, false);
}
static struct page_stat *page_stat__findnew_alloc(struct page_stat *pstat)
{
return __page_stat__findnew_alloc(pstat, true);
}
static struct page_stat *
__page_stat__findnew_caller(struct page_stat *pstat, bool create)
{
struct rb_node **node = &page_caller_tree.rb_node;
struct rb_node *parent = NULL;
struct page_stat *data;
struct sort_dimension *sort;
while (*node) {
int cmp = 0;
parent = *node;
data = rb_entry(*node, struct page_stat, node);
list_for_each_entry(sort, &page_caller_sort_input, list) {
cmp = sort->cmp(pstat, data);
if (cmp)
break;
}
if (cmp < 0)
node = &parent->rb_left;
else if (cmp > 0)
node = &parent->rb_right;
else
return data;
}
if (!create)
return NULL;
data = zalloc(sizeof(*data));
if (data != NULL) {
data->callsite = pstat->callsite;
data->order = pstat->order;
data->gfp_flags = pstat->gfp_flags;
data->migrate_type = pstat->migrate_type;
rb_link_node(&data->node, parent, node);
rb_insert_color(&data->node, &page_caller_tree);
}
return data;
}
static struct page_stat *page_stat__find_caller(struct page_stat *pstat)
{
return __page_stat__findnew_caller(pstat, false);
}
static struct page_stat *page_stat__findnew_caller(struct page_stat *pstat)
{
return __page_stat__findnew_caller(pstat, true);
}
static bool valid_page(u64 pfn_or_page)
{
if (use_pfn && pfn_or_page == -1UL)
return false;
if (!use_pfn && pfn_or_page == 0)
return false;
return true;
}
struct gfp_flag {
unsigned int flags;
char *compact_str;
char *human_readable;
};
static struct gfp_flag *gfps;
static int nr_gfps;
static int gfpcmp(const void *a, const void *b)
{
const struct gfp_flag *fa = a;
const struct gfp_flag *fb = b;
return fa->flags - fb->flags;
}
/* see include/trace/events/mmflags.h */
static const struct {
const char *original;
const char *compact;
} gfp_compact_table[] = {
{ "GFP_TRANSHUGE", "THP" },
{ "GFP_TRANSHUGE_LIGHT", "THL" },
{ "GFP_HIGHUSER_MOVABLE", "HUM" },
{ "GFP_HIGHUSER", "HU" },
{ "GFP_USER", "U" },
{ "GFP_KERNEL_ACCOUNT", "KAC" },
{ "GFP_KERNEL", "K" },
{ "GFP_NOFS", "NF" },
{ "GFP_ATOMIC", "A" },
{ "GFP_NOIO", "NI" },
{ "GFP_NOWAIT", "NW" },
{ "GFP_DMA", "D" },
{ "__GFP_HIGHMEM", "HM" },
{ "GFP_DMA32", "D32" },
{ "__GFP_HIGH", "H" },
{ "__GFP_ATOMIC", "_A" },
{ "__GFP_IO", "I" },
{ "__GFP_FS", "F" },
{ "__GFP_NOWARN", "NWR" },
{ "__GFP_RETRY_MAYFAIL", "R" },
{ "__GFP_NOFAIL", "NF" },
{ "__GFP_NORETRY", "NR" },
{ "__GFP_COMP", "C" },
{ "__GFP_ZERO", "Z" },
{ "__GFP_NOMEMALLOC", "NMA" },
{ "__GFP_MEMALLOC", "MA" },
{ "__GFP_HARDWALL", "HW" },
{ "__GFP_THISNODE", "TN" },
{ "__GFP_RECLAIMABLE", "RC" },
{ "__GFP_MOVABLE", "M" },
{ "__GFP_ACCOUNT", "AC" },
{ "__GFP_WRITE", "WR" },
{ "__GFP_RECLAIM", "R" },
{ "__GFP_DIRECT_RECLAIM", "DR" },
{ "__GFP_KSWAPD_RECLAIM", "KR" },
};
static size_t max_gfp_len;
static char *compact_gfp_flags(char *gfp_flags)
{
char *orig_flags = strdup(gfp_flags);
char *new_flags = NULL;
char *str, *pos = NULL;
size_t len = 0;
if (orig_flags == NULL)
return NULL;
str = strtok_r(orig_flags, "|", &pos);
while (str) {
size_t i;
char *new;
const char *cpt;
for (i = 0; i < ARRAY_SIZE(gfp_compact_table); i++) {
if (strcmp(gfp_compact_table[i].original, str))
continue;
cpt = gfp_compact_table[i].compact;
new = realloc(new_flags, len + strlen(cpt) + 2);
if (new == NULL) {
free(new_flags);
free(orig_flags);
return NULL;
}
new_flags = new;
if (!len) {
strcpy(new_flags, cpt);
} else {
strcat(new_flags, "|");
strcat(new_flags, cpt);
len++;
}
len += strlen(cpt);
}
str = strtok_r(NULL, "|", &pos);
}
if (max_gfp_len < len)
max_gfp_len = len;
free(orig_flags);
return new_flags;
}
static char *compact_gfp_string(unsigned long gfp_flags)
{
struct gfp_flag key = {
.flags = gfp_flags,
};
struct gfp_flag *gfp;
gfp = bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp);
if (gfp)
return gfp->compact_str;
return NULL;
}
static int parse_gfp_flags(struct evsel *evsel, struct perf_sample *sample,
unsigned int gfp_flags)
{
struct tep_record record = {
.cpu = sample->cpu,
.data = sample->raw_data,
.size = sample->raw_size,
};
struct trace_seq seq;
char *str, *pos = NULL;
if (nr_gfps) {
struct gfp_flag key = {
.flags = gfp_flags,
};
if (bsearch(&key, gfps, nr_gfps, sizeof(*gfps), gfpcmp))
return 0;
}
trace_seq_init(&seq);
tep_print_event(evsel->tp_format->tep,
&seq, &record, "%s", TEP_PRINT_INFO);
str = strtok_r(seq.buffer, " ", &pos);
while (str) {
if (!strncmp(str, "gfp_flags=", 10)) {
struct gfp_flag *new;
new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps));
if (new == NULL)
return -ENOMEM;
gfps = new;
new += nr_gfps++;
new->flags = gfp_flags;
new->human_readable = strdup(str + 10);
new->compact_str = compact_gfp_flags(str + 10);
if (!new->human_readable || !new->compact_str)
return -ENOMEM;
qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp);
}
str = strtok_r(NULL, " ", &pos);
}
trace_seq_destroy(&seq);
return 0;
}
static int perf_evsel__process_page_alloc_event(struct evsel *evsel,
struct perf_sample *sample)
{
u64 page;
unsigned int order = perf_evsel__intval(evsel, sample, "order");
unsigned int gfp_flags = perf_evsel__intval(evsel, sample, "gfp_flags");
unsigned int migrate_type = perf_evsel__intval(evsel, sample,
"migratetype");
u64 bytes = kmem_page_size << order;
u64 callsite;
struct page_stat *pstat;
struct page_stat this = {
.order = order,
.gfp_flags = gfp_flags,
.migrate_type = migrate_type,
};
if (use_pfn)
page = perf_evsel__intval(evsel, sample, "pfn");
else
page = perf_evsel__intval(evsel, sample, "page");
nr_page_allocs++;
total_page_alloc_bytes += bytes;
if (!valid_page(page)) {
nr_page_fails++;
total_page_fail_bytes += bytes;
return 0;
}
if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
return -1;
callsite = find_callsite(evsel, sample);
/*
* This is to find the current page (with correct gfp flags and
* migrate type) at free event.
*/
this.page = page;
pstat = page_stat__findnew_page(&this);
if (pstat == NULL)
return -ENOMEM;
pstat->nr_alloc++;
pstat->alloc_bytes += bytes;
pstat->callsite = callsite;
if (!live_page) {
pstat = page_stat__findnew_alloc(&this);
if (pstat == NULL)
return -ENOMEM;
pstat->nr_alloc++;
pstat->alloc_bytes += bytes;
pstat->callsite = callsite;
}
this.callsite = callsite;
pstat = page_stat__findnew_caller(&this);
if (pstat == NULL)
return -ENOMEM;
pstat->nr_alloc++;
pstat->alloc_bytes += bytes;
order_stats[order][migrate_type]++;
return 0;
}
static int perf_evsel__process_page_free_event(struct evsel *evsel,
struct perf_sample *sample)
{
u64 page;
unsigned int order = perf_evsel__intval(evsel, sample, "order");
u64 bytes = kmem_page_size << order;
struct page_stat *pstat;
struct page_stat this = {
.order = order,
};
if (use_pfn)
page = perf_evsel__intval(evsel, sample, "pfn");
else
page = perf_evsel__intval(evsel, sample, "page");
nr_page_frees++;
total_page_free_bytes += bytes;
this.page = page;
pstat = page_stat__find_page(&this);
if (pstat == NULL) {
pr_debug2("missing free at page %"PRIx64" (order: %d)\n",
page, order);
nr_page_nomatch++;
total_page_nomatch_bytes += bytes;
return 0;
}
this.gfp_flags = pstat->gfp_flags;
this.migrate_type = pstat->migrate_type;
this.callsite = pstat->callsite;
rb_erase(&pstat->node, &page_live_tree);
free(pstat);
if (live_page) {
order_stats[this.order][this.migrate_type]--;
} else {
pstat = page_stat__find_alloc(&this);
if (pstat == NULL)
return -ENOMEM;
pstat->nr_free++;
pstat->free_bytes += bytes;
}
pstat = page_stat__find_caller(&this);
if (pstat == NULL)
return -ENOENT;
pstat->nr_free++;
pstat->free_bytes += bytes;
if (live_page) {
pstat->nr_alloc--;
pstat->alloc_bytes -= bytes;
if (pstat->nr_alloc == 0) {
rb_erase(&pstat->node, &page_caller_tree);
free(pstat);
}
}
return 0;
}
static bool perf_kmem__skip_sample(struct perf_sample *sample)
{
/* skip sample based on time? */
if (perf_time__skip_sample(&ptime, sample->time))
return true;
return false;
}
typedef int (*tracepoint_handler)(struct evsel *evsel,
struct perf_sample *sample);
static int process_sample_event(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample,
struct evsel *evsel,
struct machine *machine)
{
int err = 0;
struct thread *thread = machine__findnew_thread(machine, sample->pid,
sample->tid);
if (thread == NULL) {
pr_debug("problem processing %d event, skipping it.\n",
event->header.type);
return -1;
}
if (perf_kmem__skip_sample(sample))
return 0;
dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
if (evsel->handler != NULL) {
tracepoint_handler f = evsel->handler;
err = f(evsel, sample);
}
thread__put(thread);
return err;
}
static struct perf_tool perf_kmem = {
.sample = process_sample_event,
.comm = perf_event__process_comm,
.mmap = perf_event__process_mmap,
.mmap2 = perf_event__process_mmap2,
.namespaces = perf_event__process_namespaces,
.ordered_events = true,
};
static double fragmentation(unsigned long n_req, unsigned long n_alloc)
{
if (n_alloc == 0)
return 0.0;
else
return 100.0 - (100.0 * n_req / n_alloc);
}
static void __print_slab_result(struct rb_root *root,
struct perf_session *session,
int n_lines, int is_caller)
{
struct rb_node *next;
struct machine *machine = &session->machines.host;
printf("%.105s\n", graph_dotted_line);
printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
printf("%.105s\n", graph_dotted_line);