bw_mmap_rd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * bw_mmap_rd.c - time reading & summing of a file using mmap
  3. *
  4. * Usage: bw_mmap_rd size file
  5. *
  6. * Sizes less than 2m are not recommended. Memory is read by summing it up
  7. * so the numbers include the cost of the adds. If you use sizes large
  8. * enough, you can compare to bw_mem_rd and get the cost of TLB fills
  9. * (very roughly).
  10. *
  11. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  12. * additional restriction that results may published only if
  13. * (1) the benchmark is unmodified, and
  14. * (2) the version in the sccsid below is included in the report.
  15. * Support for this development by Sun Microsystems is gratefully acknowledged.
  16. */
  17. char *id = "$Id$\n";
  18. #include "bench.h"
  19. #ifdef MAP_FILE
  20. # define MMAP_FLAGS MAP_FILE|MAP_SHARED
  21. #else
  22. # define MMAP_FLAGS MAP_SHARED
  23. #endif
  24. #define TYPE int
  25. #define MINSZ (sizeof(TYPE) * 128)
  26. #define CHK(x) if ((long)(x) == -1) { perror("x"); exit(1); }
  27. void
  28. doit(register TYPE *p, register TYPE *lastone)
  29. {
  30. register int sum = 0;
  31. while (p <= lastone) {
  32. sum += p[0]+p[1]+p[2]+p[3]+p[4]+p[5]+p[6]+p[7]+p[8]+
  33. p[9]+p[10]+p[11]+p[12]+p[13]+p[14]+p[15]+p[16]+p[17]+
  34. p[18]+p[19]+p[20]+p[21]+p[22]+p[23]+p[24]+p[25]+p[26]+
  35. p[27]+p[28]+p[29]+p[30]+p[31]+p[32]+p[33]+p[34]+p[35]+
  36. p[36]+p[37]+p[38]+p[39]+p[40]+p[41]+p[42]+p[43]+
  37. p[44]+p[45]+p[46]+p[47]+p[48]+p[49]+p[50]+p[51]+
  38. p[52]+p[53]+p[54]+p[55]+p[56]+p[57]+p[58]+p[59]+
  39. p[60]+p[61]+p[62]+p[63]+p[64]+p[65]+p[66]+p[67]+
  40. p[68]+p[69]+p[70]+p[71]+p[72]+p[73]+p[74]+p[75]+
  41. p[76]+p[77]+p[78]+p[79]+p[80]+p[81]+p[82]+p[83]+
  42. p[84]+p[85]+p[86]+p[87]+p[88]+p[89]+p[90]+p[91]+
  43. p[92]+p[93]+p[94]+p[95]+p[96]+p[97]+p[98]+p[99]+
  44. p[100]+p[101]+p[102]+p[103]+p[104]+p[105]+p[106]+
  45. p[107]+p[108]+p[109]+p[110]+p[111]+p[112]+p[113]+
  46. p[114]+p[115]+p[116]+p[117]+p[118]+p[119]+p[120]+
  47. p[121]+p[122]+p[123]+p[124]+p[125]+p[126]+p[127];
  48. p += 128;
  49. }
  50. use_int(sum);
  51. }
  52. void
  53. time_with_open(char *file, int nbytes)
  54. {
  55. int fd;
  56. TYPE *buf, *lastone;
  57. CHK(fd = open(file, 0));
  58. CHK(buf = (TYPE*)mmap(0, nbytes, PROT_READ, MMAP_FLAGS, fd, 0));
  59. lastone = (TYPE*)((char*)buf + nbytes - MINSZ);
  60. doit(buf, lastone);
  61. close(fd);
  62. munmap((void*)buf, nbytes);
  63. }
  64. int
  65. main(int ac, char **av)
  66. {
  67. int fd, nbytes;
  68. struct stat sbuf;
  69. TYPE *buf, *lastone;
  70. if (ac != 4) {
  71. fprintf(stderr,
  72. "Usage: %s size open2close|mmap_only file\n", av[0]);
  73. exit(1);
  74. }
  75. nbytes = bytes(av[1]);
  76. CHK(stat(av[3], &sbuf));
  77. if ((nbytes > sbuf.st_size) || (nbytes < MINSZ)) {
  78. exit(1);
  79. }
  80. if (!strcmp("open2close", av[2])) {
  81. BENCH(time_with_open(av[3], nbytes), 0);
  82. } else {
  83. CHK(fd = open(av[3], 0));
  84. CHK(buf = (TYPE*)mmap(0, nbytes, PROT_READ, MMAP_FLAGS, fd, 0));
  85. lastone = (TYPE*)((char*)buf + nbytes - MINSZ);
  86. BENCH(doit(buf, lastone), 0);
  87. munmap((void*)buf, nbytes);
  88. }
  89. bandwidth(nbytes, get_n(), 0);
  90. return (0);
  91. }