delayTest.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <wiringPi.h>
  28. #include <sys/time.h>
  29. #define CYCLES 1000
  30. int main()
  31. {
  32. int x ;
  33. struct timeval t1, t2 ;
  34. int t ;
  35. int max, min ;
  36. int del ;
  37. int underRuns, overRuns, exactRuns, total ;
  38. int descheds ;
  39. if (wiringPiSetup () == -1)
  40. return 1 ;
  41. piHiPri (10) ; sleep (1) ;
  42. // Baseline test
  43. gettimeofday (&t1, NULL) ;
  44. gettimeofday (&t2, NULL) ;
  45. t = t2.tv_usec - t1.tv_usec ;
  46. printf ("Baseline test: %d\n", t);
  47. for (del = 1 ; del < 200 ; ++del)
  48. {
  49. underRuns = overRuns = exactRuns = total = 0 ;
  50. descheds = 0 ;
  51. max = del ;
  52. min = del ;
  53. for (x = 0 ; x < CYCLES ; ++x)
  54. {
  55. for (;;) // Repeat this if we get a delay over 999uS
  56. { // -> High probability Linux has deschedulled us
  57. gettimeofday (&t1, NULL) ;
  58. delayMicroseconds (del) ;
  59. gettimeofday (&t2, NULL) ;
  60. if (t2.tv_usec < t1.tv_usec) // Counter wrapped
  61. t = (1000000 + t2.tv_usec) - t1.tv_usec;
  62. else
  63. t = t2.tv_usec - t1.tv_usec ;
  64. if (t > 999)
  65. {
  66. ++descheds ;
  67. continue ;
  68. }
  69. else
  70. break ;
  71. }
  72. if (t > max)
  73. {
  74. max = t ;
  75. ++overRuns ;
  76. }
  77. else if (t < min)
  78. {
  79. min = t ;
  80. ++underRuns ;
  81. }
  82. else
  83. ++exactRuns ;
  84. total += t ;
  85. }
  86. printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\n",
  87. del, min, max, underRuns, overRuns, exactRuns, total / CYCLES, descheds) ;
  88. fflush (stdout) ;
  89. delay (1) ;
  90. }
  91. return 0 ;
  92. }