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