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