Loading...
Note: File does not exist in v5.9.
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/mm/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#include <linux/types.h>
24#include <getopt.h>
25
26#define bool int
27#define true 1
28#define false 0
29#define TASK_COMM_LEN 16
30
31struct block_list {
32 char *txt;
33 char *comm; // task command name
34 char *stacktrace;
35 __u64 ts_nsec;
36 int len;
37 int num;
38 int page_num;
39 pid_t pid;
40 pid_t tgid;
41 int allocator;
42};
43enum FILTER_BIT {
44 FILTER_PID = 1<<1,
45 FILTER_TGID = 1<<2,
46 FILTER_COMM = 1<<3
47};
48enum CULL_BIT {
49 CULL_PID = 1<<1,
50 CULL_TGID = 1<<2,
51 CULL_COMM = 1<<3,
52 CULL_STACKTRACE = 1<<4,
53 CULL_ALLOCATOR = 1<<5
54};
55enum ALLOCATOR_BIT {
56 ALLOCATOR_CMA = 1<<1,
57 ALLOCATOR_SLAB = 1<<2,
58 ALLOCATOR_VMALLOC = 1<<3,
59 ALLOCATOR_OTHERS = 1<<4
60};
61enum ARG_TYPE {
62 ARG_TXT, ARG_COMM, ARG_STACKTRACE, ARG_ALLOC_TS, ARG_CULL_TIME,
63 ARG_PAGE_NUM, ARG_PID, ARG_TGID, ARG_UNKNOWN, ARG_ALLOCATOR
64};
65enum SORT_ORDER {
66 SORT_ASC = 1,
67 SORT_DESC = -1,
68};
69enum COMP_FLAG {
70 COMP_NO_FLAG = 0,
71 COMP_ALLOC = 1<<0,
72 COMP_PAGE_NUM = 1<<1,
73 COMP_PID = 1<<2,
74 COMP_STACK = 1<<3,
75 COMP_NUM = 1<<4,
76 COMP_TGID = 1<<5,
77 COMP_COMM = 1<<6
78};
79struct filter_condition {
80 pid_t *pids;
81 pid_t *tgids;
82 char **comms;
83 int pids_size;
84 int tgids_size;
85 int comms_size;
86};
87struct sort_condition {
88 int (**cmps)(const void *, const void *);
89 int *signs;
90 int size;
91};
92static struct filter_condition fc;
93static struct sort_condition sc;
94static regex_t order_pattern;
95static regex_t pid_pattern;
96static regex_t tgid_pattern;
97static regex_t comm_pattern;
98static regex_t ts_nsec_pattern;
99static struct block_list *list;
100static int list_size;
101static int max_size;
102static int cull;
103static int filter;
104static bool debug_on;
105
106static void set_single_cmp(int (*cmp)(const void *, const void *), int sign);
107
108int read_block(char *buf, char *ext_buf, int buf_size, FILE *fin)
109{
110 char *curr = buf, *const buf_end = buf + buf_size;
111
112 while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
113 if (*curr == '\n') { /* empty line */
114 return curr - buf;
115 }
116 if (!strncmp(curr, "PFN", 3)) {
117 strcpy(ext_buf, curr);
118 continue;
119 }
120 curr += strlen(curr);
121 }
122
123 return -1; /* EOF or no space left in buf. */
124}
125
126static int compare_txt(const void *p1, const void *p2)
127{
128 const struct block_list *l1 = p1, *l2 = p2;
129
130 return strcmp(l1->txt, l2->txt);
131}
132
133static int compare_stacktrace(const void *p1, const void *p2)
134{
135 const struct block_list *l1 = p1, *l2 = p2;
136
137 return strcmp(l1->stacktrace, l2->stacktrace);
138}
139
140static int compare_num(const void *p1, const void *p2)
141{
142 const struct block_list *l1 = p1, *l2 = p2;
143
144 return l1->num - l2->num;
145}
146
147static int compare_page_num(const void *p1, const void *p2)
148{
149 const struct block_list *l1 = p1, *l2 = p2;
150
151 return l1->page_num - l2->page_num;
152}
153
154static int compare_pid(const void *p1, const void *p2)
155{
156 const struct block_list *l1 = p1, *l2 = p2;
157
158 return l1->pid - l2->pid;
159}
160
161static int compare_tgid(const void *p1, const void *p2)
162{
163 const struct block_list *l1 = p1, *l2 = p2;
164
165 return l1->tgid - l2->tgid;
166}
167
168static int compare_allocator(const void *p1, const void *p2)
169{
170 const struct block_list *l1 = p1, *l2 = p2;
171
172 return l1->allocator - l2->allocator;
173}
174
175static int compare_comm(const void *p1, const void *p2)
176{
177 const struct block_list *l1 = p1, *l2 = p2;
178
179 return strcmp(l1->comm, l2->comm);
180}
181
182static int compare_ts(const void *p1, const void *p2)
183{
184 const struct block_list *l1 = p1, *l2 = p2;
185
186 return l1->ts_nsec < l2->ts_nsec ? -1 : 1;
187}
188
189static int compare_cull_condition(const void *p1, const void *p2)
190{
191 if (cull == 0)
192 return compare_txt(p1, p2);
193 if ((cull & CULL_STACKTRACE) && compare_stacktrace(p1, p2))
194 return compare_stacktrace(p1, p2);
195 if ((cull & CULL_PID) && compare_pid(p1, p2))
196 return compare_pid(p1, p2);
197 if ((cull & CULL_TGID) && compare_tgid(p1, p2))
198 return compare_tgid(p1, p2);
199 if ((cull & CULL_COMM) && compare_comm(p1, p2))
200 return compare_comm(p1, p2);
201 if ((cull & CULL_ALLOCATOR) && compare_allocator(p1, p2))
202 return compare_allocator(p1, p2);
203 return 0;
204}
205
206static int compare_sort_condition(const void *p1, const void *p2)
207{
208 int cmp = 0;
209
210 for (int i = 0; i < sc.size; ++i)
211 if (cmp == 0)
212 cmp = sc.signs[i] * sc.cmps[i](p1, p2);
213 return cmp;
214}
215
216static int remove_pattern(regex_t *pattern, char *buf, int len)
217{
218 regmatch_t pmatch[2];
219 int err;
220
221 err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
222 if (err != 0 || pmatch[1].rm_so == -1)
223 return len;
224
225 memcpy(buf + pmatch[1].rm_so,
226 buf + pmatch[1].rm_eo, len - pmatch[1].rm_eo);
227
228 return len - (pmatch[1].rm_eo - pmatch[1].rm_so);
229}
230
231static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
232{
233 int err, val_len;
234 regmatch_t pmatch[2];
235
236 err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
237 if (err != 0 || pmatch[1].rm_so == -1) {
238 if (debug_on)
239 fprintf(stderr, "no matching pattern in %s\n", buf);
240 return -1;
241 }
242 val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
243
244 memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
245
246 return 0;
247}
248
249static bool check_regcomp(regex_t *pattern, const char *regex)
250{
251 int err;
252
253 err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
254 if (err != 0 || pattern->re_nsub != 1) {
255 fprintf(stderr, "Invalid pattern %s code %d\n", regex, err);
256 return false;
257 }
258 return true;
259}
260
261static char **explode(char sep, const char *str, int *size)
262{
263 int count = 0, len = strlen(str);
264 int lastindex = -1, j = 0;
265
266 for (int i = 0; i < len; i++)
267 if (str[i] == sep)
268 count++;
269 char **ret = calloc(++count, sizeof(char *));
270
271 for (int i = 0; i < len; i++) {
272 if (str[i] == sep) {
273 ret[j] = calloc(i - lastindex, sizeof(char));
274 memcpy(ret[j++], str + lastindex + 1, i - lastindex - 1);
275 lastindex = i;
276 }
277 }
278 if (lastindex <= len - 1) {
279 ret[j] = calloc(len - lastindex, sizeof(char));
280 memcpy(ret[j++], str + lastindex + 1, strlen(str) - 1 - lastindex);
281 }
282 *size = j;
283 return ret;
284}
285
286static void free_explode(char **arr, int size)
287{
288 for (int i = 0; i < size; i++)
289 free(arr[i]);
290 free(arr);
291}
292
293# define FIELD_BUFF 25
294
295static int get_page_num(char *buf)
296{
297 int order_val;
298 char order_str[FIELD_BUFF] = {0};
299 char *endptr;
300
301 search_pattern(&order_pattern, order_str, buf);
302 errno = 0;
303 order_val = strtol(order_str, &endptr, 10);
304 if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
305 if (debug_on)
306 fprintf(stderr, "wrong order in follow buf:\n%s\n", buf);
307 return 0;
308 }
309
310 return 1 << order_val;
311}
312
313static pid_t get_pid(char *buf)
314{
315 pid_t pid;
316 char pid_str[FIELD_BUFF] = {0};
317 char *endptr;
318
319 search_pattern(&pid_pattern, pid_str, buf);
320 errno = 0;
321 pid = strtol(pid_str, &endptr, 10);
322 if (errno != 0 || endptr == pid_str || *endptr != '\0') {
323 if (debug_on)
324 fprintf(stderr, "wrong/invalid pid in follow buf:\n%s\n", buf);
325 return -1;
326 }
327
328 return pid;
329
330}
331
332static pid_t get_tgid(char *buf)
333{
334 pid_t tgid;
335 char tgid_str[FIELD_BUFF] = {0};
336 char *endptr;
337
338 search_pattern(&tgid_pattern, tgid_str, buf);
339 errno = 0;
340 tgid = strtol(tgid_str, &endptr, 10);
341 if (errno != 0 || endptr == tgid_str || *endptr != '\0') {
342 if (debug_on)
343 fprintf(stderr, "wrong/invalid tgid in follow buf:\n%s\n", buf);
344 return -1;
345 }
346
347 return tgid;
348
349}
350
351static __u64 get_ts_nsec(char *buf)
352{
353 __u64 ts_nsec;
354 char ts_nsec_str[FIELD_BUFF] = {0};
355 char *endptr;
356
357 search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
358 errno = 0;
359 ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
360 if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
361 if (debug_on)
362 fprintf(stderr, "wrong ts_nsec in follow buf:\n%s\n", buf);
363 return -1;
364 }
365
366 return ts_nsec;
367}
368
369static char *get_comm(char *buf)
370{
371 char *comm_str = malloc(TASK_COMM_LEN);
372
373 memset(comm_str, 0, TASK_COMM_LEN);
374
375 search_pattern(&comm_pattern, comm_str, buf);
376 errno = 0;
377 if (errno != 0) {
378 if (debug_on)
379 fprintf(stderr, "wrong comm in follow buf:\n%s\n", buf);
380 free(comm_str);
381 return NULL;
382 }
383
384 return comm_str;
385}
386
387static int get_arg_type(const char *arg)
388{
389 if (!strcmp(arg, "pid") || !strcmp(arg, "p"))
390 return ARG_PID;
391 else if (!strcmp(arg, "tgid") || !strcmp(arg, "tg"))
392 return ARG_TGID;
393 else if (!strcmp(arg, "name") || !strcmp(arg, "n"))
394 return ARG_COMM;
395 else if (!strcmp(arg, "stacktrace") || !strcmp(arg, "st"))
396 return ARG_STACKTRACE;
397 else if (!strcmp(arg, "txt") || !strcmp(arg, "T"))
398 return ARG_TXT;
399 else if (!strcmp(arg, "alloc_ts") || !strcmp(arg, "at"))
400 return ARG_ALLOC_TS;
401 else if (!strcmp(arg, "allocator") || !strcmp(arg, "ator"))
402 return ARG_ALLOCATOR;
403 else {
404 return ARG_UNKNOWN;
405 }
406}
407
408static int get_allocator(const char *buf, const char *migrate_info)
409{
410 char *tmp, *first_line, *second_line;
411 int allocator = 0;
412
413 if (strstr(migrate_info, "CMA"))
414 allocator |= ALLOCATOR_CMA;
415 if (strstr(migrate_info, "slab"))
416 allocator |= ALLOCATOR_SLAB;
417 tmp = strstr(buf, "__vmalloc_node_range");
418 if (tmp) {
419 second_line = tmp;
420 while (*tmp != '\n')
421 tmp--;
422 tmp--;
423 while (*tmp != '\n')
424 tmp--;
425 first_line = ++tmp;
426 tmp = strstr(tmp, "alloc_pages");
427 if (tmp && first_line <= tmp && tmp < second_line)
428 allocator |= ALLOCATOR_VMALLOC;
429 }
430 if (allocator == 0)
431 allocator = ALLOCATOR_OTHERS;
432 return allocator;
433}
434
435static bool match_num_list(int num, int *list, int list_size)
436{
437 for (int i = 0; i < list_size; ++i)
438 if (list[i] == num)
439 return true;
440 return false;
441}
442
443static bool match_str_list(const char *str, char **list, int list_size)
444{
445 for (int i = 0; i < list_size; ++i)
446 if (!strcmp(list[i], str))
447 return true;
448 return false;
449}
450
451static bool is_need(char *buf)
452{
453 if ((filter & FILTER_PID) && !match_num_list(get_pid(buf), fc.pids, fc.pids_size))
454 return false;
455 if ((filter & FILTER_TGID) &&
456 !match_num_list(get_tgid(buf), fc.tgids, fc.tgids_size))
457 return false;
458
459 char *comm = get_comm(buf);
460
461 if ((filter & FILTER_COMM) &&
462 !match_str_list(comm, fc.comms, fc.comms_size)) {
463 free(comm);
464 return false;
465 }
466 free(comm);
467 return true;
468}
469
470static bool add_list(char *buf, int len, char *ext_buf)
471{
472 if (list_size == max_size) {
473 fprintf(stderr, "max_size too small??\n");
474 return false;
475 }
476 if (!is_need(buf))
477 return true;
478 list[list_size].pid = get_pid(buf);
479 list[list_size].tgid = get_tgid(buf);
480 list[list_size].comm = get_comm(buf);
481 list[list_size].txt = malloc(len+1);
482 if (!list[list_size].txt) {
483 fprintf(stderr, "Out of memory\n");
484 return false;
485 }
486 memcpy(list[list_size].txt, buf, len);
487 if (sc.cmps[0] != compare_ts) {
488 len = remove_pattern(&ts_nsec_pattern, list[list_size].txt, len);
489 }
490 list[list_size].txt[len] = 0;
491 list[list_size].len = len;
492 list[list_size].num = 1;
493 list[list_size].page_num = get_page_num(buf);
494
495 list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
496 if (*list[list_size].stacktrace == '\n')
497 list[list_size].stacktrace++;
498 list[list_size].ts_nsec = get_ts_nsec(buf);
499 list[list_size].allocator = get_allocator(buf, ext_buf);
500 list_size++;
501 if (list_size % 1000 == 0) {
502 printf("loaded %d\r", list_size);
503 fflush(stdout);
504 }
505 return true;
506}
507
508static bool parse_cull_args(const char *arg_str)
509{
510 int size = 0;
511 char **args = explode(',', arg_str, &size);
512
513 for (int i = 0; i < size; ++i) {
514 int arg_type = get_arg_type(args[i]);
515
516 if (arg_type == ARG_PID)
517 cull |= CULL_PID;
518 else if (arg_type == ARG_TGID)
519 cull |= CULL_TGID;
520 else if (arg_type == ARG_COMM)
521 cull |= CULL_COMM;
522 else if (arg_type == ARG_STACKTRACE)
523 cull |= CULL_STACKTRACE;
524 else if (arg_type == ARG_ALLOCATOR)
525 cull |= CULL_ALLOCATOR;
526 else {
527 free_explode(args, size);
528 return false;
529 }
530 }
531 free_explode(args, size);
532 if (sc.size == 0)
533 set_single_cmp(compare_num, SORT_DESC);
534 return true;
535}
536
537static void set_single_cmp(int (*cmp)(const void *, const void *), int sign)
538{
539 if (sc.signs == NULL || sc.size < 1)
540 sc.signs = calloc(1, sizeof(int));
541 sc.signs[0] = sign;
542 if (sc.cmps == NULL || sc.size < 1)
543 sc.cmps = calloc(1, sizeof(int *));
544 sc.cmps[0] = cmp;
545 sc.size = 1;
546}
547
548static bool parse_sort_args(const char *arg_str)
549{
550 int size = 0;
551
552 if (sc.size != 0) { /* reset sort_condition */
553 free(sc.signs);
554 free(sc.cmps);
555 size = 0;
556 }
557
558 char **args = explode(',', arg_str, &size);
559
560 sc.signs = calloc(size, sizeof(int));
561 sc.cmps = calloc(size, sizeof(int *));
562 for (int i = 0; i < size; ++i) {
563 int offset = 0;
564
565 sc.signs[i] = SORT_ASC;
566 if (args[i][0] == '-' || args[i][0] == '+') {
567 if (args[i][0] == '-')
568 sc.signs[i] = SORT_DESC;
569 offset = 1;
570 }
571
572 int arg_type = get_arg_type(args[i]+offset);
573
574 if (arg_type == ARG_PID)
575 sc.cmps[i] = compare_pid;
576 else if (arg_type == ARG_TGID)
577 sc.cmps[i] = compare_tgid;
578 else if (arg_type == ARG_COMM)
579 sc.cmps[i] = compare_comm;
580 else if (arg_type == ARG_STACKTRACE)
581 sc.cmps[i] = compare_stacktrace;
582 else if (arg_type == ARG_ALLOC_TS)
583 sc.cmps[i] = compare_ts;
584 else if (arg_type == ARG_TXT)
585 sc.cmps[i] = compare_txt;
586 else if (arg_type == ARG_ALLOCATOR)
587 sc.cmps[i] = compare_allocator;
588 else {
589 free_explode(args, size);
590 sc.size = 0;
591 return false;
592 }
593 }
594 sc.size = size;
595 free_explode(args, size);
596 return true;
597}
598
599static int *parse_nums_list(char *arg_str, int *list_size)
600{
601 int size = 0;
602 char **args = explode(',', arg_str, &size);
603 int *list = calloc(size, sizeof(int));
604
605 errno = 0;
606 for (int i = 0; i < size; ++i) {
607 char *endptr = NULL;
608
609 list[i] = strtol(args[i], &endptr, 10);
610 if (errno != 0 || endptr == args[i] || *endptr != '\0') {
611 free(list);
612 return NULL;
613 }
614 }
615 *list_size = size;
616 free_explode(args, size);
617 return list;
618}
619
620static void print_allocator(FILE *out, int allocator)
621{
622 fprintf(out, "allocated by ");
623 if (allocator & ALLOCATOR_CMA)
624 fprintf(out, "CMA ");
625 if (allocator & ALLOCATOR_SLAB)
626 fprintf(out, "SLAB ");
627 if (allocator & ALLOCATOR_VMALLOC)
628 fprintf(out, "VMALLOC ");
629 if (allocator & ALLOCATOR_OTHERS)
630 fprintf(out, "OTHERS ");
631}
632
633#define BUF_SIZE (128 * 1024)
634
635static void usage(void)
636{
637 printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
638 "-a\t\t\tSort by memory allocation time.\n"
639 "-m\t\t\tSort by total memory.\n"
640 "-n\t\t\tSort by task command name.\n"
641 "-p\t\t\tSort by pid.\n"
642 "-P\t\t\tSort by tgid.\n"
643 "-s\t\t\tSort by the stacktrace.\n"
644 "-t\t\t\tSort by number of times record is seen (default).\n\n"
645 "--pid <pidlist>\t\tSelect by pid. This selects the information"
646 " of\n\t\t\tblocks whose process ID numbers appear in <pidlist>.\n"
647 "--tgid <tgidlist>\tSelect by tgid. This selects the information"
648 " of\n\t\t\tblocks whose Thread Group ID numbers appear in "
649 "<tgidlist>.\n"
650 "--name <cmdlist>\tSelect by command name. This selects the"
651 " information\n\t\t\tof blocks whose command name appears in"
652 " <cmdlist>.\n"
653 "--cull <rules>\t\tCull by user-defined rules. <rules> is a "
654 "single\n\t\t\targument in the form of a comma-separated list "
655 "with some\n\t\t\tcommon fields predefined (pid, tgid, comm, "
656 "stacktrace, allocator)\n"
657 "--sort <order>\t\tSpecify sort order as: [+|-]key[,[+|-]key[,...]]\n"
658 );
659}
660
661int main(int argc, char **argv)
662{
663 FILE *fin, *fout;
664 char *buf, *ext_buf;
665 int i, count, compare_flag;
666 struct stat st;
667 int opt;
668 struct option longopts[] = {
669 { "pid", required_argument, NULL, 1 },
670 { "tgid", required_argument, NULL, 2 },
671 { "name", required_argument, NULL, 3 },
672 { "cull", required_argument, NULL, 4 },
673 { "sort", required_argument, NULL, 5 },
674 { 0, 0, 0, 0},
675 };
676
677 compare_flag = COMP_NO_FLAG;
678
679 while ((opt = getopt_long(argc, argv, "admnpstP", longopts, NULL)) != -1)
680 switch (opt) {
681 case 'a':
682 compare_flag |= COMP_ALLOC;
683 break;
684 case 'd':
685 debug_on = true;
686 break;
687 case 'm':
688 compare_flag |= COMP_PAGE_NUM;
689 break;
690 case 'p':
691 compare_flag |= COMP_PID;
692 break;
693 case 's':
694 compare_flag |= COMP_STACK;
695 break;
696 case 't':
697 compare_flag |= COMP_NUM;
698 break;
699 case 'P':
700 compare_flag |= COMP_TGID;
701 break;
702 case 'n':
703 compare_flag |= COMP_COMM;
704 break;
705 case 1:
706 filter = filter | FILTER_PID;
707 fc.pids = parse_nums_list(optarg, &fc.pids_size);
708 if (fc.pids == NULL) {
709 fprintf(stderr, "wrong/invalid pid in from the command line:%s\n",
710 optarg);
711 exit(1);
712 }
713 break;
714 case 2:
715 filter = filter | FILTER_TGID;
716 fc.tgids = parse_nums_list(optarg, &fc.tgids_size);
717 if (fc.tgids == NULL) {
718 fprintf(stderr, "wrong/invalid tgid in from the command line:%s\n",
719 optarg);
720 exit(1);
721 }
722 break;
723 case 3:
724 filter = filter | FILTER_COMM;
725 fc.comms = explode(',', optarg, &fc.comms_size);
726 break;
727 case 4:
728 if (!parse_cull_args(optarg)) {
729 fprintf(stderr, "wrong argument after --cull option:%s\n",
730 optarg);
731 exit(1);
732 }
733 break;
734 case 5:
735 if (!parse_sort_args(optarg)) {
736 fprintf(stderr, "wrong argument after --sort option:%s\n",
737 optarg);
738 exit(1);
739 }
740 break;
741 default:
742 usage();
743 exit(1);
744 }
745
746 if (optind >= (argc - 1)) {
747 usage();
748 exit(1);
749 }
750
751 /* Only one compare option is allowed, yet we also want handle the
752 * default case were no option is provided, but we still want to
753 * match the behavior of the -t option (compare by number of times
754 * a record is seen
755 */
756 switch (compare_flag) {
757 case COMP_ALLOC:
758 set_single_cmp(compare_ts, SORT_ASC);
759 break;
760 case COMP_PAGE_NUM:
761 set_single_cmp(compare_page_num, SORT_DESC);
762 break;
763 case COMP_PID:
764 set_single_cmp(compare_pid, SORT_ASC);
765 break;
766 case COMP_STACK:
767 set_single_cmp(compare_stacktrace, SORT_ASC);
768 break;
769 case COMP_NO_FLAG:
770 case COMP_NUM:
771 set_single_cmp(compare_num, SORT_DESC);
772 break;
773 case COMP_TGID:
774 set_single_cmp(compare_tgid, SORT_ASC);
775 break;
776 case COMP_COMM:
777 set_single_cmp(compare_comm, SORT_ASC);
778 break;
779 default:
780 usage();
781 exit(1);
782 }
783
784 fin = fopen(argv[optind], "r");
785 fout = fopen(argv[optind + 1], "w");
786 if (!fin || !fout) {
787 usage();
788 perror("open: ");
789 exit(1);
790 }
791
792 if (!check_regcomp(&order_pattern, "order\\s*([0-9]*),"))
793 goto out_order;
794 if (!check_regcomp(&pid_pattern, "pid\\s*([0-9]*),"))
795 goto out_pid;
796 if (!check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) "))
797 goto out_tgid;
798 if (!check_regcomp(&comm_pattern, "tgid\\s*[0-9]*\\s*\\((.*)\\),\\s*ts"))
799 goto out_comm;
800 if (!check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns"))
801 goto out_ts;
802
803 fstat(fileno(fin), &st);
804 max_size = st.st_size / 100; /* hack ... */
805
806 list = malloc(max_size * sizeof(*list));
807 buf = malloc(BUF_SIZE);
808 ext_buf = malloc(BUF_SIZE);
809 if (!list || !buf || !ext_buf) {
810 fprintf(stderr, "Out of memory\n");
811 goto out_free;
812 }
813
814 for ( ; ; ) {
815 int buf_len = read_block(buf, ext_buf, BUF_SIZE, fin);
816
817 if (buf_len < 0)
818 break;
819 if (!add_list(buf, buf_len, ext_buf))
820 goto out_free;
821 }
822
823 printf("loaded %d\n", list_size);
824
825 printf("sorting ....\n");
826
827 qsort(list, list_size, sizeof(list[0]), compare_cull_condition);
828
829 printf("culling\n");
830
831 for (i = count = 0; i < list_size; i++) {
832 if (count == 0 ||
833 compare_cull_condition((void *)(&list[count-1]), (void *)(&list[i])) != 0) {
834 list[count++] = list[i];
835 } else {
836 list[count-1].num += list[i].num;
837 list[count-1].page_num += list[i].page_num;
838 }
839 }
840
841 qsort(list, count, sizeof(list[0]), compare_sort_condition);
842
843 for (i = 0; i < count; i++) {
844 if (cull == 0) {
845 fprintf(fout, "%d times, %d pages, ", list[i].num, list[i].page_num);
846 print_allocator(fout, list[i].allocator);
847 fprintf(fout, ":\n%s\n", list[i].txt);
848 }
849 else {
850 fprintf(fout, "%d times, %d pages",
851 list[i].num, list[i].page_num);
852 if (cull & CULL_PID || filter & FILTER_PID)
853 fprintf(fout, ", PID %d", list[i].pid);
854 if (cull & CULL_TGID || filter & FILTER_TGID)
855 fprintf(fout, ", TGID %d", list[i].tgid);
856 if (cull & CULL_COMM || filter & FILTER_COMM)
857 fprintf(fout, ", task_comm_name: %s", list[i].comm);
858 if (cull & CULL_ALLOCATOR) {
859 fprintf(fout, ", ");
860 print_allocator(fout, list[i].allocator);
861 }
862 if (cull & CULL_STACKTRACE)
863 fprintf(fout, ":\n%s", list[i].stacktrace);
864 fprintf(fout, "\n");
865 }
866 }
867
868out_free:
869 if (ext_buf)
870 free(ext_buf);
871 if (buf)
872 free(buf);
873 if (list)
874 free(list);
875out_ts:
876 regfree(&ts_nsec_pattern);
877out_comm:
878 regfree(&comm_pattern);
879out_tgid:
880 regfree(&tgid_pattern);
881out_pid:
882 regfree(&pid_pattern);
883out_order:
884 regfree(&order_pattern);
885
886 return 0;
887}