1 #include "compat-util.h"
4 static int usage(const char * argv0
)
7 "Usage: %s [-o OFFSET] [-l LENGTH] [-s] FILE...\n", argv0
);
11 static void mincore_stats(const char *path
, off_t offset
, off_t len
)
20 static const char *fmt
= sizeof(void *) == 8 ?
21 "%s: %016lx %x\n": "%s: %08lx %x\n";
23 if ((fd
= open_noatime(path
)) < 0) {
24 fprintf(stderr
, "%s: open(): %s\n", path
, strerror(errno
));
31 if (fstat(fd
, &sb
) < 0) {
32 fprintf(stderr
, "%s: fstat(%d): %s\n",
33 path
, fd
, strerror(errno
));
36 len
= sb
.st_size
- offset
;
39 vec_len
= (len
+ page_size() - 1) / page_size();
40 if (!(vec
= malloc(vec_len
))) {
41 fprintf(stderr
, "%s: malloc(%lu): %s\n",
42 path
, (unsigned long)vec_len
, strerror(errno
));
46 map_len
= PAGE_ALIGN(len
);
47 map_offset
= PAGE_ALIGN_DOWN(offset
+ 1);
49 map
= mmap(NULL
, map_len
, PROT_READ
, MAP_SHARED
, fd
, map_offset
);
50 if (map
== MAP_FAILED
) {
51 fprintf(stderr
, "%s: mmap(%lu): %s\n",
52 path
, (unsigned long)vec_len
, strerror(errno
));
56 if (mincore(map
, map_len
, vec
) < 0) {
57 fprintf(stderr
, "%s: mincore(%lu): %s\n",
58 path
, (unsigned long)vec_len
, strerror(errno
));
65 for (i
= 0; i
< vec_len
; ++i
)
68 printf("%s: %F\n", path
, (double)n
/ (double)vec_len
);
70 for (i
= 0; i
< vec_len
; ++i
)
72 (unsigned long)((page_size() * i
) + map_offset
),
83 int main(int argc
, char * const argv
[])
90 while ((opt
= getopt(argc
, argv
, "o:l:hs")) != -1) {
97 offset
= cstr_to_off_t(optarg
, &err
, 10);
98 if (*err
|| offset
< 0) {
99 fprintf(stderr
, "offset must be >= 0\n");
105 len
= cstr_to_off_t(optarg
, &err
, 10);
106 if (*err
|| len
< 0) {
107 fprintf(stderr
, "length must be >= 0\n");
115 return usage(argv
[0]);
120 return usage(argv
[0]);
122 for (; argi
< argc
; ++argi
)
123 mincore_stats(argv
[argi
], offset
, len
);