threadpool_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2019 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 
20 
22 
23 static const int kSmallThreadPoolSize = 20;
24 static const int kLargeThreadPoolSize = 100;
25 static const int kThreadSmallIter = 100;
26 static const int kThreadLargeIter = 10000;
27 
28 static void test_size_zero(void) {
29  gpr_log(GPR_INFO, "test_size_zero");
30  grpc_core::ThreadPool* pool_size_zero = new grpc_core::ThreadPool(0);
31  GPR_ASSERT(pool_size_zero->pool_capacity() == 1);
32  delete pool_size_zero;
33 }
34 
35 static void test_constructor_option(void) {
36  gpr_log(GPR_INFO, "test_constructor_option");
37  // Tests options
39  options.set_stack_size(192 * 1024); // Random non-default value
41  new grpc_core::ThreadPool(0, "test_constructor_option", options);
42  GPR_ASSERT(pool->thread_options().stack_size() == options.stack_size());
43  delete pool;
44 }
45 
46 // Simple functor for testing. It will count how many times being called.
48  public:
52  inlineable = true;
53  internal_next = this;
54  internal_success = 0;
55  }
57  static void Run(struct grpc_completion_queue_functor* cb, int /*ok*/) {
58  auto* callback = static_cast<SimpleFunctorForAdd*>(cb);
59  callback->count_.fetch_add(1, std::memory_order_relaxed);
60  }
61 
62  int count() { return count_.load(std::memory_order_relaxed); }
63 
64  private:
65  std::atomic<int> count_{0};
66 };
67 
68 static void test_add(void) {
69  gpr_log(GPR_INFO, "test_add");
72 
74  for (int i = 0; i < kThreadSmallIter; ++i) {
75  pool->Add(functor);
76  }
77  delete pool;
78  GPR_ASSERT(functor->count() == kThreadSmallIter);
79  delete functor;
80  gpr_log(GPR_DEBUG, "Done.");
81 }
82 
83 // Thread that adds closures to pool
84 class WorkThread {
85  public:
87  : num_add_(num_add), cb_(cb), pool_(pool) {
89  "thread_pool_test_add_thd",
90  [](void* th) { static_cast<WorkThread*>(th)->Run(); }, this);
91  }
93 
94  void Start() { thd_.Start(); }
95  void Join() { thd_.Join(); }
96 
97  private:
98  void Run() {
99  for (int i = 0; i < num_add_; ++i) {
100  pool_->Add(cb_);
101  }
102  }
103 
104  int num_add_;
108 };
109 
110 static void test_multi_add(void) {
111  gpr_log(GPR_INFO, "test_multi_add");
112  const int num_work_thds = 10;
114  new grpc_core::ThreadPool(kLargeThreadPoolSize, "test_multi_add");
115  SimpleFunctorForAdd* functor = new SimpleFunctorForAdd();
116  WorkThread** work_thds = static_cast<WorkThread**>(
117  gpr_zalloc(sizeof(WorkThread*) * num_work_thds));
118  gpr_log(GPR_DEBUG, "Fork threads for adding...");
119  for (int i = 0; i < num_work_thds; ++i) {
120  work_thds[i] = new WorkThread(pool, functor, kThreadLargeIter);
121  work_thds[i]->Start();
122  }
123  // Wait for all threads finish
124  gpr_log(GPR_DEBUG, "Waiting for all work threads finish...");
125  for (int i = 0; i < num_work_thds; ++i) {
126  work_thds[i]->Join();
127  delete work_thds[i];
128  }
129  gpr_free(work_thds);
130  gpr_log(GPR_DEBUG, "Done.");
131  gpr_log(GPR_DEBUG, "Waiting for all closures finish...");
132  // Destructor of thread pool will wait for all closures to finish
133  delete pool;
134  GPR_ASSERT(functor->count() == kThreadLargeIter * num_work_thds);
135  delete functor;
136  gpr_log(GPR_DEBUG, "Done.");
137 }
138 
139 // Checks the current count with a given number.
141  public:
144  inlineable = true;
146  }
148  static void Run(struct grpc_completion_queue_functor* cb, int /*ok*/) {
149  auto* callback = static_cast<SimpleFunctorCheckForAdd*>(cb);
150  (*callback->count_)++;
151  GPR_ASSERT(*callback->count_ == callback->internal_success);
152  }
153 
154  private:
155  int* count_;
156 };
157 
158 static void test_one_thread_FIFO(void) {
159  gpr_log(GPR_INFO, "test_one_thread_FIFO");
160  int counter = 0;
162  new grpc_core::ThreadPool(1, "test_one_thread_FIFO");
163  SimpleFunctorCheckForAdd** check_functors =
164  static_cast<SimpleFunctorCheckForAdd**>(
166  for (int i = 0; i < kThreadSmallIter; ++i) {
167  check_functors[i] = new SimpleFunctorCheckForAdd(i + 1, &counter);
168  pool->Add(check_functors[i]);
169  }
170  // Destructor of pool will wait until all closures finished.
171  delete pool;
172  for (int i = 0; i < kThreadSmallIter; ++i) {
173  delete check_functors[i];
174  }
175  gpr_free(check_functors);
176  gpr_log(GPR_DEBUG, "Done.");
177 }
178 
179 int main(int argc, char** argv) {
180  grpc::testing::TestEnvironment env(&argc, argv);
181  grpc_init();
182  test_size_zero();
184  test_add();
185  test_multi_add();
187  grpc_shutdown();
188  return 0;
189 }
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
test_multi_add
static void test_multi_add(void)
Definition: threadpool_test.cc:110
SimpleFunctorCheckForAdd::~SimpleFunctorCheckForAdd
~SimpleFunctorCheckForAdd()
Definition: threadpool_test.cc:147
generate.env
env
Definition: generate.py:37
WorkThread::~WorkThread
~WorkThread()
Definition: threadpool_test.cc:92
test_add
static void test_add(void)
Definition: threadpool_test.cc:68
SimpleFunctorForAdd::count
int count()
Definition: threadpool_test.cc:62
grpc_completion_queue_functor::internal_success
int internal_success
Definition: grpc_types.h:785
SimpleFunctorCheckForAdd::SimpleFunctorCheckForAdd
SimpleFunctorCheckForAdd(int ok, int *count)
Definition: threadpool_test.cc:142
WorkThread::WorkThread
WorkThread(grpc_core::ThreadPool *pool, SimpleFunctorForAdd *cb, int num_add)
Definition: threadpool_test.cc:86
options
double_dict options[]
Definition: capstone_test.c:55
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
WorkThread::Start
void Start()
Definition: threadpool_test.cc:94
kThreadSmallIter
static const int kThreadSmallIter
Definition: threadpool_test.cc:25
WorkThread::pool_
grpc_core::ThreadPool * pool_
Definition: threadpool_test.cc:106
SimpleFunctorCheckForAdd
Definition: threadpool_test.cc:140
SimpleFunctorForAdd
Definition: threadpool_test.cc:47
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
main
int main(int argc, char **argv)
Definition: threadpool_test.cc:179
kLargeThreadPoolSize
static const int kLargeThreadPoolSize
Definition: threadpool_test.cc:24
SimpleFunctorForAdd::SimpleFunctorForAdd
SimpleFunctorForAdd()
Definition: threadpool_test.cc:50
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_completion_queue_functor::functor_run
void(* functor_run)(struct grpc_completion_queue_functor *, int)
Definition: grpc_types.h:778
kSmallThreadPoolSize
static const int kSmallThreadPoolSize
Definition: threadpool_test.cc:23
test_size_zero
static void test_size_zero(void)
Definition: threadpool_test.cc:28
threadpool.h
kThreadLargeIter
static const int kThreadLargeIter
Definition: threadpool_test.cc:26
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
SimpleFunctorForAdd::count_
std::atomic< int > count_
Definition: threadpool_test.cc:65
counter
static int counter
Definition: abseil-cpp/absl/flags/reflection_test.cc:131
grpc_completion_queue_functor::internal_next
struct grpc_completion_queue_functor * internal_next
Definition: grpc_types.h:786
grpc_core::Thread::Join
void Join()
Definition: thd.h:141
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
SimpleFunctorForAdd::~SimpleFunctorForAdd
~SimpleFunctorForAdd()
Definition: threadpool_test.cc:56
grpc_core::Thread::Options
Definition: thd.h:45
SimpleFunctorCheckForAdd::Run
static void Run(struct grpc_completion_queue_functor *cb, int)
Definition: threadpool_test.cc:148
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
test_config.h
SimpleFunctorCheckForAdd::count_
int * count_
Definition: threadpool_test.cc:155
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
grpc_core::ThreadPool
Definition: src/core/lib/iomgr/executor/threadpool.h:97
WorkThread::thd_
grpc_core::Thread thd_
Definition: threadpool_test.cc:107
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
WorkThread::num_add_
int num_add_
Definition: threadpool_test.cc:104
ok
bool ok
Definition: async_end2end_test.cc:197
grpc_completion_queue_functor
Definition: grpc_types.h:773
grpc_core::ThreadPool::Add
void Add(grpc_completion_queue_functor *closure) override
Definition: threadpool.cc:122
grpc_core::Thread
Definition: thd.h:43
WorkThread::Run
void Run()
Definition: threadpool_test.cc:98
pool
InternalDescriptorPool * pool
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:807
WorkThread::Join
void Join()
Definition: threadpool_test.cc:95
WorkThread::cb_
SimpleFunctorForAdd * cb_
Definition: threadpool_test.cc:105
SimpleFunctorForAdd::Run
static void Run(struct grpc_completion_queue_functor *cb, int)
Definition: threadpool_test.cc:57
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
WorkThread
Definition: threadpool_test.cc:84
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
test_one_thread_FIFO
static void test_one_thread_FIFO(void)
Definition: threadpool_test.cc:158
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_core::ThreadPool::pool_capacity
int pool_capacity() const override
Definition: threadpool.cc:129
test_constructor_option
static void test_constructor_option(void)
Definition: threadpool_test.cc:35
grpc_completion_queue_functor::inlineable
int inlineable
Definition: grpc_types.h:782


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:37