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>
23 #include <linux/types.h>
36 static regex_t order_pattern;
37 static regex_t pid_pattern;
38 static regex_t ts_nsec_pattern;
39 static regex_t free_ts_nsec_pattern;
40 static struct block_list *list;
44 int read_block(char *buf, int buf_size, FILE *fin)
46 char *curr = buf, *const buf_end = buf + buf_size;
48 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
49 if (*curr == '\n') /* empty line */
51 if (!strncmp(curr, "PFN", 3))
56 return -1; /* EOF or no space left in buf. */
59 static int compare_txt(const void *p1, const void *p2)
61 const struct block_list *l1 = p1, *l2 = p2;
63 return strcmp(l1->txt, l2->txt);
66 static int compare_stacktrace(const void *p1, const void *p2)
68 const struct block_list *l1 = p1, *l2 = p2;
70 return strcmp(l1->stacktrace, l2->stacktrace);
73 static int compare_num(const void *p1, const void *p2)
75 const struct block_list *l1 = p1, *l2 = p2;
77 return l2->num - l1->num;
80 static int compare_page_num(const void *p1, const void *p2)
82 const struct block_list *l1 = p1, *l2 = p2;
84 return l2->page_num - l1->page_num;
87 static int compare_pid(const void *p1, const void *p2)
89 const struct block_list *l1 = p1, *l2 = p2;
91 return l1->pid - l2->pid;
94 static int compare_ts(const void *p1, const void *p2)
96 const struct block_list *l1 = p1, *l2 = p2;
98 return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
101 static int compare_free_ts(const void *p1, const void *p2)
103 const struct block_list *l1 = p1, *l2 = p2;
105 return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
108 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
111 regmatch_t pmatch[2];
113 err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
114 if (err != 0 || pmatch[1].rm_so == -1) {
115 printf("no matching pattern in %s\n", buf);
118 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
120 memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
125 static void check_regcomp(regex_t *pattern, const char *regex)
129 err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
130 if (err != 0 || pattern->re_nsub != 1) {
131 printf("Invalid pattern %s code %d\n", regex, err);
136 # define FIELD_BUFF 25
138 static int get_page_num(char *buf)
141 char order_str[FIELD_BUFF] = {0};
144 search_pattern(&order_pattern, order_str, buf);
146 order_val = strtol(order_str, &endptr, 10);
147 if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
148 printf("wrong order in follow buf:\n%s\n", buf);
152 return 1 << order_val;
155 static pid_t get_pid(char *buf)
158 char pid_str[FIELD_BUFF] = {0};
161 search_pattern(&pid_pattern, pid_str, buf);
163 pid = strtol(pid_str, &endptr, 10);
164 if (errno != 0 || endptr == pid_str || *endptr != '\0') {
165 printf("wrong/invalid pid in follow buf:\n%s\n", buf);
173 static __u64 get_ts_nsec(char *buf)
176 char ts_nsec_str[FIELD_BUFF] = {0};
179 search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
181 ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
182 if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
183 printf("wrong ts_nsec in follow buf:\n%s\n", buf);
190 static __u64 get_free_ts_nsec(char *buf)
193 char free_ts_nsec_str[FIELD_BUFF] = {0};
196 search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
198 free_ts_nsec = strtoull(free_ts_nsec_str, &endptr, 10);
199 if (errno != 0 || endptr == free_ts_nsec_str || *endptr != '\0') {
200 printf("wrong free_ts_nsec in follow buf:\n%s\n", buf);
207 static void add_list(char *buf, int len)
209 if (list_size != 0 &&
210 len == list[list_size-1].len &&
211 memcmp(buf, list[list_size-1].txt, len) == 0) {
212 list[list_size-1].num++;
213 list[list_size-1].page_num += get_page_num(buf);
216 if (list_size == max_size) {
217 printf("max_size too small??\n");
220 list[list_size].txt = malloc(len+1);
221 list[list_size].len = len;
222 list[list_size].num = 1;
223 list[list_size].page_num = get_page_num(buf);
224 memcpy(list[list_size].txt, buf, len);
225 list[list_size].txt[len] = 0;
226 list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
227 list[list_size].pid = get_pid(buf);
228 list[list_size].ts_nsec = get_ts_nsec(buf);
229 list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
231 if (list_size % 1000 == 0) {
232 printf("loaded %d\r", list_size);
237 #define BUF_SIZE (128 * 1024)
239 static void usage(void)
241 printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
242 "-m Sort by total memory.\n"
243 "-s Sort by the stack trace.\n"
244 "-t Sort by times (default).\n"
246 "-a Sort by memory allocate time.\n"
247 "-r Sort by memory release time.\n"
248 "-c Cull by comparing stacktrace instead of total block.\n"
249 "-f Filter out the information of blocks whose memory has not been released.\n"
253 int main(int argc, char **argv)
255 int (*cmp)(const void *, const void *) = compare_num;
261 struct block_list *list2;
265 while ((opt = getopt(argc, argv, "acfmprst")) != -1)
277 cmp = compare_page_num;
283 cmp = compare_free_ts;
286 cmp = compare_stacktrace;
296 if (optind >= (argc - 1)) {
301 fin = fopen(argv[optind], "r");
302 fout = fopen(argv[optind + 1], "w");
309 check_regcomp(&order_pattern, "order\\s*([0-9]*),");
310 check_regcomp(&pid_pattern, "pid\\s*([0-9]*),");
311 check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,");
312 check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns");
313 fstat(fileno(fin), &st);
314 max_size = st.st_size / 100; /* hack ... */
316 list = malloc(max_size * sizeof(*list));
317 buf = malloc(BUF_SIZE);
319 printf("Out of memory\n");
324 ret = read_block(buf, BUF_SIZE, fin);
331 printf("loaded %d\n", list_size);
333 printf("sorting ....\n");
336 qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
338 qsort(list, list_size, sizeof(list[0]), compare_txt);
340 list2 = malloc(sizeof(*list) * list_size);
342 printf("Out of memory\n");
348 long offset = cull_st ? &list[0].stacktrace - &list[0].txt : 0;
350 for (i = count = 0; i < list_size; i++) {
352 strcmp(*(&list2[count-1].txt+offset), *(&list[i].txt+offset)) != 0) {
353 list2[count++] = list[i];
355 list2[count-1].num += list[i].num;
356 list2[count-1].page_num += list[i].page_num;
360 qsort(list2, count, sizeof(list[0]), cmp);
362 for (i = 0; i < count; i++) {
363 if (filter == 1 && list2[i].free_ts_nsec != 0)
365 fprintf(fout, "%d times, %d pages:\n%s\n",
366 list2[i].num, list2[i].page_num, list2[i].txt);
368 regfree(&order_pattern);
369 regfree(&pid_pattern);
370 regfree(&ts_nsec_pattern);
371 regfree(&free_ts_nsec_pattern);