OSDN Git Service

tools/vm/page_owner_sort.c: delete invalid duplicate code
[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 #include <linux/types.h>
24
25 struct block_list {
26         char *txt;
27         char *stacktrace;
28         int len;
29         int num;
30         int page_num;
31         pid_t pid;
32         __u64 ts_nsec;
33         __u64 free_ts_nsec;
34 };
35
36 static regex_t order_pattern;
37 static regex_t pid_pattern;
38 static regex_t ts_nsec_pattern;
39 static regex_t free_ts_nsec_pattern;
40 static struct block_list *list;
41 static int list_size;
42 static int max_size;
43
44 int read_block(char *buf, int buf_size, FILE *fin)
45 {
46         char *curr = buf, *const buf_end = buf + buf_size;
47
48         while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
49                 if (*curr == '\n') /* empty line */
50                         return curr - buf;
51                 if (!strncmp(curr, "PFN", 3))
52                         continue;
53                 curr += strlen(curr);
54         }
55
56         return -1; /* EOF or no space left in buf. */
57 }
58
59 static int compare_txt(const void *p1, const void *p2)
60 {
61         const struct block_list *l1 = p1, *l2 = p2;
62
63         return strcmp(l1->txt, l2->txt);
64 }
65
66 static int compare_stacktrace(const void *p1, const void *p2)
67 {
68         const struct block_list *l1 = p1, *l2 = p2;
69
70         return strcmp(l1->stacktrace, l2->stacktrace);
71 }
72
73 static int compare_num(const void *p1, const void *p2)
74 {
75         const struct block_list *l1 = p1, *l2 = p2;
76
77         return l2->num - l1->num;
78 }
79
80 static int compare_page_num(const void *p1, const void *p2)
81 {
82         const struct block_list *l1 = p1, *l2 = p2;
83
84         return l2->page_num - l1->page_num;
85 }
86
87 static int compare_pid(const void *p1, const void *p2)
88 {
89         const struct block_list *l1 = p1, *l2 = p2;
90
91         return l1->pid - l2->pid;
92 }
93
94 static int compare_ts(const void *p1, const void *p2)
95 {
96         const struct block_list *l1 = p1, *l2 = p2;
97
98         return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
99 }
100
101 static int compare_free_ts(const void *p1, const void *p2)
102 {
103         const struct block_list *l1 = p1, *l2 = p2;
104
105         return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
106 }
107
108 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
109 {
110         int err, val_len;
111         regmatch_t pmatch[2];
112
113         err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
114         if (err != 0 || pmatch[1].rm_so == -1) {
115                 printf("no matching pattern in %s\n", buf);
116                 return -1;
117         }
118         val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
119
120         memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
121
122         return 0;
123 }
124
125 static void check_regcomp(regex_t *pattern, const char *regex)
126 {
127         int err;
128
129         err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
130         if (err != 0 || pattern->re_nsub != 1) {
131                 printf("Invalid pattern %s code %d\n", regex, err);
132                 exit(1);
133         }
134 }
135
136 # define FIELD_BUFF 25
137
138 static int get_page_num(char *buf)
139 {
140         int order_val;
141         char order_str[FIELD_BUFF] = {0};
142         char *endptr;
143
144         search_pattern(&order_pattern, order_str, buf);
145         errno = 0;
146         order_val = strtol(order_str, &endptr, 10);
147         if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
148                 printf("wrong order in follow buf:\n%s\n", buf);
149                 return 0;
150         }
151
152         return 1 << order_val;
153 }
154
155 static pid_t get_pid(char *buf)
156 {
157         pid_t pid;
158         char pid_str[FIELD_BUFF] = {0};
159         char *endptr;
160
161         search_pattern(&pid_pattern, pid_str, buf);
162         errno = 0;
163         pid = strtol(pid_str, &endptr, 10);
164         if (errno != 0 || endptr == pid_str || *endptr != '\0') {
165                 printf("wrong/invalid pid in follow buf:\n%s\n", buf);
166                 return -1;
167         }
168
169         return pid;
170
171 }
172
173 static __u64 get_ts_nsec(char *buf)
174 {
175         __u64 ts_nsec;
176         char ts_nsec_str[FIELD_BUFF] = {0};
177         char *endptr;
178
179         search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
180         errno = 0;
181         ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
182         if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
183                 printf("wrong ts_nsec in follow buf:\n%s\n", buf);
184                 return -1;
185         }
186
187         return ts_nsec;
188 }
189
190 static __u64 get_free_ts_nsec(char *buf)
191 {
192         __u64 free_ts_nsec;
193         char free_ts_nsec_str[FIELD_BUFF] = {0};
194         char *endptr;
195
196         search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
197         errno = 0;
198         free_ts_nsec = strtoull(free_ts_nsec_str, &endptr, 10);
199         if (errno != 0 || endptr == free_ts_nsec_str || *endptr != '\0') {
200                 printf("wrong free_ts_nsec in follow buf:\n%s\n", buf);
201                 return -1;
202         }
203
204         return free_ts_nsec;
205 }
206
207 static void add_list(char *buf, int len)
208 {
209         if (list_size != 0 &&
210             len == list[list_size-1].len &&
211             memcmp(buf, list[list_size-1].txt, len) == 0) {
212                 list[list_size-1].num++;
213                 list[list_size-1].page_num += get_page_num(buf);
214                 return;
215         }
216         if (list_size == max_size) {
217                 printf("max_size too small??\n");
218                 exit(1);
219         }
220         list[list_size].txt = malloc(len+1);
221         list[list_size].len = len;
222         list[list_size].num = 1;
223         list[list_size].page_num = get_page_num(buf);
224         memcpy(list[list_size].txt, buf, len);
225         list[list_size].txt[len] = 0;
226         list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
227         list[list_size].pid = get_pid(buf);
228         list[list_size].ts_nsec = get_ts_nsec(buf);
229         list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
230         list_size++;
231         if (list_size % 1000 == 0) {
232                 printf("loaded %d\r", list_size);
233                 fflush(stdout);
234         }
235 }
236
237 #define BUF_SIZE        (128 * 1024)
238
239 static void usage(void)
240 {
241         printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
242                 "-m     Sort by total memory.\n"
243                 "-s     Sort by the stack trace.\n"
244                 "-t     Sort by times (default).\n"
245                 "-p     Sort by pid.\n"
246                 "-a     Sort by memory allocate time.\n"
247                 "-r     Sort by memory release time.\n"
248                 "-c     Cull by comparing stacktrace instead of total block.\n"
249                 "-f     Filter out the information of blocks whose memory has not been released.\n"
250         );
251 }
252
253 int main(int argc, char **argv)
254 {
255         int (*cmp)(const void *, const void *) = compare_num;
256         int cull_st = 0;
257         int filter = 0;
258         FILE *fin, *fout;
259         char *buf;
260         int ret, i, count;
261         struct block_list *list2;
262         struct stat st;
263         int opt;
264
265         while ((opt = getopt(argc, argv, "acfmprst")) != -1)
266                 switch (opt) {
267                 case 'a':
268                         cmp = compare_ts;
269                         break;
270                 case 'c':
271                         cull_st = 1;
272                         break;
273                 case 'f':
274                         filter = 1;
275                         break;
276                 case 'm':
277                         cmp = compare_page_num;
278                         break;
279                 case 'p':
280                         cmp = compare_pid;
281                         break;
282                 case 'r':
283                         cmp = compare_free_ts;
284                         break;
285                 case 's':
286                         cmp = compare_stacktrace;
287                         break;
288                 case 't':
289                         cmp = compare_num;
290                         break;
291                 default:
292                         usage();
293                         exit(1);
294                 }
295
296         if (optind >= (argc - 1)) {
297                 usage();
298                 exit(1);
299         }
300
301         fin = fopen(argv[optind], "r");
302         fout = fopen(argv[optind + 1], "w");
303         if (!fin || !fout) {
304                 usage();
305                 perror("open: ");
306                 exit(1);
307         }
308
309         check_regcomp(&order_pattern, "order\\s*([0-9]*),");
310         check_regcomp(&pid_pattern, "pid\\s*([0-9]*),");
311         check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,");
312         check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns");
313         fstat(fileno(fin), &st);
314         max_size = st.st_size / 100; /* hack ... */
315
316         list = malloc(max_size * sizeof(*list));
317         buf = malloc(BUF_SIZE);
318         if (!list || !buf) {
319                 printf("Out of memory\n");
320                 exit(1);
321         }
322
323         for ( ; ; ) {
324                 ret = read_block(buf, BUF_SIZE, fin);
325                 if (ret < 0)
326                         break;
327
328                 add_list(buf, ret);
329         }
330
331         printf("loaded %d\n", list_size);
332
333         printf("sorting ....\n");
334
335         if (cull_st == 1)
336                 qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
337         else
338                 qsort(list, list_size, sizeof(list[0]), compare_txt);
339
340         list2 = malloc(sizeof(*list) * list_size);
341         if (!list2) {
342                 printf("Out of memory\n");
343                 exit(1);
344         }
345
346         printf("culling\n");
347
348         long offset = cull_st ? &list[0].stacktrace - &list[0].txt : 0;
349
350         for (i = count = 0; i < list_size; i++) {
351                 if (count == 0 ||
352                     strcmp(*(&list2[count-1].txt+offset), *(&list[i].txt+offset)) != 0) {
353                         list2[count++] = list[i];
354                 } else {
355                         list2[count-1].num += list[i].num;
356                         list2[count-1].page_num += list[i].page_num;
357                 }
358         }
359
360         qsort(list2, count, sizeof(list[0]), cmp);
361
362         for (i = 0; i < count; i++) {
363                 if (filter == 1 && list2[i].free_ts_nsec != 0)
364                         continue;
365                 fprintf(fout, "%d times, %d pages:\n%s\n",
366                                 list2[i].num, list2[i].page_num, list2[i].txt);
367         }
368         regfree(&order_pattern);
369         regfree(&pid_pattern);
370         regfree(&ts_nsec_pattern);
371         regfree(&free_ts_nsec_pattern);
372         return 0;
373 }