Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | #include "absl/random/internal/randen.h" |
| 16 | |
| 17 | #include <cstdint> |
| 18 | #include <cstdio> |
| 19 | #include <cstring> |
| 20 | |
| 21 | #include "absl/base/internal/raw_logging.h" |
| 22 | #include "absl/random/internal/nanobenchmark.h" |
| 23 | #include "absl/random/internal/platform.h" |
| 24 | #include "absl/random/internal/randen_engine.h" |
| 25 | #include "absl/random/internal/randen_hwaes.h" |
| 26 | #include "absl/random/internal/randen_slow.h" |
| 27 | #include "absl/strings/numbers.h" |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | using absl::random_internal::Randen; |
| 32 | using absl::random_internal::RandenHwAes; |
| 33 | using absl::random_internal::RandenSlow; |
| 34 | |
| 35 | using absl::random_internal_nanobenchmark::FuncInput; |
| 36 | using absl::random_internal_nanobenchmark::FuncOutput; |
| 37 | using absl::random_internal_nanobenchmark::InvariantTicksPerSecond; |
| 38 | using absl::random_internal_nanobenchmark::MeasureClosure; |
| 39 | using absl::random_internal_nanobenchmark::Params; |
| 40 | using absl::random_internal_nanobenchmark::PinThreadToCPU; |
| 41 | using absl::random_internal_nanobenchmark::Result; |
| 42 | |
| 43 | // Local state parameters. |
| 44 | static constexpr size_t kStateSizeT = Randen::kStateBytes / sizeof(uint64_t); |
| 45 | static constexpr size_t kSeedSizeT = Randen::kSeedBytes / sizeof(uint32_t); |
| 46 | |
| 47 | // Randen implementation benchmarks. |
| 48 | template <typename T> |
| 49 | struct AbsorbFn : public T { |
| 50 | mutable uint64_t state[kStateSizeT] = {}; |
| 51 | mutable uint32_t seed[kSeedSizeT] = {}; |
| 52 | |
| 53 | static constexpr size_t bytes() { return sizeof(seed); } |
| 54 | |
| 55 | FuncOutput operator()(const FuncInput num_iters) const { |
| 56 | for (size_t i = 0; i < num_iters; ++i) { |
| 57 | this->Absorb(seed, state); |
| 58 | } |
| 59 | return state[0]; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | template <typename T> |
| 64 | struct GenerateFn : public T { |
| 65 | mutable uint64_t state[kStateSizeT]; |
| 66 | GenerateFn() { std::memset(state, 0, sizeof(state)); } |
| 67 | |
| 68 | static constexpr size_t bytes() { return sizeof(state); } |
| 69 | |
| 70 | FuncOutput operator()(const FuncInput num_iters) const { |
| 71 | const auto* keys = this->GetKeys(); |
| 72 | for (size_t i = 0; i < num_iters; ++i) { |
| 73 | this->Generate(keys, state); |
| 74 | } |
| 75 | return state[0]; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | template <typename UInt> |
| 80 | struct Engine { |
| 81 | mutable absl::random_internal::randen_engine<UInt> rng; |
| 82 | |
| 83 | static constexpr size_t bytes() { return sizeof(UInt); } |
| 84 | |
| 85 | FuncOutput operator()(const FuncInput num_iters) const { |
| 86 | for (size_t i = 0; i < num_iters - 1; ++i) { |
| 87 | rng(); |
| 88 | } |
| 89 | return rng(); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | template <size_t N> |
| 94 | void Print(const char* name, const size_t n, const Result (&results)[N], |
| 95 | const size_t bytes) { |
| 96 | if (n == 0) { |
| 97 | ABSL_RAW_LOG( |
| 98 | WARNING, |
| 99 | "WARNING: Measurement failed, should not happen when using " |
| 100 | "PinThreadToCPU unless the region to measure takes > 1 second.\n"); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | static const double ns_per_tick = 1e9 / InvariantTicksPerSecond(); |
| 105 | static constexpr const double kNsPerS = 1e9; // ns/s |
| 106 | static constexpr const double kMBPerByte = 1.0 / 1048576.0; // Mb / b |
| 107 | static auto header = [] { |
| 108 | return printf("%20s %8s: %12s ticks; %9s (%9s) %8s\n", "Name", "Count", |
| 109 | "Total", "Variance", "Time", "bytes/s"); |
| 110 | }(); |
| 111 | (void)header; |
| 112 | |
| 113 | for (size_t i = 0; i < n; ++i) { |
| 114 | const double ticks_per_call = results[i].ticks / results[i].input; |
| 115 | const double ns_per_call = ns_per_tick * ticks_per_call; |
| 116 | const double bytes_per_ns = bytes / ns_per_call; |
| 117 | const double mb_per_s = bytes_per_ns * kNsPerS * kMBPerByte; |
| 118 | // Output |
| 119 | printf("%20s %8zu: %12.2f ticks; MAD=%4.2f%% (%6.1f ns) %8.1f Mb/s\n", |
| 120 | name, results[i].input, results[i].ticks, |
| 121 | results[i].variability * 100.0, ns_per_call, mb_per_s); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Fails here |
| 126 | template <typename Op, size_t N> |
| 127 | void Measure(const char* name, const FuncInput (&inputs)[N]) { |
| 128 | Op op; |
| 129 | |
| 130 | Result results[N]; |
| 131 | Params params; |
| 132 | params.verbose = false; |
| 133 | params.max_evals = 6; // avoid test timeout |
| 134 | const size_t num_results = MeasureClosure(op, inputs, N, results, params); |
| 135 | Print(name, num_results, results, op.bytes()); |
| 136 | } |
| 137 | |
| 138 | // unpredictable == 1 but the compiler does not know that. |
| 139 | void RunAll(const int argc, char* argv[]) { |
| 140 | if (argc == 2) { |
| 141 | int cpu = -1; |
| 142 | if (!absl::SimpleAtoi(argv[1], &cpu)) { |
| 143 | ABSL_RAW_LOG(FATAL, "The optional argument must be a CPU number >= 0.\n"); |
| 144 | } |
| 145 | PinThreadToCPU(cpu); |
| 146 | } |
| 147 | |
| 148 | // The compiler cannot reduce this to a constant. |
| 149 | const FuncInput unpredictable = (argc != 999); |
| 150 | static const FuncInput inputs[] = {unpredictable * 100, unpredictable * 1000}; |
| 151 | |
| 152 | #if !defined(ABSL_INTERNAL_DISABLE_AES) && ABSL_HAVE_ACCELERATED_AES |
| 153 | Measure<AbsorbFn<RandenHwAes>>("Absorb (HwAes)", inputs); |
| 154 | #endif |
| 155 | Measure<AbsorbFn<RandenSlow>>("Absorb (Slow)", inputs); |
| 156 | |
| 157 | #if !defined(ABSL_INTERNAL_DISABLE_AES) && ABSL_HAVE_ACCELERATED_AES |
| 158 | Measure<GenerateFn<RandenHwAes>>("Generate (HwAes)", inputs); |
| 159 | #endif |
| 160 | Measure<GenerateFn<RandenSlow>>("Generate (Slow)", inputs); |
| 161 | |
| 162 | // Measure the production engine. |
| 163 | static const FuncInput inputs1[] = {unpredictable * 1000, |
| 164 | unpredictable * 10000}; |
| 165 | Measure<Engine<uint64_t>>("randen_engine<uint64_t>", inputs1); |
| 166 | Measure<Engine<uint32_t>>("randen_engine<uint32_t>", inputs1); |
| 167 | } |
| 168 | |
| 169 | } // namespace |
| 170 | |
| 171 | int main(int argc, char* argv[]) { |
| 172 | RunAll(argc, argv); |
| 173 | return 0; |
| 174 | } |