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 * grep -v ^PFN page_owner_full.txt > page_owner.txt
8 * ./sort page_owner.txt sorted_page_owner.txt
13 #include <sys/types.h>
26 static struct block_list
*list
;
30 struct block_list
*block_head
;
32 int read_block(char *buf
, int buf_size
, FILE *fin
)
34 char *curr
= buf
, *const buf_end
= buf
+ buf_size
;
36 while (buf_end
- curr
> 1 && fgets(curr
, buf_end
- curr
, fin
)) {
37 if (*curr
== '\n') /* empty line */
42 return -1; /* EOF or no space left in buf. */
45 static int compare_txt(const void *p1
, const void *p2
)
47 const struct block_list
*l1
= p1
, *l2
= p2
;
49 return strcmp(l1
->txt
, l2
->txt
);
52 static int compare_num(const void *p1
, const void *p2
)
54 const struct block_list
*l1
= p1
, *l2
= p2
;
56 return l2
->num
- l1
->num
;
59 static void add_list(char *buf
, int len
)
62 len
== list
[list_size
-1].len
&&
63 memcmp(buf
, list
[list_size
-1].txt
, len
) == 0) {
64 list
[list_size
-1].num
++;
67 if (list_size
== max_size
) {
68 printf("max_size too small??\n");
71 list
[list_size
].txt
= malloc(len
+1);
72 list
[list_size
].len
= len
;
73 list
[list_size
].num
= 1;
74 memcpy(list
[list_size
].txt
, buf
, len
);
75 list
[list_size
].txt
[len
] = 0;
77 if (list_size
% 1000 == 0) {
78 printf("loaded %d\r", list_size
);
83 #define BUF_SIZE (128 * 1024)
85 int main(int argc
, char **argv
)
90 struct block_list
*list2
;
94 printf("Usage: ./program <input> <output>\n");
99 fin
= fopen(argv
[1], "r");
100 fout
= fopen(argv
[2], "w");
102 printf("Usage: ./program <input> <output>\n");
107 fstat(fileno(fin
), &st
);
108 max_size
= st
.st_size
/ 100; /* hack ... */
110 list
= malloc(max_size
* sizeof(*list
));
111 buf
= malloc(BUF_SIZE
);
113 printf("Out of memory\n");
118 ret
= read_block(buf
, BUF_SIZE
, fin
);
125 printf("loaded %d\n", list_size
);
127 printf("sorting ....\n");
129 qsort(list
, list_size
, sizeof(list
[0]), compare_txt
);
131 list2
= malloc(sizeof(*list
) * list_size
);
135 for (i
= count
= 0; i
< list_size
; i
++) {
137 strcmp(list2
[count
-1].txt
, list
[i
].txt
) != 0) {
138 list2
[count
++] = list
[i
];
140 list2
[count
-1].num
+= list
[i
].num
;
144 qsort(list2
, count
, sizeof(list
[0]), compare_num
);
146 for (i
= 0; i
< count
; i
++)
147 fprintf(fout
, "%d times:\n%s\n", list2
[i
].num
, list2
[i
].txt
);