lat_mmap.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * lat_mmap.c - time how fast a mapping can be made and broken down
  3. *
  4. * Usage: mmap size file
  5. *
  6. * XXX - If an implementation did lazy address space mapping, this test
  7. * will make that system look very good. I haven't heard of such a system.
  8. *
  9. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  10. * additional restriction that results may published only if
  11. * (1) the benchmark is unmodified, and
  12. * (2) the version in the sccsid below is included in the report.
  13. * Support for this development by Sun Microsystems is gratefully acknowledged.
  14. */
  15. char *id = "$Id$\n";
  16. #include "bench.h"
  17. #define PSIZE (16<<10)
  18. #define N 10
  19. #define STRIDE (10*PSIZE)
  20. #define MINSIZE (STRIDE*2)
  21. #define CHK(x) if ((x) == -1) { perror("x"); exit(1); }
  22. /*
  23. * This alg due to Linus. The goal is to have both sparse and full
  24. * mappings reported.
  25. */
  26. void
  27. mapit(int fd, size_t size, int random)
  28. {
  29. char *p, *where, *end;
  30. char c = size & 0xff;
  31. #ifdef MAP_FILE
  32. where = mmap(0, size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);
  33. #else
  34. where = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  35. #endif
  36. if ((int)where == -1) {
  37. perror("mmap");
  38. exit(1);
  39. }
  40. if (random) {
  41. end = where + size;
  42. for (p = where; p < end; p += STRIDE) {
  43. *p = c;
  44. }
  45. } else {
  46. end = where + (size / N);
  47. for (p = where; p < end; p += PSIZE) {
  48. *p = c;
  49. }
  50. }
  51. munmap(where, size);
  52. }
  53. int
  54. main(int ac, char **av)
  55. {
  56. int fd;
  57. size_t size;
  58. int random = 0;
  59. char *prog = av[0];
  60. if (ac != 3 && ac != 4) {
  61. fprintf(stderr, "usage: %s [-r] size file\n", prog);
  62. exit(1);
  63. }
  64. if (strcmp("-r", av[1]) == 0) {
  65. random = 1;
  66. ac--, av++;
  67. }
  68. size = bytes(av[1]);
  69. if (size < MINSIZE) {
  70. return (1);
  71. }
  72. CHK(fd = open(av[2], O_CREAT|O_RDWR, 0666));
  73. CHK(ftruncate(fd, size));
  74. BENCH(mapit(fd, size, random), 0);
  75. micromb(size, get_n());
  76. return(0);
  77. }