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