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;
48 int read_block(char *buf, int buf_size, FILE *fin)
50 char *curr = buf, *const buf_end = buf + buf_size;
52 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
53 if (*curr == '\n') /* empty line */
55 if (!strncmp(curr, "PFN", 3))
60 return -1; /* EOF or no space left in buf. */
63 static int compare_txt(const void *p1, const void *p2)
65 const struct block_list *l1 = p1, *l2 = p2;
67 return strcmp(l1->txt, l2->txt);
70 static int compare_stacktrace(const void *p1, const void *p2)
72 const struct block_list *l1 = p1, *l2 = p2;
74 return strcmp(l1->stacktrace, l2->stacktrace);
77 static int compare_num(const void *p1, const void *p2)
79 const struct block_list *l1 = p1, *l2 = p2;
81 return l2->num - l1->num;
84 static int compare_page_num(const void *p1, const void *p2)
86 const struct block_list *l1 = p1, *l2 = p2;
88 return l2->page_num - l1->page_num;
91 static int compare_pid(const void *p1, const void *p2)
93 const struct block_list *l1 = p1, *l2 = p2;
95 return l1->pid - l2->pid;
98 static int compare_tgid(const void *p1, const void *p2)
100 const struct block_list *l1 = p1, *l2 = p2;
102 return l1->tgid - l2->tgid;
105 static int compare_ts(const void *p1, const void *p2)
107 const struct block_list *l1 = p1, *l2 = p2;
109 return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
112 static int compare_free_ts(const void *p1, const void *p2)
114 const struct block_list *l1 = p1, *l2 = p2;
116 return l1->free_ts_nsec < l2->free_ts_nsec ? -1 : 1;
119 static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
122 regmatch_t pmatch[2];
124 err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
125 if (err != 0 || pmatch[1].rm_so == -1) {
126 printf("no matching pattern in %s\n", buf);
129 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
131 memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
136 static void check_regcomp(regex_t *pattern, const char *regex)
140 err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
141 if (err != 0 || pattern->re_nsub != 1) {
142 printf("Invalid pattern %s code %d\n", regex, err);
147 # define FIELD_BUFF 25
149 static int get_page_num(char *buf)
152 char order_str[FIELD_BUFF] = {0};
155 search_pattern(&order_pattern, order_str, buf);
157 order_val = strtol(order_str, &endptr, 10);
158 if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
159 printf("wrong order in follow buf:\n%s\n", buf);
163 return 1 << order_val;
166 static pid_t get_pid(char *buf)
169 char pid_str[FIELD_BUFF] = {0};
172 search_pattern(&pid_pattern, pid_str, buf);
174 pid = strtol(pid_str, &endptr, 10);
175 if (errno != 0 || endptr == pid_str || *endptr != '\0') {
176 printf("wrong/invalid pid in follow buf:\n%s\n", buf);
184 static pid_t get_tgid(char *buf)
187 char tgid_str[FIELD_BUFF] = {0};
190 search_pattern(&tgid_pattern, tgid_str, buf);
192 tgid = strtol(tgid_str, &endptr, 10);
193 if (errno != 0 || endptr == tgid_str || *endptr != '\0') {
194 printf("wrong/invalid tgid in follow buf:\n%s\n", buf);
202 static __u64 get_ts_nsec(char *buf)
205 char ts_nsec_str[FIELD_BUFF] = {0};
208 search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
210 ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
211 if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
212 printf("wrong ts_nsec in follow buf:\n%s\n", buf);
219 static __u64 get_free_ts_nsec(char *buf)
222 char free_ts_nsec_str[FIELD_BUFF] = {0};
225 search_pattern(&free_ts_nsec_pattern, free_ts_nsec_str, buf);
227 free_ts_nsec = strtoull(free_ts_nsec_str, &endptr, 10);
228 if (errno != 0 || endptr == free_ts_nsec_str || *endptr != '\0') {
229 printf("wrong free_ts_nsec in follow buf:\n%s\n", buf);
236 static void add_list(char *buf, int len)
238 if (list_size != 0 &&
239 len == list[list_size-1].len &&
240 memcmp(buf, list[list_size-1].txt, len) == 0) {
241 list[list_size-1].num++;
242 list[list_size-1].page_num += get_page_num(buf);
245 if (list_size == max_size) {
246 printf("max_size too small??\n");
250 list[list_size].free_ts_nsec = get_free_ts_nsec(buf);
251 if (filter == 1 && list[list_size].free_ts_nsec != 0)
253 list[list_size].txt = malloc(len+1);
254 if (!list[list_size].txt) {
255 printf("Out of memory\n");
259 list[list_size].len = len;
260 list[list_size].num = 1;
261 list[list_size].page_num = get_page_num(buf);
262 memcpy(list[list_size].txt, buf, len);
263 list[list_size].txt[len] = 0;
264 list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
265 if (*list[list_size].stacktrace == '\n')
266 list[list_size].stacktrace++;
267 list[list_size].pid = get_pid(buf);
268 list[list_size].tgid = get_tgid(buf);
269 list[list_size].ts_nsec = get_ts_nsec(buf);
271 if (list_size % 1000 == 0) {
272 printf("loaded %d\r", list_size);
277 #define BUF_SIZE (128 * 1024)
279 static void usage(void)
281 printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
282 "-m Sort by total memory.\n"
283 "-s Sort by the stack trace.\n"
284 "-t Sort by times (default).\n"
287 "-a Sort by memory allocate time.\n"
288 "-r Sort by memory release time.\n"
289 "-c Cull by comparing stacktrace instead of total block.\n"
290 "-f Filter out the information of blocks whose memory has been released.\n"
294 int main(int argc, char **argv)
296 int (*cmp)(const void *, const void *) = compare_num;
303 while ((opt = getopt(argc, argv, "acfmprstP")) != -1)
315 cmp = compare_page_num;
321 cmp = compare_free_ts;
324 cmp = compare_stacktrace;
337 if (optind >= (argc - 1)) {
342 fin = fopen(argv[optind], "r");
343 fout = fopen(argv[optind + 1], "w");
350 check_regcomp(&order_pattern, "order\\s*([0-9]*),");
351 check_regcomp(&pid_pattern, "pid\\s*([0-9]*),");
352 check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) ");
353 check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns,");
354 check_regcomp(&free_ts_nsec_pattern, "free_ts\\s*([0-9]*)\\s*ns");
355 fstat(fileno(fin), &st);
356 max_size = st.st_size / 100; /* hack ... */
358 list = malloc(max_size * sizeof(*list));
359 buf = malloc(BUF_SIZE);
361 printf("Out of memory\n");
366 ret = read_block(buf, BUF_SIZE, fin);
373 printf("loaded %d\n", list_size);
375 printf("sorting ....\n");
378 qsort(list, list_size, sizeof(list[0]), compare_stacktrace);
380 qsort(list, list_size, sizeof(list[0]), compare_txt);
386 long offset = cull_st ? &list[0].stacktrace - &list[0].txt : 0;
388 for (i = count = 0; i < list_size; i++) {
390 strcmp(*(&list[count-1].txt+offset), *(&list[i].txt+offset)) != 0) {
391 list[count++] = list[i];
393 list[count-1].num += list[i].num;
394 list[count-1].page_num += list[i].page_num;
398 qsort(list, count, sizeof(list[0]), cmp);
400 for (i = 0; i < count; i++) {
402 fprintf(fout, "%d times, %d pages:\n%s\n",
403 list[i].num, list[i].page_num, list[i].txt);
405 fprintf(fout, "%d times, %d pages:\n%s\n",
406 list[i].num, list[i].page_num, list[i].stacktrace);
408 regfree(&order_pattern);
409 regfree(&pid_pattern);
410 regfree(&tgid_pattern);
411 regfree(&ts_nsec_pattern);
412 regfree(&free_ts_nsec_pattern);