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