ThreadLocalDestroyBenchmark.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2018-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/Benchmark.h>
  17. #include <folly/ThreadLocal.h>
  18. #include <condition_variable>
  19. #include <mutex>
  20. #include <thread>
  21. using namespace folly;
  22. template <typename Tag1, typename Tag2>
  23. void runTestTag(int iters, int numThreads) {
  24. BenchmarkSuspender susp;
  25. ThreadLocalPtr<int, Tag1> t1;
  26. t1.reset(new int(1));
  27. std::mutex m, mw;
  28. std::condition_variable cv, cvw;
  29. bool running = true;
  30. int numRunning = 0;
  31. std::vector<std::thread> threads;
  32. for (int i = 0; i < numThreads; i++) {
  33. threads.push_back(std::thread([&]() mutable {
  34. t1.reset(new int(1));
  35. // notify if all the threads have created the t1
  36. bool notify = false;
  37. {
  38. std::lock_guard<std::mutex> lk(m);
  39. if (++numRunning == numThreads) {
  40. notify = true;
  41. }
  42. }
  43. if (notify) {
  44. cv.notify_one();
  45. }
  46. // now wait
  47. {
  48. std::unique_lock<std::mutex> lk(mw);
  49. cvw.wait(lk, [&]() { return !running; });
  50. }
  51. }));
  52. }
  53. // wait for the threads to create the t1
  54. {
  55. std::unique_lock<std::mutex> lk(m);
  56. cv.wait(lk, [&]() { return numRunning == numThreads; });
  57. }
  58. ThreadLocalPtr<int, Tag2> t3;
  59. t3.reset(new int(2));
  60. susp.dismiss();
  61. // run the test loop
  62. for (int i = 0; i < iters; i++) {
  63. ThreadLocalPtr<int, Tag2> t2;
  64. t2.reset(new int(2));
  65. }
  66. susp.rehire();
  67. {
  68. std::lock_guard<std::mutex> lk(mw);
  69. running = false;
  70. }
  71. cvw.notify_all();
  72. for (auto& t : threads) {
  73. t.join();
  74. }
  75. }
  76. void runTestSameTag(int iters, int numThreads) {
  77. runTestTag<void, void>(iters, numThreads);
  78. }
  79. void runTestDiffTag(int iters, int numThreads) {
  80. runTestTag<void, int>(iters, numThreads);
  81. }
  82. BENCHMARK_DRAW_LINE();
  83. BENCHMARK_PARAM(runTestSameTag, 1)
  84. BENCHMARK_PARAM(runTestSameTag, 128)
  85. BENCHMARK_PARAM(runTestSameTag, 256)
  86. BENCHMARK_PARAM(runTestSameTag, 512)
  87. BENCHMARK_PARAM(runTestSameTag, 1024)
  88. BENCHMARK_PARAM(runTestSameTag, 2048)
  89. BENCHMARK_DRAW_LINE();
  90. BENCHMARK_PARAM(runTestDiffTag, 1)
  91. BENCHMARK_PARAM(runTestDiffTag, 128)
  92. BENCHMARK_PARAM(runTestDiffTag, 256)
  93. BENCHMARK_PARAM(runTestDiffTag, 512)
  94. BENCHMARK_PARAM(runTestDiffTag, 1024)
  95. BENCHMARK_PARAM(runTestDiffTag, 2048)
  96. BENCHMARK_DRAW_LINE();
  97. int main(int argc, char* argv[]) {
  98. gflags::ParseCommandLineFlags(&argc, &argv, true);
  99. folly::runBenchmarks();
  100. return 0;
  101. }