2 * page-types: Tool for querying page flags
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; version 2.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should find a copy of v2 of the GNU General Public License somewhere on
14 * your Linux system; if not, write to the Free Software Foundation, Inc., 59
15 * Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * Copyright (C) 2009 Intel corporation
19 * Authors: Wu Fengguang <fengguang.wu@intel.com>
22 #define _FILE_OFFSET_BITS 64
37 #include <sys/types.h>
38 #include <sys/errno.h>
39 #include <sys/fcntl.h>
40 #include <sys/mount.h>
41 #include <sys/statfs.h>
43 #include "../../include/uapi/linux/magic.h"
44 #include "../../include/uapi/linux/kernel-page-flags.h"
45 #include <api/fs/fs.h>
53 # define STR(x) _STR(x)
57 * pagemap kernel ABI bits
60 #define PM_ENTRY_BYTES 8
61 #define PM_PFRAME_BITS 55
62 #define PM_PFRAME_MASK ((1LL << PM_PFRAME_BITS) - 1)
63 #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
64 #define MAX_SWAPFILES_SHIFT 5
65 #define PM_SWAP_OFFSET(x) (((x) & PM_PFRAME_MASK) >> MAX_SWAPFILES_SHIFT)
66 #define PM_SOFT_DIRTY (1ULL << 55)
67 #define PM_MMAP_EXCLUSIVE (1ULL << 56)
68 #define PM_FILE (1ULL << 61)
69 #define PM_SWAP (1ULL << 62)
70 #define PM_PRESENT (1ULL << 63)
77 #define PROC_KPAGEFLAGS "/proc/kpageflags"
78 #define PROC_KPAGECOUNT "/proc/kpagecount"
79 #define PROC_KPAGECGROUP "/proc/kpagecgroup"
81 #define SYS_KERNEL_MM_PAGE_IDLE "/sys/kernel/mm/page_idle/bitmap"
83 /* [32-] kernel hacking assistances */
84 #define KPF_RESERVED 32
85 #define KPF_MLOCKED 33
86 #define KPF_MAPPEDTODISK 34
87 #define KPF_PRIVATE 35
88 #define KPF_PRIVATE_2 36
89 #define KPF_OWNER_PRIVATE 37
91 #define KPF_UNCACHED 39
92 #define KPF_SOFTDIRTY 40
94 /* [48-] take some arbitrary free slots for expanding overloaded flags
95 * not part of kernel API
97 #define KPF_READAHEAD 48
98 #define KPF_SLOB_FREE 49
99 #define KPF_SLUB_FROZEN 50
100 #define KPF_SLUB_DEBUG 51
103 #define KPF_MMAP_EXCLUSIVE 63
105 #define KPF_ALL_BITS ((uint64_t)~0ULL)
106 #define KPF_HACKERS_BITS (0xffffULL << 32)
107 #define KPF_OVERLOADED_BITS (0xffffULL << 48)
108 #define BIT(name) (1ULL << KPF_##name)
109 #define BITS_COMPOUND (BIT(COMPOUND_HEAD) | BIT(COMPOUND_TAIL))
111 static const char * const page_flag_names
[] = {
112 [KPF_LOCKED
] = "L:locked",
113 [KPF_ERROR
] = "E:error",
114 [KPF_REFERENCED
] = "R:referenced",
115 [KPF_UPTODATE
] = "U:uptodate",
116 [KPF_DIRTY
] = "D:dirty",
118 [KPF_ACTIVE
] = "A:active",
119 [KPF_SLAB
] = "S:slab",
120 [KPF_WRITEBACK
] = "W:writeback",
121 [KPF_RECLAIM
] = "I:reclaim",
122 [KPF_BUDDY
] = "B:buddy",
124 [KPF_MMAP
] = "M:mmap",
125 [KPF_ANON
] = "a:anonymous",
126 [KPF_SWAPCACHE
] = "s:swapcache",
127 [KPF_SWAPBACKED
] = "b:swapbacked",
128 [KPF_COMPOUND_HEAD
] = "H:compound_head",
129 [KPF_COMPOUND_TAIL
] = "T:compound_tail",
130 [KPF_HUGE
] = "G:huge",
131 [KPF_UNEVICTABLE
] = "u:unevictable",
132 [KPF_HWPOISON
] = "X:hwpoison",
133 [KPF_NOPAGE
] = "n:nopage",
136 [KPF_BALLOON
] = "o:balloon",
137 [KPF_PGTABLE
] = "g:pgtable",
138 [KPF_ZERO_PAGE
] = "z:zero_page",
139 [KPF_IDLE
] = "i:idle_page",
141 [KPF_RESERVED
] = "r:reserved",
142 [KPF_MLOCKED
] = "m:mlocked",
143 [KPF_MAPPEDTODISK
] = "d:mappedtodisk",
144 [KPF_PRIVATE
] = "P:private",
145 [KPF_PRIVATE_2
] = "p:private_2",
146 [KPF_OWNER_PRIVATE
] = "O:owner_private",
147 [KPF_ARCH
] = "h:arch",
148 [KPF_UNCACHED
] = "c:uncached",
149 [KPF_SOFTDIRTY
] = "f:softdirty",
151 [KPF_READAHEAD
] = "I:readahead",
152 [KPF_SLOB_FREE
] = "P:slob_free",
153 [KPF_SLUB_FROZEN
] = "A:slub_frozen",
154 [KPF_SLUB_DEBUG
] = "E:slub_debug",
156 [KPF_FILE
] = "F:file",
157 [KPF_SWAP
] = "w:swap",
158 [KPF_MMAP_EXCLUSIVE
] = "1:mmap_exclusive",
166 static int opt_raw
; /* for kernel developers */
167 static int opt_list
; /* list pages (in ranges) */
168 static int opt_mark_idle
; /* set accessed bit */
169 static int opt_no_summary
; /* don't show summary */
170 static pid_t opt_pid
; /* process to walk */
171 const char *opt_file
; /* file or directory path */
172 static uint64_t opt_cgroup
; /* cgroup inode */
173 static int opt_list_cgroup
;/* list page cgroup */
174 static int opt_list_mapcnt
;/* list page map count */
175 static const char *opt_kpageflags
;/* kpageflags file to parse */
177 #define MAX_ADDR_RANGES 1024
178 static int nr_addr_ranges
;
179 static unsigned long opt_offset
[MAX_ADDR_RANGES
];
180 static unsigned long opt_size
[MAX_ADDR_RANGES
];
182 #define MAX_VMAS 10240
184 static unsigned long pg_start
[MAX_VMAS
];
185 static unsigned long pg_end
[MAX_VMAS
];
187 #define MAX_BIT_FILTERS 64
188 static int nr_bit_filters
;
189 static uint64_t opt_mask
[MAX_BIT_FILTERS
];
190 static uint64_t opt_bits
[MAX_BIT_FILTERS
];
192 static int page_size
;
194 static int pagemap_fd
;
195 static int kpageflags_fd
;
196 static int kpagecount_fd
= -1;
197 static int kpagecgroup_fd
= -1;
198 static int page_idle_fd
= -1;
200 static int opt_hwpoison
;
201 static int opt_unpoison
;
203 static const char *hwpoison_debug_fs
;
204 static int hwpoison_inject_fd
;
205 static int hwpoison_forget_fd
;
207 #define HASH_SHIFT 13
208 #define HASH_SIZE (1 << HASH_SHIFT)
209 #define HASH_MASK (HASH_SIZE - 1)
210 #define HASH_KEY(flags) (flags & HASH_MASK)
212 static unsigned long total_pages
;
213 static unsigned long nr_pages
[HASH_SIZE
];
214 static uint64_t page_flags
[HASH_SIZE
];
221 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
223 #define min_t(type, x, y) ({ \
226 __min1 < __min2 ? __min1 : __min2; })
228 #define max_t(type, x, y) ({ \
231 __max1 > __max2 ? __max1 : __max2; })
233 static unsigned long pages2mb(unsigned long pages
)
235 return (pages
* page_size
) >> 20;
238 static void fatal(const char *x
, ...)
243 vfprintf(stderr
, x
, ap
);
248 static int checked_open(const char *pathname
, int flags
)
250 int fd
= open(pathname
, flags
);
261 * pagemap/kpageflags routines
264 static unsigned long do_u64_read(int fd
, const char *name
,
271 if (index
> ULONG_MAX
/ 8)
272 fatal("index overflow: %lu\n", index
);
274 bytes
= pread(fd
, buf
, count
* 8, (off_t
)index
* 8);
280 fatal("partial read: %lu bytes\n", bytes
);
285 static unsigned long kpageflags_read(uint64_t *buf
,
289 return do_u64_read(kpageflags_fd
, opt_kpageflags
, buf
, index
, pages
);
292 static unsigned long kpagecgroup_read(uint64_t *buf
,
296 if (kpagecgroup_fd
< 0)
299 return do_u64_read(kpagecgroup_fd
, opt_kpageflags
, buf
, index
, pages
);
302 static unsigned long kpagecount_read(uint64_t *buf
,
306 return kpagecount_fd
< 0 ? pages
:
307 do_u64_read(kpagecount_fd
, PROC_KPAGECOUNT
,
311 static unsigned long pagemap_read(uint64_t *buf
,
315 return do_u64_read(pagemap_fd
, "/proc/pid/pagemap", buf
, index
, pages
);
318 static unsigned long pagemap_pfn(uint64_t val
)
322 if (val
& PM_PRESENT
)
323 pfn
= PM_PFRAME(val
);
330 static unsigned long pagemap_swap_offset(uint64_t val
)
332 return val
& PM_SWAP
? PM_SWAP_OFFSET(val
) : 0;
339 static char *page_flag_name(uint64_t flags
)
345 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
346 present
= (flags
>> i
) & 1;
347 if (!page_flag_names
[i
]) {
349 fatal("unknown flag bit %d\n", i
);
352 buf
[j
++] = present
? page_flag_names
[i
][0] : '_';
358 static char *page_flag_longname(uint64_t flags
)
360 static char buf
[1024];
363 for (i
= 0, n
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
364 if (!page_flag_names
[i
])
366 if ((flags
>> i
) & 1)
367 n
+= snprintf(buf
+ n
, sizeof(buf
) - n
, "%s,",
368 page_flag_names
[i
] + 2);
379 * page list and summary
382 static void show_page_range(unsigned long voffset
, unsigned long offset
,
383 unsigned long size
, uint64_t flags
,
384 uint64_t cgroup
, uint64_t mapcnt
)
386 static uint64_t flags0
;
387 static uint64_t cgroup0
;
388 static uint64_t mapcnt0
;
389 static unsigned long voff
;
390 static unsigned long index
;
391 static unsigned long count
;
393 if (flags
== flags0
&& cgroup
== cgroup0
&& mapcnt
== mapcnt0
&&
394 offset
== index
+ count
&& size
&& voffset
== voff
+ count
) {
401 printf("%lx\t", voff
);
403 printf("%lu\t", voff
);
405 printf("@%llu\t", (unsigned long long)cgroup0
);
407 printf("%lu\t", mapcnt0
);
408 printf("%lx\t%lx\t%s\n",
409 index
, count
, page_flag_name(flags0
));
420 static void flush_page_range(void)
422 show_page_range(0, 0, 0, 0, 0, 0);
425 static void show_page(unsigned long voffset
, unsigned long offset
,
426 uint64_t flags
, uint64_t cgroup
, uint64_t mapcnt
)
429 printf("%lx\t", voffset
);
431 printf("%lu\t", voffset
);
433 printf("@%llu\t", (unsigned long long)cgroup
);
435 printf("%lu\t", mapcnt
);
437 printf("%lx\t%s\n", offset
, page_flag_name(flags
));
440 static void show_summary(void)
444 printf(" flags\tpage-count MB"
445 " symbolic-flags\t\t\tlong-symbolic-flags\n");
447 for (i
= 0; i
< ARRAY_SIZE(nr_pages
); i
++) {
449 printf("0x%016llx\t%10lu %8lu %s\t%s\n",
450 (unsigned long long)page_flags
[i
],
452 pages2mb(nr_pages
[i
]),
453 page_flag_name(page_flags
[i
]),
454 page_flag_longname(page_flags
[i
]));
457 printf(" total\t%10lu %8lu\n",
458 total_pages
, pages2mb(total_pages
));
466 static int bit_mask_ok(uint64_t flags
)
470 for (i
= 0; i
< nr_bit_filters
; i
++) {
471 if (opt_bits
[i
] == KPF_ALL_BITS
) {
472 if ((flags
& opt_mask
[i
]) == 0)
475 if ((flags
& opt_mask
[i
]) != opt_bits
[i
])
483 static uint64_t expand_overloaded_flags(uint64_t flags
, uint64_t pme
)
485 /* SLOB/SLUB overload several page flags */
486 if (flags
& BIT(SLAB
)) {
487 if (flags
& BIT(PRIVATE
))
488 flags
^= BIT(PRIVATE
) | BIT(SLOB_FREE
);
489 if (flags
& BIT(ACTIVE
))
490 flags
^= BIT(ACTIVE
) | BIT(SLUB_FROZEN
);
491 if (flags
& BIT(ERROR
))
492 flags
^= BIT(ERROR
) | BIT(SLUB_DEBUG
);
495 /* PG_reclaim is overloaded as PG_readahead in the read path */
496 if ((flags
& (BIT(RECLAIM
) | BIT(WRITEBACK
))) == BIT(RECLAIM
))
497 flags
^= BIT(RECLAIM
) | BIT(READAHEAD
);
499 if (pme
& PM_SOFT_DIRTY
)
500 flags
|= BIT(SOFTDIRTY
);
505 if (pme
& PM_MMAP_EXCLUSIVE
)
506 flags
|= BIT(MMAP_EXCLUSIVE
);
511 static uint64_t well_known_flags(uint64_t flags
)
513 /* hide flags intended only for kernel hacker */
514 flags
&= ~KPF_HACKERS_BITS
;
516 /* hide non-hugeTLB compound pages */
517 if ((flags
& BITS_COMPOUND
) && !(flags
& BIT(HUGE
)))
518 flags
&= ~BITS_COMPOUND
;
523 static uint64_t kpageflags_flags(uint64_t flags
, uint64_t pme
)
526 flags
= expand_overloaded_flags(flags
, pme
);
528 flags
= well_known_flags(flags
);
537 static void prepare_hwpoison_fd(void)
539 char buf
[MAX_PATH
+ 1];
541 hwpoison_debug_fs
= debugfs__mount();
542 if (!hwpoison_debug_fs
) {
543 perror("mount debugfs");
547 if (opt_hwpoison
&& !hwpoison_inject_fd
) {
548 snprintf(buf
, MAX_PATH
, "%s/hwpoison/corrupt-pfn",
550 hwpoison_inject_fd
= checked_open(buf
, O_WRONLY
);
553 if (opt_unpoison
&& !hwpoison_forget_fd
) {
554 snprintf(buf
, MAX_PATH
, "%s/hwpoison/unpoison-pfn",
556 hwpoison_forget_fd
= checked_open(buf
, O_WRONLY
);
560 static int hwpoison_page(unsigned long offset
)
565 len
= sprintf(buf
, "0x%lx\n", offset
);
566 len
= write(hwpoison_inject_fd
, buf
, len
);
568 perror("hwpoison inject");
574 static int unpoison_page(unsigned long offset
)
579 len
= sprintf(buf
, "0x%lx\n", offset
);
580 len
= write(hwpoison_forget_fd
, buf
, len
);
582 perror("hwpoison forget");
588 static int mark_page_idle(unsigned long offset
)
590 static unsigned long off
;
594 if ((offset
/ 64 == off
/ 64) || buf
== 0) {
595 buf
|= 1UL << (offset
% 64);
600 len
= pwrite(page_idle_fd
, &buf
, 8, 8 * (off
/ 64));
602 perror("mark page idle");
606 buf
= 1UL << (offset
% 64);
616 static size_t hash_slot(uint64_t flags
)
618 size_t k
= HASH_KEY(flags
);
621 /* Explicitly reserve slot 0 for flags 0: the following logic
622 * cannot distinguish an unoccupied slot from slot (flags==0).
627 /* search through the remaining (HASH_SIZE-1) slots */
628 for (i
= 1; i
< ARRAY_SIZE(page_flags
); i
++, k
++) {
629 if (!k
|| k
>= ARRAY_SIZE(page_flags
))
631 if (page_flags
[k
] == 0) {
632 page_flags
[k
] = flags
;
635 if (page_flags
[k
] == flags
)
639 fatal("hash table full: bump up HASH_SHIFT?\n");
643 static void add_page(unsigned long voffset
, unsigned long offset
,
644 uint64_t flags
, uint64_t cgroup
, uint64_t mapcnt
,
647 flags
= kpageflags_flags(flags
, pme
);
649 if (!bit_mask_ok(flags
))
652 if (opt_cgroup
&& cgroup
!= (uint64_t)opt_cgroup
)
656 hwpoison_page(offset
);
658 unpoison_page(offset
);
661 mark_page_idle(offset
);
664 show_page_range(voffset
, offset
, 1, flags
, cgroup
, mapcnt
);
665 else if (opt_list
== 2)
666 show_page(voffset
, offset
, flags
, cgroup
, mapcnt
);
668 nr_pages
[hash_slot(flags
)]++;
672 #define KPAGEFLAGS_BATCH (64 << 10) /* 64k pages */
673 static void walk_pfn(unsigned long voffset
,
678 uint64_t buf
[KPAGEFLAGS_BATCH
];
679 uint64_t cgi
[KPAGEFLAGS_BATCH
];
680 uint64_t cnt
[KPAGEFLAGS_BATCH
];
686 * kpagecgroup_read() reads only if kpagecgroup were opened, but
687 * /proc/kpagecgroup might even not exist, so it's better to fill
688 * them with zeros here.
693 memset(cgi
, 0, sizeof cgi
);
696 batch
= min_t(unsigned long, count
, KPAGEFLAGS_BATCH
);
697 pages
= kpageflags_read(buf
, index
, batch
);
701 if (kpagecgroup_read(cgi
, index
, pages
) != pages
)
702 fatal("kpagecgroup returned fewer pages than expected");
704 if (kpagecount_read(cnt
, index
, batch
) != pages
)
705 fatal("kpagecount returned fewer pages than expected");
707 for (i
= 0; i
< pages
; i
++)
708 add_page(voffset
+ i
, index
+ i
,
709 buf
[i
], cgi
[i
], cnt
[i
], pme
);
716 static void walk_swap(unsigned long voffset
, uint64_t pme
)
718 uint64_t flags
= kpageflags_flags(0, pme
);
720 if (!bit_mask_ok(flags
))
727 show_page_range(voffset
, pagemap_swap_offset(pme
),
729 else if (opt_list
== 2)
730 show_page(voffset
, pagemap_swap_offset(pme
), flags
, 0, 0);
732 nr_pages
[hash_slot(flags
)]++;
736 #define PAGEMAP_BATCH (64 << 10)
737 static void walk_vma(unsigned long index
, unsigned long count
)
739 uint64_t buf
[PAGEMAP_BATCH
];
746 batch
= min_t(unsigned long, count
, PAGEMAP_BATCH
);
747 pages
= pagemap_read(buf
, index
, batch
);
751 for (i
= 0; i
< pages
; i
++) {
752 pfn
= pagemap_pfn(buf
[i
]);
754 walk_pfn(index
+ i
, pfn
, 1, buf
[i
]);
755 if (buf
[i
] & PM_SWAP
)
756 walk_swap(index
+ i
, buf
[i
]);
764 static void walk_task(unsigned long index
, unsigned long count
)
766 const unsigned long end
= index
+ count
;
770 while (index
< end
) {
772 while (pg_end
[i
] <= index
)
775 if (pg_start
[i
] >= end
)
778 start
= max_t(unsigned long, pg_start
[i
], index
);
779 index
= min_t(unsigned long, pg_end
[i
], end
);
781 assert(start
< index
);
782 walk_vma(start
, index
- start
);
786 static void add_addr_range(unsigned long offset
, unsigned long size
)
788 if (nr_addr_ranges
>= MAX_ADDR_RANGES
)
789 fatal("too many addr ranges\n");
791 opt_offset
[nr_addr_ranges
] = offset
;
792 opt_size
[nr_addr_ranges
] = min_t(unsigned long, size
, ULONG_MAX
-offset
);
796 static void walk_addr_ranges(void)
800 kpageflags_fd
= checked_open(opt_kpageflags
, O_RDONLY
);
803 add_addr_range(0, ULONG_MAX
);
805 for (i
= 0; i
< nr_addr_ranges
; i
++)
807 walk_pfn(opt_offset
[i
], opt_offset
[i
], opt_size
[i
], 0);
809 walk_task(opt_offset
[i
], opt_size
[i
]);
814 close(kpageflags_fd
);
822 static const char *page_flag_type(uint64_t flag
)
824 if (flag
& KPF_HACKERS_BITS
)
826 if (flag
& KPF_OVERLOADED_BITS
)
831 static void usage(void)
836 "page-types [options]\n"
837 " -r|--raw Raw mode, for kernel developers\n"
838 " -d|--describe flags Describe flags\n"
839 " -a|--addr addr-spec Walk a range of pages\n"
840 " -b|--bits bits-spec Walk pages with specified bits\n"
841 " -c|--cgroup path|@inode Walk pages within memory cgroup\n"
842 " -p|--pid pid Walk process address space\n"
843 " -f|--file filename Walk file address space\n"
844 " -i|--mark-idle Mark pages idle\n"
845 " -l|--list Show page details in ranges\n"
846 " -L|--list-each Show page details one by one\n"
847 " -C|--list-cgroup Show cgroup inode for pages\n"
848 " -M|--list-mapcnt Show page map count\n"
849 " -N|--no-summary Don't show summary info\n"
850 " -X|--hwpoison hwpoison pages\n"
851 " -x|--unpoison unpoison pages\n"
852 " -F|--kpageflags filename kpageflags file to parse\n"
853 " -h|--help Show this usage message\n"
855 " 0x10 bitfield format, e.g.\n"
856 " anon bit-name, e.g.\n"
857 " 0x10,anon comma-separated list, e.g.\n"
859 " N one page at offset N (unit: pages)\n"
860 " N+M pages range from N to N+M-1\n"
861 " N,M pages range from N to M-1\n"
862 " N, pages range from N to end\n"
863 " ,M pages range from 0 to M-1\n"
865 " bit1,bit2 (flags & (bit1|bit2)) != 0\n"
866 " bit1,bit2=bit1 (flags & (bit1|bit2)) == bit1\n"
867 " bit1,~bit2 (flags & (bit1|bit2)) == bit1\n"
868 " =bit1,bit2 flags == (bit1|bit2)\n"
872 for (i
= 0, j
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
873 if (!page_flag_names
[i
])
875 printf("%16s%s", page_flag_names
[i
] + 2,
876 page_flag_type(1ULL << i
));
883 "(r) raw mode bits (o) overloaded bits\n");
886 static unsigned long long parse_number(const char *str
)
888 unsigned long long n
;
890 n
= strtoll(str
, NULL
, 0);
892 if (n
== 0 && str
[0] != '0')
893 fatal("invalid name or number: %s\n", str
);
898 static void parse_pid(const char *str
)
903 opt_pid
= parse_number(str
);
905 sprintf(buf
, "/proc/%d/pagemap", opt_pid
);
906 pagemap_fd
= checked_open(buf
, O_RDONLY
);
908 sprintf(buf
, "/proc/%d/maps", opt_pid
);
909 file
= fopen(buf
, "r");
915 while (fgets(buf
, sizeof(buf
), file
) != NULL
) {
916 unsigned long vm_start
;
917 unsigned long vm_end
;
918 unsigned long long pgoff
;
924 n
= sscanf(buf
, "%lx-%lx %c%c%c%c %llx %x:%x %lu",
932 fprintf(stderr
, "unexpected line: %s\n", buf
);
935 pg_start
[nr_vmas
] = vm_start
/ page_size
;
936 pg_end
[nr_vmas
] = vm_end
/ page_size
;
937 if (++nr_vmas
>= MAX_VMAS
) {
938 fprintf(stderr
, "too many VMAs\n");
945 static void show_file(const char *name
, const struct stat
*st
)
947 unsigned long long size
= st
->st_size
;
948 char atime
[64], mtime
[64];
949 long now
= time(NULL
);
951 printf("%s\tInode: %u\tSize: %llu (%llu pages)\n",
952 name
, (unsigned)st
->st_ino
,
953 size
, (size
+ page_size
- 1) / page_size
);
955 strftime(atime
, sizeof(atime
), "%c", localtime(&st
->st_atime
));
956 strftime(mtime
, sizeof(mtime
), "%c", localtime(&st
->st_mtime
));
958 printf("Modify: %s (%ld seconds ago)\nAccess: %s (%ld seconds ago)\n",
959 mtime
, now
- st
->st_mtime
,
960 atime
, now
- st
->st_atime
);
963 static sigjmp_buf sigbus_jmp
;
965 static void * volatile sigbus_addr
;
967 static void sigbus_handler(int sig
, siginfo_t
*info
, void *ucontex
)
971 sigbus_addr
= info
? info
->si_addr
: NULL
;
972 siglongjmp(sigbus_jmp
, 1);
975 static struct sigaction sigbus_action
= {
976 .sa_sigaction
= sigbus_handler
,
977 .sa_flags
= SA_SIGINFO
,
980 static void walk_file(const char *name
, const struct stat
*st
)
982 uint8_t vec
[PAGEMAP_BATCH
];
983 uint64_t buf
[PAGEMAP_BATCH
], flags
;
986 unsigned long nr_pages
, pfn
, i
;
987 off_t off
, end
= st
->st_size
;
993 fd
= checked_open(name
, O_RDONLY
|O_NOATIME
|O_NOFOLLOW
);
995 for (off
= 0; off
< end
; off
+= len
) {
996 nr_pages
= (end
- off
+ page_size
- 1) / page_size
;
997 if (nr_pages
> PAGEMAP_BATCH
)
998 nr_pages
= PAGEMAP_BATCH
;
999 len
= nr_pages
* page_size
;
1001 ptr
= mmap(NULL
, len
, PROT_READ
, MAP_SHARED
, fd
, off
);
1002 if (ptr
== MAP_FAILED
)
1003 fatal("mmap failed: %s", name
);
1005 /* determine cached pages */
1006 if (mincore(ptr
, len
, vec
))
1007 fatal("mincore failed: %s", name
);
1009 /* turn off readahead */
1010 if (madvise(ptr
, len
, MADV_RANDOM
))
1011 fatal("madvice failed: %s", name
);
1013 if (sigsetjmp(sigbus_jmp
, 1)) {
1014 end
= off
+ sigbus_addr
? sigbus_addr
- ptr
: 0;
1015 fprintf(stderr
, "got sigbus at offset %lld: %s\n",
1016 (long long)end
, name
);
1021 for (i
= 0; i
< nr_pages
; i
++) {
1023 (void)*(volatile int *)(ptr
+ i
* page_size
);
1027 /* turn off harvesting reference bits */
1028 if (madvise(ptr
, len
, MADV_SEQUENTIAL
))
1029 fatal("madvice failed: %s", name
);
1031 if (pagemap_read(buf
, (unsigned long)ptr
/ page_size
,
1032 nr_pages
) != nr_pages
)
1033 fatal("cannot read pagemap");
1037 for (i
= 0; i
< nr_pages
; i
++) {
1038 pfn
= pagemap_pfn(buf
[i
]);
1041 if (!kpageflags_read(&flags
, pfn
, 1))
1043 if (!kpagecgroup_read(&cgroup
, pfn
, 1))
1044 fatal("kpagecgroup_read failed");
1045 if (!kpagecount_read(&mapcnt
, pfn
, 1))
1046 fatal("kpagecount_read failed");
1047 if (first
&& opt_list
) {
1050 show_file(name
, st
);
1052 add_page(off
/ page_size
+ i
, pfn
,
1053 flags
, cgroup
, mapcnt
, buf
[i
]);
1060 int walk_tree(const char *name
, const struct stat
*st
, int type
, struct FTW
*f
)
1065 if (S_ISREG(st
->st_mode
))
1066 walk_file(name
, st
);
1069 fprintf(stderr
, "cannot read dir: %s\n", name
);
1075 static void walk_page_cache(void)
1079 kpageflags_fd
= checked_open(opt_kpageflags
, O_RDONLY
);
1080 pagemap_fd
= checked_open("/proc/self/pagemap", O_RDONLY
);
1081 sigaction(SIGBUS
, &sigbus_action
, NULL
);
1083 if (stat(opt_file
, &st
))
1084 fatal("stat failed: %s\n", opt_file
);
1086 if (S_ISREG(st
.st_mode
)) {
1087 walk_file(opt_file
, &st
);
1088 } else if (S_ISDIR(st
.st_mode
)) {
1089 /* do not follow symlinks and mountpoints */
1090 if (nftw(opt_file
, walk_tree
, 64, FTW_MOUNT
| FTW_PHYS
) < 0)
1091 fatal("nftw failed: %s\n", opt_file
);
1093 fatal("unhandled file type: %s\n", opt_file
);
1095 close(kpageflags_fd
);
1097 signal(SIGBUS
, SIG_DFL
);
1100 static void parse_file(const char *name
)
1105 static void parse_cgroup(const char *path
)
1107 if (path
[0] == '@') {
1108 opt_cgroup
= parse_number(path
+ 1);
1114 if (stat(path
, &st
))
1115 fatal("stat failed: %s: %m\n", path
);
1117 if (!S_ISDIR(st
.st_mode
))
1118 fatal("cgroup supposed to be a directory: %s\n", path
);
1120 opt_cgroup
= st
.st_ino
;
1123 static void parse_addr_range(const char *optarg
)
1125 unsigned long offset
;
1129 p
= strchr(optarg
, ',');
1131 p
= strchr(optarg
, '+');
1135 size
= parse_number(p
+ 1);
1137 offset
= parse_number(optarg
);
1141 size
= parse_number(p
+ 1);
1144 fatal("invalid range: %lu,%lu\n",
1150 offset
= parse_number(optarg
);
1154 add_addr_range(offset
, size
);
1157 static void add_bits_filter(uint64_t mask
, uint64_t bits
)
1159 if (nr_bit_filters
>= MAX_BIT_FILTERS
)
1160 fatal("too much bit filters\n");
1162 opt_mask
[nr_bit_filters
] = mask
;
1163 opt_bits
[nr_bit_filters
] = bits
;
1167 static uint64_t parse_flag_name(const char *str
, int len
)
1174 if (len
<= 8 && !strncmp(str
, "compound", len
))
1175 return BITS_COMPOUND
;
1177 for (i
= 0; i
< ARRAY_SIZE(page_flag_names
); i
++) {
1178 if (!page_flag_names
[i
])
1180 if (!strncmp(str
, page_flag_names
[i
] + 2, len
))
1184 return parse_number(str
);
1187 static uint64_t parse_flag_names(const char *str
, int all
)
1189 const char *p
= str
;
1193 if (*p
== ',' || *p
== '=' || *p
== '\0') {
1194 if ((*str
!= '~') || (*str
== '~' && all
&& *++str
))
1195 flags
|= parse_flag_name(str
, p
- str
);
1206 static void parse_bits_mask(const char *optarg
)
1212 p
= strchr(optarg
, '=');
1214 mask
= KPF_ALL_BITS
;
1215 bits
= parse_flag_names(p
+ 1, 0);
1217 mask
= parse_flag_names(optarg
, 0);
1218 bits
= parse_flag_names(p
+ 1, 0);
1219 } else if (strchr(optarg
, '~')) {
1220 mask
= parse_flag_names(optarg
, 1);
1221 bits
= parse_flag_names(optarg
, 0);
1223 mask
= parse_flag_names(optarg
, 0);
1224 bits
= KPF_ALL_BITS
;
1227 add_bits_filter(mask
, bits
);
1230 static void parse_kpageflags(const char *name
)
1232 opt_kpageflags
= name
;
1235 static void describe_flags(const char *optarg
)
1237 uint64_t flags
= parse_flag_names(optarg
, 0);
1239 printf("0x%016llx\t%s\t%s\n",
1240 (unsigned long long)flags
,
1241 page_flag_name(flags
),
1242 page_flag_longname(flags
));
1245 static const struct option opts
[] = {
1246 { "raw" , 0, NULL
, 'r' },
1247 { "pid" , 1, NULL
, 'p' },
1248 { "file" , 1, NULL
, 'f' },
1249 { "addr" , 1, NULL
, 'a' },
1250 { "bits" , 1, NULL
, 'b' },
1251 { "cgroup" , 1, NULL
, 'c' },
1252 { "describe" , 1, NULL
, 'd' },
1253 { "mark-idle" , 0, NULL
, 'i' },
1254 { "list" , 0, NULL
, 'l' },
1255 { "list-each" , 0, NULL
, 'L' },
1256 { "list-cgroup", 0, NULL
, 'C' },
1257 { "list-mapcnt", 0, NULL
, 'M' },
1258 { "no-summary", 0, NULL
, 'N' },
1259 { "hwpoison" , 0, NULL
, 'X' },
1260 { "unpoison" , 0, NULL
, 'x' },
1261 { "kpageflags", 0, NULL
, 'F' },
1262 { "help" , 0, NULL
, 'h' },
1263 { NULL
, 0, NULL
, 0 }
1266 int main(int argc
, char *argv
[])
1270 page_size
= getpagesize();
1272 while ((c
= getopt_long(argc
, argv
,
1273 "rp:f:a:b:d:c:CilLMNXxF:h",
1274 opts
, NULL
)) != -1) {
1286 parse_addr_range(optarg
);
1289 parse_bits_mask(optarg
);
1292 parse_cgroup(optarg
);
1295 opt_list_cgroup
= 1;
1298 describe_flags(optarg
);
1310 opt_list_mapcnt
= 1;
1317 prepare_hwpoison_fd();
1321 prepare_hwpoison_fd();
1324 parse_kpageflags(optarg
);
1335 if (!opt_kpageflags
)
1336 opt_kpageflags
= PROC_KPAGEFLAGS
;
1338 if (opt_cgroup
|| opt_list_cgroup
)
1339 kpagecgroup_fd
= checked_open(PROC_KPAGECGROUP
, O_RDONLY
);
1341 if (opt_list
&& opt_list_mapcnt
)
1342 kpagecount_fd
= checked_open(PROC_KPAGECOUNT
, O_RDONLY
);
1344 if (opt_mark_idle
&& opt_file
)
1345 page_idle_fd
= checked_open(SYS_KERNEL_MM_PAGE_IDLE
, O_RDWR
);
1347 if (opt_list
&& opt_pid
)
1348 printf("voffset\t");
1349 if (opt_list
&& opt_file
)
1350 printf("foffset\t");
1351 if (opt_list
&& opt_list_cgroup
)
1353 if (opt_list
&& opt_list_mapcnt
)
1354 printf("map-cnt\t");
1357 printf("offset\tlen\tflags\n");
1359 printf("offset\tflags\n");
1377 if (opt_list_mapcnt
)
1378 close(kpagecount_fd
);
1380 if (page_idle_fd
>= 0)
1381 close(page_idle_fd
);