mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
4.7 KiB
189 lines
4.7 KiB
8 years ago
|
/*
|
||
|
*
|
||
8 years ago
|
* Copyright 2016 gRPC authors.
|
||
8 years ago
|
*
|
||
8 years ago
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
8 years ago
|
*
|
||
8 years ago
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
8 years ago
|
*
|
||
8 years ago
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
8 years ago
|
*
|
||
|
*/
|
||
|
|
||
5 years ago
|
#include "src/core/lib/gprpp/mpscq.h"
|
||
8 years ago
|
|
||
7 years ago
|
#include <inttypes.h>
|
||
8 years ago
|
#include <stdlib.h>
|
||
|
|
||
|
#include <grpc/support/alloc.h>
|
||
|
#include <grpc/support/log.h>
|
||
|
#include <grpc/support/sync.h>
|
||
7 years ago
|
|
||
|
#include "src/core/lib/gpr/useful.h"
|
||
7 years ago
|
#include "src/core/lib/gprpp/thd.h"
|
||
8 years ago
|
#include "test/core/util/test_config.h"
|
||
|
|
||
5 years ago
|
using grpc_core::MultiProducerSingleConsumerQueue;
|
||
|
|
||
8 years ago
|
typedef struct test_node {
|
||
5 years ago
|
MultiProducerSingleConsumerQueue::Node node;
|
||
8 years ago
|
size_t i;
|
||
7 years ago
|
size_t* ctr;
|
||
8 years ago
|
} test_node;
|
||
|
|
||
7 years ago
|
static test_node* new_node(size_t i, size_t* ctr) {
|
||
5 years ago
|
test_node* n = grpc_core::New<test_node>();
|
||
8 years ago
|
n->i = i;
|
||
|
n->ctr = ctr;
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
static void test_serial(void) {
|
||
|
gpr_log(GPR_DEBUG, "test_serial");
|
||
5 years ago
|
MultiProducerSingleConsumerQueue q;
|
||
8 years ago
|
for (size_t i = 0; i < 10000000; i++) {
|
||
5 years ago
|
q.Push(&new_node(i, nullptr)->node);
|
||
8 years ago
|
}
|
||
|
for (size_t i = 0; i < 10000000; i++) {
|
||
5 years ago
|
test_node* n = reinterpret_cast<test_node*>(q.Pop());
|
||
8 years ago
|
GPR_ASSERT(n);
|
||
|
GPR_ASSERT(n->i == i);
|
||
|
gpr_free(n);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
typedef struct {
|
||
|
size_t ctr;
|
||
5 years ago
|
MultiProducerSingleConsumerQueue* q;
|
||
7 years ago
|
gpr_event* start;
|
||
8 years ago
|
} thd_args;
|
||
|
|
||
8 years ago
|
#define THREAD_ITERATIONS 10000
|
||
8 years ago
|
|
||
7 years ago
|
static void test_thread(void* args) {
|
||
7 years ago
|
thd_args* a = static_cast<thd_args*>(args);
|
||
8 years ago
|
gpr_event_wait(a->start, gpr_inf_future(GPR_CLOCK_REALTIME));
|
||
|
for (size_t i = 1; i <= THREAD_ITERATIONS; i++) {
|
||
5 years ago
|
a->q->Push(&new_node(i, &a->ctr)->node);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
static void test_mt(void) {
|
||
|
gpr_log(GPR_DEBUG, "test_mt");
|
||
|
gpr_event start;
|
||
|
gpr_event_init(&start);
|
||
7 years ago
|
grpc_core::Thread thds[100];
|
||
8 years ago
|
thd_args ta[GPR_ARRAY_SIZE(thds)];
|
||
5 years ago
|
MultiProducerSingleConsumerQueue q;
|
||
8 years ago
|
for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
|
||
|
ta[i].ctr = 0;
|
||
|
ta[i].q = &q;
|
||
|
ta[i].start = &start;
|
||
7 years ago
|
thds[i] = grpc_core::Thread("grpc_mt_test", test_thread, &ta[i]);
|
||
7 years ago
|
thds[i].Start();
|
||
8 years ago
|
}
|
||
|
size_t num_done = 0;
|
||
|
size_t spins = 0;
|
||
7 years ago
|
gpr_event_set(&start, (void*)1);
|
||
8 years ago
|
while (num_done != GPR_ARRAY_SIZE(thds)) {
|
||
5 years ago
|
MultiProducerSingleConsumerQueue::Node* n;
|
||
|
while ((n = q.Pop()) == nullptr) {
|
||
8 years ago
|
spins++;
|
||
|
}
|
||
7 years ago
|
test_node* tn = reinterpret_cast<test_node*>(n);
|
||
8 years ago
|
GPR_ASSERT(*tn->ctr == tn->i - 1);
|
||
|
*tn->ctr = tn->i;
|
||
|
if (tn->i == THREAD_ITERATIONS) num_done++;
|
||
|
gpr_free(tn);
|
||
|
}
|
||
|
gpr_log(GPR_DEBUG, "spins: %" PRIdPTR, spins);
|
||
7 years ago
|
for (auto& th : thds) {
|
||
|
th.Join();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
typedef struct {
|
||
7 years ago
|
thd_args* ta;
|
||
8 years ago
|
size_t num_thds;
|
||
|
gpr_mu mu;
|
||
|
size_t num_done;
|
||
|
size_t spins;
|
||
5 years ago
|
MultiProducerSingleConsumerQueue* q;
|
||
7 years ago
|
gpr_event* start;
|
||
8 years ago
|
} pull_args;
|
||
|
|
||
7 years ago
|
static void pull_thread(void* arg) {
|
||
7 years ago
|
pull_args* pa = static_cast<pull_args*>(arg);
|
||
8 years ago
|
gpr_event_wait(pa->start, gpr_inf_future(GPR_CLOCK_REALTIME));
|
||
|
|
||
|
for (;;) {
|
||
|
gpr_mu_lock(&pa->mu);
|
||
|
if (pa->num_done == pa->num_thds) {
|
||
|
gpr_mu_unlock(&pa->mu);
|
||
|
return;
|
||
|
}
|
||
5 years ago
|
MultiProducerSingleConsumerQueue::Node* n;
|
||
|
while ((n = pa->q->Pop()) == nullptr) {
|
||
8 years ago
|
pa->spins++;
|
||
|
}
|
||
7 years ago
|
test_node* tn = reinterpret_cast<test_node*>(n);
|
||
8 years ago
|
GPR_ASSERT(*tn->ctr == tn->i - 1);
|
||
|
*tn->ctr = tn->i;
|
||
|
if (tn->i == THREAD_ITERATIONS) pa->num_done++;
|
||
|
gpr_free(tn);
|
||
|
gpr_mu_unlock(&pa->mu);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void test_mt_multipop(void) {
|
||
|
gpr_log(GPR_DEBUG, "test_mt_multipop");
|
||
|
gpr_event start;
|
||
|
gpr_event_init(&start);
|
||
7 years ago
|
grpc_core::Thread thds[50];
|
||
|
grpc_core::Thread pull_thds[50];
|
||
8 years ago
|
thd_args ta[GPR_ARRAY_SIZE(thds)];
|
||
5 years ago
|
MultiProducerSingleConsumerQueue q;
|
||
8 years ago
|
for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
|
||
|
ta[i].ctr = 0;
|
||
|
ta[i].q = &q;
|
||
|
ta[i].start = &start;
|
||
7 years ago
|
thds[i] = grpc_core::Thread("grpc_multipop_test", test_thread, &ta[i]);
|
||
7 years ago
|
thds[i].Start();
|
||
8 years ago
|
}
|
||
|
pull_args pa;
|
||
|
pa.ta = ta;
|
||
|
pa.num_thds = GPR_ARRAY_SIZE(thds);
|
||
|
pa.spins = 0;
|
||
|
pa.num_done = 0;
|
||
|
pa.q = &q;
|
||
|
pa.start = &start;
|
||
|
gpr_mu_init(&pa.mu);
|
||
|
for (size_t i = 0; i < GPR_ARRAY_SIZE(pull_thds); i++) {
|
||
7 years ago
|
pull_thds[i] = grpc_core::Thread("grpc_multipop_pull", pull_thread, &pa);
|
||
7 years ago
|
pull_thds[i].Start();
|
||
8 years ago
|
}
|
||
7 years ago
|
gpr_event_set(&start, (void*)1);
|
||
7 years ago
|
for (auto& pth : pull_thds) {
|
||
7 years ago
|
pth.Join();
|
||
8 years ago
|
}
|
||
|
gpr_log(GPR_DEBUG, "spins: %" PRIdPTR, pa.spins);
|
||
7 years ago
|
for (auto& th : thds) {
|
||
|
th.Join();
|
||
8 years ago
|
}
|
||
6 years ago
|
gpr_mu_destroy(&pa.mu);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
int main(int argc, char** argv) {
|
||
6 years ago
|
grpc::testing::TestEnvironment env(argc, argv);
|
||
8 years ago
|
test_serial();
|
||
|
test_mt();
|
||
|
test_mt_multipop();
|
||
|
return 0;
|
||
|
}
|