dispatch_drift.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #ifdef __APPLE__
  21. #include <mach/mach_time.h>
  22. #endif
  23. #include <dispatch/dispatch.h>
  24. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  25. #include <sys/time.h>
  26. #include <unistd.h>
  27. #endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #ifdef __APPLE__
  31. #include <TargetConditionals.h>
  32. #endif
  33. #include <bsdtests.h>
  34. #include "dispatch_test.h"
  35. #if LENIENT_DEADLINES
  36. #define ACCEPTABLE_DRIFT 0.1
  37. #else
  38. #define ACCEPTABLE_DRIFT 0.001
  39. #endif
  40. int
  41. main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
  42. {
  43. __block uint32_t count = 0;
  44. __block double last_jitter = 0;
  45. __block double drift_sum = 0;
  46. #if defined(_WIN32)
  47. // 25 times a second (Windows timer resolution is poor)
  48. uint64_t interval = 1000000000 / 25;
  49. #else
  50. // 100 times a second
  51. uint64_t interval = 1000000000 / 100;
  52. #endif
  53. double interval_d = interval / 1000000000.0;
  54. // for 25 seconds
  55. unsigned int target = (unsigned int)(25.0 / interval_d);
  56. dispatch_test_start("Dispatch Timer Drift");
  57. dispatch_source_t t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
  58. test_ptr_notnull("dispatch_source_create", t);
  59. dispatch_source_set_timer(t, dispatch_time(DISPATCH_TIME_NOW, (int64_t)interval), interval, 0);
  60. dispatch_source_set_event_handler(t, ^{
  61. struct timeval now_tv;
  62. static double first = 0;
  63. gettimeofday(&now_tv, NULL);
  64. double now = now_tv.tv_sec + now_tv.tv_usec / 1000000.0;
  65. if (count == 0) {
  66. // Because this is taken at 1st timer fire,
  67. // later jitter values may be negitave.
  68. // This doesn't effect the drift calculation.
  69. first = now;
  70. }
  71. double goal = first + interval_d * count;
  72. double jitter = goal - now;
  73. double drift = jitter - last_jitter;
  74. drift_sum += drift;
  75. printf("%4d: jitter %f, drift %f\n", count, jitter, drift);
  76. if (target <= ++count) {
  77. drift_sum /= count - 1;
  78. if (drift_sum < 0) {
  79. drift_sum = -drift_sum;
  80. }
  81. double acceptable_drift = ACCEPTABLE_DRIFT;
  82. test_double_less_than("drift", drift_sum, acceptable_drift);
  83. test_stop();
  84. }
  85. last_jitter = jitter;
  86. });
  87. dispatch_resume(t);
  88. dispatch_main();
  89. return 0;
  90. }