lat_pagefault.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * lat_pagefault.c - time a page fault in
  3. *
  4. * Usage: lat_pagefault file [file file...]
  5. *
  6. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  7. * additional restriction that results may published only if
  8. * (1) the benchmark is unmodified, and
  9. * (2) the version in the sccsid below is included in the report.
  10. * Support for this development by Sun Microsystems is gratefully acknowledged.
  11. */
  12. char *id = "$Id$\n";
  13. #include "bench.h"
  14. #define CHK(x) if ((x) == -1) { perror("x"); exit(1); }
  15. void timeit(char *file, char *where, int size);
  16. int
  17. main(int ac, char **av)
  18. {
  19. #ifdef MS_INVALIDATE
  20. int fd;
  21. char *where;
  22. struct stat sbuf;
  23. if (ac != 2) {
  24. fprintf(stderr, "usage: %s file\n", av[0]);
  25. exit(1);
  26. }
  27. CHK(fd = open(av[1], 0));
  28. CHK(fstat(fd, &sbuf));
  29. sbuf.st_size &= ~(16*1024 - 1); /* align it */
  30. if (sbuf.st_size < 1024*1024) {
  31. fprintf(stderr, "%s: %s too small\n", av[0], av[2]);
  32. exit(1);
  33. }
  34. where = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  35. if (msync(where, sbuf.st_size, MS_INVALIDATE) != 0) {
  36. perror("msync");
  37. exit(1);
  38. }
  39. timeit(av[1], where, sbuf.st_size);
  40. munmap(where, sbuf.st_size);
  41. #endif
  42. return(0);
  43. }
  44. /*
  45. * Get page fault times by going backwards in a stride of 256K
  46. * We don't want to do this in a loop, it needs a hi res clock.
  47. * XXX - hires.
  48. */
  49. void
  50. timeit(char *file, char *where, int size)
  51. {
  52. char *end = where + size - 16*1024;
  53. int sum = 0;
  54. int n = 0, usecs = 0;
  55. start(0);
  56. while (end > where) {
  57. sum += *end;
  58. end -= 256*1024;
  59. n++;
  60. }
  61. usecs = stop(0,0);
  62. fprintf(stderr, "n=%d, usecs=%lu\n", (int)n, (unsigned long)usecs);
  63. use_int(sum);
  64. fprintf(stderr, "Pagefaults on %s: %d usecs\n", file, usecs/n);
  65. }