2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Baton.h>
18 #include <folly/test/DeterministicSchedule.h>
20 #include <semaphore.h>
21 #include <gflags/gflags.h>
22 #include <gtest/gtest.h>
23 #include <folly/Benchmark.h>
25 using namespace folly;
26 using namespace folly::test;
27 using folly::detail::EmulatedFutexAtomic;
29 typedef DeterministicSchedule DSched;
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) {
49 for (int i = 0; i < numRounds; ++i) {
57 TEST(Baton, pingpong) {
58 DSched sched(DSched::uniform(0));
60 run_pingpong_test<DeterministicAtomic>(1000);
63 BENCHMARK(baton_pingpong, iters) {
64 run_pingpong_test<std::atomic>(iters);
67 BENCHMARK(baton_pingpong_emulated_futex, iters) {
68 run_pingpong_test<EmulatedFutexAtomic>(iters);
71 BENCHMARK(posix_sem_pingpong, iters) {
74 sem_t* b = sems + 2; // to get it on a different cache line
78 auto thr = std::thread([=]{
79 for (int i = 0; i < iters; ++i) {
84 for (int i = 0; i < iters; ++i) {
91 template <template<typename> class Atom, typename Clock>
92 void run_basic_timed_wait_tests() {
95 // tests if early delivery works fine
96 EXPECT_TRUE(b.timed_wait(Clock::now()));
99 template <template<typename> class Atom, typename Clock>
100 void run_timed_wait_tmo_tests() {
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
111 template <template<typename> class Atom, typename Clock>
112 void run_timed_wait_regular_test() {
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
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));
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>();
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>();
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>();
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>();
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>();
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>();
178 template <template<typename> class Atom>
179 void run_try_wait_tests() {
181 EXPECT_FALSE(b.try_wait());
183 EXPECT_TRUE(b.try_wait());
186 TEST(Baton, try_wait) {
187 run_try_wait_tests<std::atomic>();
188 run_try_wait_tests<EmulatedFutexAtomic>();
189 run_try_wait_tests<DeterministicAtomic>();
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
197 int main(int argc, char** argv) {
198 testing::InitGoogleTest(&argc, argv);
199 gflags::ParseCommandLineFlags(&argc, &argv, true);
201 auto rv = RUN_ALL_TESTS();
202 if (!rv && FLAGS_benchmark) {
203 folly::runBenchmarks();