RcuTest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/synchronization/Rcu.h>
  17. #include <thread>
  18. #include <vector>
  19. #include <glog/logging.h>
  20. #include <folly/Benchmark.h>
  21. #include <folly/Random.h>
  22. #include <folly/portability/GFlags.h>
  23. #include <folly/portability/GTest.h>
  24. using namespace folly;
  25. DEFINE_int64(iters, 100000, "Number of iterations");
  26. DEFINE_uint64(threads, 32, "Number of threads");
  27. TEST(RcuTest, Basic) {
  28. auto foo = new int(2);
  29. rcu_retire(foo);
  30. }
  31. class des {
  32. bool* d_;
  33. public:
  34. des(bool* d) : d_(d) {}
  35. ~des() {
  36. *d_ = true;
  37. }
  38. };
  39. TEST(RcuTest, Guard) {
  40. bool del = false;
  41. auto foo = new des(&del);
  42. { rcu_reader g; }
  43. rcu_retire(foo);
  44. synchronize_rcu();
  45. EXPECT_TRUE(del);
  46. }
  47. TEST(RcuTest, Perf) {
  48. long i = FLAGS_iters;
  49. auto start = std::chrono::steady_clock::now();
  50. while (i-- > 0) {
  51. rcu_reader g;
  52. }
  53. auto diff = std::chrono::steady_clock::now() - start;
  54. printf(
  55. "Total time %li ns \n",
  56. std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count() /
  57. FLAGS_iters);
  58. }
  59. TEST(RcuTest, ResetPerf) {
  60. long i = FLAGS_iters;
  61. auto start = std::chrono::steady_clock::now();
  62. while (i-- > 0) {
  63. rcu_retire<int>(nullptr, [](int*) {});
  64. }
  65. auto diff = std::chrono::steady_clock::now() - start;
  66. printf(
  67. "Total time %li ns \n",
  68. std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count() /
  69. FLAGS_iters);
  70. }
  71. TEST(RcuTest, SlowReader) {
  72. std::thread t;
  73. {
  74. rcu_reader g;
  75. t = std::thread([&]() { synchronize_rcu(); });
  76. usleep(100); // Wait for synchronize to start
  77. }
  78. t.join();
  79. }
  80. rcu_reader tryretire(des* obj) {
  81. rcu_reader g;
  82. rcu_retire(obj);
  83. return g;
  84. }
  85. TEST(RcuTest, CopyGuard) {
  86. bool del = false;
  87. auto foo = new des(&del);
  88. {
  89. auto res = tryretire(foo);
  90. EXPECT_FALSE(del);
  91. }
  92. rcu_barrier();
  93. EXPECT_TRUE(del);
  94. }
  95. TEST(RcuTest, Stress) {
  96. std::vector<std::thread> threads;
  97. constexpr uint32_t sz = 1000;
  98. std::atomic<int*> ints[sz];
  99. for (uint32_t i = 0; i < sz; i++) {
  100. ints[i].store(new int(0));
  101. }
  102. for (unsigned th = 0; th < FLAGS_threads; th++) {
  103. threads.push_back(std::thread([&]() {
  104. for (int i = 0; i < FLAGS_iters / 100; i++) {
  105. rcu_reader g;
  106. int sum = 0;
  107. int* ptrs[sz];
  108. for (uint32_t j = 0; j < sz; j++) {
  109. ptrs[j] = ints[j].load(std::memory_order_acquire);
  110. }
  111. for (uint32_t j = 0; j < sz; j++) {
  112. sum += *ptrs[j];
  113. }
  114. EXPECT_EQ(sum, 0);
  115. }
  116. }));
  117. }
  118. std::atomic<bool> done{false};
  119. std::thread updater([&]() {
  120. while (!done.load()) {
  121. auto newint = new int(0);
  122. auto oldint = ints[folly::Random::rand32() % sz].exchange(newint);
  123. rcu_retire<int>(oldint, [](int* obj) {
  124. *obj = folly::Random::rand32();
  125. delete obj;
  126. });
  127. }
  128. });
  129. for (auto& t : threads) {
  130. t.join();
  131. }
  132. done = true;
  133. updater.join();
  134. // Cleanup for asan
  135. synchronize_rcu();
  136. for (uint32_t i = 0; i < sz; i++) {
  137. delete ints[i].exchange(nullptr);
  138. }
  139. }
  140. TEST(RcuTest, Synchronize) {
  141. std::vector<std::thread> threads;
  142. for (unsigned th = 0; th < FLAGS_threads; th++) {
  143. threads.push_back(std::thread([&]() {
  144. for (int i = 0; i < 10; i++) {
  145. synchronize_rcu();
  146. }
  147. }));
  148. }
  149. for (auto& t : threads) {
  150. t.join();
  151. }
  152. }
  153. TEST(RcuTest, NewDomainTest) {
  154. struct UniqueTag;
  155. rcu_domain<UniqueTag> newdomain(nullptr);
  156. synchronize_rcu(&newdomain);
  157. }
  158. TEST(RcuTest, NewDomainGuardTest) {
  159. struct UniqueTag;
  160. rcu_domain<UniqueTag> newdomain(nullptr);
  161. bool del = false;
  162. auto foo = new des(&del);
  163. { rcu_reader_domain<UniqueTag> g(&newdomain); }
  164. rcu_retire(foo, {}, &newdomain);
  165. synchronize_rcu(&newdomain);
  166. EXPECT_TRUE(del);
  167. }
  168. TEST(RcuTest, MovableReader) {
  169. {
  170. rcu_reader g;
  171. rcu_reader f(std::move(g));
  172. }
  173. synchronize_rcu();
  174. {
  175. rcu_reader g(std::defer_lock);
  176. rcu_reader f;
  177. g = std::move(f);
  178. }
  179. synchronize_rcu();
  180. }
  181. TEST(RcuTest, SynchronizeInCall) {
  182. rcu_default_domain()->call([]() { synchronize_rcu(); });
  183. synchronize_rcu();
  184. }
  185. TEST(RcuTest, MoveReaderBetweenThreads) {
  186. rcu_reader g;
  187. std::thread t([f = std::move(g)] {});
  188. t.join();
  189. synchronize_rcu();
  190. }
  191. TEST(RcuTest, ForkTest) {
  192. rcu_token epoch;
  193. std::thread t([&]() { epoch = rcu_default_domain()->lock_shared(); });
  194. t.join();
  195. auto pid = fork();
  196. if (pid) {
  197. // parent
  198. rcu_default_domain()->unlock_shared(std::move(epoch));
  199. synchronize_rcu();
  200. int status;
  201. auto pid2 = wait(&status);
  202. EXPECT_EQ(status, 0);
  203. EXPECT_EQ(pid, pid2);
  204. } else {
  205. // child
  206. synchronize_rcu();
  207. exit(0); // Do not print gtest results
  208. }
  209. }
  210. TEST(RcuTest, ThreadLocalList) {
  211. struct TTag;
  212. folly::detail::ThreadCachedLists<TTag> lists;
  213. std::vector<std::thread> threads{FLAGS_threads};
  214. std::atomic<unsigned long> done{FLAGS_threads};
  215. for (auto& tr : threads) {
  216. tr = std::thread([&]() {
  217. for (int i = 0; i < FLAGS_iters; i++) {
  218. auto node = new folly::detail::ThreadCachedListsBase::Node;
  219. lists.push(node);
  220. }
  221. --done;
  222. });
  223. }
  224. while (done.load() > 0) {
  225. folly::detail::ThreadCachedLists<TTag>::ListHead list{};
  226. lists.collect(list);
  227. list.forEach([](folly::detail::ThreadCachedLists<TTag>::Node* node) {
  228. delete node;
  229. });
  230. }
  231. for (auto& thread : threads) {
  232. thread.join();
  233. }
  234. // Run cleanup pass one more time to make ASAN happy
  235. folly::detail::ThreadCachedLists<TTag>::ListHead list{};
  236. lists.collect(list);
  237. list.forEach(
  238. [](folly::detail::ThreadCachedLists<TTag>::Node* node) { delete node; });
  239. }
  240. TEST(RcuTest, ThreadDeath) {
  241. bool del = false;
  242. std::thread t([&] {
  243. auto foo = new des(&del);
  244. rcu_retire(foo);
  245. });
  246. t.join();
  247. synchronize_rcu();
  248. EXPECT_TRUE(del);
  249. }
  250. TEST(RcuTest, RcuObjBase) {
  251. bool retired = false;
  252. struct base_test : rcu_obj_base<base_test> {
  253. bool* ret_;
  254. base_test(bool* ret) : ret_(ret) {}
  255. ~base_test() {
  256. (*ret_) = true;
  257. }
  258. };
  259. auto foo = new base_test(&retired);
  260. foo->retire();
  261. synchronize_rcu();
  262. EXPECT_TRUE(retired);
  263. }