1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * User-space helper to sort the output of /sys/kernel/debug/page_owner
4  *
5  * Example use:
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
10  *
11  * See Documentation/vm/page_owner.rst
12 */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <regex.h>
22 #include <errno.h>
23 
24 struct block_list {
25 	char *txt;
26 	int len;
27 	int num;
28 	int page_num;
29 };
30 
31 static int sort_by_memory;
32 static regex_t order_pattern;
33 static struct block_list *list;
34 static int list_size;
35 static int max_size;
36 
37 struct block_list *block_head;
38 
read_block(char * buf,int buf_size,FILE * fin)39 int read_block(char *buf, int buf_size, FILE *fin)
40 {
41 	char *curr = buf, *const buf_end = buf + buf_size;
42 
43 	while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
44 		if (*curr == '\n') /* empty line */
45 			return curr - buf;
46 		if (!strncmp(curr, "PFN", 3))
47 			continue;
48 		curr += strlen(curr);
49 	}
50 
51 	return -1; /* EOF or no space left in buf. */
52 }
53 
compare_txt(const void * p1,const void * p2)54 static int compare_txt(const void *p1, const void *p2)
55 {
56 	const struct block_list *l1 = p1, *l2 = p2;
57 
58 	return strcmp(l1->txt, l2->txt);
59 }
60 
compare_num(const void * p1,const void * p2)61 static int compare_num(const void *p1, const void *p2)
62 {
63 	const struct block_list *l1 = p1, *l2 = p2;
64 
65 	return l2->num - l1->num;
66 }
67 
compare_page_num(const void * p1,const void * p2)68 static int compare_page_num(const void *p1, const void *p2)
69 {
70 	const struct block_list *l1 = p1, *l2 = p2;
71 
72 	return l2->page_num - l1->page_num;
73 }
74 
get_page_num(char * buf)75 static int get_page_num(char *buf)
76 {
77 	int err, val_len, order_val;
78 	char order_str[4] = {0};
79 	char *endptr;
80 	regmatch_t pmatch[2];
81 
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);
85 		return 0;
86 	}
87 	val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
88 	if (val_len > 2) /* max_order should not exceed 2 digits */
89 		goto wrong_order;
90 
91 	memcpy(order_str, buf + pmatch[1].rm_so, val_len);
92 
93 	errno = 0;
94 	order_val = strtol(order_str, &endptr, 10);
95 	if (errno != 0 || endptr == order_str || *endptr != '\0')
96 		goto wrong_order;
97 
98 	return 1 << order_val;
99 
100 wrong_order:
101 	printf("wrong order in follow buf:\n%s\n", buf);
102 	return 0;
103 }
104 
add_list(char * buf,int len)105 static void add_list(char *buf, int len)
106 {
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);
112 		return;
113 	}
114 	if (list_size == max_size) {
115 		printf("max_size too small??\n");
116 		exit(1);
117 	}
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;
124 	list_size++;
125 	if (list_size % 1000 == 0) {
126 		printf("loaded %d\r", list_size);
127 		fflush(stdout);
128 	}
129 }
130 
131 #define BUF_SIZE	(128 * 1024)
132 
usage(void)133 static void usage(void)
134 {
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"
137 	);
138 }
139 
main(int argc,char ** argv)140 int main(int argc, char **argv)
141 {
142 	FILE *fin, *fout;
143 	char *buf;
144 	int ret, i, count;
145 	struct block_list *list2;
146 	struct stat st;
147 	int err;
148 	int opt;
149 
150 	while ((opt = getopt(argc, argv, "m")) != -1)
151 		switch (opt) {
152 		case 'm':
153 			sort_by_memory = 1;
154 			break;
155 		default:
156 			usage();
157 			exit(1);
158 		}
159 
160 	if (optind >= (argc - 1)) {
161 		usage();
162 		exit(1);
163 	}
164 
165 	fin = fopen(argv[optind], "r");
166 	fout = fopen(argv[optind + 1], "w");
167 	if (!fin || !fout) {
168 		usage();
169 		perror("open: ");
170 		exit(1);
171 	}
172 
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",
176 			argv[0], err);
177 		exit(1);
178 	}
179 
180 	fstat(fileno(fin), &st);
181 	max_size = st.st_size / 100; /* hack ... */
182 
183 	list = malloc(max_size * sizeof(*list));
184 	buf = malloc(BUF_SIZE);
185 	if (!list || !buf) {
186 		printf("Out of memory\n");
187 		exit(1);
188 	}
189 
190 	for ( ; ; ) {
191 		ret = read_block(buf, BUF_SIZE, fin);
192 		if (ret < 0)
193 			break;
194 
195 		add_list(buf, ret);
196 	}
197 
198 	printf("loaded %d\n", list_size);
199 
200 	printf("sorting ....\n");
201 
202 	qsort(list, list_size, sizeof(list[0]), compare_txt);
203 
204 	list2 = malloc(sizeof(*list) * list_size);
205 	if (!list2) {
206 		printf("Out of memory\n");
207 		exit(1);
208 	}
209 
210 	printf("culling\n");
211 
212 	for (i = count = 0; i < list_size; i++) {
213 		if (count == 0 ||
214 		    strcmp(list2[count-1].txt, list[i].txt) != 0) {
215 			list2[count++] = list[i];
216 		} else {
217 			list2[count-1].num += list[i].num;
218 			list2[count-1].page_num += list[i].page_num;
219 		}
220 	}
221 
222 	if (sort_by_memory)
223 		qsort(list2, count, sizeof(list[0]), compare_page_num);
224 	else
225 		qsort(list2, count, sizeof(list[0]), compare_num);
226 
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);
230 
231 	regfree(&order_pattern);
232 	return 0;
233 }
234