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 {
59 char comm[TASK_COMM_LEN];
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;
74 int read_block(char *buf, int buf_size, FILE *fin)
76 char *curr = buf, *const buf_end = buf + buf_size;
78 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
79 if (*curr == '\n') /* empty line */
81 if (!strncmp(curr, "PFN", 3))
86 return -1; /* EOF or no space left in buf. */
89 static int compare_txt(const void *p1, const void *p2)
91 const struct block_list *l1 = p1, *l2 = p2;
93 return strcmp(l1->txt, l2->txt);
96 static int compare_stacktrace(const void *p1, const void *p2)
98 const struct block_list *l1 = p1, *l2 = p2;
100 return strcmp(l1->stacktrace, l2->stacktrace);
103 static int compare_num(const void *p1, const void *p2)
105 const struct block_list *l1 = p1, *l2 = p2;
107 return l2->num - l1->num;
110 static int compare_page_num(const void *p1, const void *p2)
112 const struct block_list *l1 = p1, *l2 = p2;
114 return l2->page_num - l1->page_num;
117 static int compare_pid(const void *p1, const void *p2)
119 const struct block_list *l1 = p1, *l2 = p2;
121 return l1->pid - l2->pid;
124 static int compare_tgid(const void *p1, const void *p2)
126 const struct block_list *l1 = p1, *l2 = p2;
128 return l1->tgid - l2->tgid;
131 static int compare_comm(const void *p1, const void *p2)
133 const struct block_list *l1 = p1, *l2 = p2;
135 return strcmp(l1->comm, l2->comm);
138 static int compare_ts(const void *p1, const void *p2)
140 const struct block_list *l1 = p1, *l2 = p2;
142 return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
145 static int compare_free_ts(const void *p1, const void *p2)
147 const struct block_list *l1 = p1, *l2 = p2;
149 return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
153 static int compare_release(const void *p1, const void *p2)
155 const struct block_list *l1 = p1, *l2 = p2;
157 if (!l1->free_ts_nsec && !l2->free_ts_nsec)
159 if (l1->free_ts_nsec && l2->free_ts_nsec)
161 return l1->free_ts_nsec ? 1 : -1;
165 static int compare_cull_condition(const void *p1, const void *p2)
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);
182 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
185 regmatch_t pmatch[2];
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);
192 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
194 memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
199 static void check_regcomp(regex_t *pattern, const char *regex)
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);
210 static char **explode(char sep, const char *str, int *size)
212 int count = 0, len = strlen(str);
213 int lastindex = -1, j = 0;
215 for (int i = 0; i < len; i++)
218 char **ret = calloc(++count, sizeof(char *));
220 for (int i = 0; i < len; i++) {
222 ret[j] = calloc(i - lastindex, sizeof(char));
223 memcpy(ret[j++], str + lastindex + 1, i - lastindex - 1);
227 if (lastindex <= len - 1) {
228 ret[j] = calloc(len - lastindex, sizeof(char));
229 memcpy(ret[j++], str + lastindex + 1, strlen(str) - 1 - lastindex);
235 static void free_explode(char **arr, int size)
237 for (int i = 0; i < size; i++)
242 # define FIELD_BUFF 25
244 static int get_page_num(char *buf)
247 char order_str[FIELD_BUFF] = {0};
250 search_pattern(&order_pattern, order_str, buf);
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);
258 return 1 << order_val;
261 static pid_t get_pid(char *buf)
264 char pid_str[FIELD_BUFF] = {0};
267 search_pattern(&pid_pattern, pid_str, buf);
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);
279 static pid_t get_tgid(char *buf)
282 char tgid_str[FIELD_BUFF] = {0};
285 search_pattern(&tgid_pattern, tgid_str, buf);
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);
297 static __u64 get_ts_nsec(char *buf)
300 char ts_nsec_str[FIELD_BUFF] = {0};
303 search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
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);
314 static __u64 get_free_ts_nsec(char *buf)
317 char free_ts_nsec_str[FIELD_BUFF] = {0};
320 search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
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);
331 static char *get_comm(char *buf)
333 char *comm_str = malloc(TASK_COMM_LEN);
335 memset(comm_str, 0, TASK_COMM_LEN);
337 search_pattern(&comm_pattern, comm_str, buf);
340 printf("wrong comm in follow buf:\n%s\n", buf);
347 static bool is_need(char *buf)
349 if ((filter & FILTER_UNRELEASE) && get_free_ts_nsec(buf) != 0)
351 if ((filter & FILTER_PID) && get_pid(buf) != fc.pid)
353 if ((filter & FILTER_TGID) && get_tgid(buf) != fc.tgid)
356 char *comm = get_comm(buf);
358 if ((filter & FILTER_COMM) &&
359 strncmp(comm, fc.comm, TASK_COMM_LEN) != 0) {
366 static void add_list(char *buf, int len)
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);
375 if (list_size == max_size) {
376 printf("max_size too small??\n");
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");
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);
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);
401 if (list_size % 1000 == 0) {
402 printf("loaded %d\r", list_size);
407 static bool parse_cull_args(const char *arg_str)
410 char **args = explode(',', arg_str, &size);
412 for (int i = 0; i < size; ++i)
413 if (!strcmp(args[i], "pid") || !strcmp(args[i], "p"))
415 else if (!strcmp(args[i], "tgid") || !strcmp(args[i], "tg"))
417 else if (!strcmp(args[i], "name") || !strcmp(args[i], "n"))
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;
424 free_explode(args, size);
427 free_explode(args, size);
431 #define BUF_SIZE (128 * 1024)
433 static void usage(void)
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"
453 int main(int argc, char **argv)
455 int (*cmp)(const void *, const void *) = compare_num;
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 },
469 while ((opt = getopt_long(argc, argv, "acfmnprstP", longopts, NULL)) != -1)
475 cull = cull | CULL_STACKTRACE;
478 filter = filter | FILTER_UNRELEASE;
481 cmp = compare_page_num;
487 cmp = compare_free_ts;
490 cmp = compare_stacktrace;
502 filter = filter | FILTER_PID;
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);
511 filter = filter | FILTER_TGID;
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);
520 filter = filter | FILTER_COMM;
521 strncpy(fc.comm, optarg, TASK_COMM_LEN);
522 fc.comm[TASK_COMM_LEN-1] = '\0';
525 if (!parse_cull_args(optarg)) {
526 printf("wrong argument after --cull in from the command line:%s\n",
536 if (optind >= (argc - 1)) {
541 fin = fopen(argv[optind], "r");
542 fout = fopen(argv[optind + 1], "w");
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 ... */
558 list = malloc(max_size * sizeof(*list));
559 buf = malloc(BUF_SIZE);
561 printf("Out of memory\n");
566 ret = read_block(buf, BUF_SIZE, fin);
573 printf("loaded %d\n", list_size);
575 printf("sorting ....\n");
577 qsort(list, list_size, sizeof(list[0]), compare_cull_condition);
581 for (i = count = 0; i < list_size; i++) {
583 compare_cull_condition((void *)(&list[count-1]), (void *)(&list[i])) != 0) {
584 list[count++] = list[i];
586 list[count-1].num += list[i].num;
587 list[count-1].page_num += list[i].page_num;
591 qsort(list, count, sizeof(list[0]), cmp);
593 for (i = 0; i < count; i++) {
595 fprintf(fout, "%d times, %d pages:\n%s\n",
596 list[i].num, list[i].page_num, list[i].txt);
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);
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);