1
0

bthread_setconcurrency_unittest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include <gflags/gflags.h>
  19. #include "butil/atomicops.h"
  20. #include "butil/time.h"
  21. #include "butil/macros.h"
  22. #include "butil/logging.h"
  23. #include "butil/thread_local.h"
  24. #include <bthread/butex.h>
  25. #include "butil/logging.h"
  26. #include "bthread/bthread.h"
  27. #include "bthread/task_control.h"
  28. namespace bthread {
  29. extern TaskControl* g_task_control;
  30. }
  31. namespace {
  32. void* dummy(void*) {
  33. return NULL;
  34. }
  35. TEST(BthreadTest, setconcurrency) {
  36. ASSERT_EQ(8 + BTHREAD_EPOLL_THREAD_NUM, (size_t)bthread_getconcurrency());
  37. ASSERT_EQ(EINVAL, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY - 1));
  38. ASSERT_EQ(EINVAL, bthread_setconcurrency(0));
  39. ASSERT_EQ(EINVAL, bthread_setconcurrency(-1));
  40. ASSERT_EQ(EINVAL, bthread_setconcurrency(BTHREAD_MAX_CONCURRENCY + 1));
  41. ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY));
  42. ASSERT_EQ(BTHREAD_MIN_CONCURRENCY, bthread_getconcurrency());
  43. ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY + 1));
  44. ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 1, bthread_getconcurrency());
  45. ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY)); // smaller value
  46. bthread_t th;
  47. ASSERT_EQ(0, bthread_start_urgent(&th, NULL, dummy, NULL));
  48. ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 1, bthread_getconcurrency());
  49. ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY + 5));
  50. ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 5, bthread_getconcurrency());
  51. ASSERT_EQ(EPERM, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY + 1));
  52. ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 5, bthread_getconcurrency());
  53. }
  54. static butil::atomic<int> *odd;
  55. static butil::atomic<int> *even;
  56. static butil::atomic<int> nbthreads(0);
  57. static butil::atomic<int> npthreads(0);
  58. static BAIDU_THREAD_LOCAL bool counted = false;
  59. static butil::atomic<bool> stop (false);
  60. static void *odd_thread(void *) {
  61. nbthreads.fetch_add(1);
  62. while (!stop) {
  63. if (!counted) {
  64. counted = true;
  65. npthreads.fetch_add(1);
  66. }
  67. bthread::butex_wake_all(even);
  68. bthread::butex_wait(odd, 0, NULL);
  69. }
  70. return NULL;
  71. }
  72. static void *even_thread(void *) {
  73. nbthreads.fetch_add(1);
  74. while (!stop) {
  75. if (!counted) {
  76. counted = true;
  77. npthreads.fetch_add(1);
  78. }
  79. bthread::butex_wake_all(odd);
  80. bthread::butex_wait(even, 0, NULL);
  81. }
  82. return NULL;
  83. }
  84. TEST(BthreadTest, setconcurrency_with_running_bthread) {
  85. odd = bthread::butex_create_checked<butil::atomic<int> >();
  86. even = bthread::butex_create_checked<butil::atomic<int> >();
  87. ASSERT_TRUE(odd != NULL && even != NULL);
  88. *odd = 0;
  89. *even = 0;
  90. std::vector<bthread_t> tids;
  91. const int N = 500;
  92. for (int i = 0; i < N; ++i) {
  93. bthread_t tid;
  94. bthread_start_background(&tid, &BTHREAD_ATTR_SMALL, odd_thread, NULL);
  95. tids.push_back(tid);
  96. bthread_start_background(&tid, &BTHREAD_ATTR_SMALL, even_thread, NULL);
  97. tids.push_back(tid);
  98. }
  99. for (int i = 100; i <= N; ++i) {
  100. ASSERT_EQ(0, bthread_setconcurrency(i));
  101. ASSERT_EQ(i, bthread_getconcurrency());
  102. }
  103. usleep(1000 * N);
  104. *odd = 1;
  105. *even = 1;
  106. stop = true;
  107. bthread::butex_wake_all(odd);
  108. bthread::butex_wake_all(even);
  109. for (size_t i = 0; i < tids.size(); ++i) {
  110. bthread_join(tids[i], NULL);
  111. }
  112. LOG(INFO) << "All bthreads has quit";
  113. ASSERT_EQ(2*N, nbthreads);
  114. // This is not necessarily true, not all workers need to run sth.
  115. //ASSERT_EQ(N, npthreads);
  116. LOG(INFO) << "Touched pthreads=" << npthreads;
  117. }
  118. void* sleep_proc(void*) {
  119. usleep(100000);
  120. return NULL;
  121. }
  122. void* add_concurrency_proc(void*) {
  123. bthread_t tid;
  124. bthread_start_background(&tid, &BTHREAD_ATTR_SMALL, sleep_proc, NULL);
  125. bthread_join(tid, NULL);
  126. return NULL;
  127. }
  128. bool set_min_concurrency(int num) {
  129. std::stringstream ss;
  130. ss << num;
  131. std::string ret = GFLAGS_NS::SetCommandLineOption("bthread_min_concurrency", ss.str().c_str());
  132. return !ret.empty();
  133. }
  134. int get_min_concurrency() {
  135. std::string ret;
  136. GFLAGS_NS::GetCommandLineOption("bthread_min_concurrency", &ret);
  137. return atoi(ret.c_str());
  138. }
  139. TEST(BthreadTest, min_concurrency) {
  140. ASSERT_EQ(1, set_min_concurrency(-1)); // set min success
  141. ASSERT_EQ(1, set_min_concurrency(0)); // set min success
  142. ASSERT_EQ(0, get_min_concurrency());
  143. int conn = bthread_getconcurrency();
  144. int add_conn = 100;
  145. ASSERT_EQ(0, set_min_concurrency(conn + 1)); // set min failed
  146. ASSERT_EQ(0, get_min_concurrency());
  147. ASSERT_EQ(1, set_min_concurrency(conn - 1)); // set min success
  148. ASSERT_EQ(conn - 1, get_min_concurrency());
  149. ASSERT_EQ(EINVAL, bthread_setconcurrency(conn - 2)); // set max failed
  150. ASSERT_EQ(0, bthread_setconcurrency(conn + add_conn + 1)); // set max success
  151. ASSERT_EQ(0, bthread_setconcurrency(conn + add_conn)); // set max success
  152. ASSERT_EQ(conn + add_conn, bthread_getconcurrency());
  153. ASSERT_EQ(conn, bthread::g_task_control->concurrency());
  154. ASSERT_EQ(1, set_min_concurrency(conn + 1)); // set min success
  155. ASSERT_EQ(conn + 1, get_min_concurrency());
  156. ASSERT_EQ(conn + 1, bthread::g_task_control->concurrency());
  157. std::vector<bthread_t> tids;
  158. for (int i = 0; i < conn; ++i) {
  159. bthread_t tid;
  160. bthread_start_background(&tid, &BTHREAD_ATTR_SMALL, sleep_proc, NULL);
  161. tids.push_back(tid);
  162. }
  163. for (int i = 0; i < add_conn; ++i) {
  164. bthread_t tid;
  165. bthread_start_background(&tid, &BTHREAD_ATTR_SMALL, add_concurrency_proc, NULL);
  166. tids.push_back(tid);
  167. }
  168. for (size_t i = 0; i < tids.size(); ++i) {
  169. bthread_join(tids[i], NULL);
  170. }
  171. ASSERT_EQ(conn + add_conn, bthread_getconcurrency());
  172. ASSERT_EQ(conn + add_conn, bthread::g_task_control->concurrency());
  173. }
  174. } // namespace