Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/mman.h>
  3. #include <sys/stat.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8.  
  9. static int do_mincore(char *path)
  10. {
  11. unsigned long nr_pages, nr_present, i;
  12. struct stat statbuf;
  13. unsigned char *bits;
  14. int ret = 1;
  15. void *map;
  16. int fd;
  17.  
  18. fd = open(path, O_RDONLY);
  19. if (fd == -1)
  20. goto out;
  21.  
  22. if (fstat(fd, &statbuf) == -1)
  23. goto out_fd;
  24.  
  25. nr_pages = (statbuf.st_size + 4095) / 4096;
  26.  
  27. bits = malloc(nr_pages);
  28. if (bits == NULL)
  29. goto out_fd;
  30.  
  31. map = mmap(NULL, statbuf.st_size, PROT_NONE, MAP_SHARED, fd, 0);
  32. if (map == MAP_FAILED)
  33. goto out_bits;
  34.  
  35. if (mincore(map, statbuf.st_size, bits) == -1)
  36. goto out_map;
  37.  
  38. nr_present = 0;
  39. for (i = 0; i < nr_pages; i++)
  40. if (bits[i])
  41. nr_present++;
  42. printf("%lu/%lu %s\n", nr_present, nr_pages, path);
  43. ret = 0;
  44. out_map:
  45. munmap(map, statbuf.st_size);
  46. out_bits:
  47. free(bits);
  48. out_fd:
  49. close(fd);
  50. out:
  51. return ret;
  52. }
  53.  
  54. int main(int ac, char **av)
  55. {
  56. int ret = 0;
  57. int i;
  58.  
  59. if (ac == 1) {
  60. printf("usage: mincore FILE...\n");
  61. return 1;
  62. }
  63.  
  64. for (i = 1; i < ac; i++)
  65. ret |= do_mincore(av[i]);
  66.  
  67. return ret;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement