delayTest.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * delayTest.c:
  3. * Just a little test program I'm using to experiment with
  4. * various timings and latency, etc.
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <sys/time.h>
  28. #define CYCLES 1000
  29. int main()
  30. {
  31. int x ;
  32. struct timeval t1, t2, t3 ;
  33. int t ;
  34. int max, min ;
  35. int del ;
  36. int underRuns, overRuns, exactRuns, bogusRuns, total ;
  37. int descheds ;
  38. // Baseline test
  39. gettimeofday (&t1, NULL) ;
  40. gettimeofday (&t2, NULL) ;
  41. t = t2.tv_usec - t1.tv_usec ;
  42. printf ("Baseline test: %d\n", t);
  43. for (del = 1 ; del < 200 ; ++del)
  44. {
  45. underRuns = overRuns = exactRuns = total = 0 ;
  46. descheds = 0 ;
  47. max = 0 ;
  48. min = 999 ;
  49. for (x = 0 ; x < CYCLES ; ++x)
  50. {
  51. for (;;) // Repeat this if we get a delay over 999uS
  52. { // -> High probability Linux has deschedulled us
  53. gettimeofday (&t1, NULL) ;
  54. usleep (del) ;
  55. // delayMicroseconds (del) ;
  56. gettimeofday (&t2, NULL) ;
  57. timersub (&t2, &t1, &t3) ;
  58. t = t3.tv_usec ;
  59. if (t > 999)
  60. {
  61. ++descheds ;
  62. continue ;
  63. }
  64. else
  65. break ;
  66. }
  67. if (t == del)
  68. ++exactRuns ;
  69. else if (t < del)
  70. ++underRuns ;
  71. else if (t > del)
  72. ++overRuns ;
  73. if (t > max)
  74. max = t ;
  75. else if (t < min)
  76. min = t ;
  77. total += t ;
  78. }
  79. printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\n",
  80. del, min, max, underRuns, overRuns, exactRuns, total / CYCLES, descheds) ;
  81. fflush (stdout) ;
  82. usleep (1000) ;
  83. }
  84. return 0 ;
  85. }