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