]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - tools/vm/page_owner_sort.c
tools/vm/page_owner_sort.c: count and sort by mem
[mirror_ubuntu-kernels.git] / tools / vm / page_owner_sort.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
48c96a36
JK
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
5b94ce2f 7 * ./page_owner_sort page_owner_full.txt sorted_page_owner.txt
f7df2b1c
ZW
8 * Or sort by total memory:
9 * ./page_owner_sort -m page_owner_full.txt sorted_page_owner.txt
aff876dc
MC
10 *
11 * See Documentation/vm/page_owner.rst
48c96a36
JK
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>
f7df2b1c
ZW
21#include <regex.h>
22#include <errno.h>
48c96a36
JK
23
24struct block_list {
25 char *txt;
26 int len;
27 int num;
f7df2b1c 28 int page_num;
48c96a36
JK
29};
30
f7df2b1c
ZW
31static int sort_by_memory;
32static regex_t order_pattern;
48c96a36
JK
33static struct block_list *list;
34static int list_size;
35static int max_size;
36
37struct block_list *block_head;
38
39int 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;
5b94ce2f
CH
46 if (!strncmp(curr, "PFN", 3))
47 continue;
48c96a36
JK
48 curr += strlen(curr);
49 }
50
51 return -1; /* EOF or no space left in buf. */
52}
53
54static 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
61static 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
f7df2b1c
ZW
68static 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
75static 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
100wrong_order:
101 printf("wrong order in follow buf:\n%s\n", buf);
102 return 0;
103}
104
48c96a36
JK
105static 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++;
f7df2b1c 111 list[list_size-1].page_num += get_page_num(buf);
48c96a36
JK
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;
f7df2b1c 121 list[list_size].page_num = get_page_num(buf);
48c96a36
JK
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
37137675 131#define BUF_SIZE (128 * 1024)
48c96a36 132
f7df2b1c
ZW
133static 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
48c96a36
JK
140int main(int argc, char **argv)
141{
142 FILE *fin, *fout;
37137675 143 char *buf;
48c96a36
JK
144 int ret, i, count;
145 struct block_list *list2;
146 struct stat st;
f7df2b1c
ZW
147 int err;
148 int opt;
48c96a36 149
f7df2b1c
ZW
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();
48c96a36
JK
162 exit(1);
163 }
164
f7df2b1c
ZW
165 fin = fopen(argv[optind], "r");
166 fout = fopen(argv[optind + 1], "w");
48c96a36 167 if (!fin || !fout) {
f7df2b1c 168 usage();
48c96a36
JK
169 perror("open: ");
170 exit(1);
171 }
172
f7df2b1c
ZW
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
48c96a36
JK
180 fstat(fileno(fin), &st);
181 max_size = st.st_size / 100; /* hack ... */
182
183 list = malloc(max_size * sizeof(*list));
37137675
JK
184 buf = malloc(BUF_SIZE);
185 if (!list || !buf) {
186 printf("Out of memory\n");
187 exit(1);
188 }
48c96a36
JK
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);
85f29cd6
TB
205 if (!list2) {
206 printf("Out of memory\n");
207 exit(1);
208 }
48c96a36
JK
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;
f7df2b1c 218 list2[count-1].page_num += list[i].page_num;
48c96a36
JK
219 }
220 }
221
f7df2b1c
ZW
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);
48c96a36
JK
226
227 for (i = 0; i < count; i++)
f7df2b1c
ZW
228 fprintf(fout, "%d times, %d pages:\n%s\n",
229 list2[i].num, list2[i].page_num, list2[i].txt);
48c96a36 230
f7df2b1c 231 regfree(&order_pattern);
48c96a36
JK
232 return 0;
233}