Debian Codegraph @

test/core/surface/completion_queue_threading_test.cc

raw
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
19 #include <inttypes.h>
20 #include <stdlib.h>
21
22 #include "absl/status/status.h"
23 #include "gtest/gtest.h"
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/sync.h>
29 #include <grpc/support/time.h>
30
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/gprpp/thd.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "src/core/lib/surface/completion_queue.h"
35 #include "test/core/util/test_config.h"
36
37 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
38
39 static void* create_test_tag(void) {
40 static intptr_t i = 0;
41 return reinterpret_cast<void*>(++i);
42 }
43
44 /* helper for tests to shutdown correctly and tersely */
45 static void shutdown_and_destroy(grpc_completion_queue* cc) {
46 grpc_event ev;
47 grpc_completion_queue_shutdown(cc);
48
49 switch (grpc_get_cq_completion_type(cc)) {
50 case GRPC_CQ_NEXT: {
51 ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME),
52 nullptr);
53 break;
54 }
55 case GRPC_CQ_PLUCK: {
56 ev = grpc_completion_queue_pluck(
57 cc, create_test_tag(), gpr_inf_past(GPR_CLOCK_REALTIME), nullptr);
58 break;
59 }
60 default: {
61 gpr_log(GPR_ERROR, "Unknown completion type");
62 break;
63 }
64 }
65
66 ASSERT_EQ(ev.type, GRPC_QUEUE_SHUTDOWN);
67 grpc_completion_queue_destroy(cc);
68 }
69
70 static void do_nothing_end_completion(void* /*arg*/,
71 grpc_cq_completion* /*c*/) {}
72
73 struct thread_state {
74 grpc_completion_queue* cc;
75 void* tag;
76 };
77
78 static void pluck_one(void* arg) {
79 struct thread_state* state = static_cast<struct thread_state*>(arg);
80 grpc_completion_queue_pluck(state->cc, state->tag,
81 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
82 }
83
84 static void test_too_many_plucks(void) {
85 grpc_event ev;
86 grpc_completion_queue* cc;
87 void* tags[GRPC_MAX_COMPLETION_QUEUE_PLUCKERS];
88 grpc_cq_completion completions[GPR_ARRAY_SIZE(tags)];
89 grpc_core::Thread threads[GPR_ARRAY_SIZE(tags)];
90 struct thread_state thread_states[GPR_ARRAY_SIZE(tags)];
91 grpc_core::ExecCtx exec_ctx;
92 unsigned i, j;
93
94 LOG_TEST("test_too_many_plucks");
95
96 cc = grpc_completion_queue_create_for_pluck(nullptr);
97
98 for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) {
99 tags[i] = create_test_tag();
100 for (j = 0; j < i; j++) {
101 ASSERT_NE(tags[i], tags[j]);
102 }
103 thread_states[i].cc = cc;
104 thread_states[i].tag = tags[i];
105 threads[i] =
106 grpc_core::Thread("grpc_pluck_test", pluck_one, thread_states + i);
107 threads[i].Start();
108 }
109
110 /* wait until all other threads are plucking */
111 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1000));
112
113 ev = grpc_completion_queue_pluck(cc, create_test_tag(),
114 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
115 ASSERT_EQ(ev.type, GRPC_QUEUE_TIMEOUT);
116
117 for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) {
118 ASSERT_TRUE(grpc_cq_begin_op(cc, tags[i]));
119 grpc_cq_end_op(cc, tags[i], absl::OkStatus(), do_nothing_end_completion,
120 nullptr, &completions[i]);
121 }
122
123 for (auto& th : threads) {
124 th.Join();
125 }
126
127 shutdown_and_destroy(cc);
128 }
129
130 #define TEST_THREAD_EVENTS 10000
131
132 typedef struct test_thread_options {
133 gpr_event on_started;
134 gpr_event* phase1;
135 gpr_event on_phase1_done;
136 gpr_event* phase2;
137 gpr_event on_finished;
138 size_t events_triggered;
139 int id;
140 grpc_completion_queue* cc;
141 } test_thread_options;
142
143 gpr_timespec ten_seconds_time(void) {
144 return grpc_timeout_seconds_to_deadline(10);
145 }
146
147 static void free_completion(void* /*arg*/, grpc_cq_completion* completion) {
148 gpr_free(completion);
149 }
150
151 static void producer_thread(void* arg) {
152 test_thread_options* opt = static_cast<test_thread_options*>(arg);
153 int i;
154
155 gpr_log(GPR_INFO, "producer %d started", opt->id);
156 gpr_event_set(&opt->on_started, reinterpret_cast<void*>(1));
157 ASSERT_TRUE(gpr_event_wait(opt->phase1, ten_seconds_time()));
158
159 gpr_log(GPR_INFO, "producer %d phase 1", opt->id);
160 for (i = 0; i < TEST_THREAD_EVENTS; i++) {
161 ASSERT_TRUE(grpc_cq_begin_op(opt->cc, (void*)(intptr_t)1));
162 }
163
164 gpr_log(GPR_INFO, "producer %d phase 1 done", opt->id);
165 gpr_event_set(&opt->on_phase1_done, reinterpret_cast<void*>(1));
166 ASSERT_TRUE(gpr_event_wait(opt->phase2, ten_seconds_time()));
167
168 gpr_log(GPR_INFO, "producer %d phase 2", opt->id);
169 for (i = 0; i < TEST_THREAD_EVENTS; i++) {
170 grpc_core::ExecCtx exec_ctx;
171 grpc_cq_end_op(opt->cc, reinterpret_cast<void*>(1), absl::OkStatus(),
172 free_completion, nullptr,
173 static_cast<grpc_cq_completion*>(
174 gpr_malloc(sizeof(grpc_cq_completion))));
175 opt->events_triggered++;
176 }
177
178 gpr_log(GPR_INFO, "producer %d phase 2 done", opt->id);
179 gpr_event_set(&opt->on_finished, reinterpret_cast<void*>(1));
180 }
181
182 static void consumer_thread(void* arg) {
183 test_thread_options* opt = static_cast<test_thread_options*>(arg);
184 grpc_event ev;
185
186 gpr_log(GPR_INFO, "consumer %d started", opt->id);
187 gpr_event_set(&opt->on_started, reinterpret_cast<void*>(1));
188 ASSERT_TRUE(gpr_event_wait(opt->phase1, ten_seconds_time()));
189
190 gpr_log(GPR_INFO, "consumer %d phase 1", opt->id);
191
192 gpr_log(GPR_INFO, "consumer %d phase 1 done", opt->id);
193 gpr_event_set(&opt->on_phase1_done, reinterpret_cast<void*>(1));
194 ASSERT_TRUE(gpr_event_wait(opt->phase2, ten_seconds_time()));
195
196 gpr_log(GPR_INFO, "consumer %d phase 2", opt->id);
197 for (;;) {
198 ev = grpc_completion_queue_next(
199 opt->cc, gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
200 switch (ev.type) {
201 case GRPC_OP_COMPLETE:
202 ASSERT_TRUE(ev.success);
203 opt->events_triggered++;
204 break;
205 case GRPC_QUEUE_SHUTDOWN:
206 gpr_log(GPR_INFO, "consumer %d phase 2 done", opt->id);
207 gpr_event_set(&opt->on_finished, reinterpret_cast<void*>(1));
208 return;
209 case GRPC_QUEUE_TIMEOUT:
210 gpr_log(GPR_ERROR, "Invalid timeout received");
211 abort();
212 }
213 }
214 }
215
216 static void test_threading(size_t producers, size_t consumers) {
217 test_thread_options* options = static_cast<test_thread_options*>(
218 gpr_malloc((producers + consumers) * sizeof(test_thread_options)));
219 gpr_event phase1 = GPR_EVENT_INIT;
220 gpr_event phase2 = GPR_EVENT_INIT;
221 grpc_completion_queue* cc = grpc_completion_queue_create_for_next(nullptr);
222 size_t i;
223 size_t total_consumed = 0;
224 static int optid = 101;
225
226 gpr_log(GPR_INFO, "%s: %" PRIuPTR " producers, %" PRIuPTR " consumers",
227 "test_threading", producers, consumers);
228
229 /* start all threads: they will wait for phase1 */
230 grpc_core::Thread* threads = static_cast<grpc_core::Thread*>(
231 gpr_malloc(sizeof(*threads) * (producers + consumers)));
232 for (i = 0; i < producers + consumers; i++) {
233 gpr_event_init(&options[i].on_started);
234 gpr_event_init(&options[i].on_phase1_done);
235 gpr_event_init(&options[i].on_finished);
236 options[i].phase1 = &phase1;
237 options[i].phase2 = &phase2;
238 options[i].events_triggered = 0;
239 options[i].cc = cc;
240 options[i].id = optid++;
241
242 bool ok;
243 threads[i] = grpc_core::Thread(
244 i < producers ? "grpc_producer" : "grpc_consumer",
245 i < producers ? producer_thread : consumer_thread, options + i, &ok);
246 ASSERT_TRUE(ok);
247 threads[i].Start();
248 gpr_event_wait(&options[i].on_started, ten_seconds_time());
249 }
250
251 /* start phase1: producers will pre-declare all operations they will
252 complete */
253 gpr_log(GPR_INFO, "start phase 1");
254 gpr_event_set(&phase1, reinterpret_cast<void*>(1));
255
256 gpr_log(GPR_INFO, "wait phase 1");
257 for (i = 0; i < producers + consumers; i++) {
258 ASSERT_TRUE(gpr_event_wait(&options[i].on_phase1_done, ten_seconds_time()));
259 }
260 gpr_log(GPR_INFO, "done phase 1");
261
262 /* start phase2: operations will complete, and consumers will consume them */
263 gpr_log(GPR_INFO, "start phase 2");
264 gpr_event_set(&phase2, reinterpret_cast<void*>(1));
265
266 /* in parallel, we shutdown the completion channel - all events should still
267 be consumed */
268 grpc_completion_queue_shutdown(cc);
269
270 /* join all threads */
271 gpr_log(GPR_INFO, "wait phase 2");
272 for (i = 0; i < producers + consumers; i++) {
273 ASSERT_TRUE(gpr_event_wait(&options[i].on_finished, ten_seconds_time()));
274 }
275 gpr_log(GPR_INFO, "done phase 2");
276
277 /* destroy the completion channel */
278 grpc_completion_queue_destroy(cc);
279
280 for (i = 0; i < producers + consumers; i++) {
281 threads[i].Join();
282 }
283 gpr_free(threads);
284
285 /* verify that everything was produced and consumed */
286 for (i = 0; i < producers + consumers; i++) {
287 if (i < producers) {
288 ASSERT_EQ(options[i].events_triggered, TEST_THREAD_EVENTS);
289 } else {
290 total_consumed += options[i].events_triggered;
291 }
292 }
293 ASSERT_EQ(total_consumed, producers * TEST_THREAD_EVENTS);
294
295 gpr_free(options);
296 }
297
298 TEST(CompletionQueueThreadingTest, MainTest) {
299 grpc_init();
300 test_too_many_plucks();
301 test_threading(1, 1);
302 test_threading(1, 10);
303 test_threading(10, 1);
304 test_threading(10, 10);
305 grpc_shutdown();
306 }
307
308 int main(int argc, char** argv) {
309 grpc::testing::TestEnvironment env(&argc, argv);
310 ::testing::InitGoogleTest(&argc, argv);
311 grpc::testing::TestGrpcScope grpc_scope;
312 return RUN_ALL_TESTS();
313 }