1 // SPDX-License-Identifier: GPL-2.0
3 * User-space helper to sort the output of /sys/kernel/debug/page_owner
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
11 * See Documentation/mm/page_owner.rst
16 #include <sys/types.h>
23 #include <linux/types.h>
29 #define TASK_COMM_LEN 16
33 char *comm; // task command name
45 FILTER_UNRELEASE = 1<<1,
51 CULL_UNRELEASE = 1<<1,
55 CULL_STACKTRACE = 1<<5,
60 ALLOCATOR_SLAB = 1<<2,
61 ALLOCATOR_VMALLOC = 1<<3,
62 ALLOCATOR_OTHERS = 1<<4
65 ARG_TXT, ARG_COMM, ARG_STACKTRACE, ARG_ALLOC_TS, ARG_FREE_TS,
66 ARG_CULL_TIME, ARG_PAGE_NUM, ARG_PID, ARG_TGID, ARG_UNKNOWN, ARG_FREE,
73 struct filter_condition {
81 struct sort_condition {
82 int (**cmps)(const void *, const void *);
86 static struct filter_condition fc;
87 static struct sort_condition sc;
88 static regex_t order_pattern;
89 static regex_t pid_pattern;
90 static regex_t tgid_pattern;
91 static regex_t comm_pattern;
92 static regex_t ts_nsec_pattern;
93 static regex_t free_ts_nsec_pattern;
94 static struct block_list *list;
101 static void set_single_cmp(int (*cmp)(const void *, const void *), int sign);
103 int read_block(char *buf, char *ext_buf, int buf_size, FILE *fin)
105 char *curr = buf, *const buf_end = buf + buf_size;
107 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
108 if (*curr == '\n') { /* empty line */
111 if (!strncmp(curr, "PFN", 3)) {
112 strcpy(ext_buf, curr);
115 curr += strlen(curr);
118 return -1; /* EOF or no space left in buf. */
121 static int compare_txt(const void *p1, const void *p2)
123 const struct block_list *l1 = p1, *l2 = p2;
125 return strcmp(l1->txt, l2->txt);
128 static int compare_stacktrace(const void *p1, const void *p2)
130 const struct block_list *l1 = p1, *l2 = p2;
132 return strcmp(l1->stacktrace, l2->stacktrace);
135 static int compare_num(const void *p1, const void *p2)
137 const struct block_list *l1 = p1, *l2 = p2;
139 return l1->num - l2->num;
142 static int compare_page_num(const void *p1, const void *p2)
144 const struct block_list *l1 = p1, *l2 = p2;
146 return l1->page_num - l2->page_num;
149 static int compare_pid(const void *p1, const void *p2)
151 const struct block_list *l1 = p1, *l2 = p2;
153 return l1->pid - l2->pid;
156 static int compare_tgid(const void *p1, const void *p2)
158 const struct block_list *l1 = p1, *l2 = p2;
160 return l1->tgid - l2->tgid;
163 static int compare_allocator(const void *p1, const void *p2)
165 const struct block_list *l1 = p1, *l2 = p2;
167 return l1->allocator - l2->allocator;
170 static int compare_comm(const void *p1, const void *p2)
172 const struct block_list *l1 = p1, *l2 = p2;
174 return strcmp(l1->comm, l2->comm);
177 static int compare_ts(const void *p1, const void *p2)
179 const struct block_list *l1 = p1, *l2 = p2;
181 return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
184 static int compare_free_ts(const void *p1, const void *p2)
186 const struct block_list *l1 = p1, *l2 = p2;
188 return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
191 static int compare_release(const void *p1, const void *p2)
193 const struct block_list *l1 = p1, *l2 = p2;
195 if (!l1->free_ts_nsec && !l2->free_ts_nsec)
197 if (l1->free_ts_nsec && l2->free_ts_nsec)
199 return l1->free_ts_nsec ? 1 : -1;
202 static int compare_cull_condition(const void *p1, const void *p2)
205 return compare_txt(p1, p2);
206 if ((cull & CULL_STACKTRACE) && compare_stacktrace(p1, p2))
207 return compare_stacktrace(p1, p2);
208 if ((cull & CULL_PID) && compare_pid(p1, p2))
209 return compare_pid(p1, p2);
210 if ((cull & CULL_TGID) && compare_tgid(p1, p2))
211 return compare_tgid(p1, p2);
212 if ((cull & CULL_COMM) && compare_comm(p1, p2))
213 return compare_comm(p1, p2);
214 if ((cull & CULL_UNRELEASE) && compare_release(p1, p2))
215 return compare_release(p1, p2);
216 if ((cull & CULL_ALLOCATOR) && compare_allocator(p1, p2))
217 return compare_allocator(p1, p2);
221 static int compare_sort_condition(const void *p1, const void *p2)
225 for (int i = 0; i < sc.size; ++i)
227 cmp = sc.signs[i] * sc.cmps[i](p1, p2);
231 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
234 regmatch_t pmatch[2];
236 err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
237 if (err != 0 || pmatch[1].rm_so == -1) {
239 fprintf(stderr, "no matching pattern in %s\n", buf);
242 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
244 memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
249 static bool check_regcomp(regex_t *pattern, const char *regex)
253 err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
254 if (err != 0 || pattern->re_nsub != 1) {
255 fprintf(stderr, "Invalid pattern %s code %d\n", regex, err);
261 static char **explode(char sep, const char *str, int *size)
263 int count = 0, len = strlen(str);
264 int lastindex = -1, j = 0;
266 for (int i = 0; i < len; i++)
269 char **ret = calloc(++count, sizeof(char *));
271 for (int i = 0; i < len; i++) {
273 ret[j] = calloc(i - lastindex, sizeof(char));
274 memcpy(ret[j++], str + lastindex + 1, i - lastindex - 1);
278 if (lastindex <= len - 1) {
279 ret[j] = calloc(len - lastindex, sizeof(char));
280 memcpy(ret[j++], str + lastindex + 1, strlen(str) - 1 - lastindex);
286 static void free_explode(char **arr, int size)
288 for (int i = 0; i < size; i++)
293 # define FIELD_BUFF 25
295 static int get_page_num(char *buf)
298 char order_str[FIELD_BUFF] = {0};
301 search_pattern(&order_pattern, order_str, buf);
303 order_val = strtol(order_str, &endptr, 10);
304 if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
306 fprintf(stderr, "wrong order in follow buf:\n%s\n", buf);
310 return 1 << order_val;
313 static pid_t get_pid(char *buf)
316 char pid_str[FIELD_BUFF] = {0};
319 search_pattern(&pid_pattern, pid_str, buf);
321 pid = strtol(pid_str, &endptr, 10);
322 if (errno != 0 || endptr == pid_str || *endptr != '\0') {
324 fprintf(stderr, "wrong/invalid pid in follow buf:\n%s\n", buf);
332 static pid_t get_tgid(char *buf)
335 char tgid_str[FIELD_BUFF] = {0};
338 search_pattern(&tgid_pattern, tgid_str, buf);
340 tgid = strtol(tgid_str, &endptr, 10);
341 if (errno != 0 || endptr == tgid_str || *endptr != '\0') {
343 fprintf(stderr, "wrong/invalid tgid in follow buf:\n%s\n", buf);
351 static __u64 get_ts_nsec(char *buf)
354 char ts_nsec_str[FIELD_BUFF] = {0};
357 search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
359 ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
360 if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
362 fprintf(stderr, "wrong ts_nsec in follow buf:\n%s\n", buf);
369 static __u64 get_free_ts_nsec(char *buf)
372 char free_ts_nsec_str[FIELD_BUFF] = {0};
375 search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
377 free_ts_nsec = strtoull(free_ts_nsec_str, &endptr, 10);
378 if (errno != 0 || endptr == free_ts_nsec_str || *endptr != '\0') {
380 fprintf(stderr, "wrong free_ts_nsec in follow buf:\n%s\n", buf);
387 static char *get_comm(char *buf)
389 char *comm_str = malloc(TASK_COMM_LEN);
391 memset(comm_str, 0, TASK_COMM_LEN);
393 search_pattern(&comm_pattern, comm_str, buf);
397 fprintf(stderr, "wrong comm in follow buf:\n%s\n", buf);
404 static int get_arg_type(const char *arg)
406 if (!strcmp(arg, "pid") || !strcmp(arg, "p"))
408 else if (!strcmp(arg, "tgid") || !strcmp(arg, "tg"))
410 else if (!strcmp(arg, "name") || !strcmp(arg, "n"))
412 else if (!strcmp(arg, "stacktrace") || !strcmp(arg, "st"))
413 return ARG_STACKTRACE;
414 else if (!strcmp(arg, "free") || !strcmp(arg, "f"))
416 else if (!strcmp(arg, "txt") || !strcmp(arg, "T"))
418 else if (!strcmp(arg, "free_ts") || !strcmp(arg, "ft"))
420 else if (!strcmp(arg, "alloc_ts") || !strcmp(arg, "at"))
422 else if (!strcmp(arg, "allocator") || !strcmp(arg, "ator"))
423 return ARG_ALLOCATOR;
429 static int get_allocator(const char *buf, const char *migrate_info)
431 char *tmp, *first_line, *second_line;
434 if (strstr(migrate_info, "CMA"))
435 allocator |= ALLOCATOR_CMA;
436 if (strstr(migrate_info, "slab"))
437 allocator |= ALLOCATOR_SLAB;
438 tmp = strstr(buf, "__vmalloc_node_range");
447 tmp = strstr(tmp, "alloc_pages");
448 if (tmp && first_line <= tmp && tmp < second_line)
449 allocator |= ALLOCATOR_VMALLOC;
452 allocator = ALLOCATOR_OTHERS;
456 static bool match_num_list(int num, int *list, int list_size)
458 for (int i = 0; i < list_size; ++i)
464 static bool match_str_list(const char *str, char **list, int list_size)
466 for (int i = 0; i < list_size; ++i)
467 if (!strcmp(list[i], str))
472 static bool is_need(char *buf)
474 __u64 ts_nsec, free_ts_nsec;
476 ts_nsec = get_ts_nsec(buf);
477 free_ts_nsec = get_free_ts_nsec(buf);
479 if ((filter & FILTER_UNRELEASE) && free_ts_nsec != 0 && ts_nsec < free_ts_nsec)
481 if ((filter & FILTER_PID) && !match_num_list(get_pid(buf), fc.pids, fc.pids_size))
483 if ((filter & FILTER_TGID) &&
484 !match_num_list(get_tgid(buf), fc.tgids, fc.tgids_size))
487 char *comm = get_comm(buf);
489 if ((filter & FILTER_COMM) &&
490 !match_str_list(comm, fc.comms, fc.comms_size)) {
498 static bool add_list(char *buf, int len, char *ext_buf)
500 if (list_size != 0 &&
501 len == list[list_size-1].len &&
502 memcmp(buf, list[list_size-1].txt, len) == 0) {
503 list[list_size-1].num++;
504 list[list_size-1].page_num += get_page_num(buf);
507 if (list_size == max_size) {
508 fprintf(stderr, "max_size too small??\n");
513 list[list_size].pid = get_pid(buf);
514 list[list_size].tgid = get_tgid(buf);
515 list[list_size].comm = get_comm(buf);
516 list[list_size].txt = malloc(len+1);
517 if (!list[list_size].txt) {
518 fprintf(stderr, "Out of memory\n");
521 memcpy(list[list_size].txt, buf, len);
522 list[list_size].txt[len] = 0;
523 list[list_size].len = len;
524 list[list_size].num = 1;
525 list[list_size].page_num = get_page_num(buf);
527 list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
528 if (*list[list_size].stacktrace == '\n')
529 list[list_size].stacktrace++;
530 list[list_size].ts_nsec = get_ts_nsec(buf);
531 list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
532 list[list_size].allocator = get_allocator(buf, ext_buf);
534 if (list_size % 1000 == 0) {
535 printf("loaded %d\r", list_size);
541 static bool parse_cull_args(const char *arg_str)
544 char **args = explode(',', arg_str, &size);
546 for (int i = 0; i < size; ++i) {
547 int arg_type = get_arg_type(args[i]);
549 if (arg_type == ARG_PID)
551 else if (arg_type == ARG_TGID)
553 else if (arg_type == ARG_COMM)
555 else if (arg_type == ARG_STACKTRACE)
556 cull |= CULL_STACKTRACE;
557 else if (arg_type == ARG_FREE)
558 cull |= CULL_UNRELEASE;
559 else if (arg_type == ARG_ALLOCATOR)
560 cull |= CULL_ALLOCATOR;
562 free_explode(args, size);
566 free_explode(args, size);
568 set_single_cmp(compare_num, SORT_DESC);
572 static void set_single_cmp(int (*cmp)(const void *, const void *), int sign)
574 if (sc.signs == NULL || sc.size < 1)
575 sc.signs = calloc(1, sizeof(int));
577 if (sc.cmps == NULL || sc.size < 1)
578 sc.cmps = calloc(1, sizeof(int *));
583 static bool parse_sort_args(const char *arg_str)
587 if (sc.size != 0) { /* reset sort_condition */
593 char **args = explode(',', arg_str, &size);
595 sc.signs = calloc(size, sizeof(int));
596 sc.cmps = calloc(size, sizeof(int *));
597 for (int i = 0; i < size; ++i) {
600 sc.signs[i] = SORT_ASC;
601 if (args[i][0] == '-' || args[i][0] == '+') {
602 if (args[i][0] == '-')
603 sc.signs[i] = SORT_DESC;
607 int arg_type = get_arg_type(args[i]+offset);
609 if (arg_type == ARG_PID)
610 sc.cmps[i] = compare_pid;
611 else if (arg_type == ARG_TGID)
612 sc.cmps[i] = compare_tgid;
613 else if (arg_type == ARG_COMM)
614 sc.cmps[i] = compare_comm;
615 else if (arg_type == ARG_STACKTRACE)
616 sc.cmps[i] = compare_stacktrace;
617 else if (arg_type == ARG_ALLOC_TS)
618 sc.cmps[i] = compare_ts;
619 else if (arg_type == ARG_FREE_TS)
620 sc.cmps[i] = compare_free_ts;
621 else if (arg_type == ARG_TXT)
622 sc.cmps[i] = compare_txt;
623 else if (arg_type == ARG_ALLOCATOR)
624 sc.cmps[i] = compare_allocator;
626 free_explode(args, size);
632 free_explode(args, size);
636 static int *parse_nums_list(char *arg_str, int *list_size)
639 char **args = explode(',', arg_str, &size);
640 int *list = calloc(size, sizeof(int));
643 for (int i = 0; i < size; ++i) {
646 list[i] = strtol(args[i], &endptr, 10);
647 if (errno != 0 || endptr == args[i] || *endptr != '\0') {
653 free_explode(args, size);
657 static void print_allocator(FILE *out, int allocator)
659 fprintf(out, "allocated by ");
660 if (allocator & ALLOCATOR_CMA)
661 fprintf(out, "CMA ");
662 if (allocator & ALLOCATOR_SLAB)
663 fprintf(out, "SLAB ");
664 if (allocator & ALLOCATOR_VMALLOC)
665 fprintf(out, "VMALLOC ");
666 if (allocator & ALLOCATOR_OTHERS)
667 fprintf(out, "OTHERS ");
670 #define BUF_SIZE (128 * 1024)
672 static void usage(void)
674 printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
675 "-m\t\tSort by total memory.\n"
676 "-s\t\tSort by the stack trace.\n"
677 "-t\t\tSort by times (default).\n"
678 "-p\t\tSort by pid.\n"
679 "-P\t\tSort by tgid.\n"
680 "-n\t\tSort by task command name.\n"
681 "-a\t\tSort by memory allocate time.\n"
682 "-r\t\tSort by memory release time.\n"
683 "-f\t\tFilter out the information of blocks whose memory has been released.\n"
684 "-d\t\tPrint debug information.\n"
685 "--pid <pidlist>\tSelect by pid. This selects the information of blocks whose process ID numbers appear in <pidlist>.\n"
686 "--tgid <tgidlist>\tSelect by tgid. This selects the information of blocks whose Thread Group ID numbers appear in <tgidlist>.\n"
687 "--name <cmdlist>\n\t\tSelect by command name. This selects the information of blocks whose command name appears in <cmdlist>.\n"
688 "--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"
689 "--sort <order>\tSpecify sort order as: [+|-]key[,[+|-]key[,...]]\n"
693 int main(int argc, char **argv)
700 struct option longopts[] = {
701 { "pid", required_argument, NULL, 1 },
702 { "tgid", required_argument, NULL, 2 },
703 { "name", required_argument, NULL, 3 },
704 { "cull", required_argument, NULL, 4 },
705 { "sort", required_argument, NULL, 5 },
709 while ((opt = getopt_long(argc, argv, "adfmnprstP", longopts, NULL)) != -1)
712 set_single_cmp(compare_ts, SORT_ASC);
718 filter = filter | FILTER_UNRELEASE;
721 set_single_cmp(compare_page_num, SORT_DESC);
724 set_single_cmp(compare_pid, SORT_ASC);
727 set_single_cmp(compare_free_ts, SORT_ASC);
730 set_single_cmp(compare_stacktrace, SORT_ASC);
733 set_single_cmp(compare_num, SORT_DESC);
736 set_single_cmp(compare_tgid, SORT_ASC);
739 set_single_cmp(compare_comm, SORT_ASC);
742 filter = filter | FILTER_PID;
743 fc.pids = parse_nums_list(optarg, &fc.pids_size);
744 if (fc.pids == NULL) {
745 fprintf(stderr, "wrong/invalid pid in from the command line:%s\n",
751 filter = filter | FILTER_TGID;
752 fc.tgids = parse_nums_list(optarg, &fc.tgids_size);
753 if (fc.tgids == NULL) {
754 fprintf(stderr, "wrong/invalid tgid in from the command line:%s\n",
760 filter = filter | FILTER_COMM;
761 fc.comms = explode(',', optarg, &fc.comms_size);
764 if (!parse_cull_args(optarg)) {
765 fprintf(stderr, "wrong argument after --cull option:%s\n",
771 if (!parse_sort_args(optarg)) {
772 fprintf(stderr, "wrong argument after --sort option:%s\n",
782 if (optind >= (argc - 1)) {
787 fin = fopen(argv[optind], "r");
788 fout = fopen(argv[optind + 1], "w");
795 if (!check_regcomp(&order_pattern, "order\\s*([0-9]*),"))
797 if (!check_regcomp(&pid_pattern, "pid\\s*([0-9]*),"))
799 if (!check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) "))
801 if (!check_regcomp(&comm_pattern, "tgid\\s*[0-9]*\\s*\\((.*)\\),\\s*ts"))
803 if (!check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,"))
805 if (!check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns"))
808 fstat(fileno(fin), &st);
809 max_size = st.st_size / 100; /* hack ... */
811 list = malloc(max_size * sizeof(*list));
812 buf = malloc(BUF_SIZE);
813 ext_buf = malloc(BUF_SIZE);
814 if (!list || !buf || !ext_buf) {
815 fprintf(stderr, "Out of memory\n");
820 int buf_len = read_block(buf, ext_buf, BUF_SIZE, fin);
824 if (!add_list(buf, buf_len, ext_buf))
828 printf("loaded %d\n", list_size);
830 printf("sorting ....\n");
832 qsort(list, list_size, sizeof(list[0]), compare_cull_condition);
836 for (i = count = 0; i < list_size; i++) {
838 compare_cull_condition((void *)(&list[count-1]), (void *)(&list[i])) != 0) {
839 list[count++] = list[i];
841 list[count-1].num += list[i].num;
842 list[count-1].page_num += list[i].page_num;
846 qsort(list, count, sizeof(list[0]), compare_sort_condition);
848 for (i = 0; i < count; i++) {
850 fprintf(fout, "%d times, %d pages, ", list[i].num, list[i].page_num);
851 print_allocator(fout, list[i].allocator);
852 fprintf(fout, ":\n%s\n", list[i].txt);
855 fprintf(fout, "%d times, %d pages",
856 list[i].num, list[i].page_num);
857 if (cull & CULL_PID || filter & FILTER_PID)
858 fprintf(fout, ", PID %d", list[i].pid);
859 if (cull & CULL_TGID || filter & FILTER_TGID)
860 fprintf(fout, ", TGID %d", list[i].tgid);
861 if (cull & CULL_COMM || filter & FILTER_COMM)
862 fprintf(fout, ", task_comm_name: %s", list[i].comm);
863 if (cull & CULL_ALLOCATOR) {
865 print_allocator(fout, list[i].allocator);
867 if (cull & CULL_UNRELEASE)
868 fprintf(fout, " (%s)",
869 list[i].free_ts_nsec ? "UNRELEASED" : "RELEASED");
870 if (cull & CULL_STACKTRACE)
871 fprintf(fout, ":\n%s", list[i].stacktrace);
884 regfree(&free_ts_nsec_pattern);
886 regfree(&ts_nsec_pattern);
888 regfree(&comm_pattern);
890 regfree(&tgid_pattern);
892 regfree(&pid_pattern);
894 regfree(&order_pattern);