bw_file_rd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * bw_file_rd.c - time reading & summing of a file
  3. *
  4. * Usage: bw_file_rd size file
  5. *
  6. * The intent is that the file is in memory.
  7. * Disk benchmarking is done with lmdd.
  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 CHK(x) if ((int)(x) == -1) { perror("x"); exit(1); }
  18. #ifndef MIN
  19. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  20. #endif
  21. #define TYPE int
  22. #define MINSZ (sizeof(TYPE) * 128)
  23. TYPE *buf; /* do the I/O here */
  24. TYPE *lastone; /* I/O ends here + MINSZ */
  25. int xfersize; /* do it in units of this */
  26. int count; /* bytes to move (can't be modified) */
  27. void
  28. doit(int fd)
  29. {
  30. int sum = 0, size;
  31. register TYPE *p, *end;
  32. size = count;
  33. end = lastone;
  34. while (size >= 0) {
  35. if (read(fd, buf, MIN(size, xfersize)) <= 0) {
  36. break;
  37. }
  38. for (p = buf; p <= end; ) {
  39. sum +=
  40. p[0]+p[1]+p[2]+p[3]+p[4]+p[5]+p[6]+p[7]+
  41. p[8]+p[9]+p[10]+p[11]+p[12]+p[13]+p[14]+
  42. p[15]+p[16]+p[17]+p[18]+p[19]+p[20]+p[21]+
  43. p[22]+p[23]+p[24]+p[25]+p[26]+p[27]+p[28]+
  44. p[29]+p[30]+p[31]+p[32]+p[33]+p[34]+p[35]+
  45. p[36]+p[37]+p[38]+p[39]+p[40]+p[41]+p[42]+
  46. p[43]+p[44]+p[45]+p[46]+p[47]+p[48]+p[49]+
  47. p[50]+p[51]+p[52]+p[53]+p[54]+p[55]+p[56]+
  48. p[57]+p[58]+p[59]+p[60]+p[61]+p[62]+p[63]+
  49. p[64]+p[65]+p[66]+p[67]+p[68]+p[69]+p[70]+
  50. p[71]+p[72]+p[73]+p[74]+p[75]+p[76]+p[77]+
  51. p[78]+p[79]+p[80]+p[81]+p[82]+p[83]+p[84]+
  52. p[85]+p[86]+p[87]+p[88]+p[89]+p[90]+p[91]+
  53. p[92]+p[93]+p[94]+p[95]+p[96]+p[97]+p[98]+
  54. p[99]+p[100]+p[101]+p[102]+p[103]+p[104]+
  55. p[105]+p[106]+p[107]+p[108]+p[109]+p[110]+
  56. p[111]+p[112]+p[113]+p[114]+p[115]+p[116]+
  57. p[117]+p[118]+p[119]+p[120]+p[121]+p[122]+
  58. p[123]+p[124]+p[125]+p[126]+p[127];
  59. p += 128;
  60. }
  61. size -= xfersize;
  62. }
  63. use_int(sum);
  64. }
  65. void
  66. time_with_open(char *file)
  67. {
  68. int fd = open(file, 0);
  69. doit(fd);
  70. close(fd);
  71. }
  72. void
  73. time_io_only(int fd)
  74. {
  75. lseek(fd, 0, 0);
  76. doit(fd);
  77. }
  78. int
  79. main(ac, av)
  80. char **av;
  81. {
  82. int fd;
  83. if (ac != 4) {
  84. fprintf(stderr,
  85. "Usage: %s size open2close|io_only file, min size=%uk\n",
  86. av[0], XFERSIZE>>10);
  87. exit(1);
  88. }
  89. count = bytes(av[1]);
  90. if (count < MINSZ) {
  91. exit(1); /* I want this to be quiet */
  92. }
  93. if (count < XFERSIZE) {
  94. xfersize = count;
  95. } else {
  96. xfersize = XFERSIZE;
  97. }
  98. buf = (TYPE *)valloc(XFERSIZE);
  99. lastone = (TYPE*)((char*)buf + xfersize - MINSZ);
  100. bzero((void*)buf, XFERSIZE);
  101. if (!strcmp("open2close", av[2])) {
  102. BENCH(time_with_open(av[3]), 0);
  103. } else {
  104. CHK(fd = open(av[3], 0));
  105. BENCH(time_io_only(fd), 0);
  106. close(fd);
  107. }
  108. bandwidth(count, get_n(), 0);
  109. return (0);
  110. }