2021-08-02 20:14:11 +08:00
|
|
|
#include "LibcBenchmark.h"
|
|
|
|
#include "LibcMemoryBenchmark.h"
|
|
|
|
#include "MemorySizeDistributions.h"
|
|
|
|
#include "benchmark/benchmark.h"
|
2021-08-03 18:42:28 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include <chrono>
|
2021-08-02 20:14:11 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <random>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-08-03 18:42:28 +08:00
|
|
|
using llvm::Align;
|
|
|
|
using llvm::ArrayRef;
|
|
|
|
using llvm::Twine;
|
|
|
|
using llvm::libc_benchmarks::BzeroConfiguration;
|
|
|
|
using llvm::libc_benchmarks::ComparisonSetup;
|
|
|
|
using llvm::libc_benchmarks::CopySetup;
|
2021-10-15 17:26:12 +08:00
|
|
|
using llvm::libc_benchmarks::MemcmpOrBcmpConfiguration;
|
2021-08-03 18:42:28 +08:00
|
|
|
using llvm::libc_benchmarks::MemcpyConfiguration;
|
2021-11-30 18:46:16 +08:00
|
|
|
using llvm::libc_benchmarks::MemmoveConfiguration;
|
2021-08-03 18:42:28 +08:00
|
|
|
using llvm::libc_benchmarks::MemorySizeDistribution;
|
|
|
|
using llvm::libc_benchmarks::MemsetConfiguration;
|
2021-11-30 18:46:16 +08:00
|
|
|
using llvm::libc_benchmarks::MoveSetup;
|
2021-08-03 18:42:28 +08:00
|
|
|
using llvm::libc_benchmarks::OffsetDistribution;
|
|
|
|
using llvm::libc_benchmarks::SetSetup;
|
|
|
|
|
|
|
|
// Alignment to use for when accessing the buffers.
|
2021-08-02 20:14:11 +08:00
|
|
|
static constexpr Align kBenchmarkAlignment = Align::Constant<1>();
|
|
|
|
|
2021-08-03 18:42:28 +08:00
|
|
|
static std::mt19937_64 &getGenerator() {
|
|
|
|
static std::mt19937_64 Generator(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch().count());
|
|
|
|
return Generator;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SetupType, typename ConfigurationType> struct Runner {
|
|
|
|
Runner(benchmark::State &S, llvm::ArrayRef<ConfigurationType> Configurations)
|
|
|
|
: State(S), Distribution(SetupType::getDistributions()[State.range(0)]),
|
2021-08-02 20:14:11 +08:00
|
|
|
Probabilities(Distribution.Probabilities),
|
|
|
|
SizeSampler(Probabilities.begin(), Probabilities.end()),
|
2021-08-03 18:42:28 +08:00
|
|
|
OffsetSampler(Setup.BufferSize, Probabilities.size() - 1,
|
|
|
|
kBenchmarkAlignment),
|
|
|
|
Configuration(Configurations[State.range(1)]) {
|
|
|
|
for (auto &P : Setup.Parameters) {
|
|
|
|
P.OffsetBytes = OffsetSampler(getGenerator());
|
|
|
|
P.SizeBytes = SizeSampler(getGenerator());
|
|
|
|
Setup.checkValid(P);
|
2021-08-02 20:14:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:42:28 +08:00
|
|
|
~Runner() {
|
2022-12-01 18:19:16 +08:00
|
|
|
const size_t TotalBytes =
|
|
|
|
(State.iterations() * Setup.getBatchBytes()) / Setup.BatchSize;
|
2021-08-02 20:14:11 +08:00
|
|
|
State.SetBytesProcessed(TotalBytes);
|
2021-08-03 18:42:28 +08:00
|
|
|
State.SetItemsProcessed(State.iterations());
|
|
|
|
State.SetLabel((Twine(Configuration.Name) + "," + Distribution.Name).str());
|
2021-08-02 20:14:11 +08:00
|
|
|
State.counters["bytes_per_cycle"] = benchmark::Counter(
|
|
|
|
TotalBytes / benchmark::CPUInfo::Get().cycles_per_second,
|
|
|
|
benchmark::Counter::kIsRate);
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:42:28 +08:00
|
|
|
inline void runBatch() {
|
|
|
|
for (const auto &P : Setup.Parameters)
|
|
|
|
benchmark::DoNotOptimize(Setup.Call(P, Configuration.Function));
|
2021-08-02 20:14:11 +08:00
|
|
|
}
|
|
|
|
|
2021-08-03 18:42:28 +08:00
|
|
|
size_t getBatchSize() const { return Setup.BatchSize; }
|
|
|
|
|
2021-08-02 20:14:11 +08:00
|
|
|
private:
|
2021-08-03 18:42:28 +08:00
|
|
|
SetupType Setup;
|
2021-08-02 20:14:11 +08:00
|
|
|
benchmark::State &State;
|
|
|
|
MemorySizeDistribution Distribution;
|
|
|
|
ArrayRef<double> Probabilities;
|
|
|
|
std::discrete_distribution<unsigned> SizeSampler;
|
|
|
|
OffsetDistribution OffsetSampler;
|
2021-08-03 18:42:28 +08:00
|
|
|
ConfigurationType Configuration;
|
2021-08-02 20:14:11 +08:00
|
|
|
};
|
|
|
|
|
2021-08-03 18:42:28 +08:00
|
|
|
#define BENCHMARK_MEMORY_FUNCTION(BM_NAME, SETUP, CONFIGURATION_TYPE, \
|
|
|
|
CONFIGURATION_ARRAY_REF) \
|
|
|
|
void BM_NAME(benchmark::State &State) { \
|
|
|
|
Runner<SETUP, CONFIGURATION_TYPE> Setup(State, CONFIGURATION_ARRAY_REF); \
|
|
|
|
const size_t BatchSize = Setup.getBatchSize(); \
|
|
|
|
while (State.KeepRunningBatch(BatchSize)) \
|
|
|
|
Setup.runBatch(); \
|
|
|
|
} \
|
|
|
|
BENCHMARK(BM_NAME)->Apply([](benchmark::internal::Benchmark *benchmark) { \
|
|
|
|
const int64_t DistributionSize = SETUP::getDistributions().size(); \
|
|
|
|
const int64_t ConfigurationSize = CONFIGURATION_ARRAY_REF.size(); \
|
|
|
|
for (int64_t DistIndex = 0; DistIndex < DistributionSize; ++DistIndex) \
|
|
|
|
for (int64_t ConfIndex = 0; ConfIndex < ConfigurationSize; ++ConfIndex) \
|
|
|
|
benchmark->Args({DistIndex, ConfIndex}); \
|
|
|
|
})
|
2021-08-02 20:14:11 +08:00
|
|
|
|
2021-09-30 21:10:45 +08:00
|
|
|
extern llvm::ArrayRef<MemcpyConfiguration> getMemcpyConfigurations();
|
2021-08-03 18:42:28 +08:00
|
|
|
BENCHMARK_MEMORY_FUNCTION(BM_Memcpy, CopySetup, MemcpyConfiguration,
|
2021-09-30 21:10:45 +08:00
|
|
|
getMemcpyConfigurations());
|
|
|
|
|
2021-11-30 18:46:16 +08:00
|
|
|
extern llvm::ArrayRef<MemmoveConfiguration> getMemmoveConfigurations();
|
|
|
|
BENCHMARK_MEMORY_FUNCTION(BM_Memmove, MoveSetup, MemmoveConfiguration,
|
|
|
|
getMemmoveConfigurations());
|
|
|
|
|
2021-10-15 17:26:12 +08:00
|
|
|
extern llvm::ArrayRef<MemcmpOrBcmpConfiguration> getMemcmpConfigurations();
|
|
|
|
BENCHMARK_MEMORY_FUNCTION(BM_Memcmp, ComparisonSetup, MemcmpOrBcmpConfiguration,
|
2021-09-30 21:10:45 +08:00
|
|
|
getMemcmpConfigurations());
|
|
|
|
|
2021-10-15 17:26:12 +08:00
|
|
|
extern llvm::ArrayRef<MemcmpOrBcmpConfiguration> getBcmpConfigurations();
|
|
|
|
BENCHMARK_MEMORY_FUNCTION(BM_Bcmp, ComparisonSetup, MemcmpOrBcmpConfiguration,
|
2021-09-30 21:10:45 +08:00
|
|
|
getBcmpConfigurations());
|
|
|
|
|
|
|
|
extern llvm::ArrayRef<MemsetConfiguration> getMemsetConfigurations();
|
2021-08-03 18:42:28 +08:00
|
|
|
BENCHMARK_MEMORY_FUNCTION(BM_Memset, SetSetup, MemsetConfiguration,
|
2021-09-30 21:10:45 +08:00
|
|
|
getMemsetConfigurations());
|
|
|
|
|
|
|
|
extern llvm::ArrayRef<BzeroConfiguration> getBzeroConfigurations();
|
2021-08-03 18:42:28 +08:00
|
|
|
BENCHMARK_MEMORY_FUNCTION(BM_Bzero, SetSetup, BzeroConfiguration,
|
2021-09-30 21:10:45 +08:00
|
|
|
getBzeroConfigurations());
|