Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 | // Our goal is to measure the cost of various C++ atomic operations. |
| 18 | // Android doesn't really control those. But since some of these operations can be quite |
| 19 | // expensive, this may be useful input for development of higher level code. |
| 20 | // Expected mappings from C++ atomics to hardware primitives can be found at |
| 21 | // http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html . |
| 22 | |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 23 | #include <atomic> |
| 24 | #include <mutex> |
| 25 | |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 26 | #include <benchmark/benchmark.h> |
| 27 | #include "util.h" |
| 28 | |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 29 | // We time atomic operations separated by a volatile (not atomic!) increment. This ensures |
| 30 | // that the compiler emits memory instructions (e.g. load or store) prior to any fence or the |
| 31 | // like. That in turn ensures that the CPU has outstanding memory operations when the fence |
| 32 | // is executed. |
| 33 | |
| 34 | // In most respects, we compute best case values. Since there is only one thread, there are no |
| 35 | // coherence misses. |
| 36 | |
| 37 | // We assume that the compiler is not smart enough to optimize away fences in a single-threaded |
| 38 | // program. If that changes, we'll need to add a second thread. |
| 39 | |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame^] | 40 | static volatile unsigned counter; |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 41 | |
| 42 | std::atomic<int> test_loc(0); |
| 43 | |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame^] | 44 | static volatile unsigned sink; |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 45 | |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame^] | 46 | static std::mutex mtx; |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 47 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 48 | void BM_atomic_empty(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 49 | while (state.KeepRunning()) { |
| 50 | ++counter; |
| 51 | } |
| 52 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 53 | BIONIC_BENCHMARK(BM_atomic_empty); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 54 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 55 | static void BM_atomic_load_relaxed(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 56 | unsigned result = 0; |
| 57 | while (state.KeepRunning()) { |
| 58 | result += test_loc.load(std::memory_order_relaxed); |
| 59 | ++counter; |
| 60 | } |
| 61 | sink = result; |
| 62 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 63 | BIONIC_BENCHMARK(BM_atomic_load_relaxed); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 64 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 65 | static void BM_atomic_load_acquire(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 66 | unsigned result = 0; |
| 67 | while (state.KeepRunning()) { |
| 68 | result += test_loc.load(std::memory_order_acquire); |
| 69 | ++counter; |
| 70 | } |
| 71 | sink = result; |
| 72 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 73 | BIONIC_BENCHMARK(BM_atomic_load_acquire); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 74 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 75 | static void BM_atomic_store_release(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 76 | int i = counter; |
| 77 | while (state.KeepRunning()) { |
| 78 | test_loc.store(++i, std::memory_order_release); |
| 79 | ++counter; |
| 80 | } |
| 81 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 82 | BIONIC_BENCHMARK(BM_atomic_store_release); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 83 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 84 | static void BM_atomic_store_seq_cst(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 85 | int i = counter; |
| 86 | while (state.KeepRunning()) { |
| 87 | test_loc.store(++i, std::memory_order_seq_cst); |
| 88 | ++counter; |
| 89 | } |
| 90 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 91 | BIONIC_BENCHMARK(BM_atomic_store_seq_cst); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 92 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 93 | static void BM_atomic_fetch_add_relaxed(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 94 | unsigned result = 0; |
| 95 | while (state.KeepRunning()) { |
| 96 | result += test_loc.fetch_add(1, std::memory_order_relaxed); |
| 97 | ++counter; |
| 98 | } |
| 99 | sink = result; |
| 100 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 101 | BIONIC_BENCHMARK(BM_atomic_fetch_add_relaxed); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 102 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 103 | static void BM_atomic_fetch_add_seq_cst(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 104 | unsigned result = 0; |
| 105 | while (state.KeepRunning()) { |
| 106 | result += test_loc.fetch_add(1, std::memory_order_seq_cst); |
| 107 | ++counter; |
| 108 | } |
| 109 | sink = result; |
| 110 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 111 | BIONIC_BENCHMARK(BM_atomic_fetch_add_seq_cst); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 112 | |
| 113 | // The fence benchmarks include a relaxed load to make it much harder to optimize away |
| 114 | // the fence. |
| 115 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 116 | static void BM_atomic_acquire_fence(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 117 | unsigned result = 0; |
| 118 | while (state.KeepRunning()) { |
| 119 | result += test_loc.load(std::memory_order_relaxed); |
| 120 | std::atomic_thread_fence(std::memory_order_acquire); |
| 121 | ++counter; |
| 122 | } |
| 123 | sink = result; |
| 124 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 125 | BIONIC_BENCHMARK(BM_atomic_acquire_fence); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 126 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 127 | static void BM_atomic_seq_cst_fence(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 128 | unsigned result = 0; |
| 129 | while (state.KeepRunning()) { |
| 130 | result += test_loc.load(std::memory_order_relaxed); |
| 131 | std::atomic_thread_fence(std::memory_order_seq_cst); |
| 132 | ++counter; |
| 133 | } |
| 134 | sink = result; |
| 135 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 136 | BIONIC_BENCHMARK(BM_atomic_seq_cst_fence); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 137 | |
| 138 | // For comparison, also throw in a critical section version: |
| 139 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 140 | static void BM_atomic_fetch_add_cs(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 141 | unsigned result = 0; |
| 142 | while (state.KeepRunning()) { |
| 143 | { |
| 144 | std::lock_guard<std::mutex> _(mtx); |
| 145 | result += ++counter; |
| 146 | } |
| 147 | } |
| 148 | sink = result; |
| 149 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 150 | BIONIC_BENCHMARK(BM_atomic_fetch_add_cs); |