1 #include "compat-util.h"
3 static int usage(const char * argv0
)
5 fprintf(stderr
, "Usage: %s [-o OFFSET] [-l LENGTH] FILE...\n", argv0
);
9 static void mincore_stats(const char *path
, off_t offset
, off_t len
)
18 static const char *fmt
= sizeof(void *) == 8 ?
19 "%s: %016lx %x\n": "%s: %08lx %x\n";
21 if ((fd
= open(path
, O_RDONLY
|O_NOATIME
)) < 0) {
22 fprintf(stderr
, "%s: open(): %s\n", path
, strerror(errno
));
29 if (fstat(fd
, &sb
) < 0) {
30 fprintf(stderr
, "%s: fstat(%d): %s\n",
31 path
, fd
, strerror(errno
));
34 len
= sb
.st_size
- offset
;
37 vec_len
= (len
+ page_size() - 1) / page_size();
38 if (!(vec
= malloc(vec_len
))) {
39 fprintf(stderr
, "%s: malloc(%lu): %s\n",
40 path
, (unsigned long)vec_len
, strerror(errno
));
44 map_len
= PAGE_ALIGN(len
);
45 map_offset
= PAGE_ALIGN_DOWN(offset
+ 1);
47 map
= mmap(NULL
, map_len
, PROT_READ
, MAP_SHARED
, fd
, map_offset
);
49 fprintf(stderr
, "%s: mmap(%lu): %s\n",
50 path
, (unsigned long)vec_len
, strerror(errno
));
54 if (mincore(map
, map_len
, vec
) < 0) {
55 fprintf(stderr
, "%s: mincore(%lu): %s\n",
56 path
, (unsigned long)vec_len
, strerror(errno
));
60 for (i
= 0; i
< vec_len
; ++i
)
62 (unsigned long)((page_size() * i
) + map_offset
),
72 int main(int argc
, char * const argv
[])
79 while ((opt
= getopt(argc
, argv
, "o:l:h")) != -1) {
85 offset
= cstr_to_off_t(optarg
, &err
, 10);
86 if (*err
|| offset
< 0) {
87 fprintf(stderr
, "offset must be >= 0\n");
92 len
= cstr_to_off_t(optarg
, &err
, 10);
93 if (*err
|| len
< 0) {
94 fprintf(stderr
, "length must be >= 0\n");
99 return usage(argv
[0]);
104 return usage(argv
[0]);
106 for (; argi
< argc
; ++argi
)
107 mincore_stats(argv
[argi
], offset
, len
);