OSDN Git Service

tools/vm/page_owner_sort.c: sort by stacktrace before culling
[uclinux-h8/linux.git] / tools / vm / page_owner_sort.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * User-space helper to sort the output of /sys/kernel/debug/page_owner
4  *
5  * Example use:
6  * cat /sys/kernel/debug/page_owner > page_owner_full.txt
7  * ./page_owner_sort page_owner_full.txt sorted_page_owner.txt
8  * Or sort by total memory:
9  * ./page_owner_sort -m page_owner_full.txt sorted_page_owner.txt
10  *
11  * See Documentation/vm/page_owner.rst
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <regex.h>
22 #include <errno.h>
23
24 struct block_list {
25         char *txt;
26         char *stacktrace;
27         int len;
28         int num;
29         int page_num;
30 };
31
32 static int sort_by_memory;
33 static regex_t order_pattern;
34 static struct block_list *list;
35 static int list_size;
36 static int max_size;
37
38 struct block_list *block_head;
39
40 int read_block(char *buf, int buf_size, FILE *fin)
41 {
42         char *curr = buf, *const buf_end = buf + buf_size;
43
44         while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
45                 if (*curr == '\n') /* empty line */
46                         return curr - buf;
47                 if (!strncmp(curr, "PFN", 3))
48                         continue;
49                 curr += strlen(curr);
50         }
51
52         return -1; /* EOF or no space left in buf. */
53 }
54
55 static int compare_stacktrace(const void *p1, const void *p2)
56 {
57         const struct block_list *l1 = p1, *l2 = p2;
58
59         return strcmp(l1->stacktrace, l2->stacktrace);
60 }
61
62 static int compare_num(const void *p1, const void *p2)
63 {
64         const struct block_list *l1 = p1, *l2 = p2;
65
66         return l2->num - l1->num;
67 }
68
69 static int compare_page_num(const void *p1, const void *p2)
70 {
71         const struct block_list *l1 = p1, *l2 = p2;
72
73         return l2->page_num - l1->page_num;
74 }
75
76 static int get_page_num(char *buf)
77 {
78         int err, val_len, order_val;
79         char order_str[4] = {0};
80         char *endptr;
81         regmatch_t pmatch[2];
82
83         err = regexec(&order_pattern, buf, 2, pmatch, REG_NOTBOL);
84         if (err != 0 || pmatch[1].rm_so == -1) {
85                 printf("no order pattern in %s\n", buf);
86                 return 0;
87         }
88         val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
89         if (val_len > 2) /* max_order should not exceed 2 digits */
90                 goto wrong_order;
91
92         memcpy(order_str, buf + pmatch[1].rm_so, val_len);
93
94         errno = 0;
95         order_val = strtol(order_str, &endptr, 10);
96         if (errno != 0 || endptr == order_str || *endptr != '\0')
97                 goto wrong_order;
98
99         return 1 << order_val;
100
101 wrong_order:
102         printf("wrong order in follow buf:\n%s\n", buf);
103         return 0;
104 }
105
106 static void add_list(char *buf, int len)
107 {
108         if (list_size != 0 &&
109             len == list[list_size-1].len &&
110             memcmp(buf, list[list_size-1].txt, len) == 0) {
111                 list[list_size-1].num++;
112                 list[list_size-1].page_num += get_page_num(buf);
113                 return;
114         }
115         if (list_size == max_size) {
116                 printf("max_size too small??\n");
117                 exit(1);
118         }
119         list[list_size].txt = malloc(len+1);
120         list[list_size].len = len;
121         list[list_size].num = 1;
122         list[list_size].page_num = get_page_num(buf);
123         memcpy(list[list_size].txt, buf, len);
124         list[list_size].txt[len] = 0;
125         list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
126         list_size++;
127         if (list_size % 1000 == 0) {
128                 printf("loaded %d\r", list_size);
129                 fflush(stdout);
130         }
131 }
132
133 #define BUF_SIZE        (128 * 1024)
134
135 static void usage(void)
136 {
137         printf("Usage: ./page_owner_sort [-m] <input> <output>\n"
138                 "-m     Sort by total memory. If this option is unset, sort by times\n"
139         );
140 }
141
142 int main(int argc, char **argv)
143 {
144         FILE *fin, *fout;
145         char *buf;
146         int ret, i, count;
147         struct block_list *list2;
148         struct stat st;
149         int err;
150         int opt;
151
152         while ((opt = getopt(argc, argv, "m")) != -1)
153                 switch (opt) {
154                 case 'm':
155                         sort_by_memory = 1;
156                         break;
157                 default:
158                         usage();
159                         exit(1);
160                 }
161
162         if (optind >= (argc - 1)) {
163                 usage();
164                 exit(1);
165         }
166
167         fin = fopen(argv[optind], "r");
168         fout = fopen(argv[optind + 1], "w");
169         if (!fin || !fout) {
170                 usage();
171                 perror("open: ");
172                 exit(1);
173         }
174
175         err = regcomp(&order_pattern, "order\\s*([0-9]*),", REG_EXTENDED|REG_NEWLINE);
176         if (err != 0 || order_pattern.re_nsub != 1) {
177                 printf("%s: Invalid pattern 'order\\s*([0-9]*),' code %d\n",
178                         argv[0], err);
179                 exit(1);
180         }
181
182         fstat(fileno(fin), &st);
183         max_size = st.st_size / 100; /* hack ... */
184
185         list = malloc(max_size * sizeof(*list));
186         buf = malloc(BUF_SIZE);
187         if (!list || !buf) {
188                 printf("Out of memory\n");
189                 exit(1);
190         }
191
192         for ( ; ; ) {
193                 ret = read_block(buf, BUF_SIZE, fin);
194                 if (ret < 0)
195                         break;
196
197                 add_list(buf, ret);
198         }
199
200         printf("loaded %d\n", list_size);
201
202         printf("sorting ....\n");
203
204         qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
205
206         list2 = malloc(sizeof(*list) * list_size);
207         if (!list2) {
208                 printf("Out of memory\n");
209                 exit(1);
210         }
211
212         printf("culling\n");
213
214         for (i = count = 0; i < list_size; i++) {
215                 if (count == 0 ||
216                     strcmp(list2[count-1].stacktrace, list[i].stacktrace) != 0) {
217                         list2[count++] = list[i];
218                 } else {
219                         list2[count-1].num += list[i].num;
220                         list2[count-1].page_num += list[i].page_num;
221                 }
222         }
223
224         if (sort_by_memory)
225                 qsort(list2, count, sizeof(list[0]), compare_page_num);
226         else
227                 qsort(list2, count, sizeof(list[0]), compare_num);
228
229         for (i = 0; i < count; i++)
230                 fprintf(fout, "%d times, %d pages:\n%s\n",
231                                 list2[i].num, list2[i].page_num, list2[i].txt);
232
233         regfree(&order_pattern);
234         return 0;
235 }