dispatch_timer_short.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2010-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. #include <stdlib.h>
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #ifdef __APPLE__
  26. #include <mach/mach_time.h>
  27. #include <libkern/OSAtomic.h>
  28. #endif
  29. #include <dispatch/dispatch.h>
  30. #include <bsdtests.h>
  31. #include "dispatch_test.h"
  32. #define delay (1ull * NSEC_PER_SEC)
  33. #define interval (5ull * NSEC_PER_USEC)
  34. #define N 25000
  35. static dispatch_source_t t[N];
  36. static dispatch_queue_t q;
  37. static dispatch_group_t g;
  38. static volatile int32_t count;
  39. static mach_timebase_info_data_t tbi;
  40. static uint64_t start, last;
  41. #define elapsed_ms(x) (((now-(x))*tbi.numer/tbi.denom)/(1000ull*NSEC_PER_USEC))
  42. static
  43. void
  44. test_fin(void *cxt)
  45. {
  46. uint32_t finalCount = (uint32_t)count;
  47. fprintf(stderr, "Called back every %llu us on average\n",
  48. (delay/finalCount)/NSEC_PER_USEC);
  49. test_long_less_than("Frequency", 1,
  50. (long)ceil((double)delay/(double)(finalCount*interval)));
  51. int i;
  52. for (i = 0; i < N; i++) {
  53. dispatch_source_cancel(t[i]);
  54. dispatch_release(t[i]);
  55. }
  56. dispatch_resume(q);
  57. dispatch_release(q);
  58. dispatch_release(g);
  59. test_ptr("finalizer ran", cxt, cxt);
  60. test_stop();
  61. }
  62. static
  63. void
  64. test_short_timer(void)
  65. {
  66. // Add a large number of timers with suspended target queue in front of
  67. // the timer being measured <rdar://problem/7401353>
  68. g = dispatch_group_create();
  69. q = dispatch_queue_create("q", NULL);
  70. int i;
  71. for (i = 0; i < N; i++) {
  72. t[i] = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);
  73. dispatch_source_set_timer(t[i], DISPATCH_TIME_NOW, interval, 0);
  74. dispatch_group_enter(g);
  75. dispatch_source_set_registration_handler(t[i], ^{
  76. dispatch_suspend(t[i]);
  77. dispatch_group_leave(g);
  78. });
  79. dispatch_resume(t[i]);
  80. }
  81. // Wait for registration & configuration of all timers
  82. dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
  83. dispatch_suspend(q);
  84. for (i = 0; i < N; i++) {
  85. dispatch_resume(t[i]);
  86. }
  87. dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
  88. 0, 0, dispatch_get_global_queue(0, 0));
  89. test_ptr_notnull("dispatch_source_create", s);
  90. dispatch_source_set_timer(s, DISPATCH_TIME_NOW, interval, 0);
  91. dispatch_source_set_event_handler(s, ^{
  92. uint64_t now = mach_absolute_time();
  93. if (!count) {
  94. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay),
  95. dispatch_get_global_queue(0, 0), ^{
  96. dispatch_source_cancel(s);
  97. dispatch_release(s);
  98. });
  99. fprintf(stderr, "First timer callback (after %4llu ms)\n",
  100. elapsed_ms(start));
  101. }
  102. OSAtomicIncrement32(&count);
  103. if (elapsed_ms(last) >= 100) {
  104. fprintf(stderr, "%5d timer callbacks (after %4llu ms)\n", count,
  105. elapsed_ms(start));
  106. last = now;
  107. }
  108. });
  109. dispatch_set_context(s, s);
  110. dispatch_set_finalizer_f(s, test_fin);
  111. fprintf(stderr, "Scheduling %llu us timer\n", interval/NSEC_PER_USEC);
  112. start = last = mach_absolute_time();
  113. dispatch_resume(s);
  114. }
  115. int
  116. main(void)
  117. {
  118. dispatch_test_start("Dispatch Short Timer"); // <rdar://problem/7765184>
  119. mach_timebase_info(&tbi);
  120. test_short_timer();
  121. dispatch_main();
  122. return 0;
  123. }