/contrib/ntp/util/hist.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 113 lines · 81 code · 11 blank · 21 comment · 18 complexity · dbfbc28d6d711eb34fabf46990f1242f MD5 · raw file

  1. /*
  2. * This program can be used to calibrate the clock reading jitter of a
  3. * particular CPU and operating system. It first tickles every element
  4. * of an array, in order to force pages into memory, then repeatedly calls
  5. * gettimeofday() and, finally, writes out the time values for later
  6. * analysis. From this you can determine the jitter and if the clock ever
  7. * runs backwards.
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include "ntp_types.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #define NBUF 100001 /* size of basic histogram */
  16. #define NSRT 20000 /* size of overflow histogram */
  17. #define NCNT (600 * 1000000) /* sample interval (us) */
  18. int col P((long *, long *));
  19. int
  20. main(
  21. int argc,
  22. char *argv[]
  23. )
  24. {
  25. struct timeval ts, tr, tp;
  26. struct timezone tzp;
  27. int i, j, n;
  28. long t, u, v, w, gtod[NBUF], ovfl[NSRT];
  29. /*
  30. * Force pages into memory
  31. */
  32. for (i = 0; i < NBUF; i++)
  33. gtod[i] = 0;
  34. for (i = 0; i < NSRT; i++)
  35. ovfl[i] = 0;
  36. /*
  37. * Construct histogram
  38. */
  39. n = 0;
  40. gettimeofday(&ts, &tzp);
  41. t = ts.tv_sec * 1000000 + ts.tv_usec;
  42. v = t;
  43. while (1) {
  44. gettimeofday(&tr, &tzp);
  45. u = tr.tv_sec * 1000000 + tr.tv_usec;
  46. if (u - v > NCNT)
  47. break;
  48. w = u - t;
  49. if (w <= 0) {
  50. /*
  51. printf("error <= 0 %ld %d %d, %d %d\n", w, ts.tv_sec,
  52. ts.tv_usec, tr.tv_sec, tr.tv_usec);
  53. */
  54. } else if (w > NBUF - 1) {
  55. ovfl[n] = w;
  56. if (n < NSRT - 1)
  57. n++;
  58. } else {
  59. gtod[w]++;
  60. }
  61. ts = tr;
  62. t = u;
  63. }
  64. /*
  65. * Write out histogram
  66. */
  67. for (i = 0; i < NBUF - 1; i++) {
  68. if (gtod[i] > 0)
  69. printf("%ld %ld\n", i, gtod[i]);
  70. }
  71. if (n == 0)
  72. return;
  73. qsort(
  74. #ifdef QSORT_USES_VOID_P
  75. (void *)
  76. #else
  77. (char *)
  78. #endif
  79. ovfl, (size_t)n, sizeof(long), col);
  80. w = 0;
  81. j = 0;
  82. for (i = 0; i < n; i++) {
  83. if (ovfl[i] != w) {
  84. if (j > 0)
  85. printf("%ld %ld\n", w, j);
  86. w = ovfl[i];
  87. j = 1;
  88. } else
  89. j++;
  90. }
  91. if (j > 0)
  92. printf("%ld %ld\n", w, j);
  93. exit(0);
  94. }
  95. int
  96. col(
  97. long *x,
  98. long *y
  99. )
  100. {
  101. return (*x - *y);
  102. }