tools/vm/page_owner_sort.c: support for multi-value selection in single argument
[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 #include <getopt.h>
25
26 #define bool int
27 #define true 1
28 #define false 0
29 #define TASK_COMM_LEN 16
30
31 struct block_list {
32         char *txt;
33         char *comm; // task command name
34         char *stacktrace;
35         __u64 ts_nsec;
36         __u64 free_ts_nsec;
37         int len;
38         int num;
39         int page_num;
40         pid_t pid;
41         pid_t tgid;
42 };
43 enum FILTER_BIT {
44         FILTER_UNRELEASE = 1<<1,
45         FILTER_PID = 1<<2,
46         FILTER_TGID = 1<<3,
47         FILTER_COMM = 1<<4
48 };
49 enum CULL_BIT {
50         CULL_UNRELEASE = 1<<1,
51         CULL_PID = 1<<2,
52         CULL_TGID = 1<<3,
53         CULL_COMM = 1<<4,
54         CULL_STACKTRACE = 1<<5
55 };
56 struct filter_condition {
57         pid_t *tgids;
58         int tgids_size;
59         pid_t *pids;
60         int pids_size;
61         char **comms;
62         int comms_size;
63 };
64 static struct filter_condition fc;
65 static regex_t order_pattern;
66 static regex_t pid_pattern;
67 static regex_t tgid_pattern;
68 static regex_t comm_pattern;
69 static regex_t ts_nsec_pattern;
70 static regex_t free_ts_nsec_pattern;
71 static struct block_list *list;
72 static int list_size;
73 static int max_size;
74 static int cull;
75 static int filter;
76
77 int read_block(char *buf, int buf_size, FILE *fin)
78 {
79         char *curr = buf, *const buf_end = buf + buf_size;
80
81         while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
82                 if (*curr == '\n') /* empty line */
83                         return curr - buf;
84                 if (!strncmp(curr, "PFN", 3))
85                         continue;
86                 curr += strlen(curr);
87         }
88
89         return -1; /* EOF or no space left in buf. */
90 }
91
92 static int compare_txt(const void *p1, const void *p2)
93 {
94         const struct block_list *l1 = p1, *l2 = p2;
95
96         return strcmp(l1->txt, l2->txt);
97 }
98
99 static int compare_stacktrace(const void *p1, const void *p2)
100 {
101         const struct block_list *l1 = p1, *l2 = p2;
102
103         return strcmp(l1->stacktrace, l2->stacktrace);
104 }
105
106 static int compare_num(const void *p1, const void *p2)
107 {
108         const struct block_list *l1 = p1, *l2 = p2;
109
110         return l2->num - l1->num;
111 }
112
113 static int compare_page_num(const void *p1, const void *p2)
114 {
115         const struct block_list *l1 = p1, *l2 = p2;
116
117         return l2->page_num - l1->page_num;
118 }
119
120 static int compare_pid(const void *p1, const void *p2)
121 {
122         const struct block_list *l1 = p1, *l2 = p2;
123
124         return l1->pid - l2->pid;
125 }
126
127 static int compare_tgid(const void *p1, const void *p2)
128 {
129         const struct block_list *l1 = p1, *l2 = p2;
130
131         return l1->tgid - l2->tgid;
132 }
133
134 static int compare_comm(const void *p1, const void *p2)
135 {
136         const struct block_list *l1 = p1, *l2 = p2;
137
138         return strcmp(l1->comm, l2->comm);
139 }
140
141 static int compare_ts(const void *p1, const void *p2)
142 {
143         const struct block_list *l1 = p1, *l2 = p2;
144
145         return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
146 }
147
148 static int compare_free_ts(const void *p1, const void *p2)
149 {
150         const struct block_list *l1 = p1, *l2 = p2;
151
152         return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
153 }
154
155 static int compare_release(const void *p1, const void *p2)
156 {
157         const struct block_list *l1 = p1, *l2 = p2;
158
159         if (!l1->free_ts_nsec && !l2->free_ts_nsec)
160                 return 0;
161         if (l1->free_ts_nsec && l2->free_ts_nsec)
162                 return 0;
163         return l1->free_ts_nsec ? 1 : -1;
164 }
165
166 static int compare_cull_condition(const void *p1, const void *p2)
167 {
168         if (cull == 0)
169                 return compare_txt(p1, p2);
170         if ((cull & CULL_STACKTRACE) && compare_stacktrace(p1, p2))
171                 return compare_stacktrace(p1, p2);
172         if ((cull & CULL_PID) && compare_pid(p1, p2))
173                 return compare_pid(p1, p2);
174         if ((cull & CULL_TGID) && compare_tgid(p1, p2))
175                 return compare_tgid(p1, p2);
176         if ((cull & CULL_COMM) && compare_comm(p1, p2))
177                 return compare_comm(p1, p2);
178         if ((cull & CULL_UNRELEASE) && compare_release(p1, p2))
179                 return compare_release(p1, p2);
180         return 0;
181 }
182
183 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
184 {
185         int err, val_len;
186         regmatch_t pmatch[2];
187
188         err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
189         if (err != 0 || pmatch[1].rm_so == -1) {
190                 fprintf(stderr, "no matching pattern in %s\n", buf);
191                 return -1;
192         }
193         val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
194
195         memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
196
197         return 0;
198 }
199
200 static void check_regcomp(regex_t *pattern, const char *regex)
201 {
202         int err;
203
204         err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
205         if (err != 0 || pattern->re_nsub != 1) {
206                 fprintf(stderr, "Invalid pattern %s code %d\n", regex, err);
207                 exit(1);
208         }
209 }
210
211 static char **explode(char sep, const char *str, int *size)
212 {
213         int count = 0, len = strlen(str);
214         int lastindex = -1, j = 0;
215
216         for (int i = 0; i < len; i++)
217                 if (str[i] == sep)
218                         count++;
219         char **ret = calloc(++count, sizeof(char *));
220
221         for (int i = 0; i < len; i++) {
222                 if (str[i] == sep) {
223                         ret[j] = calloc(i - lastindex, sizeof(char));
224                         memcpy(ret[j++], str + lastindex + 1, i - lastindex - 1);
225                         lastindex = i;
226                 }
227         }
228         if (lastindex <= len - 1) {
229                 ret[j] = calloc(len - lastindex, sizeof(char));
230                 memcpy(ret[j++], str + lastindex + 1, strlen(str) - 1 - lastindex);
231         }
232         *size = j;
233         return ret;
234 }
235
236 static void free_explode(char **arr, int size)
237 {
238         for (int i = 0; i < size; i++)
239                 free(arr[i]);
240         free(arr);
241 }
242
243 # define FIELD_BUFF 25
244
245 static int get_page_num(char *buf)
246 {
247         int order_val;
248         char order_str[FIELD_BUFF] = {0};
249         char *endptr;
250
251         search_pattern(&order_pattern, order_str, buf);
252         errno = 0;
253         order_val = strtol(order_str, &endptr, 10);
254         if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
255                 fprintf(stderr, "wrong order in follow buf:\n%s\n", buf);
256                 return 0;
257         }
258
259         return 1 << order_val;
260 }
261
262 static pid_t get_pid(char *buf)
263 {
264         pid_t pid;
265         char pid_str[FIELD_BUFF] = {0};
266         char *endptr;
267
268         search_pattern(&pid_pattern, pid_str, buf);
269         errno = 0;
270         pid = strtol(pid_str, &endptr, 10);
271         if (errno != 0 || endptr == pid_str || *endptr != '\0') {
272                 fprintf(stderr, "wrong/invalid pid in follow buf:\n%s\n", buf);
273                 return -1;
274         }
275
276         return pid;
277
278 }
279
280 static pid_t get_tgid(char *buf)
281 {
282         pid_t tgid;
283         char tgid_str[FIELD_BUFF] = {0};
284         char *endptr;
285
286         search_pattern(&tgid_pattern, tgid_str, buf);
287         errno = 0;
288         tgid = strtol(tgid_str, &endptr, 10);
289         if (errno != 0 || endptr == tgid_str || *endptr != '\0') {
290                 fprintf(stderr, "wrong/invalid tgid in follow buf:\n%s\n", buf);
291                 return -1;
292         }
293
294         return tgid;
295
296 }
297
298 static __u64 get_ts_nsec(char *buf)
299 {
300         __u64 ts_nsec;
301         char ts_nsec_str[FIELD_BUFF] = {0};
302         char *endptr;
303
304         search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
305         errno = 0;
306         ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
307         if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
308                 fprintf(stderr, "wrong ts_nsec in follow buf:\n%s\n", buf);
309                 return -1;
310         }
311
312         return ts_nsec;
313 }
314
315 static __u64 get_free_ts_nsec(char *buf)
316 {
317         __u64 free_ts_nsec;
318         char free_ts_nsec_str[FIELD_BUFF] = {0};
319         char *endptr;
320
321         search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
322         errno = 0;
323         free_ts_nsec = strtoull(free_ts_nsec_str, &endptr, 10);
324         if (errno != 0 || endptr == free_ts_nsec_str || *endptr != '\0') {
325                 fprintf(stderr, "wrong free_ts_nsec in follow buf:\n%s\n", buf);
326                 return -1;
327         }
328
329         return free_ts_nsec;
330 }
331
332 static char *get_comm(char *buf)
333 {
334         char *comm_str = malloc(TASK_COMM_LEN);
335
336         memset(comm_str, 0, TASK_COMM_LEN);
337
338         search_pattern(&comm_pattern, comm_str, buf);
339         errno = 0;
340         if (errno != 0) {
341                 fprintf(stderr, "wrong comm in follow buf:\n%s\n", buf);
342                 return NULL;
343         }
344
345         return comm_str;
346 }
347
348 static bool match_num_list(int num, int *list, int list_size)
349 {
350         for (int i = 0; i < list_size; ++i)
351                 if (list[i] == num)
352                         return true;
353         return false;
354 }
355
356 static bool match_str_list(const char *str, char **list, int list_size)
357 {
358         for (int i = 0; i < list_size; ++i)
359                 if (!strcmp(list[i], str))
360                         return true;
361         return false;
362 }
363
364 static bool is_need(char *buf)
365 {
366                 if ((filter & FILTER_UNRELEASE) && get_free_ts_nsec(buf) != 0)
367                         return false;
368                 if ((filter & FILTER_PID) && !match_num_list(get_pid(buf), fc.pids, fc.pids_size))
369                         return false;
370                 if ((filter & FILTER_TGID) &&
371                         !match_num_list(get_tgid(buf), fc.tgids, fc.tgids_size))
372                         return false;
373
374                 char *comm = get_comm(buf);
375
376                 if ((filter & FILTER_COMM) &&
377                 !match_str_list(comm, fc.comms, fc.comms_size)) {
378                         free(comm);
379                         return false;
380                 }
381                 free(comm);
382                 return true;
383 }
384
385 static void add_list(char *buf, int len)
386 {
387         if (list_size != 0 &&
388                 len == list[list_size-1].len &&
389                 memcmp(buf, list[list_size-1].txt, len) == 0) {
390                 list[list_size-1].num++;
391                 list[list_size-1].page_num += get_page_num(buf);
392                 return;
393         }
394         if (list_size == max_size) {
395                 fprintf(stderr, "max_size too small??\n");
396                 exit(1);
397         }
398         if (!is_need(buf))
399                 return;
400         list[list_size].pid = get_pid(buf);
401         list[list_size].tgid = get_tgid(buf);
402         list[list_size].comm = get_comm(buf);
403         list[list_size].txt = malloc(len+1);
404         if (!list[list_size].txt) {
405                 fprintf(stderr, "Out of memory\n");
406                 exit(1);
407         }
408         memcpy(list[list_size].txt, buf, len);
409         list[list_size].txt[len] = 0;
410         list[list_size].len = len;
411         list[list_size].num = 1;
412         list[list_size].page_num = get_page_num(buf);
413
414         list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
415         if (*list[list_size].stacktrace == '\n')
416                 list[list_size].stacktrace++;
417         list[list_size].ts_nsec = get_ts_nsec(buf);
418         list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
419         list_size++;
420         if (list_size % 1000 == 0) {
421                 printf("loaded %d\r", list_size);
422                 fflush(stdout);
423         }
424 }
425
426 static bool parse_cull_args(const char *arg_str)
427 {
428         int size = 0;
429         char **args = explode(',', arg_str, &size);
430
431         for (int i = 0; i < size; ++i)
432                 if (!strcmp(args[i], "pid") || !strcmp(args[i], "p"))
433                         cull |= CULL_PID;
434                 else if (!strcmp(args[i], "tgid") || !strcmp(args[i], "tg"))
435                         cull |= CULL_TGID;
436                 else if (!strcmp(args[i], "name") || !strcmp(args[i], "n"))
437                         cull |= CULL_COMM;
438                 else if (!strcmp(args[i], "stacktrace") || !strcmp(args[i], "st"))
439                         cull |= CULL_STACKTRACE;
440                 else if (!strcmp(args[i], "free") || !strcmp(args[i], "f"))
441                         cull |= CULL_UNRELEASE;
442                 else {
443                         free_explode(args, size);
444                         return false;
445                 }
446         free_explode(args, size);
447         return true;
448 }
449
450 static int *parse_nums_list(char *arg_str, int *list_size)
451 {
452         int size = 0;
453         char **args = explode(',', arg_str, &size);
454         int *list = calloc(size, sizeof(int));
455
456         errno = 0;
457         for (int i = 0; i < size; ++i) {
458                 char *endptr = NULL;
459
460                 list[i] = strtol(args[i], &endptr, 10);
461                 if (errno != 0 || endptr == args[i] || *endptr != '\0') {
462                         free(list);
463                         return NULL;
464                 }
465         }
466         *list_size = size;
467         free_explode(args, size);
468         return list;
469 }
470
471 #define BUF_SIZE        (128 * 1024)
472
473 static void usage(void)
474 {
475         printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
476                 "-m\t\tSort by total memory.\n"
477                 "-s\t\tSort by the stack trace.\n"
478                 "-t\t\tSort by times (default).\n"
479                 "-p\t\tSort by pid.\n"
480                 "-P\t\tSort by tgid.\n"
481                 "-n\t\tSort by task command name.\n"
482                 "-a\t\tSort by memory allocate time.\n"
483                 "-r\t\tSort by memory release time.\n"
484                 "-f\t\tFilter out the information of blocks whose memory has been released.\n"
485                 "--pid <pidlist>\tSelect by pid. This selects the information of blocks whose process ID numbers appear in <pidlist>.\n"
486                 "--tgid <tgidlist>\tSelect by tgid. This selects the information of blocks whose Thread Group ID numbers appear in <tgidlist>.\n"
487                 "--name <cmdlist>\n\t\tSelect by command name. This selects the information of blocks whose command name appears in <cmdlist>.\n"
488                 "--cull <rules>\tCull by user-defined rules. <rules> is a single argument in the form of a comma-separated list with some common fields predefined\n"
489         );
490 }
491
492 int main(int argc, char **argv)
493 {
494         int (*cmp)(const void *, const void *) = compare_num;
495         FILE *fin, *fout;
496         char *buf;
497         int ret, i, count;
498         struct stat st;
499         int opt;
500         struct option longopts[] = {
501                 { "pid", required_argument, NULL, 1 },
502                 { "tgid", required_argument, NULL, 2 },
503                 { "name", required_argument, NULL, 3 },
504                 { "cull",  required_argument, NULL, 4 },
505                 { 0, 0, 0, 0},
506         };
507
508         while ((opt = getopt_long(argc, argv, "afmnprstP", longopts, NULL)) != -1)
509                 switch (opt) {
510                 case 'a':
511                         cmp = compare_ts;
512                         break;
513                 case 'f':
514                         filter = filter | FILTER_UNRELEASE;
515                         break;
516                 case 'm':
517                         cmp = compare_page_num;
518                         break;
519                 case 'p':
520                         cmp = compare_pid;
521                         break;
522                 case 'r':
523                         cmp = compare_free_ts;
524                         break;
525                 case 's':
526                         cmp = compare_stacktrace;
527                         break;
528                 case 't':
529                         cmp = compare_num;
530                         break;
531                 case 'P':
532                         cmp = compare_tgid;
533                         break;
534                 case 'n':
535                         cmp = compare_comm;
536                         break;
537                 case 1:
538                         filter = filter | FILTER_PID;
539                         fc.pids = parse_nums_list(optarg, &fc.pids_size);
540                         if (fc.pids == NULL) {
541                                 fprintf(stderr, "wrong/invalid pid in from the command line:%s\n",
542                                                 optarg);
543                                 exit(1);
544                         }
545                         break;
546                 case 2:
547                         filter = filter | FILTER_TGID;
548                         fc.tgids = parse_nums_list(optarg, &fc.tgids_size);
549                         if (fc.tgids == NULL) {
550                                 fprintf(stderr, "wrong/invalid tgid in from the command line:%s\n",
551                                                 optarg);
552                                 exit(1);
553                         }
554                         break;
555                 case 3:
556                         filter = filter | FILTER_COMM;
557                         fc.comms = explode(',', optarg, &fc.comms_size);
558                         break;
559                 case 4:
560                         if (!parse_cull_args(optarg)) {
561                                 fprintf(stderr, "wrong argument after --cull option:%s\n",
562                                                 optarg);
563                                 exit(1);
564                         }
565                         break;
566                 default:
567                         usage();
568                         exit(1);
569                 }
570
571         if (optind >= (argc - 1)) {
572                 usage();
573                 exit(1);
574         }
575
576         fin = fopen(argv[optind], "r");
577         fout = fopen(argv[optind + 1], "w");
578         if (!fin || !fout) {
579                 usage();
580                 perror("open: ");
581                 exit(1);
582         }
583
584         check_regcomp(&order_pattern, "order\\s*([0-9]*),");
585         check_regcomp(&pid_pattern, "pid\\s*([0-9]*),");
586         check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) ");
587         check_regcomp(&comm_pattern, "tgid\\s*[0-9]*\\s*\\((.*)\\),\\s*ts");
588         check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,");
589         check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns");
590         fstat(fileno(fin), &st);
591         max_size = st.st_size / 100; /* hack ... */
592
593         list = malloc(max_size * sizeof(*list));
594         buf = malloc(BUF_SIZE);
595         if (!list || !buf) {
596                 fprintf(stderr, "Out of memory\n");
597                 exit(1);
598         }
599
600         for ( ; ; ) {
601                 ret = read_block(buf, BUF_SIZE, fin);
602                 if (ret < 0)
603                         break;
604                 add_list(buf, ret);
605         }
606
607         printf("loaded %d\n", list_size);
608
609         printf("sorting ....\n");
610
611         qsort(list, list_size, sizeof(list[0]), compare_cull_condition);
612
613         printf("culling\n");
614
615         for (i = count = 0; i < list_size; i++) {
616                 if (count == 0 ||
617                     compare_cull_condition((void *)(&list[count-1]), (void *)(&list[i])) != 0) {
618                         list[count++] = list[i];
619                 } else {
620                         list[count-1].num += list[i].num;
621                         list[count-1].page_num += list[i].page_num;
622                 }
623         }
624
625         qsort(list, count, sizeof(list[0]), cmp);
626
627         for (i = 0; i < count; i++) {
628                 if (cull == 0)
629                         fprintf(fout, "%d times, %d pages:\n%s\n",
630                                         list[i].num, list[i].page_num, list[i].txt);
631                 else {
632                         fprintf(fout, "%d times, %d pages",
633                                         list[i].num, list[i].page_num);
634                         if (cull & CULL_PID || filter & FILTER_PID)
635                                 fprintf(fout, ", PID %d", list[i].pid);
636                         if (cull & CULL_TGID || filter & FILTER_TGID)
637                                 fprintf(fout, ", TGID %d", list[i].pid);
638                         if (cull & CULL_COMM || filter & FILTER_COMM)
639                                 fprintf(fout, ", task_comm_name: %s", list[i].comm);
640                         if (cull & CULL_UNRELEASE)
641                                 fprintf(fout, " (%s)",
642                                                 list[i].free_ts_nsec ? "UNRELEASED" : "RELEASED");
643                         if (cull & CULL_STACKTRACE)
644                                 fprintf(fout, ":\n%s", list[i].stacktrace);
645                         fprintf(fout, "\n");
646                 }
647         }
648         regfree(&order_pattern);
649         regfree(&pid_pattern);
650         regfree(&tgid_pattern);
651         regfree(&comm_pattern);
652         regfree(&ts_nsec_pattern);
653         regfree(&free_ts_nsec_pattern);
654         return 0;
655 }