completion_queue_threading_test.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/alloc.h>
  19. #include <grpc/support/log.h>
  20. #include <grpc/support/time.h>
  21. #include "src/core/lib/gpr/useful.h"
  22. #include "src/core/lib/gprpp/thd.h"
  23. #include "src/core/lib/iomgr/iomgr.h"
  24. #include "src/core/lib/surface/completion_queue.h"
  25. #include "test/core/util/test_config.h"
  26. #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
  27. static void* create_test_tag(void) {
  28. static intptr_t i = 0;
  29. return reinterpret_cast<void*>(++i);
  30. }
  31. /* helper for tests to shutdown correctly and tersely */
  32. static void shutdown_and_destroy(grpc_completion_queue* cc) {
  33. grpc_event ev;
  34. grpc_completion_queue_shutdown(cc);
  35. switch (grpc_get_cq_completion_type(cc)) {
  36. case GRPC_CQ_NEXT: {
  37. ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME),
  38. nullptr);
  39. break;
  40. }
  41. case GRPC_CQ_PLUCK: {
  42. ev = grpc_completion_queue_pluck(
  43. cc, create_test_tag(), gpr_inf_past(GPR_CLOCK_REALTIME), nullptr);
  44. break;
  45. }
  46. default: {
  47. gpr_log(GPR_ERROR, "Unknown completion type");
  48. break;
  49. }
  50. }
  51. GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN);
  52. grpc_completion_queue_destroy(cc);
  53. }
  54. static void do_nothing_end_completion(void* /*arg*/,
  55. grpc_cq_completion* /*c*/) {}
  56. struct thread_state {
  57. grpc_completion_queue* cc;
  58. void* tag;
  59. };
  60. static void pluck_one(void* arg) {
  61. struct thread_state* state = static_cast<struct thread_state*>(arg);
  62. grpc_completion_queue_pluck(state->cc, state->tag,
  63. gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
  64. }
  65. static void test_too_many_plucks(void) {
  66. grpc_event ev;
  67. grpc_completion_queue* cc;
  68. void* tags[GRPC_MAX_COMPLETION_QUEUE_PLUCKERS];
  69. grpc_cq_completion completions[GPR_ARRAY_SIZE(tags)];
  70. grpc_core::Thread threads[GPR_ARRAY_SIZE(tags)];
  71. struct thread_state thread_states[GPR_ARRAY_SIZE(tags)];
  72. grpc_core::ExecCtx exec_ctx;
  73. unsigned i, j;
  74. LOG_TEST("test_too_many_plucks");
  75. cc = grpc_completion_queue_create_for_pluck(nullptr);
  76. for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) {
  77. tags[i] = create_test_tag();
  78. for (j = 0; j < i; j++) {
  79. GPR_ASSERT(tags[i] != tags[j]);
  80. }
  81. thread_states[i].cc = cc;
  82. thread_states[i].tag = tags[i];
  83. threads[i] =
  84. grpc_core::Thread("grpc_pluck_test", pluck_one, thread_states + i);
  85. threads[i].Start();
  86. }
  87. /* wait until all other threads are plucking */
  88. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1000));
  89. ev = grpc_completion_queue_pluck(cc, create_test_tag(),
  90. gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
  91. GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT);
  92. for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) {
  93. GPR_ASSERT(grpc_cq_begin_op(cc, tags[i]));
  94. grpc_cq_end_op(cc, tags[i], GRPC_ERROR_NONE, do_nothing_end_completion,
  95. nullptr, &completions[i]);
  96. }
  97. for (auto& th : threads) {
  98. th.Join();
  99. }
  100. shutdown_and_destroy(cc);
  101. }
  102. #define TEST_THREAD_EVENTS 10000
  103. typedef struct test_thread_options {
  104. gpr_event on_started;
  105. gpr_event* phase1;
  106. gpr_event on_phase1_done;
  107. gpr_event* phase2;
  108. gpr_event on_finished;
  109. size_t events_triggered;
  110. int id;
  111. grpc_completion_queue* cc;
  112. } test_thread_options;
  113. gpr_timespec ten_seconds_time(void) {
  114. return grpc_timeout_seconds_to_deadline(10);
  115. }
  116. static void free_completion(void* /*arg*/, grpc_cq_completion* completion) {
  117. gpr_free(completion);
  118. }
  119. static void producer_thread(void* arg) {
  120. test_thread_options* opt = static_cast<test_thread_options*>(arg);
  121. int i;
  122. gpr_log(GPR_INFO, "producer %d started", opt->id);
  123. gpr_event_set(&opt->on_started, reinterpret_cast<void*>(1));
  124. GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time()));
  125. gpr_log(GPR_INFO, "producer %d phase 1", opt->id);
  126. for (i = 0; i < TEST_THREAD_EVENTS; i++) {
  127. GPR_ASSERT(grpc_cq_begin_op(opt->cc, (void*)(intptr_t)1));
  128. }
  129. gpr_log(GPR_INFO, "producer %d phase 1 done", opt->id);
  130. gpr_event_set(&opt->on_phase1_done, reinterpret_cast<void*>(1));
  131. GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time()));
  132. gpr_log(GPR_INFO, "producer %d phase 2", opt->id);
  133. for (i = 0; i < TEST_THREAD_EVENTS; i++) {
  134. grpc_core::ExecCtx exec_ctx;
  135. grpc_cq_end_op(opt->cc, reinterpret_cast<void*>(1), GRPC_ERROR_NONE,
  136. free_completion, nullptr,
  137. static_cast<grpc_cq_completion*>(
  138. gpr_malloc(sizeof(grpc_cq_completion))));
  139. opt->events_triggered++;
  140. }
  141. gpr_log(GPR_INFO, "producer %d phase 2 done", opt->id);
  142. gpr_event_set(&opt->on_finished, reinterpret_cast<void*>(1));
  143. }
  144. static void consumer_thread(void* arg) {
  145. test_thread_options* opt = static_cast<test_thread_options*>(arg);
  146. grpc_event ev;
  147. gpr_log(GPR_INFO, "consumer %d started", opt->id);
  148. gpr_event_set(&opt->on_started, reinterpret_cast<void*>(1));
  149. GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time()));
  150. gpr_log(GPR_INFO, "consumer %d phase 1", opt->id);
  151. gpr_log(GPR_INFO, "consumer %d phase 1 done", opt->id);
  152. gpr_event_set(&opt->on_phase1_done, reinterpret_cast<void*>(1));
  153. GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time()));
  154. gpr_log(GPR_INFO, "consumer %d phase 2", opt->id);
  155. for (;;) {
  156. ev = grpc_completion_queue_next(
  157. opt->cc, gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
  158. switch (ev.type) {
  159. case GRPC_OP_COMPLETE:
  160. GPR_ASSERT(ev.success);
  161. opt->events_triggered++;
  162. break;
  163. case GRPC_QUEUE_SHUTDOWN:
  164. gpr_log(GPR_INFO, "consumer %d phase 2 done", opt->id);
  165. gpr_event_set(&opt->on_finished, reinterpret_cast<void*>(1));
  166. return;
  167. case GRPC_QUEUE_TIMEOUT:
  168. gpr_log(GPR_ERROR, "Invalid timeout received");
  169. abort();
  170. }
  171. }
  172. }
  173. static void test_threading(size_t producers, size_t consumers) {
  174. test_thread_options* options = static_cast<test_thread_options*>(
  175. gpr_malloc((producers + consumers) * sizeof(test_thread_options)));
  176. gpr_event phase1 = GPR_EVENT_INIT;
  177. gpr_event phase2 = GPR_EVENT_INIT;
  178. grpc_completion_queue* cc = grpc_completion_queue_create_for_next(nullptr);
  179. size_t i;
  180. size_t total_consumed = 0;
  181. static int optid = 101;
  182. gpr_log(GPR_INFO, "%s: %" PRIuPTR " producers, %" PRIuPTR " consumers",
  183. "test_threading", producers, consumers);
  184. /* start all threads: they will wait for phase1 */
  185. grpc_core::Thread* threads = static_cast<grpc_core::Thread*>(
  186. gpr_malloc(sizeof(*threads) * (producers + consumers)));
  187. for (i = 0; i < producers + consumers; i++) {
  188. gpr_event_init(&options[i].on_started);
  189. gpr_event_init(&options[i].on_phase1_done);
  190. gpr_event_init(&options[i].on_finished);
  191. options[i].phase1 = &phase1;
  192. options[i].phase2 = &phase2;
  193. options[i].events_triggered = 0;
  194. options[i].cc = cc;
  195. options[i].id = optid++;
  196. bool ok;
  197. threads[i] = grpc_core::Thread(
  198. i < producers ? "grpc_producer" : "grpc_consumer",
  199. i < producers ? producer_thread : consumer_thread, options + i, &ok);
  200. GPR_ASSERT(ok);
  201. threads[i].Start();
  202. gpr_event_wait(&options[i].on_started, ten_seconds_time());
  203. }
  204. /* start phase1: producers will pre-declare all operations they will
  205. complete */
  206. gpr_log(GPR_INFO, "start phase 1");
  207. gpr_event_set(&phase1, reinterpret_cast<void*>(1));
  208. gpr_log(GPR_INFO, "wait phase 1");
  209. for (i = 0; i < producers + consumers; i++) {
  210. GPR_ASSERT(gpr_event_wait(&options[i].on_phase1_done, ten_seconds_time()));
  211. }
  212. gpr_log(GPR_INFO, "done phase 1");
  213. /* start phase2: operations will complete, and consumers will consume them */
  214. gpr_log(GPR_INFO, "start phase 2");
  215. gpr_event_set(&phase2, reinterpret_cast<void*>(1));
  216. /* in parallel, we shutdown the completion channel - all events should still
  217. be consumed */
  218. grpc_completion_queue_shutdown(cc);
  219. /* join all threads */
  220. gpr_log(GPR_INFO, "wait phase 2");
  221. for (i = 0; i < producers + consumers; i++) {
  222. GPR_ASSERT(gpr_event_wait(&options[i].on_finished, ten_seconds_time()));
  223. }
  224. gpr_log(GPR_INFO, "done phase 2");
  225. /* destroy the completion channel */
  226. grpc_completion_queue_destroy(cc);
  227. for (i = 0; i < producers + consumers; i++) {
  228. threads[i].Join();
  229. }
  230. gpr_free(threads);
  231. /* verify that everything was produced and consumed */
  232. for (i = 0; i < producers + consumers; i++) {
  233. if (i < producers) {
  234. GPR_ASSERT(options[i].events_triggered == TEST_THREAD_EVENTS);
  235. } else {
  236. total_consumed += options[i].events_triggered;
  237. }
  238. }
  239. GPR_ASSERT(total_consumed == producers * TEST_THREAD_EVENTS);
  240. gpr_free(options);
  241. }
  242. int main(int argc, char** argv) {
  243. grpc::testing::TestEnvironment env(argc, argv);
  244. grpc_init();
  245. test_too_many_plucks();
  246. test_threading(1, 1);
  247. test_threading(1, 10);
  248. test_threading(10, 1);
  249. test_threading(10, 10);
  250. grpc_shutdown();
  251. return 0;
  252. }