Copyright 2014->2015
[folly.git] / folly / test / BatonTest.cpp
1 /*
2  * Copyright 2015 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/Baton.h>
18 #include <folly/test/DeterministicSchedule.h>
19 #include <thread>
20 #include <semaphore.h>
21 #include <gflags/gflags.h>
22 #include <gtest/gtest.h>
23 #include <folly/Benchmark.h>
24
25 using namespace folly;
26 using namespace folly::test;
27 using folly::detail::EmulatedFutexAtomic;
28
29 typedef DeterministicSchedule DSched;
30
31 TEST(Baton, basic) {
32   Baton<> b;
33   b.post();
34   b.wait();
35 }
36
37 template <template<typename> class Atom>
38 void run_pingpong_test(int numRounds) {
39   Baton<Atom> batons[17];
40   Baton<Atom>& a = batons[0];
41   Baton<Atom>& b = batons[16]; // to get it on a different cache line
42   auto thr = DSched::thread([&]{
43     for (int i = 0; i < numRounds; ++i) {
44       a.wait();
45       a.reset();
46       b.post();
47     }
48   });
49   for (int i = 0; i < numRounds; ++i) {
50     a.post();
51     b.wait();
52     b.reset();
53   }
54   DSched::join(thr);
55 }
56
57 TEST(Baton, pingpong) {
58   DSched sched(DSched::uniform(0));
59
60   run_pingpong_test<DeterministicAtomic>(1000);
61 }
62
63 BENCHMARK(baton_pingpong, iters) {
64   run_pingpong_test<std::atomic>(iters);
65 }
66
67 BENCHMARK(baton_pingpong_emulated_futex, iters) {
68   run_pingpong_test<EmulatedFutexAtomic>(iters);
69 }
70
71 BENCHMARK(posix_sem_pingpong, iters) {
72   sem_t sems[3];
73   sem_t* a = sems + 0;
74   sem_t* b = sems + 2; // to get it on a different cache line
75
76   sem_init(a, 0, 0);
77   sem_init(b, 0, 0);
78   auto thr = std::thread([=]{
79     for (size_t i = 0; i < iters; ++i) {
80       sem_wait(a);
81       sem_post(b);
82     }
83   });
84   for (size_t i = 0; i < iters; ++i) {
85     sem_post(a);
86     sem_wait(b);
87   }
88   thr.join();
89 }
90
91 template <template<typename> class Atom, typename Clock>
92 void run_basic_timed_wait_tests() {
93   Baton<Atom> b;
94   b.post();
95   // tests if early delivery works fine
96   EXPECT_TRUE(b.timed_wait(Clock::now()));
97 }
98
99 template <template<typename> class Atom, typename Clock>
100 void run_timed_wait_tmo_tests() {
101   Baton<Atom> b;
102
103   auto thr = DSched::thread([&]{
104     bool rv = b.timed_wait(Clock::now() + std::chrono::milliseconds(1));
105     // main thread is guaranteed to not post until timeout occurs
106     EXPECT_FALSE(rv);
107   });
108   DSched::join(thr);
109 }
110
111 template <template<typename> class Atom, typename Clock>
112 void run_timed_wait_regular_test() {
113   Baton<Atom> b;
114
115   auto thr = DSched::thread([&] {
116     // To wait forever we'd like to use time_point<Clock>::max, but
117     // std::condition_variable does math to convert the timeout to
118     // system_clock without handling overflow.
119     auto farFuture = Clock::now() + std::chrono::hours(1000);
120     bool rv = b.timed_wait(farFuture);
121     if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
122       // DeterministicAtomic ignores actual times, so doesn't guarantee
123       // a lack of timeout
124       EXPECT_TRUE(rv);
125     }
126   });
127
128   if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
129     // If we are using std::atomic (or EmulatedFutexAtomic) then
130     // a sleep here guarantees to a large extent that 'thr' will
131     // execute wait before we post it, thus testing late delivery. For
132     // DeterministicAtomic, we just rely on DeterministicSchedule to do
133     // the scheduling.  The test won't fail if we lose the race, we just
134     // don't get coverage.
135     std::this_thread::sleep_for(std::chrono::milliseconds(2));
136   }
137
138   b.post();
139   DSched::join(thr);
140 }
141
142 TEST(Baton, timed_wait_basic_system_clock) {
143   run_basic_timed_wait_tests<std::atomic, std::chrono::system_clock>();
144   run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
145   run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::system_clock>();
146 }
147
148 TEST(Baton, timed_wait_timeout_system_clock) {
149   run_timed_wait_tmo_tests<std::atomic, std::chrono::system_clock>();
150   run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::system_clock>();
151   run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::system_clock>();
152 }
153
154 TEST(Baton, timed_wait_system_clock) {
155   run_timed_wait_regular_test<std::atomic, std::chrono::system_clock>();
156   run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::system_clock>();
157   run_timed_wait_regular_test<DeterministicAtomic, std::chrono::system_clock>();
158 }
159
160 TEST(Baton, timed_wait_basic_steady_clock) {
161   run_basic_timed_wait_tests<std::atomic, std::chrono::steady_clock>();
162   run_basic_timed_wait_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
163   run_basic_timed_wait_tests<DeterministicAtomic, std::chrono::steady_clock>();
164 }
165
166 TEST(Baton, timed_wait_timeout_steady_clock) {
167   run_timed_wait_tmo_tests<std::atomic, std::chrono::steady_clock>();
168   run_timed_wait_tmo_tests<EmulatedFutexAtomic, std::chrono::steady_clock>();
169   run_timed_wait_tmo_tests<DeterministicAtomic, std::chrono::steady_clock>();
170 }
171
172 TEST(Baton, timed_wait_steady_clock) {
173   run_timed_wait_regular_test<std::atomic, std::chrono::steady_clock>();
174   run_timed_wait_regular_test<EmulatedFutexAtomic, std::chrono::steady_clock>();
175   run_timed_wait_regular_test<DeterministicAtomic, std::chrono::steady_clock>();
176 }
177
178 template <template<typename> class Atom>
179 void run_try_wait_tests() {
180   Baton<Atom> b;
181   EXPECT_FALSE(b.try_wait());
182   b.post();
183   EXPECT_TRUE(b.try_wait());
184 }
185
186 TEST(Baton, try_wait) {
187   run_try_wait_tests<std::atomic>();
188   run_try_wait_tests<EmulatedFutexAtomic>();
189   run_try_wait_tests<DeterministicAtomic>();
190 }
191
192 // I am omitting a benchmark result snapshot because these microbenchmarks
193 // mainly illustrate that PreBlockAttempts is very effective for rapid
194 // handoffs.  The performance of Baton and sem_t is essentially identical
195 // to the required futex calls for the blocking case
196
197 int main(int argc, char** argv) {
198   testing::InitGoogleTest(&argc, argv);
199   gflags::ParseCommandLineFlags(&argc, &argv, true);
200
201   auto rv = RUN_ALL_TESTS();
202   if (!rv && FLAGS_benchmark) {
203     folly::runBenchmarks();
204   }
205   return rv;
206 }