llvm-project
135 строк · 5.1 Кб
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-exception
6//
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 required
20// alignment.
21// When alignment is set, the distribution is scaled down by `Factor` and scaled
22// up again by the same amount during sampling.
23static std::uniform_int_distribution<uint32_t>
24getOffsetDistribution(size_t BufferSize, size_t MaxSizeValue,
25MaybeAlign AccessAlignment) {
26if (AccessAlignment && *AccessAlignment > AlignedBuffer::Alignment)
27report_fatal_error(
28"AccessAlignment must be less or equal to AlignedBuffer::Alignment");
29if (!AccessAlignment)
30return std::uniform_int_distribution<uint32_t>(0, 0); // Always 0.
31// If we test up to Size bytes, the returned offset must stay under
32// BuffersSize - Size.
33int64_t MaxOffset = BufferSize;
34MaxOffset -= MaxSizeValue;
35MaxOffset -= 1;
36if (MaxOffset < 0)
37report_fatal_error(
38"BufferSize too small to exercise specified Size configuration");
39MaxOffset /= AccessAlignment->value();
40return std::uniform_int_distribution<uint32_t>(0, MaxOffset);
41}
42
43OffsetDistribution::OffsetDistribution(size_t BufferSize, size_t MaxSizeValue,
44MaybeAlign AccessAlignment)
45: Distribution(
46getOffsetDistribution(BufferSize, MaxSizeValue, AccessAlignment)),
47Factor(AccessAlignment.valueOrOne().value()) {}
48
49// Precomputes offset where to insert mismatches between the two buffers.
50MismatchOffsetDistribution::MismatchOffsetDistribution(size_t BufferSize,
51size_t MaxSizeValue,
52size_t MismatchAt)
53: MismatchAt(MismatchAt) {
54if (MismatchAt <= 1)
55return;
56for (size_t I = MaxSizeValue + 1; I < BufferSize; I += MaxSizeValue)
57MismatchIndices.push_back(I);
58if (MismatchIndices.empty())
59report_fatal_error("Unable to generate mismatch");
60MismatchIndexSelector =
61std::uniform_int_distribution<size_t>(0, MismatchIndices.size() - 1);
62}
63
64static size_t getL1DataCacheSize() {
65const std::vector<CacheInfo> &CacheInfos = HostState::get().Caches;
66const auto IsL1DataCache = [](const CacheInfo &CI) {
67return CI.Type == "Data" && CI.Level == 1;
68};
69const auto CacheIt = find_if(CacheInfos, IsL1DataCache);
70if (CacheIt != CacheInfos.end())
71return CacheIt->Size;
72report_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() {
80return getL1DataCacheSize() - L1LeftAsideBytes - ParameterStorageBytes;
81}
82
83ParameterBatch::ParameterBatch(size_t BufferCount)
84: BufferSize(getAvailableBufferSize() / BufferCount),
85BatchSize(ParameterStorageBytes / sizeof(ParameterType)),
86Parameters(BatchSize) {
87if (BufferSize <= 0 || BatchSize < 100)
88report_fatal_error("Not enough L1 cache");
89const size_t ParameterBytes = Parameters.size() * sizeof(ParameterType);
90const size_t BufferBytes = BufferSize * BufferCount;
91if (ParameterBytes + BufferBytes + L1LeftAsideBytes > getL1DataCacheSize())
92report_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 {
99size_t BatchBytes = 0;
100for (auto &P : Parameters)
101BatchBytes += P.SizeBytes;
102return BatchBytes;
103}
104
105void ParameterBatch::checkValid(const ParameterType &P) const {
106if (P.OffsetBytes + P.SizeBytes >= BufferSize)
107report_fatal_error(
108llvm::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),
118DstBuffer(ParameterBatch::BufferSize) {}
119
120MoveSetup::MoveSetup()
121: ParameterBatch(3), Buffer(ParameterBatch::BufferSize * 3) {}
122
123ComparisonSetup::ComparisonSetup()
124: ParameterBatch(2), LhsBuffer(ParameterBatch::BufferSize),
125RhsBuffer(ParameterBatch::BufferSize) {
126// The memcmp buffers always compare equal.
127memset(LhsBuffer.begin(), 0xF, BufferSize);
128memset(RhsBuffer.begin(), 0xF, BufferSize);
129}
130
131SetSetup::SetSetup()
132: ParameterBatch(1), DstBuffer(ParameterBatch::BufferSize) {}
133
134} // namespace libc_benchmarks
135} // namespace llvm
136