dispatch_starfish.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.h>
  22. #include <mach/mach_time.h>
  23. #endif
  24. #include <dispatch/dispatch.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <assert.h>
  28. #ifdef __APPLE__
  29. #include <TargetConditionals.h>
  30. #endif
  31. #include <bsdtests.h>
  32. #include "dispatch_test.h"
  33. #define COUNT 1000ul
  34. #define LAPS 10ul
  35. #if LENIENT_DEADLINES
  36. #define ACCEPTABLE_LATENCY 10000
  37. #elif TARGET_OS_EMBEDDED
  38. #define ACCEPTABLE_LATENCY 3000
  39. #else
  40. #define ACCEPTABLE_LATENCY 1000
  41. #endif
  42. static dispatch_queue_t queues[COUNT];
  43. static size_t lap_count_down = LAPS;
  44. static size_t count_down;
  45. static uint64_t start;
  46. static mach_timebase_info_data_t tbi;
  47. static void do_test(void);
  48. static void
  49. collect(void *context __attribute__((unused)))
  50. {
  51. uint64_t delta;
  52. long double math;
  53. size_t i;
  54. if (--count_down) {
  55. return;
  56. }
  57. delta = mach_absolute_time() - start;
  58. delta *= tbi.numer;
  59. delta /= tbi.denom;
  60. math = delta;
  61. math /= COUNT * COUNT * 2ul + COUNT * 2ul;
  62. printf("lap: %zd\n", lap_count_down);
  63. printf("count: %lu\n", COUNT);
  64. printf("delta: %lu ns\n", (unsigned long)delta);
  65. printf("math: %Lf ns / lap\n", math);
  66. for (i = 0; i < COUNT; i++) {
  67. dispatch_release(queues[i]);
  68. }
  69. // our malloc could be a lot better,
  70. // this result is really a malloc torture test
  71. test_long_less_than("Latency" , (long)math, ACCEPTABLE_LATENCY);
  72. if (--lap_count_down) {
  73. return do_test();
  74. }
  75. // give the threads some time to settle before test_stop() runs "leaks"
  76. // ...also note, this is a total cheat. dispatch_after lets this
  77. // thread go idle, so dispatch cleans up the continuations cache.
  78. // Doign the "old style" sleep left that stuff around and leaks
  79. // took a LONG TIME to complete. Long enough that the test harness
  80. // decided to kill us.
  81. dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), NULL, test_stop_after_delay);
  82. }
  83. static void
  84. pong(void *context)
  85. {
  86. dispatch_queue_t this_q = context;
  87. size_t replies = (size_t)dispatch_get_context(this_q);
  88. dispatch_set_context(this_q, (void *)--replies);
  89. if (!replies) {
  90. //printf("collect from: %s\n", dispatch_queue_get_label(dispatch_get_current_queue()));
  91. dispatch_async_f(dispatch_get_main_queue(), NULL, collect);
  92. }
  93. }
  94. static void
  95. ping(void *context)
  96. {
  97. dispatch_queue_t reply_q = context;
  98. dispatch_async_f(reply_q, reply_q, pong);
  99. }
  100. static void
  101. start_node(void *context)
  102. {
  103. dispatch_queue_t this_q = context;
  104. size_t i;
  105. dispatch_set_context(this_q, (void *)COUNT);
  106. for (i = 0; i < COUNT; i++) {
  107. dispatch_async_f(queues[i], this_q, ping);
  108. }
  109. }
  110. void
  111. do_test(void)
  112. {
  113. dispatch_queue_t soup = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
  114. kern_return_t kr;
  115. char buf[1000];
  116. size_t i;
  117. count_down = COUNT;
  118. kr = mach_timebase_info(&tbi);
  119. assert(kr == 0);
  120. start = mach_absolute_time();
  121. for (i = 0; i < COUNT; i++) {
  122. snprintf(buf, sizeof(buf), "com.example.starfish-node#%zd", i);
  123. queues[i] = dispatch_queue_create(buf, NULL);
  124. dispatch_suspend(queues[i]);
  125. dispatch_set_target_queue(queues[i], soup);
  126. }
  127. for (i = 0; i < COUNT; i++) {
  128. dispatch_async_f(queues[i], queues[i], start_node);
  129. }
  130. for (i = 0; i < COUNT; i++) {
  131. dispatch_resume(queues[i]);
  132. }
  133. }
  134. int
  135. main(void)
  136. {
  137. dispatch_test_start("Dispatch Starfish");
  138. do_test();
  139. dispatch_main();
  140. }