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>
31 static int sort_by_memory;
32 static regex_t order_pattern;
33 static struct block_list *list;
37 struct block_list *block_head;
39 int read_block(char *buf, int buf_size, FILE *fin)
41 char *curr = buf, *const buf_end = buf + buf_size;
43 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
44 if (*curr == '\n') /* empty line */
46 if (!strncmp(curr, "PFN", 3))
51 return -1; /* EOF or no space left in buf. */
54 static int compare_txt(const void *p1, const void *p2)
56 const struct block_list *l1 = p1, *l2 = p2;
58 return strcmp(l1->txt, l2->txt);
61 static int compare_num(const void *p1, const void *p2)
63 const struct block_list *l1 = p1, *l2 = p2;
65 return l2->num - l1->num;
68 static int compare_page_num(const void *p1, const void *p2)
70 const struct block_list *l1 = p1, *l2 = p2;
72 return l2->page_num - l1->page_num;
75 static int get_page_num(char *buf)
77 int err, val_len, order_val;
78 char order_str[4] = {0};
82 err = regexec(&order_pattern, buf, 2, pmatch, REG_NOTBOL);
83 if (err != 0 || pmatch[1].rm_so == -1) {
84 printf("no order pattern in %s\n", buf);
87 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
88 if (val_len > 2) /* max_order should not exceed 2 digits */
91 memcpy(order_str, buf + pmatch[1].rm_so, val_len);
94 order_val = strtol(order_str, &endptr, 10);
95 if (errno != 0 || endptr == order_str || *endptr != '\0')
98 return 1 << order_val;
101 printf("wrong order in follow buf:\n%s\n", buf);
105 static void add_list(char *buf, int len)
107 if (list_size != 0 &&
108 len == list[list_size-1].len &&
109 memcmp(buf, list[list_size-1].txt, len) == 0) {
110 list[list_size-1].num++;
111 list[list_size-1].page_num += get_page_num(buf);
114 if (list_size == max_size) {
115 printf("max_size too small??\n");
118 list[list_size].txt = malloc(len+1);
119 list[list_size].len = len;
120 list[list_size].num = 1;
121 list[list_size].page_num = get_page_num(buf);
122 memcpy(list[list_size].txt, buf, len);
123 list[list_size].txt[len] = 0;
125 if (list_size % 1000 == 0) {
126 printf("loaded %d\r", list_size);
131 #define BUF_SIZE (128 * 1024)
133 static void usage(void)
135 printf("Usage: ./page_owner_sort [-m] <input> <output>\n"
136 "-m Sort by total memory. If this option is unset, sort by times\n"
140 int main(int argc, char **argv)
145 struct block_list *list2;
150 while ((opt = getopt(argc, argv, "m")) != -1)
160 if (optind >= (argc - 1)) {
165 fin = fopen(argv[optind], "r");
166 fout = fopen(argv[optind + 1], "w");
173 err = regcomp(&order_pattern, "order\\s*([0-9]*),", REG_EXTENDED|REG_NEWLINE);
174 if (err != 0 || order_pattern.re_nsub != 1) {
175 printf("%s: Invalid pattern 'order\\s*([0-9]*),' code %d\n",
180 fstat(fileno(fin), &st);
181 max_size = st.st_size / 100; /* hack ... */
183 list = malloc(max_size * sizeof(*list));
184 buf = malloc(BUF_SIZE);
186 printf("Out of memory\n");
191 ret = read_block(buf, BUF_SIZE, fin);
198 printf("loaded %d\n", list_size);
200 printf("sorting ....\n");
202 qsort(list, list_size, sizeof(list[0]), compare_txt);
204 list2 = malloc(sizeof(*list) * list_size);
206 printf("Out of memory\n");
212 for (i = count = 0; i < list_size; i++) {
214 strcmp(list2[count-1].txt, list[i].txt) != 0) {
215 list2[count++] = list[i];
217 list2[count-1].num += list[i].num;
218 list2[count-1].page_num += list[i].page_num;
223 qsort(list2, count, sizeof(list[0]), compare_page_num);
225 qsort(list2, count, sizeof(list[0]), compare_num);
227 for (i = 0; i < count; i++)
228 fprintf(fout, "%d times, %d pages:\n%s\n",
229 list2[i].num, list2[i].page_num, list2[i].txt);
231 regfree(&order_pattern);