bthread_mutex_unittest.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "butil/compat.h"
  19. #include "butil/time.h"
  20. #include "butil/macros.h"
  21. #include "butil/string_printf.h"
  22. #include "butil/logging.h"
  23. #include "bthread/bthread.h"
  24. #include "bthread/butex.h"
  25. #include "bthread/task_control.h"
  26. #include "bthread/mutex.h"
  27. #include "butil/gperftools_profiler.h"
  28. namespace {
  29. inline unsigned* get_butex(bthread_mutex_t & m) {
  30. return m.butex;
  31. }
  32. long start_time = butil::cpuwide_time_ms();
  33. int c = 0;
  34. void* locker(void* arg) {
  35. bthread_mutex_t* m = (bthread_mutex_t*)arg;
  36. bthread_mutex_lock(m);
  37. printf("[%" PRIu64 "] I'm here, %d, %" PRId64 "ms\n",
  38. pthread_numeric_id(), ++c, butil::cpuwide_time_ms() - start_time);
  39. bthread_usleep(10000);
  40. bthread_mutex_unlock(m);
  41. return NULL;
  42. }
  43. TEST(MutexTest, sanity) {
  44. bthread_mutex_t m;
  45. ASSERT_EQ(0, bthread_mutex_init(&m, NULL));
  46. ASSERT_EQ(0u, *get_butex(m));
  47. ASSERT_EQ(0, bthread_mutex_lock(&m));
  48. ASSERT_EQ(1u, *get_butex(m));
  49. bthread_t th1;
  50. ASSERT_EQ(0, bthread_start_urgent(&th1, NULL, locker, &m));
  51. usleep(5000); // wait for locker to run.
  52. ASSERT_EQ(257u, *get_butex(m)); // contention
  53. ASSERT_EQ(0, bthread_mutex_unlock(&m));
  54. ASSERT_EQ(0, bthread_join(th1, NULL));
  55. ASSERT_EQ(0u, *get_butex(m));
  56. ASSERT_EQ(0, bthread_mutex_destroy(&m));
  57. }
  58. TEST(MutexTest, used_in_pthread) {
  59. bthread_mutex_t m;
  60. ASSERT_EQ(0, bthread_mutex_init(&m, NULL));
  61. pthread_t th[8];
  62. for (size_t i = 0; i < ARRAY_SIZE(th); ++i) {
  63. ASSERT_EQ(0, pthread_create(&th[i], NULL, locker, &m));
  64. }
  65. for (size_t i = 0; i < ARRAY_SIZE(th); ++i) {
  66. pthread_join(th[i], NULL);
  67. }
  68. ASSERT_EQ(0u, *get_butex(m));
  69. ASSERT_EQ(0, bthread_mutex_destroy(&m));
  70. }
  71. void* do_locks(void *arg) {
  72. struct timespec t = { -2, 0 };
  73. EXPECT_EQ(ETIMEDOUT, bthread_mutex_timedlock((bthread_mutex_t*)arg, &t));
  74. return NULL;
  75. }
  76. TEST(MutexTest, timedlock) {
  77. bthread_cond_t c;
  78. bthread_mutex_t m1;
  79. bthread_mutex_t m2;
  80. ASSERT_EQ(0, bthread_cond_init(&c, NULL));
  81. ASSERT_EQ(0, bthread_mutex_init(&m1, NULL));
  82. ASSERT_EQ(0, bthread_mutex_init(&m2, NULL));
  83. struct timespec t = { -2, 0 };
  84. bthread_mutex_lock (&m1);
  85. bthread_mutex_lock (&m2);
  86. bthread_t pth;
  87. ASSERT_EQ(0, bthread_start_urgent(&pth, NULL, do_locks, &m1));
  88. ASSERT_EQ(ETIMEDOUT, bthread_cond_timedwait(&c, &m2, &t));
  89. ASSERT_EQ(0, bthread_join(pth, NULL));
  90. bthread_mutex_unlock(&m1);
  91. bthread_mutex_unlock(&m2);
  92. bthread_mutex_destroy(&m1);
  93. bthread_mutex_destroy(&m2);
  94. }
  95. TEST(MutexTest, cpp_wrapper) {
  96. bthread::Mutex mutex;
  97. ASSERT_TRUE(mutex.try_lock());
  98. mutex.unlock();
  99. mutex.lock();
  100. mutex.unlock();
  101. {
  102. BAIDU_SCOPED_LOCK(mutex);
  103. }
  104. {
  105. std::unique_lock<bthread::Mutex> lck1;
  106. std::unique_lock<bthread::Mutex> lck2(mutex);
  107. lck1.swap(lck2);
  108. lck1.unlock();
  109. lck1.lock();
  110. }
  111. ASSERT_TRUE(mutex.try_lock());
  112. mutex.unlock();
  113. {
  114. BAIDU_SCOPED_LOCK(*mutex.native_handler());
  115. }
  116. {
  117. std::unique_lock<bthread_mutex_t> lck1;
  118. std::unique_lock<bthread_mutex_t> lck2(*mutex.native_handler());
  119. lck1.swap(lck2);
  120. lck1.unlock();
  121. lck1.lock();
  122. }
  123. ASSERT_TRUE(mutex.try_lock());
  124. mutex.unlock();
  125. }
  126. bool g_started = false;
  127. bool g_stopped = false;
  128. template <typename Mutex>
  129. struct BAIDU_CACHELINE_ALIGNMENT PerfArgs {
  130. Mutex* mutex;
  131. int64_t counter;
  132. int64_t elapse_ns;
  133. bool ready;
  134. PerfArgs() : mutex(NULL), counter(0), elapse_ns(0), ready(false) {}
  135. };
  136. template <typename Mutex>
  137. void* add_with_mutex(void* void_arg) {
  138. PerfArgs<Mutex>* args = (PerfArgs<Mutex>*)void_arg;
  139. args->ready = true;
  140. butil::Timer t;
  141. while (!g_stopped) {
  142. if (g_started) {
  143. break;
  144. }
  145. bthread_usleep(1000);
  146. }
  147. t.start();
  148. while (!g_stopped) {
  149. BAIDU_SCOPED_LOCK(*args->mutex);
  150. ++args->counter;
  151. }
  152. t.stop();
  153. args->elapse_ns = t.n_elapsed();
  154. return NULL;
  155. }
  156. int g_prof_name_counter = 0;
  157. template <typename Mutex, typename ThreadId,
  158. typename ThreadCreateFn, typename ThreadJoinFn>
  159. void PerfTest(Mutex* mutex,
  160. ThreadId* /*dummy*/,
  161. int thread_num,
  162. const ThreadCreateFn& create_fn,
  163. const ThreadJoinFn& join_fn) {
  164. g_started = false;
  165. g_stopped = false;
  166. ThreadId threads[thread_num];
  167. std::vector<PerfArgs<Mutex> > args(thread_num);
  168. for (int i = 0; i < thread_num; ++i) {
  169. args[i].mutex = mutex;
  170. create_fn(&threads[i], NULL, add_with_mutex<Mutex>, &args[i]);
  171. }
  172. while (true) {
  173. bool all_ready = true;
  174. for (int i = 0; i < thread_num; ++i) {
  175. if (!args[i].ready) {
  176. all_ready = false;
  177. break;
  178. }
  179. }
  180. if (all_ready) {
  181. break;
  182. }
  183. usleep(1000);
  184. }
  185. g_started = true;
  186. char prof_name[32];
  187. snprintf(prof_name, sizeof(prof_name), "mutex_perf_%d.prof", ++g_prof_name_counter);
  188. ProfilerStart(prof_name);
  189. usleep(500 * 1000);
  190. ProfilerStop();
  191. g_stopped = true;
  192. int64_t wait_time = 0;
  193. int64_t count = 0;
  194. for (int i = 0; i < thread_num; ++i) {
  195. join_fn(threads[i], NULL);
  196. wait_time += args[i].elapse_ns;
  197. count += args[i].counter;
  198. }
  199. LOG(INFO) << butil::class_name<Mutex>() << " in "
  200. << ((void*)create_fn == (void*)pthread_create ? "pthread" : "bthread")
  201. << " thread_num=" << thread_num
  202. << " count=" << count
  203. << " average_time=" << wait_time / (double)count;
  204. }
  205. TEST(MutexTest, performance) {
  206. const int thread_num = 12;
  207. butil::Mutex base_mutex;
  208. PerfTest(&base_mutex, (pthread_t*)NULL, thread_num, pthread_create, pthread_join);
  209. PerfTest(&base_mutex, (bthread_t*)NULL, thread_num, bthread_start_background, bthread_join);
  210. bthread::Mutex bth_mutex;
  211. PerfTest(&bth_mutex, (pthread_t*)NULL, thread_num, pthread_create, pthread_join);
  212. PerfTest(&bth_mutex, (bthread_t*)NULL, thread_num, bthread_start_background, bthread_join);
  213. }
  214. void* loop_until_stopped(void* arg) {
  215. bthread::Mutex *m = (bthread::Mutex*)arg;
  216. while (!g_stopped) {
  217. BAIDU_SCOPED_LOCK(*m);
  218. bthread_usleep(20);
  219. }
  220. return NULL;
  221. }
  222. TEST(MutexTest, mix_thread_types) {
  223. g_stopped = false;
  224. const int N = 16;
  225. const int M = N * 2;
  226. bthread::Mutex m;
  227. pthread_t pthreads[N];
  228. bthread_t bthreads[M];
  229. // reserve enough workers for test. This is a must since we have
  230. // BTHREAD_ATTR_PTHREAD bthreads which may cause deadlocks (the
  231. // bhtread_usleep below can't be scheduled and g_stopped is never
  232. // true, thus loop_until_stopped spins forever)
  233. bthread_setconcurrency(M);
  234. for (int i = 0; i < N; ++i) {
  235. ASSERT_EQ(0, pthread_create(&pthreads[i], NULL, loop_until_stopped, &m));
  236. }
  237. for (int i = 0; i < M; ++i) {
  238. const bthread_attr_t *attr = i % 2 ? NULL : &BTHREAD_ATTR_PTHREAD;
  239. ASSERT_EQ(0, bthread_start_urgent(&bthreads[i], attr, loop_until_stopped, &m));
  240. }
  241. bthread_usleep(1000L * 1000);
  242. g_stopped = true;
  243. for (int i = 0; i < M; ++i) {
  244. bthread_join(bthreads[i], NULL);
  245. }
  246. for (int i = 0; i < N; ++i) {
  247. pthread_join(pthreads[i], NULL);
  248. }
  249. }
  250. } // namespace