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>
37 static regex_t order_pattern;
38 static regex_t pid_pattern;
39 static regex_t tgid_pattern;
40 static regex_t ts_nsec_pattern;
41 static regex_t free_ts_nsec_pattern;
42 static struct block_list *list;
46 int read_block(char *buf, int buf_size, FILE *fin)
48 char *curr = buf, *const buf_end = buf + buf_size;
50 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
51 if (*curr == '\n') /* empty line */
53 if (!strncmp(curr, "PFN", 3))
58 return -1; /* EOF or no space left in buf. */
61 static int compare_txt(const void *p1, const void *p2)
63 const struct block_list *l1 = p1, *l2 = p2;
65 return strcmp(l1->txt, l2->txt);
68 static int compare_stacktrace(const void *p1, const void *p2)
70 const struct block_list *l1 = p1, *l2 = p2;
72 return strcmp(l1->stacktrace, l2->stacktrace);
75 static int compare_num(const void *p1, const void *p2)
77 const struct block_list *l1 = p1, *l2 = p2;
79 return l2->num - l1->num;
82 static int compare_page_num(const void *p1, const void *p2)
84 const struct block_list *l1 = p1, *l2 = p2;
86 return l2->page_num - l1->page_num;
89 static int compare_pid(const void *p1, const void *p2)
91 const struct block_list *l1 = p1, *l2 = p2;
93 return l1->pid - l2->pid;
96 static int compare_tgid(const void *p1, const void *p2)
98 const struct block_list *l1 = p1, *l2 = p2;
100 return l1->tgid - l2->tgid;
103 static int compare_ts(const void *p1, const void *p2)
105 const struct block_list *l1 = p1, *l2 = p2;
107 return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
110 static int compare_free_ts(const void *p1, const void *p2)
112 const struct block_list *l1 = p1, *l2 = p2;
114 return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
117 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
120 regmatch_t pmatch[2];
122 err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
123 if (err != 0 || pmatch[1].rm_so == -1) {
124 printf("no matching pattern in %s\n", buf);
127 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
129 memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
134 static void check_regcomp(regex_t *pattern, const char *regex)
138 err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
139 if (err != 0 || pattern->re_nsub != 1) {
140 printf("Invalid pattern %s code %d\n", regex, err);
145 # define FIELD_BUFF 25
147 static int get_page_num(char *buf)
150 char order_str[FIELD_BUFF] = {0};
153 search_pattern(&order_pattern, order_str, buf);
155 order_val = strtol(order_str, &endptr, 10);
156 if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
157 printf("wrong order in follow buf:\n%s\n", buf);
161 return 1 << order_val;
164 static pid_t get_pid(char *buf)
167 char pid_str[FIELD_BUFF] = {0};
170 search_pattern(&pid_pattern, pid_str, buf);
172 pid = strtol(pid_str, &endptr, 10);
173 if (errno != 0 || endptr == pid_str || *endptr != '\0') {
174 printf("wrong/invalid pid in follow buf:\n%s\n", buf);
182 static pid_t get_tgid(char *buf)
185 char tgid_str[FIELD_BUFF] = {0};
188 search_pattern(&tgid_pattern, tgid_str, buf);
190 tgid = strtol(tgid_str, &endptr, 10);
191 if (errno != 0 || endptr == tgid_str || *endptr != '\0') {
192 printf("wrong/invalid tgid in follow buf:\n%s\n", buf);
200 static __u64 get_ts_nsec(char *buf)
203 char ts_nsec_str[FIELD_BUFF] = {0};
206 search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
208 ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
209 if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
210 printf("wrong ts_nsec in follow buf:\n%s\n", buf);
217 static __u64 get_free_ts_nsec(char *buf)
220 char free_ts_nsec_str[FIELD_BUFF] = {0};
223 search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
225 free_ts_nsec = strtoull(free_ts_nsec_str, &endptr, 10);
226 if (errno != 0 || endptr == free_ts_nsec_str || *endptr != '\0') {
227 printf("wrong free_ts_nsec in follow buf:\n%s\n", buf);
234 static void add_list(char *buf, int len)
236 if (list_size != 0 &&
237 len == list[list_size-1].len &&
238 memcmp(buf, list[list_size-1].txt, len) == 0) {
239 list[list_size-1].num++;
240 list[list_size-1].page_num += get_page_num(buf);
243 if (list_size == max_size) {
244 printf("max_size too small??\n");
248 list[list_size].txt = malloc(len+1);
249 if (!list[list_size].txt) {
250 printf("Out of memory\n");
254 list[list_size].len = len;
255 list[list_size].num = 1;
256 list[list_size].page_num = get_page_num(buf);
257 memcpy(list[list_size].txt, buf, len);
258 list[list_size].txt[len] = 0;
259 list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
260 list[list_size].pid = get_pid(buf);
261 list[list_size].tgid = get_tgid(buf);
262 list[list_size].ts_nsec = get_ts_nsec(buf);
263 list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
265 if (list_size % 1000 == 0) {
266 printf("loaded %d\r", list_size);
271 #define BUF_SIZE (128 * 1024)
273 static void usage(void)
275 printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
276 "-m Sort by total memory.\n"
277 "-s Sort by the stack trace.\n"
278 "-t Sort by times (default).\n"
281 "-a Sort by memory allocate time.\n"
282 "-r Sort by memory release time.\n"
283 "-c Cull by comparing stacktrace instead of total block.\n"
284 "-f Filter out the information of blocks whose memory has been released.\n"
288 int main(int argc, char **argv)
290 int (*cmp)(const void *, const void *) = compare_num;
296 struct block_list *list2;
300 while ((opt = getopt(argc, argv, "acfmprstP")) != -1)
312 cmp = compare_page_num;
318 cmp = compare_free_ts;
321 cmp = compare_stacktrace;
334 if (optind >= (argc - 1)) {
339 fin = fopen(argv[optind], "r");
340 fout = fopen(argv[optind + 1], "w");
347 check_regcomp(&order_pattern, "order\\s*([0-9]*),");
348 check_regcomp(&pid_pattern, "pid\\s*([0-9]*),");
349 check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) ");
350 check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,");
351 check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns");
352 fstat(fileno(fin), &st);
353 max_size = st.st_size / 100; /* hack ... */
355 list = malloc(max_size * sizeof(*list));
356 buf = malloc(BUF_SIZE);
358 printf("Out of memory\n");
363 ret = read_block(buf, BUF_SIZE, fin);
370 printf("loaded %d\n", list_size);
372 printf("sorting ....\n");
375 qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
377 qsort(list, list_size, sizeof(list[0]), compare_txt);
379 list2 = malloc(sizeof(*list) * list_size);
381 printf("Out of memory\n");
387 long offset = cull_st ? &list[0].stacktrace - &list[0].txt : 0;
389 for (i = count = 0; i < list_size; i++) {
391 strcmp(*(&list2[count-1].txt+offset), *(&list[i].txt+offset)) != 0) {
392 list2[count++] = list[i];
394 list2[count-1].num += list[i].num;
395 list2[count-1].page_num += list[i].page_num;
399 qsort(list2, count, sizeof(list[0]), cmp);
401 for (i = 0; i < count; i++) {
402 if (filter == 1 && list2[i].free_ts_nsec != 0)
404 fprintf(fout, "%d times, %d pages:\n%s\n",
405 list2[i].num, list2[i].page_num, list2[i].txt);
407 regfree(&order_pattern);
408 regfree(&pid_pattern);
409 regfree(&tgid_pattern);
410 regfree(&ts_nsec_pattern);
411 regfree(&free_ts_nsec_pattern);