Debian Codegraph @

test/core/gprpp/mpscq_test.cc

raw
1 /*
2 *
3 * Copyright 2016 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 "src/core/lib/gprpp/mpscq.h"
20
21 #include <inttypes.h>
22 #include <stdlib.h>
23
24 #include "gtest/gtest.h"
25
26 #include <grpc/grpc.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 "test/core/util/test_config.h"
34
35 using grpc_core::MultiProducerSingleConsumerQueue;
36
37 typedef struct test_node {
38 MultiProducerSingleConsumerQueue::Node node;
39 size_t i;
40 size_t* ctr;
41 } test_node;
42
43 static test_node* new_node(size_t i, size_t* ctr) {
44 test_node* n = new test_node();
45 n->i = i;
46 n->ctr = ctr;
47 return n;
48 }
49
50 TEST(MpscqTest, Serial) {
51 gpr_log(GPR_DEBUG, "test_serial");
52 MultiProducerSingleConsumerQueue q;
53 for (size_t i = 0; i < 10000000; i++) {
54 q.Push(&new_node(i, nullptr)->node);
55 }
56 for (size_t i = 0; i < 10000000; i++) {
57 test_node* n = reinterpret_cast<test_node*>(q.Pop());
58 ASSERT_NE(n, nullptr);
59 ASSERT_EQ(n->i, i);
60 delete n;
61 }
62 }
63
64 typedef struct {
65 size_t ctr;
66 MultiProducerSingleConsumerQueue* q;
67 gpr_event* start;
68 } thd_args;
69
70 #define THREAD_ITERATIONS 10000
71
72 static void test_thread(void* args) {
73 thd_args* a = static_cast<thd_args*>(args);
74 gpr_event_wait(a->start, gpr_inf_future(GPR_CLOCK_REALTIME));
75 for (size_t i = 1; i <= THREAD_ITERATIONS; i++) {
76 a->q->Push(&new_node(i, &a->ctr)->node);
77 }
78 }
79
80 TEST(MpscqTest, Mt) {
81 gpr_log(GPR_DEBUG, "test_mt");
82 gpr_event start;
83 gpr_event_init(&start);
84 grpc_core::Thread thds[100];
85 thd_args ta[GPR_ARRAY_SIZE(thds)];
86 MultiProducerSingleConsumerQueue q;
87 for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
88 ta[i].ctr = 0;
89 ta[i].q = &q;
90 ta[i].start = &start;
91 thds[i] = grpc_core::Thread("grpc_mt_test", test_thread, &ta[i]);
92 thds[i].Start();
93 }
94 size_t num_done = 0;
95 size_t spins = 0;
96 gpr_event_set(&start, reinterpret_cast<void*>(1));
97 while (num_done != GPR_ARRAY_SIZE(thds)) {
98 MultiProducerSingleConsumerQueue::Node* n;
99 while ((n = q.Pop()) == nullptr) {
100 spins++;
101 }
102 test_node* tn = reinterpret_cast<test_node*>(n);
103 ASSERT_EQ(*tn->ctr, tn->i - 1);
104 *tn->ctr = tn->i;
105 if (tn->i == THREAD_ITERATIONS) num_done++;
106 delete tn;
107 }
108 gpr_log(GPR_DEBUG, "spins: %" PRIdPTR, spins);
109 for (auto& th : thds) {
110 th.Join();
111 }
112 }
113
114 typedef struct {
115 thd_args* ta;
116 size_t num_thds;
117 gpr_mu mu;
118 size_t num_done;
119 size_t spins;
120 MultiProducerSingleConsumerQueue* q;
121 gpr_event* start;
122 } pull_args;
123
124 static void pull_thread(void* arg) {
125 pull_args* pa = static_cast<pull_args*>(arg);
126 gpr_event_wait(pa->start, gpr_inf_future(GPR_CLOCK_REALTIME));
127
128 for (;;) {
129 gpr_mu_lock(&pa->mu);
130 if (pa->num_done == pa->num_thds) {
131 gpr_mu_unlock(&pa->mu);
132 return;
133 }
134 MultiProducerSingleConsumerQueue::Node* n;
135 while ((n = pa->q->Pop()) == nullptr) {
136 pa->spins++;
137 }
138 test_node* tn = reinterpret_cast<test_node*>(n);
139 ASSERT_EQ(*tn->ctr, tn->i - 1);
140 *tn->ctr = tn->i;
141 if (tn->i == THREAD_ITERATIONS) pa->num_done++;
142 delete tn;
143 gpr_mu_unlock(&pa->mu);
144 }
145 }
146
147 TEST(MpscqTest, MtMultipop) {
148 gpr_log(GPR_DEBUG, "test_mt_multipop");
149 gpr_event start;
150 gpr_event_init(&start);
151 grpc_core::Thread thds[50];
152 grpc_core::Thread pull_thds[50];
153 thd_args ta[GPR_ARRAY_SIZE(thds)];
154 MultiProducerSingleConsumerQueue q;
155 for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
156 ta[i].ctr = 0;
157 ta[i].q = &q;
158 ta[i].start = &start;
159 thds[i] = grpc_core::Thread("grpc_multipop_test", test_thread, &ta[i]);
160 thds[i].Start();
161 }
162 pull_args pa;
163 pa.ta = ta;
164 pa.num_thds = GPR_ARRAY_SIZE(thds);
165 pa.spins = 0;
166 pa.num_done = 0;
167 pa.q = &q;
168 pa.start = &start;
169 gpr_mu_init(&pa.mu);
170 for (size_t i = 0; i < GPR_ARRAY_SIZE(pull_thds); i++) {
171 pull_thds[i] = grpc_core::Thread("grpc_multipop_pull", pull_thread, &pa);
172 pull_thds[i].Start();
173 }
174 gpr_event_set(&start, reinterpret_cast<void*>(1));
175 for (auto& pth : pull_thds) {
176 pth.Join();
177 }
178 gpr_log(GPR_DEBUG, "spins: %" PRIdPTR, pa.spins);
179 for (auto& th : thds) {
180 th.Join();
181 }
182 gpr_mu_destroy(&pa.mu);
183 }
184
185 int main(int argc, char** argv) {
186 grpc::testing::TestEnvironment env(&argc, argv);
187 ::testing::InitGoogleTest(&argc, argv);
188 return RUN_ALL_TESTS();
189 }