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/vm/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
44 FILTER_UNRELEASE = 1<<1,
50 CULL_UNRELEASE = 1<<1,
54 CULL_STACKTRACE = 1<<5
56 struct filter_condition {
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;
77 int read_block(char *buf, int buf_size, FILE *fin)
79 char *curr = buf, *const buf_end = buf + buf_size;
81 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
82 if (*curr == '\n') /* empty line */
84 if (!strncmp(curr, "PFN", 3))
89 return -1; /* EOF or no space left in buf. */
92 static int compare_txt(const void *p1, const void *p2)
94 const struct block_list *l1 = p1, *l2 = p2;
96 return strcmp(l1->txt, l2->txt);
99 static int compare_stacktrace(const void *p1, const void *p2)
101 const struct block_list *l1 = p1, *l2 = p2;
103 return strcmp(l1->stacktrace, l2->stacktrace);
106 static int compare_num(const void *p1, const void *p2)
108 const struct block_list *l1 = p1, *l2 = p2;
110 return l2->num - l1->num;
113 static int compare_page_num(const void *p1, const void *p2)
115 const struct block_list *l1 = p1, *l2 = p2;
117 return l2->page_num - l1->page_num;
120 static int compare_pid(const void *p1, const void *p2)
122 const struct block_list *l1 = p1, *l2 = p2;
124 return l1->pid - l2->pid;
127 static int compare_tgid(const void *p1, const void *p2)
129 const struct block_list *l1 = p1, *l2 = p2;
131 return l1->tgid - l2->tgid;
134 static int compare_comm(const void *p1, const void *p2)
136 const struct block_list *l1 = p1, *l2 = p2;
138 return strcmp(l1->comm, l2->comm);
141 static int compare_ts(const void *p1, const void *p2)
143 const struct block_list *l1 = p1, *l2 = p2;
145 return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
148 static int compare_free_ts(const void *p1, const void *p2)
150 const struct block_list *l1 = p1, *l2 = p2;
152 return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
155 static int compare_release(const void *p1, const void *p2)
157 const struct block_list *l1 = p1, *l2 = p2;
159 if (!l1->free_ts_nsec && !l2->free_ts_nsec)
161 if (l1->free_ts_nsec && l2->free_ts_nsec)
163 return l1->free_ts_nsec ? 1 : -1;
166 static int compare_cull_condition(const void *p1, const void *p2)
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);
183 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
186 regmatch_t pmatch[2];
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);
193 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
195 memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
200 static void check_regcomp(regex_t *pattern, const char *regex)
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);
211 static char **explode(char sep, const char *str, int *size)
213 int count = 0, len = strlen(str);
214 int lastindex = -1, j = 0;
216 for (int i = 0; i < len; i++)
219 char **ret = calloc(++count, sizeof(char *));
221 for (int i = 0; i < len; i++) {
223 ret[j] = calloc(i - lastindex, sizeof(char));
224 memcpy(ret[j++], str + lastindex + 1, i - lastindex - 1);
228 if (lastindex <= len - 1) {
229 ret[j] = calloc(len - lastindex, sizeof(char));
230 memcpy(ret[j++], str + lastindex + 1, strlen(str) - 1 - lastindex);
236 static void free_explode(char **arr, int size)
238 for (int i = 0; i < size; i++)
243 # define FIELD_BUFF 25
245 static int get_page_num(char *buf)
248 char order_str[FIELD_BUFF] = {0};
251 search_pattern(&order_pattern, order_str, buf);
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);
259 return 1 << order_val;
262 static pid_t get_pid(char *buf)
265 char pid_str[FIELD_BUFF] = {0};
268 search_pattern(&pid_pattern, pid_str, buf);
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);
280 static pid_t get_tgid(char *buf)
283 char tgid_str[FIELD_BUFF] = {0};
286 search_pattern(&tgid_pattern, tgid_str, buf);
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);
298 static __u64 get_ts_nsec(char *buf)
301 char ts_nsec_str[FIELD_BUFF] = {0};
304 search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
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);
315 static __u64 get_free_ts_nsec(char *buf)
318 char free_ts_nsec_str[FIELD_BUFF] = {0};
321 search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
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);
332 static char *get_comm(char *buf)
334 char *comm_str = malloc(TASK_COMM_LEN);
336 memset(comm_str, 0, TASK_COMM_LEN);
338 search_pattern(&comm_pattern, comm_str, buf);
341 fprintf(stderr, "wrong comm in follow buf:\n%s\n", buf);
348 static bool match_num_list(int num, int *list, int list_size)
350 for (int i = 0; i < list_size; ++i)
356 static bool match_str_list(const char *str, char **list, int list_size)
358 for (int i = 0; i < list_size; ++i)
359 if (!strcmp(list[i], str))
364 static bool is_need(char *buf)
366 if ((filter & FILTER_UNRELEASE) && get_free_ts_nsec(buf) != 0)
368 if ((filter & FILTER_PID) && !match_num_list(get_pid(buf), fc.pids, fc.pids_size))
370 if ((filter & FILTER_TGID) &&
371 !match_num_list(get_tgid(buf), fc.tgids, fc.tgids_size))
374 char *comm = get_comm(buf);
376 if ((filter & FILTER_COMM) &&
377 !match_str_list(comm, fc.comms, fc.comms_size)) {
385 static void add_list(char *buf, int len)
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);
394 if (list_size == max_size) {
395 fprintf(stderr, "max_size too small??\n");
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");
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);
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);
420 if (list_size % 1000 == 0) {
421 printf("loaded %d\r", list_size);
426 static bool parse_cull_args(const char *arg_str)
429 char **args = explode(',', arg_str, &size);
431 for (int i = 0; i < size; ++i)
432 if (!strcmp(args[i], "pid") || !strcmp(args[i], "p"))
434 else if (!strcmp(args[i], "tgid") || !strcmp(args[i], "tg"))
436 else if (!strcmp(args[i], "name") || !strcmp(args[i], "n"))
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;
443 free_explode(args, size);
446 free_explode(args, size);
450 static int *parse_nums_list(char *arg_str, int *list_size)
453 char **args = explode(',', arg_str, &size);
454 int *list = calloc(size, sizeof(int));
457 for (int i = 0; i < size; ++i) {
460 list[i] = strtol(args[i], &endptr, 10);
461 if (errno != 0 || endptr == args[i] || *endptr != '\0') {
467 free_explode(args, size);
471 #define BUF_SIZE (128 * 1024)
473 static void usage(void)
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"
492 int main(int argc, char **argv)
494 int (*cmp)(const void *, const void *) = compare_num;
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 },
508 while ((opt = getopt_long(argc, argv, "afmnprstP", longopts, NULL)) != -1)
514 filter = filter | FILTER_UNRELEASE;
517 cmp = compare_page_num;
523 cmp = compare_free_ts;
526 cmp = compare_stacktrace;
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",
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",
556 filter = filter | FILTER_COMM;
557 fc.comms = explode(',', optarg, &fc.comms_size);
560 if (!parse_cull_args(optarg)) {
561 fprintf(stderr, "wrong argument after --cull option:%s\n",
571 if (optind >= (argc - 1)) {
576 fin = fopen(argv[optind], "r");
577 fout = fopen(argv[optind + 1], "w");
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 ... */
593 list = malloc(max_size * sizeof(*list));
594 buf = malloc(BUF_SIZE);
596 fprintf(stderr, "Out of memory\n");
601 ret = read_block(buf, BUF_SIZE, fin);
607 printf("loaded %d\n", list_size);
609 printf("sorting ....\n");
611 qsort(list, list_size, sizeof(list[0]), compare_cull_condition);
615 for (i = count = 0; i < list_size; i++) {
617 compare_cull_condition((void *)(&list[count-1]), (void *)(&list[i])) != 0) {
618 list[count++] = list[i];
620 list[count-1].num += list[i].num;
621 list[count-1].page_num += list[i].page_num;
625 qsort(list, count, sizeof(list[0]), cmp);
627 for (i = 0; i < count; i++) {
629 fprintf(fout, "%d times, %d pages:\n%s\n",
630 list[i].num, list[i].page_num, list[i].txt);
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);
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);