9eb14ec9456efc9158ce9f426678350db05a1ba0
[folly.git] / folly / test / ThreadLocalBenchmark.cpp
1 /*
2  * Copyright 2017 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/ThreadLocal.h>
18
19 #include <sys/types.h>
20
21 #include <array>
22 #include <atomic>
23 #include <condition_variable>
24 #include <map>
25 #include <mutex>
26 #include <set>
27 #include <thread>
28
29 #include <boost/thread/tss.hpp>
30 #include <glog/logging.h>
31
32 #include <folly/Benchmark.h>
33 #include <folly/experimental/io/FsUtil.h>
34 #include <folly/portability/GFlags.h>
35
36 using namespace folly;
37
38 // Simple reference implementation using pthread_get_specific
39 template <typename T>
40 class PThreadGetSpecific {
41  public:
42   PThreadGetSpecific() : key_(0) { pthread_key_create(&key_, OnThreadExit); }
43
44   T* get() const { return static_cast<T*>(pthread_getspecific(key_)); }
45
46   void reset(T* t) {
47     delete get();
48     pthread_setspecific(key_, t);
49   }
50   static void OnThreadExit(void* obj) { delete static_cast<T*>(obj); }
51
52  private:
53   pthread_key_t key_;
54 };
55
56 DEFINE_int32(numThreads, 8, "Number simultaneous threads for benchmarks.");
57
58 #define REG(var)                                         \
59   BENCHMARK(FB_CONCATENATE(BM_mt_, var), iters) {        \
60     const int itersPerThread = iters / FLAGS_numThreads; \
61     std::vector<std::thread> threads;                    \
62     for (int i = 0; i < FLAGS_numThreads; ++i) {         \
63       threads.push_back(std::thread([&]() {              \
64         var.reset(new int(0));                           \
65         for (int j = 0; j < itersPerThread; ++j) {       \
66           ++(*var.get());                                \
67         }                                                \
68       }));                                               \
69     }                                                    \
70     for (auto& t : threads) {                            \
71       t.join();                                          \
72     }                                                    \
73   }
74
75 ThreadLocalPtr<int> tlp;
76 REG(tlp);
77 PThreadGetSpecific<int> pthread_get_specific;
78 REG(pthread_get_specific);
79 boost::thread_specific_ptr<int> boost_tsp;
80 REG(boost_tsp);
81 BENCHMARK_DRAW_LINE();
82
83 int main(int argc, char** argv) {
84   gflags::ParseCommandLineFlags(&argc, &argv, true);
85   gflags::SetCommandLineOptionWithMode(
86       "bm_max_iters", "100000000", gflags::SET_FLAG_IF_DEFAULT);
87   folly::runBenchmarks();
88   return 0;
89 }
90
91 /*
92 Ran with 24 threads on dual 12-core Xeon(R) X5650 @ 2.67GHz with 12-MB caches
93
94 Benchmark                               Iters   Total t    t/iter iter/sec
95 ------------------------------------------------------------------------------
96 *       BM_mt_tlp                   100000000  39.88 ms  398.8 ps  2.335 G
97  +5.91% BM_mt_pthread_get_specific  100000000  42.23 ms  422.3 ps  2.205 G
98  + 295% BM_mt_boost_tsp             100000000  157.8 ms  1.578 ns  604.5 M
99 ------------------------------------------------------------------------------
100 */