brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.1 KiB · 9307ee4 Raw
136 lines · cpp
1//===-- Benchmark memory specific tools -----------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "LibcMemoryBenchmark.h"10#include "llvm/ADT/SmallVector.h"11#include "llvm/ADT/Twine.h"12#include "llvm/Support/ErrorHandling.h"13#include "llvm/Support/MathExtras.h"14#include <algorithm>15 16namespace llvm {17namespace libc_benchmarks {18 19// Returns a distribution that samples the buffer to satisfy the required20// alignment.21// When alignment is set, the distribution is scaled down by `Factor` and scaled22// up again by the same amount during sampling.23static std::uniform_int_distribution<uint32_t>24getOffsetDistribution(size_t BufferSize, size_t MaxSizeValue,25                      MaybeAlign AccessAlignment) {26  if (AccessAlignment && *AccessAlignment > AlignedBuffer::Alignment)27    report_fatal_error(28        "AccessAlignment must be less or equal to AlignedBuffer::Alignment");29  if (!AccessAlignment)30    return std::uniform_int_distribution<uint32_t>(0, 0); // Always 0.31  // If we test up to Size bytes, the returned offset must stay under32  // BuffersSize - Size.33  int64_t MaxOffset = BufferSize;34  MaxOffset -= MaxSizeValue;35  MaxOffset -= 1;36  if (MaxOffset < 0)37    report_fatal_error(38        "BufferSize too small to exercise specified Size configuration");39  MaxOffset /= AccessAlignment->value();40  return std::uniform_int_distribution<uint32_t>(0, MaxOffset);41}42 43OffsetDistribution::OffsetDistribution(size_t BufferSize, size_t MaxSizeValue,44                                       MaybeAlign AccessAlignment)45    : Distribution(46          getOffsetDistribution(BufferSize, MaxSizeValue, AccessAlignment)),47      Factor(AccessAlignment.valueOrOne().value()) {}48 49// Precomputes offset where to insert mismatches between the two buffers.50MismatchOffsetDistribution::MismatchOffsetDistribution(size_t BufferSize,51                                                       size_t MaxSizeValue,52                                                       size_t MismatchAt)53    : MismatchAt(MismatchAt) {54  if (MismatchAt <= 1)55    return;56  for (size_t i = MaxSizeValue + 1; i < BufferSize; i += MaxSizeValue)57    MismatchIndices.push_back(i);58  if (MismatchIndices.empty())59    report_fatal_error("Unable to generate mismatch");60  MismatchIndexSelector =61      std::uniform_int_distribution<size_t>(0, MismatchIndices.size() - 1);62}63 64static size_t getL1DataCacheSize() {65  const std::vector<CacheInfo> &CacheInfos = HostState::get().Caches;66  const auto IsL1DataCache = [](const CacheInfo &CI) {67    return CI.Type == "Data" && CI.Level == 1;68  };69  const auto CacheIt = find_if(CacheInfos, IsL1DataCache);70  if (CacheIt != CacheInfos.end())71    return CacheIt->Size;72  report_fatal_error("Unable to read L1 Cache Data Size");73}74 75static constexpr int64_t KiB = 1024;76static constexpr int64_t ParameterStorageBytes = 4 * KiB;77static constexpr int64_t L1LeftAsideBytes = 1 * KiB;78 79static size_t getAvailableBufferSize() {80  return getL1DataCacheSize() - L1LeftAsideBytes - ParameterStorageBytes;81}82 83ParameterBatch::ParameterBatch(size_t BufferCount)84    : BufferSize(getAvailableBufferSize() / BufferCount),85      BatchSize(ParameterStorageBytes / sizeof(ParameterType)),86      Parameters(BatchSize) {87  if (BufferSize <= 0 || BatchSize < 100)88    report_fatal_error("Not enough L1 cache");89  const size_t ParameterBytes = Parameters.size() * sizeof(ParameterType);90  const size_t BufferBytes = BufferSize * BufferCount;91  if (ParameterBytes + BufferBytes + L1LeftAsideBytes > getL1DataCacheSize())92    report_fatal_error(93        "We're splitting a buffer of the size of the L1 cache between a data "94        "buffer and a benchmark parameters buffer, so by construction the "95        "total should not exceed the size of the L1 cache");96}97 98size_t ParameterBatch::getBatchBytes() const {99  size_t BatchBytes = 0;100  for (auto &P : Parameters)101    BatchBytes += P.SizeBytes;102  return BatchBytes;103}104 105void ParameterBatch::checkValid(const ParameterType &P) const {106  if (P.OffsetBytes + P.SizeBytes >= BufferSize)107    report_fatal_error(108        llvm::Twine("Call would result in buffer overflow: Offset=")109            .concat(llvm::Twine(P.OffsetBytes))110            .concat(", Size=")111            .concat(llvm::Twine(P.SizeBytes))112            .concat(", BufferSize=")113            .concat(llvm::Twine(BufferSize)));114}115 116CopySetup::CopySetup()117    : ParameterBatch(2), SrcBuffer(ParameterBatch::BufferSize),118      DstBuffer(ParameterBatch::BufferSize) {}119 120MoveSetup::MoveSetup()121    : ParameterBatch(3), Buffer(ParameterBatch::BufferSize * 3) {}122 123ComparisonSetup::ComparisonSetup()124    : ParameterBatch(2), LhsBuffer(ParameterBatch::BufferSize),125      RhsBuffer(ParameterBatch::BufferSize) {126  // The memcmp buffers always compare equal.127  memset(LhsBuffer.begin(), 0xF, BufferSize);128  memset(RhsBuffer.begin(), 0xF, BufferSize);129}130 131SetSetup::SetSetup()132    : ParameterBatch(1), DstBuffer(ParameterBatch::BufferSize) {}133 134} // namespace libc_benchmarks135} // namespace llvm136