Paula Toth | 21b6e3d | 2020-04-08 17:16:30 | [diff] [blame] | 1 | //===-- Benchmark ---------------------------------------------------------===// |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 9 | #include "JSON.h" |
| 10 | #include "LibcBenchmark.h" |
| 11 | #include "LibcMemoryBenchmark.h" |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 12 | #include "MemorySizeDistributions.h" |
Petr Hosek | 75b31cf | 2024-07-12 16:28:41 | [diff] [blame] | 13 | #include "src/__support/macros/config.h" |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 14 | #include "llvm/Support/CommandLine.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
| 16 | #include "llvm/Support/FileSystem.h" |
| 17 | #include "llvm/Support/JSON.h" |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 18 | #include "llvm/Support/MathExtras.h" |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
Guillaume Chatelet | ba69466 | 2021-06-23 14:19:40 | [diff] [blame] | 22 | #include <cstring> |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 23 | #include <unistd.h> |
Guillaume Chatelet | ba69466 | 2021-06-23 14:19:40 | [diff] [blame] | 24 | |
Petr Hosek | 75b31cf | 2024-07-12 16:28:41 | [diff] [blame] | 25 | namespace LIBC_NAMESPACE_DECL { |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 26 | |
| 27 | extern void *memcpy(void *__restrict, const void *__restrict, size_t); |
Guillaume Chatelet | 9438e58 | 2021-11-30 10:46:16 | [diff] [blame] | 28 | extern void *memmove(void *, const void *, size_t); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 29 | extern void *memset(void *, int, size_t); |
Guillaume Chatelet | ba69466 | 2021-06-23 14:19:40 | [diff] [blame] | 30 | extern void bzero(void *, size_t); |
| 31 | extern int memcmp(const void *, const void *, size_t); |
Guillaume Chatelet | f839169 | 2021-08-19 17:55:16 | [diff] [blame] | 32 | extern int bcmp(const void *, const void *, size_t); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 33 | |
Petr Hosek | 75b31cf | 2024-07-12 16:28:41 | [diff] [blame] | 34 | } // namespace LIBC_NAMESPACE_DECL |
Siva Chandra Reddy | 4960d18 | 2020-04-28 21:00:12 | [diff] [blame] | 35 | |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 36 | namespace llvm { |
| 37 | namespace libc_benchmarks { |
| 38 | |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 39 | static cl::opt<std::string> |
| 40 | StudyName("study-name", cl::desc("The name for this study"), cl::Required); |
| 41 | |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 42 | static cl::opt<std::string> |
| 43 | SizeDistributionName("size-distribution-name", |
| 44 | cl::desc("The name of the distribution to use")); |
| 45 | |
Dmitry Vyukov | 62bfa2a | 2023-10-26 09:06:15 | [diff] [blame] | 46 | static cl::opt<bool> SweepMode( |
| 47 | "sweep-mode", |
| 48 | cl::desc( |
| 49 | "If set, benchmark all sizes from sweep-min-size to sweep-max-size")); |
| 50 | |
| 51 | static cl::opt<uint32_t> |
| 52 | SweepMinSize("sweep-min-size", |
| 53 | cl::desc("The minimum size to use in sweep-mode"), |
| 54 | cl::init(0)); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 55 | |
| 56 | static cl::opt<uint32_t> |
| 57 | SweepMaxSize("sweep-max-size", |
| 58 | cl::desc("The maximum size to use in sweep-mode"), |
| 59 | cl::init(256)); |
| 60 | |
| 61 | static cl::opt<uint32_t> |
| 62 | AlignedAccess("aligned-access", |
| 63 | cl::desc("The alignment to use when accessing the buffers\n" |
| 64 | "Default is unaligned\n" |
| 65 | "Use 0 to disable address randomization"), |
| 66 | cl::init(1)); |
| 67 | |
| 68 | static cl::opt<std::string> Output("output", |
| 69 | cl::desc("Specify output filename"), |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 70 | cl::value_desc("filename"), cl::init("-")); |
| 71 | |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 72 | static cl::opt<uint32_t> |
| 73 | NumTrials("num-trials", cl::desc("The number of benchmarks run to perform"), |
| 74 | cl::init(1)); |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 75 | |
Guillaume Chatelet | 5705910 | 2021-06-10 13:04:56 | [diff] [blame] | 76 | #if defined(LIBC_BENCHMARK_FUNCTION_MEMCPY) |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 77 | #define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_MEMCPY |
Andre Vieira | 62725b4 | 2021-08-04 08:17:12 | [diff] [blame] | 78 | using BenchmarkSetup = CopySetup; |
Guillaume Chatelet | 9438e58 | 2021-11-30 10:46:16 | [diff] [blame] | 79 | #elif defined(LIBC_BENCHMARK_FUNCTION_MEMMOVE) |
| 80 | #define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_MEMMOVE |
| 81 | using BenchmarkSetup = MoveSetup; |
Guillaume Chatelet | 5705910 | 2021-06-10 13:04:56 | [diff] [blame] | 82 | #elif defined(LIBC_BENCHMARK_FUNCTION_MEMSET) |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 83 | #define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_MEMSET |
Andre Vieira | 62725b4 | 2021-08-04 08:17:12 | [diff] [blame] | 84 | using BenchmarkSetup = SetSetup; |
Guillaume Chatelet | ba69466 | 2021-06-23 14:19:40 | [diff] [blame] | 85 | #elif defined(LIBC_BENCHMARK_FUNCTION_BZERO) |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 86 | #define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_BZERO |
Andre Vieira | 62725b4 | 2021-08-04 08:17:12 | [diff] [blame] | 87 | using BenchmarkSetup = SetSetup; |
Guillaume Chatelet | ba69466 | 2021-06-23 14:19:40 | [diff] [blame] | 88 | #elif defined(LIBC_BENCHMARK_FUNCTION_MEMCMP) |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 89 | #define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_MEMCMP |
Andre Vieira | 62725b4 | 2021-08-04 08:17:12 | [diff] [blame] | 90 | using BenchmarkSetup = ComparisonSetup; |
Guillaume Chatelet | f839169 | 2021-08-19 17:55:16 | [diff] [blame] | 91 | #elif defined(LIBC_BENCHMARK_FUNCTION_BCMP) |
| 92 | #define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_BCMP |
| 93 | using BenchmarkSetup = ComparisonSetup; |
Guillaume Chatelet | 5705910 | 2021-06-10 13:04:56 | [diff] [blame] | 94 | #else |
| 95 | #error "Missing LIBC_BENCHMARK_FUNCTION_XXX definition" |
| 96 | #endif |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 97 | |
Andre Vieira | 62725b4 | 2021-08-04 08:17:12 | [diff] [blame] | 98 | struct MemfunctionBenchmarkBase : public BenchmarkSetup { |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 99 | MemfunctionBenchmarkBase() : ReportProgress(isatty(fileno(stdout))) {} |
| 100 | virtual ~MemfunctionBenchmarkBase() {} |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 101 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 102 | virtual Study run() = 0; |
| 103 | |
| 104 | CircularArrayRef<ParameterBatch::ParameterType> |
| 105 | generateBatch(size_t Iterations) { |
| 106 | randomize(); |
serge-sans-paille | 062579a | 2023-01-09 17:11:07 | [diff] [blame] | 107 | return cycle(ArrayRef(Parameters), Iterations); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 108 | } |
| 109 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 110 | protected: |
| 111 | Study createStudy() { |
| 112 | Study Study; |
Andre Vieira | 62725b4 | 2021-08-04 08:17:12 | [diff] [blame] | 113 | // Setup study. |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 114 | Study.StudyName = StudyName; |
| 115 | Runtime &RI = Study.Runtime; |
| 116 | RI.Host = HostState::get(); |
| 117 | RI.BufferSize = BufferSize; |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 118 | RI.BatchParameterCount = BatchSize; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 119 | |
| 120 | BenchmarkOptions &BO = RI.BenchmarkOptions; |
| 121 | BO.MinDuration = std::chrono::milliseconds(1); |
| 122 | BO.MaxDuration = std::chrono::seconds(1); |
| 123 | BO.MaxIterations = 10'000'000U; |
| 124 | BO.MinSamples = 4; |
| 125 | BO.MaxSamples = 1000; |
| 126 | BO.Epsilon = 0.01; // 1% |
| 127 | BO.ScalingFactor = 1.4; |
| 128 | |
| 129 | StudyConfiguration &SC = Study.Configuration; |
| 130 | SC.NumTrials = NumTrials; |
| 131 | SC.IsSweepMode = SweepMode; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 132 | SC.AccessAlignment = MaybeAlign(AlignedAccess); |
Guillaume Chatelet | 5705910 | 2021-06-10 13:04:56 | [diff] [blame] | 133 | SC.Function = LIBC_BENCHMARK_FUNCTION_NAME; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 134 | return Study; |
| 135 | } |
| 136 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 137 | void runTrials(const BenchmarkOptions &Options, |
| 138 | std::vector<Duration> &Measurements) { |
| 139 | for (size_t i = 0; i < NumTrials; ++i) { |
| 140 | const BenchmarkResult Result = benchmark( |
| 141 | Options, *this, [this](ParameterBatch::ParameterType Parameter) { |
| 142 | return Call(Parameter, LIBC_BENCHMARK_FUNCTION); |
| 143 | }); |
| 144 | Measurements.push_back(Result.BestGuess); |
| 145 | reportProgress(Measurements); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | virtual void randomize() = 0; |
| 150 | |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 151 | private: |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 152 | bool ReportProgress; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 153 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 154 | void reportProgress(const std::vector<Duration> &Measurements) { |
| 155 | if (!ReportProgress) |
| 156 | return; |
Guillaume Chatelet | 4ebed71 | 2021-01-13 14:06:36 | [diff] [blame] | 157 | static size_t LastPercent = -1; |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 158 | const size_t TotalSteps = Measurements.capacity(); |
| 159 | const size_t Steps = Measurements.size(); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 160 | const size_t Percent = 100 * Steps / TotalSteps; |
Guillaume Chatelet | 4ebed71 | 2021-01-13 14:06:36 | [diff] [blame] | 161 | if (Percent == LastPercent) |
| 162 | return; |
| 163 | LastPercent = Percent; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 164 | size_t I = 0; |
| 165 | errs() << '['; |
| 166 | for (; I <= Percent; ++I) |
| 167 | errs() << '#'; |
| 168 | for (; I <= 100; ++I) |
| 169 | errs() << '_'; |
Guillaume Chatelet | 4ebed71 | 2021-01-13 14:06:36 | [diff] [blame] | 170 | errs() << "] " << Percent << '%' << '\r'; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 171 | } |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 172 | }; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 173 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 174 | struct MemfunctionBenchmarkSweep final : public MemfunctionBenchmarkBase { |
| 175 | MemfunctionBenchmarkSweep() |
| 176 | : OffsetSampler(MemfunctionBenchmarkBase::BufferSize, SweepMaxSize, |
| 177 | MaybeAlign(AlignedAccess)) {} |
| 178 | |
| 179 | virtual void randomize() override { |
| 180 | for (auto &P : Parameters) { |
| 181 | P.OffsetBytes = OffsetSampler(Gen); |
| 182 | P.SizeBytes = CurrentSweepSize; |
| 183 | checkValid(P); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 187 | virtual Study run() override { |
| 188 | Study Study = createStudy(); |
| 189 | Study.Configuration.SweepModeMaxSize = SweepMaxSize; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 190 | BenchmarkOptions &BO = Study.Runtime.BenchmarkOptions; |
| 191 | BO.MinDuration = std::chrono::milliseconds(1); |
| 192 | BO.InitialIterations = 100; |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 193 | auto &Measurements = Study.Measurements; |
| 194 | Measurements.reserve(NumTrials * SweepMaxSize); |
Dmitry Vyukov | 62bfa2a | 2023-10-26 09:06:15 | [diff] [blame] | 195 | for (size_t Size = SweepMinSize; Size <= SweepMaxSize; ++Size) { |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 196 | CurrentSweepSize = Size; |
| 197 | runTrials(BO, Measurements); |
| 198 | } |
| 199 | return Study; |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | size_t CurrentSweepSize = 0; |
| 204 | OffsetDistribution OffsetSampler; |
| 205 | std::mt19937_64 Gen; |
| 206 | }; |
| 207 | |
| 208 | struct MemfunctionBenchmarkDistribution final |
| 209 | : public MemfunctionBenchmarkBase { |
| 210 | MemfunctionBenchmarkDistribution(MemorySizeDistribution Distribution) |
| 211 | : Distribution(Distribution), Probabilities(Distribution.Probabilities), |
| 212 | SizeSampler(Probabilities.begin(), Probabilities.end()), |
| 213 | OffsetSampler(MemfunctionBenchmarkBase::BufferSize, |
| 214 | Probabilities.size() - 1, MaybeAlign(AlignedAccess)) {} |
| 215 | |
| 216 | virtual void randomize() override { |
| 217 | for (auto &P : Parameters) { |
| 218 | P.OffsetBytes = OffsetSampler(Gen); |
| 219 | P.SizeBytes = SizeSampler(Gen); |
| 220 | checkValid(P); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 224 | virtual Study run() override { |
| 225 | Study Study = createStudy(); |
| 226 | Study.Configuration.SizeDistributionName = Distribution.Name.str(); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 227 | BenchmarkOptions &BO = Study.Runtime.BenchmarkOptions; |
| 228 | BO.MinDuration = std::chrono::milliseconds(10); |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 229 | BO.InitialIterations = BatchSize * 10; |
| 230 | auto &Measurements = Study.Measurements; |
| 231 | Measurements.reserve(NumTrials); |
| 232 | runTrials(BO, Measurements); |
| 233 | return Study; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 234 | } |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 235 | |
| 236 | private: |
| 237 | MemorySizeDistribution Distribution; |
| 238 | ArrayRef<double> Probabilities; |
| 239 | std::discrete_distribution<unsigned> SizeSampler; |
| 240 | OffsetDistribution OffsetSampler; |
| 241 | std::mt19937_64 Gen; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 242 | }; |
| 243 | |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 244 | void writeStudy(const Study &S) { |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 245 | std::error_code EC; |
| 246 | raw_fd_ostream FOS(Output, EC); |
| 247 | if (EC) |
| 248 | report_fatal_error(Twine("Could not open file: ") |
| 249 | .concat(EC.message()) |
| 250 | .concat(", ") |
| 251 | .concat(Output)); |
| 252 | json::OStream JOS(FOS); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 253 | serializeToJson(S, JOS); |
Guillaume Chatelet | 4ebed71 | 2021-01-13 14:06:36 | [diff] [blame] | 254 | FOS << "\n"; |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void main() { |
| 258 | checkRequirements(); |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 259 | if (!isPowerOf2_32(AlignedAccess)) |
| 260 | report_fatal_error(AlignedAccess.ArgStr + |
| 261 | Twine(" must be a power of two or zero")); |
| 262 | |
| 263 | const bool HasDistributionName = !SizeDistributionName.empty(); |
| 264 | if (SweepMode && HasDistributionName) |
| 265 | report_fatal_error("Select only one of `--" + Twine(SweepMode.ArgStr) + |
| 266 | "` or `--" + Twine(SizeDistributionName.ArgStr) + "`"); |
| 267 | |
| 268 | std::unique_ptr<MemfunctionBenchmarkBase> Benchmark; |
| 269 | if (SweepMode) |
| 270 | Benchmark.reset(new MemfunctionBenchmarkSweep()); |
| 271 | else |
| 272 | Benchmark.reset(new MemfunctionBenchmarkDistribution(getDistributionOrDie( |
Andre Vieira | 62725b4 | 2021-08-04 08:17:12 | [diff] [blame] | 273 | BenchmarkSetup::getDistributions(), SizeDistributionName))); |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 274 | writeStudy(Benchmark->run()); |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | } // namespace libc_benchmarks |
| 278 | } // namespace llvm |
| 279 | |
Guillaume Chatelet | 6a9cfe3 | 2021-07-28 14:52:29 | [diff] [blame] | 280 | #ifndef NDEBUG |
| 281 | #error For reproducibility benchmarks should not be compiled in DEBUG mode. |
| 282 | #endif |
| 283 | |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 284 | int main(int argc, char **argv) { |
| 285 | llvm::cl::ParseCommandLineOptions(argc, argv); |
Guillaume Chatelet | d791c7d | 2020-12-17 13:16:14 | [diff] [blame] | 286 | llvm::libc_benchmarks::main(); |
Guillaume Chatelet | 2179e25 | 2020-01-06 12:17:04 | [diff] [blame] | 287 | return EXIT_SUCCESS; |
| 288 | } |