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