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 | |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 40 | // We increment the counter this way to avoid -Wdeprecated-volatile warnings. |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame] | 41 | static volatile unsigned counter; |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 42 | #define INC_COUNTER() counter = counter + 1 |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 43 | |
| 44 | std::atomic<int> test_loc(0); |
| 45 | |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame] | 46 | static volatile unsigned sink; |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 47 | |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame] | 48 | static std::mutex mtx; |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 49 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 50 | void BM_atomic_empty(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 51 | while (state.KeepRunning()) { |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 52 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 55 | BIONIC_BENCHMARK(BM_atomic_empty); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 56 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 57 | static void BM_atomic_load_relaxed(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 58 | unsigned result = 0; |
| 59 | while (state.KeepRunning()) { |
| 60 | result += test_loc.load(std::memory_order_relaxed); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 61 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 62 | } |
| 63 | sink = result; |
| 64 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 65 | BIONIC_BENCHMARK(BM_atomic_load_relaxed); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 66 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 67 | static void BM_atomic_load_acquire(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 68 | unsigned result = 0; |
| 69 | while (state.KeepRunning()) { |
| 70 | result += test_loc.load(std::memory_order_acquire); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 71 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 72 | } |
| 73 | sink = result; |
| 74 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 75 | BIONIC_BENCHMARK(BM_atomic_load_acquire); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 76 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 77 | static void BM_atomic_store_release(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 78 | int i = counter; |
| 79 | while (state.KeepRunning()) { |
| 80 | test_loc.store(++i, std::memory_order_release); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 81 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 84 | BIONIC_BENCHMARK(BM_atomic_store_release); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 85 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 86 | static void BM_atomic_store_seq_cst(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 87 | int i = counter; |
| 88 | while (state.KeepRunning()) { |
| 89 | test_loc.store(++i, std::memory_order_seq_cst); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 90 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 93 | BIONIC_BENCHMARK(BM_atomic_store_seq_cst); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 94 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 95 | static void BM_atomic_fetch_add_relaxed(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 96 | unsigned result = 0; |
| 97 | while (state.KeepRunning()) { |
| 98 | result += test_loc.fetch_add(1, std::memory_order_relaxed); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 99 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 100 | } |
| 101 | sink = result; |
| 102 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 103 | BIONIC_BENCHMARK(BM_atomic_fetch_add_relaxed); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 104 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 105 | static void BM_atomic_fetch_add_seq_cst(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 106 | unsigned result = 0; |
| 107 | while (state.KeepRunning()) { |
| 108 | result += test_loc.fetch_add(1, std::memory_order_seq_cst); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 109 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 110 | } |
| 111 | sink = result; |
| 112 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 113 | BIONIC_BENCHMARK(BM_atomic_fetch_add_seq_cst); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 114 | |
| 115 | // The fence benchmarks include a relaxed load to make it much harder to optimize away |
| 116 | // the fence. |
| 117 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 118 | static void BM_atomic_acquire_fence(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 119 | unsigned result = 0; |
| 120 | while (state.KeepRunning()) { |
| 121 | result += test_loc.load(std::memory_order_relaxed); |
| 122 | std::atomic_thread_fence(std::memory_order_acquire); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 123 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 124 | } |
| 125 | sink = result; |
| 126 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 127 | BIONIC_BENCHMARK(BM_atomic_acquire_fence); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 128 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 129 | static void BM_atomic_seq_cst_fence(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 130 | unsigned result = 0; |
| 131 | while (state.KeepRunning()) { |
| 132 | result += test_loc.load(std::memory_order_relaxed); |
| 133 | std::atomic_thread_fence(std::memory_order_seq_cst); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 134 | INC_COUNTER(); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 135 | } |
| 136 | sink = result; |
| 137 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 138 | BIONIC_BENCHMARK(BM_atomic_seq_cst_fence); |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 139 | |
| 140 | // For comparison, also throw in a critical section version: |
| 141 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 142 | static void BM_atomic_fetch_add_cs(benchmark::State& state) { |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 143 | unsigned result = 0; |
| 144 | while (state.KeepRunning()) { |
| 145 | { |
| 146 | std::lock_guard<std::mutex> _(mtx); |
Elliott Hughes | 66abb0a | 2024-02-07 22:43:15 +0000 | [diff] [blame^] | 147 | INC_COUNTER(); |
| 148 | result += counter; |
Hans Boehm | 3f55787 | 2017-01-23 17:30:44 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | sink = result; |
| 152 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 153 | BIONIC_BENCHMARK(BM_atomic_fetch_add_cs); |