1 #include "compat-util.h"
3 static void mincore_stats(const char *path
, off_t offset
, off_t len
)
12 static const char *fmt
= sizeof(void *) == 8 ?
13 "%s: %016lx %x\n": "%s: %08lx %x\n";
15 if ((fd
= open(path
, O_RDONLY
|O_NOATIME
)) < 0) {
16 fprintf(stderr
, "%s: open(): %s\n", path
, strerror(errno
));
23 if (fstat(fd
, &sb
) < 0) {
24 fprintf(stderr
, "%s: fstat(%d): %s\n",
25 path
, fd
, strerror(errno
));
31 vec_len
= (len
+ page_size() - 1) / page_size();
32 if (!(vec
= malloc(vec_len
))) {
33 fprintf(stderr
, "%s: malloc(%lu): %s\n",
34 path
, (unsigned long)vec_len
, strerror(errno
));
38 map_len
= PAGE_ALIGN(len
);
39 map_offset
= PAGE_ALIGN_DOWN(offset
);
41 map
= mmap(NULL
, map_len
, PROT_READ
, MAP_SHARED
, fd
, map_offset
);
43 fprintf(stderr
, "%s: mmap(%lu): %s\n",
44 path
, (unsigned long)vec_len
, strerror(errno
));
48 if (mincore(map
, map_len
, vec
) < 0) {
49 fprintf(stderr
, "%s: mincore(%lu): %s\n",
50 path
, (unsigned long)vec_len
, strerror(errno
));
54 for (i
= 0; i
< vec_len
; ++i
)
56 (unsigned long)(page_size() * i
) + map_offset
,
66 int main(int argc
, char * const argv
[])
73 while ((opt
= getopt(argc
, argv
, "o:l:")) != -1) {
79 offset
= strtol(optarg
, &err
, 10);
80 if (*err
|| offset
< 0) {
81 fprintf(stderr
, "offset must be >= 0\n");
86 len
= strtol(optarg
, &err
, 10);
87 if (*err
|| len
< 0) {
88 fprintf(stderr
, "length must be >= 0\n");
94 "Usage: %s [-o offset] "
95 "[-l length] <files>\n", argv
[0]);
100 for (; argi
< argc
; ++argi
)
101 mincore_stats(argv
[argi
], offset
, len
);