diff options
Diffstat (limited to 'mincore.c')
-rw-r--r-- | mincore.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/mincore.c b/mincore.c new file mode 100644 index 0000000..3309bd9 --- /dev/null +++ b/mincore.c @@ -0,0 +1,63 @@ +#include "compat-util.h" + +static void mincore_stats(const char *path) +{ + struct stat sb; + char *map; + unsigned char *vec; + size_t vec_len; + size_t map_len; + int fd; + size_t i; + + if ((fd = open(path, O_RDONLY)) < 0) { + fprintf(stderr, "%s: open(): %s\n", path, strerror(errno)); + return; + } + + if (fstat(fd, &sb) < 0) { + fprintf(stderr, "%s: fstat(%d): %s\n", + path, fd, strerror(errno)); + goto err_close; + } + + vec_len = (sb.st_size + page_size() - 1) / page_size(); + if (!(vec = malloc(vec_len))) { + fprintf(stderr, "%s: malloc(%lu): %s\n", + path, (unsigned long)vec_len, strerror(errno)); + goto err_close; + } + + map_len = PAGE_ALIGN(sb.st_size); + if (!(map = mmap(NULL, map_len, PROT_READ, MAP_SHARED, fd, 0))) { + fprintf(stderr, "%s: mmap(%lu): %s\n", + path, (unsigned long)vec_len, strerror(errno)); + goto err_free; + } + + if (mincore(map, map_len, vec) < 0) { + fprintf(stderr, "%s: mincore(%lu): %s\n", + path, (unsigned long)vec_len, strerror(errno)); + goto err_munmap; + } + + for (i = 0; i < vec_len; ++i) + printf("%s: [%08lu] %x\n", + path, (unsigned long)i, + (vec[i])); +err_munmap: + munmap(map, map_len); +err_free: + free(vec); +err_close: + close(fd); +} + +int main(int argc, char * const argv[]) +{ + int i; + + for (i = 1; i < argc; ++i) + mincore_stats(argv[i]); + return 0; +} |