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
9 * See Documentation/vm/page_owner.rst
14 #include <sys/types.h>
27 static struct block_list
*list
;
31 struct block_list
*block_head
;
33 int read_block(char *buf
, int buf_size
, FILE *fin
)
35 char *curr
= buf
, *const buf_end
= buf
+ buf_size
;
37 while (buf_end
- curr
> 1 && fgets(curr
, buf_end
- curr
, fin
)) {
38 if (*curr
== '\n') /* empty line */
40 if (!strncmp(curr
, "PFN", 3))
45 return -1; /* EOF or no space left in buf. */
48 static int compare_txt(const void *p1
, const void *p2
)
50 const struct block_list
*l1
= p1
, *l2
= p2
;
52 return strcmp(l1
->txt
, l2
->txt
);
55 static int compare_num(const void *p1
, const void *p2
)
57 const struct block_list
*l1
= p1
, *l2
= p2
;
59 return l2
->num
- l1
->num
;
62 static void add_list(char *buf
, int len
)
65 len
== list
[list_size
-1].len
&&
66 memcmp(buf
, list
[list_size
-1].txt
, len
) == 0) {
67 list
[list_size
-1].num
++;
70 if (list_size
== max_size
) {
71 printf("max_size too small??\n");
74 list
[list_size
].txt
= malloc(len
+1);
75 list
[list_size
].len
= len
;
76 list
[list_size
].num
= 1;
77 memcpy(list
[list_size
].txt
, buf
, len
);
78 list
[list_size
].txt
[len
] = 0;
80 if (list_size
% 1000 == 0) {
81 printf("loaded %d\r", list_size
);
86 #define BUF_SIZE (128 * 1024)
88 int main(int argc
, char **argv
)
93 struct block_list
*list2
;
97 printf("Usage: ./program <input> <output>\n");
102 fin
= fopen(argv
[1], "r");
103 fout
= fopen(argv
[2], "w");
105 printf("Usage: ./program <input> <output>\n");
110 fstat(fileno(fin
), &st
);
111 max_size
= st
.st_size
/ 100; /* hack ... */
113 list
= malloc(max_size
* sizeof(*list
));
114 buf
= malloc(BUF_SIZE
);
116 printf("Out of memory\n");
121 ret
= read_block(buf
, BUF_SIZE
, fin
);
128 printf("loaded %d\n", list_size
);
130 printf("sorting ....\n");
132 qsort(list
, list_size
, sizeof(list
[0]), compare_txt
);
134 list2
= malloc(sizeof(*list
) * list_size
);
138 for (i
= count
= 0; i
< list_size
; i
++) {
140 strcmp(list2
[count
-1].txt
, list
[i
].txt
) != 0) {
141 list2
[count
++] = list
[i
];
143 list2
[count
-1].num
+= list
[i
].num
;
147 qsort(list2
, count
, sizeof(list
[0]), compare_num
);
149 for (i
= 0; i
< count
; i
++)
150 fprintf(fout
, "%d times:\n%s\n", list2
[i
].num
, list2
[i
].txt
);