proxygen
ThreadLocalAccessBenchmark.cpp
Go to the documentation of this file.
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 
17 #include <folly/Benchmark.h>
18 #include <folly/ThreadLocal.h>
19 #include <condition_variable>
20 #include <mutex>
21 #include <thread>
22 
23 using namespace folly;
24 
25 namespace {
27  class NewTag;
29 
30  public:
31  void set() {
32  *val_ = 0;
33  }
34 
35  void access() {
36  for (const auto& i : val_.accessAllThreads()) {
37  (void)i;
38  }
39  }
40 };
41 } // namespace
42 void runTest(int iters, int numThreads) {
43  BenchmarkSuspender susp;
44 
45  std::vector<SimpleThreadCachedInt> stci(numThreads);
46 
47  std::mutex m, mw;
48  std::condition_variable cv, cvw;
49  bool running = true;
50  int numRunning = 0;
51 
52  std::vector<std::thread> threads;
53  for (int i = 0; i < numThreads; i++) {
54  threads.push_back(std::thread([i,
55  numThreads,
56  &stci,
57  &m,
58  &mw,
59  &cv,
60  &cvw,
61  &running,
62  &numRunning]() mutable {
63  stci[i].set();
64 
65  // notify if all the threads have created the
66  // thread local var
67  bool notify = false;
68  {
69  std::lock_guard<std::mutex> lk(m);
70  if (++numRunning == numThreads) {
71  notify = true;
72  }
73  }
74 
75  if (notify) {
76  cv.notify_one();
77  }
78 
79  // now wait
80  {
81  std::unique_lock<std::mutex> lk(mw);
82  cvw.wait(lk, [&]() { return !running; });
83  }
84  }));
85  }
86 
87  // wait for the threads to create the thread locals
88  {
89  std::unique_lock<std::mutex> lk(m);
90  cv.wait(lk, [&]() { return numRunning == numThreads; });
91  }
92 
93  susp.dismiss();
94 
95  // run the test loop
96  for (int i = 0; i < iters; i++) {
97  stci[i % numThreads].access();
98  }
99 
100  susp.rehire();
101 
102  {
103  std::lock_guard<std::mutex> lk(mw);
104  running = false;
105  }
106 
107  cvw.notify_all();
108 
109  for (auto& t : threads) {
110  t.join();
111  }
112 }
113 
122 
123 int main(int argc, char* argv[]) {
124  gflags::ParseCommandLineFlags(&argc, &argv, true);
126 
127  return 0;
128 }
Accessor accessAllThreads() const
Definition: ThreadLocal.h:87
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
std::vector< std::thread::id > threads
char ** argv
static map< string, int > m
void runTest(int iters, int numThreads)
#define BENCHMARK_PARAM(name, param)
Definition: Benchmark.h:417
int main(int argc, char *argv[])
std::mutex mutex
BENCHMARK_DRAW_LINE()