// SPDX-License-Identifier: GPL-2.0
/*
* User-space helper to sort the output of /sys/kernel/debug/page_owner
*
* Example use:
* cat /sys/kernel/debug/page_owner > page_owner_full.txt
* ./page_owner_sort page_owner_full.txt sorted_page_owner.txt
* Or sort by total memory:
* ./page_owner_sort -m page_owner_full.txt sorted_page_owner.txt
*
* See Documentation/mm/page_owner.rst
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include <errno.h>
#include <linux/types.h>
#include <getopt.h>
#define TASK_COMM_LEN 16
struct block_list {
char *txt;
char *comm; // task command name
char *stacktrace;
__u64 ts_nsec;
int len;
int num;
int page_num;
pid_t pid;
pid_t tgid;
int allocator;
};
enum FILTER_BIT {
FILTER_PID = 1<<1,
FILTER_TGID = 1<<2,
FILTER_COMM = 1<<3
};
enum CULL_BIT {
CULL_PID = 1<<1,
CULL_TGID = 1<<2,
CULL_COMM = 1<<3,
CULL_STACKTRACE = 1<<4,
CULL_ALLOCATOR = 1<<5
};
enum ALLOCATOR_BIT {
ALLOCATOR_CMA = 1<<1,
ALLOCATOR_SLAB = 1<<2,
ALLOCATOR_VMALLOC = 1<<3,
ALLOCATOR_OTHERS = 1<<4
};
enum ARG_TYPE {
ARG_TXT, ARG_COMM, ARG_STACKTRACE, ARG_ALLOC_TS, ARG_CULL_TIME,
ARG_PAGE_NUM, ARG_PID, ARG_TGID, ARG_UNKNOWN, ARG_ALLOCATOR
};
enum SORT_ORDER {
SORT_ASC = 1,
SORT_DESC = -1,
};
enum COMP_FLAG {
COMP_NO_FLAG = 0,
COMP_ALLOC = 1<<0,
COMP_PAGE_NUM = 1<<1,
COMP_PID = 1<<2,
COMP_STACK = 1<<3,
COMP_NUM = 1<<4,
COMP_TGID = 1<<5,
COMP_COMM = 1<<6
};
struct filter_condition {
pid_t *pids;
pid_t *tgids;
char **comms;
int pids_size;
int tgids_size;
int comms_size;
};
struct sort_condition {
int (**cmps)(const void *, const void *);
int *signs;
int size;
};
static struct filter_condition fc;
static struct sort_condition sc;
static regex_t order_pattern;
static regex_t pid_pattern;
static regex_t tgid_pattern;
static regex_t comm_pattern;
static regex_t ts_nsec_pattern;
static struct block_list *list;
static int list_size;
static int max_size;
static int cull;
static int filter;
static bool debug_on;
static void set_single_cmp(int (*cmp)(const void *, const void *), int sign);
int read_block(char *buf, char *ext_buf, int buf_size, FILE *fin)
{
char *curr = buf, *const buf_end = buf + buf_size;
while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
if (*curr == '\n') { /* empty line */
return curr - buf;
}
if (!strncmp(curr, "PFN", 3)) {
strcpy(ext_buf, curr);
continue;
}
curr += strlen(curr);
}
return -1; /* EOF or no space left in buf. */
}
static int compare_txt(const void *p1, const void *p2)
{
const struct block_list *l1 = p1, *l2 = p2;
return strcmp(l1->txt, l2->txt);
}
static int compare_stacktrace(const void *p1, const void *p2)
{
const struct block_list *l1 = p1, *l2 = p2;
return strcmp(l1->stacktrace, l2->stacktrace);
}
static int compare_num(const void *p1, const void *p2)
{
const struct block_list *l1 = p1, *l2 = p2;
return l1->num - l2->num;
}
static int compare_page_num(const void *p1, const void *p2)
{
const struct block_list *l1 = p1, *l2 = p2;
return l1->page_num - l2->page_num;
}
static int compare_pid(const void *p1, const void *p2)
{
const struct block_li