9ping.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. typedef uint64_t uint64_t;
  12. #define TWID64 ((uint64_t)~(uint64_t)0)
  13. uint64_t
  14. unittoull(char *s)
  15. {
  16. char *es;
  17. uint64_t n;
  18. if(s == nil)
  19. return TWID64;
  20. n = strtoul(s, &es, 0);
  21. if(*es == 'k' || *es == 'K'){
  22. n *= 1024;
  23. es++;
  24. }else if(*es == 'm' || *es == 'M'){
  25. n *= 1024*1024;
  26. es++;
  27. }else if(*es == 'g' || *es == 'G'){
  28. n *= 1024*1024*1024;
  29. es++;
  30. }
  31. if(*es != '\0')
  32. return TWID64;
  33. return n;
  34. }
  35. void
  36. main(int argc, char *argv[])
  37. {
  38. int fd, i;
  39. int n = 1000, m;
  40. int s = 1;
  41. double *t, t0, t1;
  42. unsigned char *buf;
  43. double a, d, max, min;
  44. m = OREAD;
  45. ARGBEGIN{
  46. case 'n':
  47. n = atoi(ARGF());
  48. break;
  49. case 's':
  50. s = unittoull(ARGF());
  51. if(s < 1 || s > 1024*1024)
  52. sysfatal("bad size");
  53. break;
  54. case 'r':
  55. m = OREAD;
  56. break;
  57. case 'w':
  58. m = OWRITE;
  59. break;
  60. }ARGEND
  61. fd = 0;
  62. if(argc == 1){
  63. fd = open(argv[0], m);
  64. if(fd < 0)
  65. sysfatal("could not open file: %s: %r", argv[0]);
  66. }
  67. buf = malloc(s);
  68. t = malloc(n*sizeof(double));
  69. t0 = nsec();
  70. for(i=0; i<n; i++){
  71. if(m == OREAD){
  72. if(pread(fd, buf, s, 0) < s)
  73. sysfatal("bad read: %r");
  74. }else{
  75. if(pwrite(fd, buf, s, 0) < s)
  76. sysfatal("bad write: %r");
  77. }
  78. t1 = nsec();
  79. t[i] = (t1 - t0)*1e-3;
  80. t0 = t1;
  81. }
  82. a = 0.;
  83. d = 0.;
  84. max = 0.;
  85. min = 1e12;
  86. for(i=0; i<n; i++){
  87. a += t[i];
  88. if(max < t[i])
  89. max = t[i];
  90. if(min > t[i])
  91. min = t[i];
  92. }
  93. a /= n;
  94. for(i=0; i<n; i++)
  95. d += (a - t[i]) * (a - t[i]);
  96. d /= n;
  97. d = sqrt(d);
  98. print("avg = %.0fµs min = %.0fµs max = %.0fµs dev = %.0fµs\n", a, min, max, d);
  99. exits(0);
  100. }