2 * Copyright 2017 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>
21 #include <folly/Benchmark.h>
22 #include <folly/portability/GFlags.h>
23 #include <folly/portability/GTest.h>
24 #include <folly/portability/Semaphore.h>
25 #include <folly/test/BatonTestHelpers.h>
26 #include <folly/test/DeterministicSchedule.h>
28 using namespace folly;
29 using namespace folly::test;
30 using folly::detail::EmulatedFutexAtomic;
32 typedef DeterministicSchedule DSched;
34 BENCHMARK(baton_pingpong_single_poster_blocking, iters) {
35 run_pingpong_test<std::atomic, true, true>(iters);
38 BENCHMARK(baton_pingpong_multi_poster_blocking, iters) {
39 run_pingpong_test<std::atomic, false, true>(iters);
42 BENCHMARK(baton_pingpong_single_poster_nonblocking, iters) {
43 run_pingpong_test<std::atomic, true, false>(iters);
46 BENCHMARK(baton_pingpong_multi_poster_nonblocking, iters) {
47 run_pingpong_test<std::atomic, false, false>(iters);
52 BENCHMARK(baton_pingpong_emulated_futex_single_poster_blocking, iters) {
53 run_pingpong_test<EmulatedFutexAtomic, true, true>(iters);
56 BENCHMARK(baton_pingpong_emulated_futex_multi_poster_blocking, iters) {
57 run_pingpong_test<EmulatedFutexAtomic, false, true>(iters);
60 BENCHMARK(baton_pingpong_emulated_futex_single_poster_nonblocking, iters) {
61 run_pingpong_test<EmulatedFutexAtomic, true, false>(iters);
64 BENCHMARK(baton_pingpong_emulated_futex_multi_poster_nonblocking, iters) {
65 run_pingpong_test<EmulatedFutexAtomic, false, false>(iters);
70 BENCHMARK(posix_sem_pingpong, iters) {
73 sem_t* b = sems + 2; // to get it on a different cache line
77 auto thr = std::thread([=] {
78 for (size_t i = 0; i < iters; ++i) {
83 for (size_t i = 0; i < iters; ++i) {
90 // I am omitting a benchmark result snapshot because these microbenchmarks
91 // mainly illustrate that PreBlockAttempts is very effective for rapid
92 // handoffs. The performance of Baton and sem_t is essentially identical
93 // to the required futex calls for the blocking case
95 int main(int argc, char** argv) {
96 testing::InitGoogleTest(&argc, argv);
97 gflags::ParseCommandLineFlags(&argc, &argv, true);
99 auto rv = RUN_ALL_TESTS();
100 if (!rv && FLAGS_benchmark) {
101 folly::runBenchmarks();