1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
// Copyright 2019 The TCMalloc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tcmalloc/span.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <new>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/internal/spinlock.h"
#include "absl/base/optimization.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/random/random.h"
#include "absl/types/span.h"
#include "tcmalloc/common.h"
#include "tcmalloc/experiment.h"
#include "tcmalloc/experiment_config.h"
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/pages.h"
#include "tcmalloc/static_vars.h"
namespace tcmalloc {
namespace tcmalloc_internal {
namespace {
constexpr uint64_t kSpanAllocTime = 1234;
class RawSpan {
public:
void Init(size_t size_class, uint32_t max_cache_array_size,
uint32_t max_cache_size) {
size_t size = tc_globals.sizemap().class_to_size(size_class);
auto npages = Length(tc_globals.sizemap().class_to_pages(size_class));
size_t objects_per_span = npages.in_bytes() / size;
// Dynamically allocate so ASan can flag if we run out of bounds.
size_t span_size = Span::CalcSizeOf(max_cache_array_size);
buf_ = ::operator new(span_size, std::align_val_t(alignof(Span)));
int res = posix_memalign(&mem_, kPageSize, npages.in_bytes());
TC_CHECK_EQ(res, 0);
span_ = new (buf_) Span(Range(PageIdContaining(mem_), npages));
TC_CHECK_EQ(span_->BuildFreelist(size, objects_per_span, {}, max_cache_size,
kSpanAllocTime),
0);
}
~RawSpan() {
free(mem_);
::operator delete(buf_, std::align_val_t(alignof(Span)));
}
Span& span() { return *span_; }
private:
void* buf_ = nullptr;
void* mem_ = nullptr;
Span* span_;
};
class SpanTest : public testing::TestWithParam<std::tuple<size_t, size_t>> {
protected:
size_t size_class_;
size_t size_;
size_t npages_;
size_t batch_size_;
size_t objects_per_span_;
uint32_t reciprocal_;
uint32_t max_cache_size_;
uint32_t max_cache_array_size_;
RawSpan raw_span_;
private:
void SetUp() override {
size_class_ = std::get<0>(GetParam());
max_cache_array_size_ = std::get<1>(GetParam());
max_cache_size_ = max_cache_array_size_ == Span::kCacheSize
? Span::kCacheSize
: Span::kLargeCacheSize;
ASSERT_THAT(max_cache_array_size_,
testing::AnyOf(testing::Eq(Span::kCacheSize),
testing::Eq(Span::kLargeCacheArraySize)));
ASSERT_LE(max_cache_size_, max_cache_array_size_);
size_ = tc_globals.sizemap().class_to_size(size_class_);
if (size_ == 0) {
GTEST_SKIP() << "Skipping empty size class.";
}
npages_ = tc_globals.sizemap().class_to_pages(size_class_);
batch_size_ = tc_globals.sizemap().num_objects_to_move(size_class_);
objects_per_span_ = npages_ * kPageSize / size_;
reciprocal_ = Span::CalcReciprocal(size_);
raw_span_.Init(size_class_, max_cache_array_size_, max_cache_size_);
}
void TearDown() override {}
};
TEST_P(SpanTest, FreelistBasic) {
Span& span_ = raw_span_.span();
EXPECT_FALSE(span_.FreelistEmpty(size_));
void* batch[kMaxObjectsToMove];
size_t popped = 0;
size_t want = 1;
char* start = static_cast<char*>(span_.start_address());
std::vector<bool> objects(objects_per_span_);
for (size_t x = 0; x < 2; ++x) {
// Pop all objects in batches of varying size and ensure that we've got
// all objects.
for (;;) {
size_t n = span_.FreelistPopBatch(absl::MakeSpan(batch, want), size_);
popped += n;
EXPECT_NEAR(
span_.Fragmentation(size_),
static_cast<double>(objects_per_span_) / static_cast<double>(popped) -
1.,
1e-5);
EXPECT_EQ(span_.FreelistEmpty(size_), popped == objects_per_span_);
for (size_t i = 0; i < n; ++i) {
void* p = batch[i];
uintptr_t off = reinterpret_cast<char*>(p) - start;
EXPECT_LT(off, span_.bytes_in_span());
EXPECT_EQ(off % size_, 0);
size_t idx = off / size_;
EXPECT_FALSE(objects[idx]);
objects[idx] = true;
}
if (n < want) {
break;
}
++want;
if (want > batch_size_) {
want = 1;
}
}
EXPECT_TRUE(span_.FreelistEmpty(size_));
EXPECT_EQ(span_.FreelistPopBatch(absl::MakeSpan(batch, 1), size_), 0);
EXPECT_EQ(popped, objects_per_span_);
// Push all objects back except the last one (which would not be pushed).
for (size_t idx = 0; idx < objects_per_span_ - 1; ++idx) {
EXPECT_TRUE(objects[idx]);
bool ok = span_.FreelistPush(start + idx * size_, size_, reciprocal_,
max_cache_size_);
EXPECT_TRUE(ok);
EXPECT_FALSE(span_.FreelistEmpty(size_));
objects[idx] = false;
--popped;
}
// On the last iteration we can actually push the last object.
if (x == 1) {
bool ok = span_.FreelistPush(start + (objects_per_span_ - 1) * size_,
size_, reciprocal_, max_cache_size_);
EXPECT_FALSE(ok);
}
}
}
TEST_P(SpanTest, AllocTime) {
Span& span_ = raw_span_.span();
if (span_.UseBitmapForSize(size_) ||
max_cache_size_ != Span::kLargeCacheSize) {
EXPECT_EQ(span_.AllocTime(size_, max_cache_size_), 0);
} else {
EXPECT_EQ(span_.AllocTime(size_, max_cache_size_), kSpanAllocTime);
}
}
TEST_P(SpanTest, FreelistRandomized) {
Span& span_ = raw_span_.span();
char* start = static_cast<char*>(span_.start_address());
// Do a bunch of random pushes/pops with random batch size.
absl::BitGen rng;
absl::flat_hash_set<void*> objects;
void* batch[kMaxObjectsToMove];
for (size_t x = 0; x < 10000; ++x) {
if (!objects.empty() && absl::Bernoulli(rng, 1.0 / 2)) {
void* p = *objects.begin();
if (span_.FreelistPush(p, size_, reciprocal_, max_cache_size_)) {
objects.erase(objects.begin());
} else {
EXPECT_EQ(objects.size(), 1);
}
EXPECT_EQ(span_.FreelistEmpty(size_), objects_per_span_ == 1);
} else {
size_t want = absl::Uniform<int32_t>(rng, 0, batch_size_) + 1;
size_t n = span_.FreelistPopBatch(absl::MakeSpan(batch, want), size_);
if (n < want) {
EXPECT_TRUE(span_.FreelistEmpty(size_));
}
for (size_t i = 0; i < n; ++i) {
EXPECT_TRUE(objects.insert(batch[i]).second);
}
}
}
EXPECT_TRUE(span_.AllocTime(size_, max_cache_size_) == 0 ||
span_.AllocTime(size_, max_cache_size_) == kSpanAllocTime);
// Now pop everything what's there.
for (;;) {
size_t n =
span_.FreelistPopBatch(absl::MakeSpan(batch, batch_size_), size_);
for (size_t i = 0; i < n; ++i) {
EXPECT_TRUE(objects.insert(batch[i]).second);
}
if (n < batch_size_) {
break;
}
}
// Check that we have collected all objects.
EXPECT_EQ(objects.size(), objects_per_span_);
for (void* p : objects) {
uintptr_t off = reinterpret_cast<char*>(p) - start;
EXPECT_LT(off, span_.bytes_in_span());
EXPECT_EQ(off % size_, 0);
}
}
INSTANTIATE_TEST_SUITE_P(
All, SpanTest,
testing::Combine(testing::Range(size_t(1), kNumClasses),
testing::Values(Span::kCacheSize,
Span::kLargeCacheArraySize)));
TEST(SpanAllocatorTest, Alignment) {
Range r(PageId{1}, Length{2});
constexpr int kNumSpans = 1000;
std::vector<Span*> spans;
spans.reserve(kNumSpans);
{
#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING
PageHeapSpinLockHolder l;
#endif // TCMALLOC_INTERNAL_LEGACY_LOCKING
for (int i = 0; i < kNumSpans; ++i) {
spans.push_back(Span::New(r));
}
}
absl::flat_hash_map<uintptr_t, int> address_mod_cacheline;
for (Span* s : spans) {
++address_mod_cacheline[reinterpret_cast<uintptr_t>(s) %
ABSL_CACHELINE_SIZE];
}
// TODO(b/304135905): Remove the opt out.
if (tcmalloc_big_span()) {
EXPECT_EQ(address_mod_cacheline[0], kNumSpans);
} else {
EXPECT_LT(address_mod_cacheline[0], kNumSpans);
}
// Verify alignof is respected.
for (auto [alignment, count] : address_mod_cacheline) {
EXPECT_EQ(alignment % alignof(Span), 0);
}
{
#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING
PageHeapSpinLockHolder l;
#endif // TCMALLOC_INTERNAL_LEGACY_LOCKING
for (Span* s : spans) {
Span::Delete(s);
}
}
}
} // namespace
} // namespace tcmalloc_internal
} // namespace tcmalloc
|