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>
32 static int sort_by_memory;
33 static regex_t order_pattern;
34 static struct block_list *list;
38 struct block_list *block_head;
40 int read_block(char *buf, int buf_size, FILE *fin)
42 char *curr = buf, *const buf_end = buf + buf_size;
44 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
45 if (*curr == '\n') /* empty line */
47 if (!strncmp(curr, "PFN", 3))
52 return -1; /* EOF or no space left in buf. */
55 static int compare_stacktrace(const void *p1, const void *p2)
57 const struct block_list *l1 = p1, *l2 = p2;
59 return strcmp(l1->stacktrace, l2->stacktrace);
62 static int compare_num(const void *p1, const void *p2)
64 const struct block_list *l1 = p1, *l2 = p2;
66 return l2->num - l1->num;
69 static int compare_page_num(const void *p1, const void *p2)
71 const struct block_list *l1 = p1, *l2 = p2;
73 return l2->page_num - l1->page_num;
76 static int get_page_num(char *buf)
78 int err, val_len, order_val;
79 char order_str[4] = {0};
83 err = regexec(&order_pattern, buf, 2, pmatch, REG_NOTBOL);
84 if (err != 0 || pmatch[1].rm_so == -1) {
85 printf("no order pattern in %s\n", buf);
88 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
89 if (val_len > 2) /* max_order should not exceed 2 digits */
92 memcpy(order_str, buf + pmatch[1].rm_so, val_len);
95 order_val = strtol(order_str, &endptr, 10);
96 if (errno != 0 || endptr == order_str || *endptr != '\0')
99 return 1 << order_val;
102 printf("wrong order in follow buf:\n%s\n", buf);
106 static void add_list(char *buf, int len)
108 if (list_size != 0 &&
109 len == list[list_size-1].len &&
110 memcmp(buf, list[list_size-1].txt, len) == 0) {
111 list[list_size-1].num++;
112 list[list_size-1].page_num += get_page_num(buf);
115 if (list_size == max_size) {
116 printf("max_size too small??\n");
119 list[list_size].txt = malloc(len+1);
120 list[list_size].len = len;
121 list[list_size].num = 1;
122 list[list_size].page_num = get_page_num(buf);
123 memcpy(list[list_size].txt, buf, len);
124 list[list_size].txt[len] = 0;
125 list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
127 if (list_size % 1000 == 0) {
128 printf("loaded %d\r", list_size);
133 #define BUF_SIZE (128 * 1024)
135 static void usage(void)
137 printf("Usage: ./page_owner_sort [-m] <input> <output>\n"
138 "-m Sort by total memory. If this option is unset, sort by times\n"
142 int main(int argc, char **argv)
147 struct block_list *list2;
152 while ((opt = getopt(argc, argv, "m")) != -1)
162 if (optind >= (argc - 1)) {
167 fin = fopen(argv[optind], "r");
168 fout = fopen(argv[optind + 1], "w");
175 err = regcomp(&order_pattern, "order\\s*([0-9]*),", REG_EXTENDED|REG_NEWLINE);
176 if (err != 0 || order_pattern.re_nsub != 1) {
177 printf("%s: Invalid pattern 'order\\s*([0-9]*),' code %d\n",
182 fstat(fileno(fin), &st);
183 max_size = st.st_size / 100; /* hack ... */
185 list = malloc(max_size * sizeof(*list));
186 buf = malloc(BUF_SIZE);
188 printf("Out of memory\n");
193 ret = read_block(buf, BUF_SIZE, fin);
200 printf("loaded %d\n", list_size);
202 printf("sorting ....\n");
204 qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
206 list2 = malloc(sizeof(*list) * list_size);
208 printf("Out of memory\n");
214 for (i = count = 0; i < list_size; i++) {
216 strcmp(list2[count-1].stacktrace, list[i].stacktrace) != 0) {
217 list2[count++] = list[i];
219 list2[count-1].num += list[i].num;
220 list2[count-1].page_num += list[i].page_num;
225 qsort(list2, count, sizeof(list[0]), compare_page_num);
227 qsort(list2, count, sizeof(list[0]), compare_num);
229 for (i = 0; i < count; i++)
230 fprintf(fout, "%d times, %d pages:\n%s\n",
231 list2[i].num, list2[i].page_num, list2[i].txt);
233 regfree(&order_pattern);