2 * page-types: Tool for querying page flags
4 * Copyright (C) 2009 Intel corporation
5 * Copyright (C) 2009 Wu Fengguang <fengguang.wu@intel.com>
16 #include <sys/types.h>
17 #include <sys/errno.h>
18 #include <sys/fcntl.h>
26 #define PROC_KPAGEFLAGS "/proc/kpageflags"
28 /* copied from kpageflags_read() */
31 #define KPF_REFERENCED 2
32 #define KPF_UPTODATE 3
37 #define KPF_WRITEBACK 8
41 /* [11-20] new additions in 2.6.31 */
44 #define KPF_SWAPCACHE 13
45 #define KPF_SWAPBACKED 14
46 #define KPF_COMPOUND_HEAD 15
47 #define KPF_COMPOUND_TAIL 16
49 #define KPF_UNEVICTABLE 18
52 /* [32-] kernel hacking assistances */
53 #define KPF_RESERVED 32
54 #define KPF_MLOCKED 33
55 #define KPF_MAPPEDTODISK 34
56 #define KPF_PRIVATE 35
57 #define KPF_PRIVATE_2 36
58 #define KPF_OWNER_PRIVATE 37
60 #define KPF_UNCACHED 39
62 /* [48-] take some arbitrary free slots for expanding overloaded flags
63 * not part of kernel API
65 #define KPF_READAHEAD 48
66 #define KPF_SLOB_FREE 49
67 #define KPF_SLUB_FROZEN 50
68 #define KPF_SLUB_DEBUG 51
70 #define KPF_ALL_BITS ((uint64_t)~0ULL)
71 #define KPF_HACKERS_BITS (0xffffULL << 32)
72 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
73 #define BIT(name) (1ULL << KPF_##name)
74 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
76 static char *page_flag_names[] = {
77 [KPF_LOCKED] = "L:locked",
78 [KPF_ERROR] = "E:error",
79 [KPF_REFERENCED] = "R:referenced",
80 [KPF_UPTODATE] = "U:uptodate",
81 [KPF_DIRTY] = "D:dirty",
83 [KPF_ACTIVE] = "A:active",
84 [KPF_SLAB] = "S:slab",
85 [KPF_WRITEBACK] = "W:writeback",
86 [KPF_RECLAIM] = "I:reclaim",
87 [KPF_BUDDY] = "B:buddy",
89 [KPF_MMAP] = "M:mmap",
90 [KPF_ANON] = "a:anonymous",
91 [KPF_SWAPCACHE] = "s:swapcache",
92 [KPF_SWAPBACKED] = "b:swapbacked",
93 [KPF_COMPOUND_HEAD] = "H:compound_head",
94 [KPF_COMPOUND_TAIL] = "T:compound_tail",
95 [KPF_HUGE] = "G:huge",
96 [KPF_UNEVICTABLE] = "u:unevictable",
97 [KPF_NOPAGE] = "n:nopage",
99 [KPF_RESERVED] = "r:reserved",
100 [KPF_MLOCKED] = "m:mlocked",
101 [KPF_MAPPEDTODISK] = "d:mappedtodisk",
102 [KPF_PRIVATE] = "P:private",
103 [KPF_PRIVATE_2] = "p:private_2",
104 [KPF_OWNER_PRIVATE] = "O:owner_private",
105 [KPF_ARCH] = "h:arch",
106 [KPF_UNCACHED] = "c:uncached",
108 [KPF_READAHEAD] = "I:readahead",
109 [KPF_SLOB_FREE] = "P:slob_free",
110 [KPF_SLUB_FROZEN] = "A:slub_frozen",
111 [KPF_SLUB_DEBUG] = "E:slub_debug",
119 static int opt_raw; /* for kernel developers */
120 static int opt_list; /* list pages (in ranges) */
121 static int opt_no_summary; /* don't show summary */
122 static pid_t opt_pid; /* process to walk */
124 #define MAX_ADDR_RANGES 1024
125 static int nr_addr_ranges;
126 static unsigned long opt_offset[MAX_ADDR_RANGES];
127 static unsigned long opt_size[MAX_ADDR_RANGES];
129 #define MAX_BIT_FILTERS 64
130 static int nr_bit_filters;
131 static uint64_t opt_mask[MAX_BIT_FILTERS];
132 static uint64_t opt_bits[MAX_BIT_FILTERS];
134 static int page_size;
136 #define PAGES_BATCH (64 << 10) /* 64k pages */
137 static int kpageflags_fd;
138 static uint64_t kpageflags_buf[KPF_BYTES * PAGES_BATCH];
140 #define HASH_SHIFT 13
141 #define HASH_SIZE (1 << HASH_SHIFT)
142 #define HASH_MASK (HASH_SIZE - 1)
143 #define HASH_KEY(flags) (flags & HASH_MASK)
145 static unsigned long total_pages;
146 static unsigned long nr_pages[HASH_SIZE];
147 static uint64_t page_flags[HASH_SIZE];
154 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
156 #define min_t(type, x, y) ({ \
159 __min1 < __min2 ? __min1 : __min2; })
161 unsigned long pages2mb(unsigned long pages)
163 return (pages * page_size) >> 20;
166 void fatal(const char *x, ...)
171 vfprintf(stderr, x, ap);
181 char *page_flag_name(uint64_t flags)
187 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
188 present = (flags >> i) & 1;
189 if (!page_flag_names[i]) {
191 fatal("unkown flag bit %d\n", i);
194 buf[j++] = present ? page_flag_names[i][0] : '_';
200 char *page_flag_longname(uint64_t flags)
202 static char buf[1024];
205 for (i = 0, n = 0; i < ARRAY_SIZE(page_flag_names); i++) {
206 if (!page_flag_names[i])
208 if ((flags >> i) & 1)
209 n += snprintf(buf + n, sizeof(buf) - n, "%s,",
210 page_flag_names[i] + 2);
221 * page list and summary
224 void show_page_range(unsigned long offset, uint64_t flags)
226 static uint64_t flags0;
227 static unsigned long index;
228 static unsigned long count;
230 if (flags == flags0 && offset == index + count) {
236 printf("%lu\t%lu\t%s\n",
237 index, count, page_flag_name(flags0));
244 void show_page(unsigned long offset, uint64_t flags)
246 printf("%lu\t%s\n", offset, page_flag_name(flags));
249 void show_summary(void)
253 printf(" flags\tpage-count MB"
254 " symbolic-flags\t\t\tlong-symbolic-flags\n");
256 for (i = 0; i < ARRAY_SIZE(nr_pages); i++) {
258 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
259 (unsigned long long)page_flags[i],
261 pages2mb(nr_pages[i]),
262 page_flag_name(page_flags[i]),
263 page_flag_longname(page_flags[i]));
266 printf(" total\t%10lu %8lu\n",
267 total_pages, pages2mb(total_pages));
275 int bit_mask_ok(uint64_t flags)
279 for (i = 0; i < nr_bit_filters; i++) {
280 if (opt_bits[i] == KPF_ALL_BITS) {
281 if ((flags & opt_mask[i]) == 0)
284 if ((flags & opt_mask[i]) != opt_bits[i])
292 uint64_t expand_overloaded_flags(uint64_t flags)
294 /* SLOB/SLUB overload several page flags */
295 if (flags & BIT(SLAB)) {
296 if (flags & BIT(PRIVATE))
297 flags ^= BIT(PRIVATE) | BIT(SLOB_FREE);
298 if (flags & BIT(ACTIVE))
299 flags ^= BIT(ACTIVE) | BIT(SLUB_FROZEN);
300 if (flags & BIT(ERROR))
301 flags ^= BIT(ERROR) | BIT(SLUB_DEBUG);
304 /* PG_reclaim is overloaded as PG_readahead in the read path */
305 if ((flags & (BIT(RECLAIM) | BIT(WRITEBACK))) == BIT(RECLAIM))
306 flags ^= BIT(RECLAIM) | BIT(READAHEAD);
311 uint64_t well_known_flags(uint64_t flags)
313 /* hide flags intended only for kernel hacker */
314 flags &= ~KPF_HACKERS_BITS;
316 /* hide non-hugeTLB compound pages */
317 if ((flags & BITS_COMPOUND) && !(flags & BIT(HUGE)))
318 flags &= ~BITS_COMPOUND;
328 int hash_slot(uint64_t flags)
330 int k = HASH_KEY(flags);
333 /* Explicitly reserve slot 0 for flags 0: the following logic
334 * cannot distinguish an unoccupied slot from slot (flags==0).
339 /* search through the remaining (HASH_SIZE-1) slots */
340 for (i = 1; i < ARRAY_SIZE(page_flags); i++, k++) {
341 if (!k || k >= ARRAY_SIZE(page_flags))
343 if (page_flags[k] == 0) {
344 page_flags[k] = flags;
347 if (page_flags[k] == flags)
351 fatal("hash table full: bump up HASH_SHIFT?\n");
355 void add_page(unsigned long offset, uint64_t flags)
357 flags = expand_overloaded_flags(flags);
360 flags = well_known_flags(flags);
362 if (!bit_mask_ok(flags))
366 show_page_range(offset, flags);
367 else if (opt_list == 2)
368 show_page(offset, flags);
370 nr_pages[hash_slot(flags)]++;
374 void walk_pfn(unsigned long index, unsigned long count)
380 if (index > ULONG_MAX / KPF_BYTES)
381 fatal("index overflow: %lu\n", index);
383 lseek(kpageflags_fd, index * KPF_BYTES, SEEK_SET);
386 batch = min_t(unsigned long, count, PAGES_BATCH);
387 n = read(kpageflags_fd, kpageflags_buf, batch * KPF_BYTES);
391 perror(PROC_KPAGEFLAGS);
395 if (n % KPF_BYTES != 0)
396 fatal("partial read: %lu bytes\n", n);
399 for (i = 0; i < n; i++)
400 add_page(index + i, kpageflags_buf[i]);
407 void walk_addr_ranges(void)
411 kpageflags_fd = open(PROC_KPAGEFLAGS, O_RDONLY);
412 if (kpageflags_fd < 0) {
413 perror(PROC_KPAGEFLAGS);
418 walk_pfn(0, ULONG_MAX);
420 for (i = 0; i < nr_addr_ranges; i++)
421 walk_pfn(opt_offset[i], opt_size[i]);
423 close(kpageflags_fd);
431 const char *page_flag_type(uint64_t flag)
433 if (flag & KPF_HACKERS_BITS)
435 if (flag & KPF_OVERLOADED_BITS)
445 "page-types [options]\n"
446 " -r|--raw Raw mode, for kernel developers\n"
447 " -a|--addr addr-spec Walk a range of pages\n"
448 " -b|--bits bits-spec Walk pages with specified bits\n"
449 #if 0 /* planned features */
450 " -p|--pid pid Walk process address space\n"
451 " -f|--file filename Walk file address space\n"
453 " -l|--list Show page details in ranges\n"
454 " -L|--list-each Show page details one by one\n"
455 " -N|--no-summary Don't show summay info\n"
456 " -h|--help Show this usage message\n"
458 " N one page at offset N (unit: pages)\n"
459 " N+M pages range from N to N+M-1\n"
460 " N,M pages range from N to M-1\n"
461 " N, pages range from N to end\n"
462 " ,M pages range from 0 to M\n"
464 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
465 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
466 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
467 " =bit1,bit2 flags == (bit1|bit2)\n"
471 for (i = 0, j = 0; i < ARRAY_SIZE(page_flag_names); i++) {
472 if (!page_flag_names[i])
474 printf("%16s%s", page_flag_names[i] + 2,
475 page_flag_type(1ULL << i));
482 "(r) raw mode bits (o) overloaded bits\n");
485 unsigned long long parse_number(const char *str)
487 unsigned long long n;
489 n = strtoll(str, NULL, 0);
491 if (n == 0 && str[0] != '0')
492 fatal("invalid name or number: %s\n", str);
497 void parse_pid(const char *str)
499 opt_pid = parse_number(str);
502 void parse_file(const char *name)
506 void add_addr_range(unsigned long offset, unsigned long size)
508 if (nr_addr_ranges >= MAX_ADDR_RANGES)
509 fatal("too much addr ranges\n");
511 opt_offset[nr_addr_ranges] = offset;
512 opt_size[nr_addr_ranges] = size;
516 void parse_addr_range(const char *optarg)
518 unsigned long offset;
522 p = strchr(optarg, ',');
524 p = strchr(optarg, '+');
528 size = parse_number(p + 1);
530 offset = parse_number(optarg);
534 size = parse_number(p + 1);
537 fatal("invalid range: %lu,%lu\n",
543 offset = parse_number(optarg);
547 add_addr_range(offset, size);
550 void add_bits_filter(uint64_t mask, uint64_t bits)
552 if (nr_bit_filters >= MAX_BIT_FILTERS)
553 fatal("too much bit filters\n");
555 opt_mask[nr_bit_filters] = mask;
556 opt_bits[nr_bit_filters] = bits;
560 uint64_t parse_flag_name(const char *str, int len)
567 if (len <= 8 && !strncmp(str, "compound", len))
568 return BITS_COMPOUND;
570 for (i = 0; i < ARRAY_SIZE(page_flag_names); i++) {
571 if (!page_flag_names[i])
573 if (!strncmp(str, page_flag_names[i] + 2, len))
577 return parse_number(str);
580 uint64_t parse_flag_names(const char *str, int all)
586 if (*p == ',' || *p == '=' || *p == '\0') {
587 if ((*str != '~') || (*str == '~' && all && *++str))
588 flags |= parse_flag_name(str, p - str);
599 void parse_bits_mask(const char *optarg)
605 p = strchr(optarg, '=');
608 bits = parse_flag_names(p + 1, 0);
610 mask = parse_flag_names(optarg, 0);
611 bits = parse_flag_names(p + 1, 0);
612 } else if (strchr(optarg, '~')) {
613 mask = parse_flag_names(optarg, 1);
614 bits = parse_flag_names(optarg, 0);
616 mask = parse_flag_names(optarg, 0);
620 add_bits_filter(mask, bits);
624 struct option opts[] = {
625 { "raw" , 0, NULL, 'r' },
626 { "pid" , 1, NULL, 'p' },
627 { "file" , 1, NULL, 'f' },
628 { "addr" , 1, NULL, 'a' },
629 { "bits" , 1, NULL, 'b' },
630 { "list" , 0, NULL, 'l' },
631 { "list-each" , 0, NULL, 'L' },
632 { "no-summary", 0, NULL, 'N' },
633 { "help" , 0, NULL, 'h' },
634 { NULL , 0, NULL, 0 }
637 int main(int argc, char *argv[])
641 page_size = getpagesize();
643 while ((c = getopt_long(argc, argv,
644 "rp:f:a:b:lLNh", opts, NULL)) != -1) {
656 parse_addr_range(optarg);
659 parse_bits_mask(optarg);
680 printf("offset\tcount\tflags\n");
682 printf("offset\tflags\n");
687 show_page_range(0, 0); /* drain the buffer */