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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://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.
*/
/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#include <seastar/testing/test_case.hh>
#include <seastar/core/memory.hh>
#include <seastar/core/smp.hh>
#include <seastar/core/temporary_buffer.hh>
#include <seastar/util/memory_diagnostics.hh>
#include <seastar/util/log.hh>
#include <memory>
#include <new>
#include <vector>
#include <future>
#include <iostream>
#include <malloc.h>
using namespace seastar;
SEASTAR_TEST_CASE(alloc_almost_all_and_realloc_it_with_a_smaller_size) {
#ifndef SEASTAR_DEFAULT_ALLOCATOR
auto all = memory::stats().total_memory();
auto reserve = size_t(0.02 * all);
auto to_alloc = all - (reserve + (10 << 20));
auto orig_to_alloc = to_alloc;
auto obj = malloc(to_alloc);
while (!obj) {
to_alloc *= 0.9;
obj = malloc(to_alloc);
}
BOOST_REQUIRE(to_alloc > orig_to_alloc / 4);
BOOST_REQUIRE(obj != nullptr);
auto obj2 = realloc(obj, to_alloc - (1 << 20));
BOOST_REQUIRE(obj == obj2);
free(obj2);
#endif
return make_ready_future<>();
}
SEASTAR_TEST_CASE(malloc_0_and_free_it) {
#ifndef SEASTAR_DEFAULT_ALLOCATOR
auto obj = malloc(0);
BOOST_REQUIRE(obj != nullptr);
free(obj);
#endif
return make_ready_future<>();
}
SEASTAR_TEST_CASE(test_live_objects_counter_with_cross_cpu_free) {
return smp::submit_to(1, [] {
auto ret = std::vector<std::unique_ptr<bool>>(1000000);
for (auto& o : ret) {
o = std::make_unique<bool>(false);
}
return ret;
}).then([] (auto&& vec) {
vec.clear(); // cause cross-cpu free
BOOST_REQUIRE(memory::stats().live_objects() < std::numeric_limits<size_t>::max() / 2);
});
}
SEASTAR_TEST_CASE(test_aligned_alloc) {
for (size_t align = sizeof(void*); align <= 65536; align <<= 1) {
for (size_t size = align; size <= align * 2; size <<= 1) {
void *p = aligned_alloc(align, size);
BOOST_REQUIRE(p != nullptr);
BOOST_REQUIRE((reinterpret_cast<uintptr_t>(p) % align) == 0);
::memset(p, 0, size);
free(p);
}
}
return make_ready_future<>();
}
SEASTAR_TEST_CASE(test_temporary_buffer_aligned) {
for (size_t align = sizeof(void*); align <= 65536; align <<= 1) {
for (size_t size = align; size <= align * 2; size <<= 1) {
auto buf = temporary_buffer<char>::aligned(align, size);
void *p = buf.get_write();
BOOST_REQUIRE(p != nullptr);
BOOST_REQUIRE((reinterpret_cast<uintptr_t>(p) % align) == 0);
::memset(p, 0, size);
}
}
return make_ready_future<>();
}
SEASTAR_TEST_CASE(test_memory_diagnostics) {
auto report = memory::generate_memory_diagnostics_report();
#ifdef SEASTAR_DEFAULT_ALLOCATOR
BOOST_REQUIRE(report.length() == 0); // empty report with default allocator
#else
// since the output format is unstructured text, not much
// to do except test that we get a non-empty string
BOOST_REQUIRE(report.length() > 0);
// useful while debugging diagnostics
// fmt::print("--------------------\n{}--------------------", report);
#endif
return make_ready_future<>();
}
#ifndef SEASTAR_DEFAULT_ALLOCATOR
struct thread_alloc_info {
memory::statistics before;
memory::statistics after;
void *ptr;
};
template <typename Func>
thread_alloc_info run_with_stats(Func&& f) {
return std::async([&f](){
auto before = seastar::memory::stats();
void* ptr = f();
auto after = seastar::memory::stats();
return thread_alloc_info{before, after, ptr};
}).get();
}
template <typename Func>
void test_allocation_function(Func f) {
// alien alloc and free
auto alloc_info = run_with_stats(f);
auto free_info = std::async([p = alloc_info.ptr]() {
auto before = seastar::memory::stats();
free(p);
auto after = seastar::memory::stats();
return thread_alloc_info{before, after, nullptr};
}).get();
// there were mallocs
BOOST_REQUIRE(alloc_info.after.foreign_mallocs() - alloc_info.before.foreign_mallocs() > 0);
// mallocs balanced with frees
BOOST_REQUIRE(alloc_info.after.foreign_mallocs() - alloc_info.before.foreign_mallocs() == free_info.after.foreign_frees() - free_info.before.foreign_frees());
// alien alloc reactor free
auto info = run_with_stats(f);
auto before_cross_frees = memory::stats().foreign_cross_frees();
free(info.ptr);
BOOST_REQUIRE(memory::stats().foreign_cross_frees() - before_cross_frees == 1);
// reactor alloc, alien free
void *p = f();
auto alien_cross_frees = std::async([p]() {
auto frees_before = memory::stats().cross_cpu_frees();
free(p);
return memory::stats().cross_cpu_frees()-frees_before;
}).get();
BOOST_REQUIRE(alien_cross_frees == 1);
}
SEASTAR_TEST_CASE(test_foreign_function_use_glibc_malloc) {
test_allocation_function([]() ->void * { return malloc(1); });
test_allocation_function([]() { return realloc(NULL, 10); });
test_allocation_function([]() {
auto p = malloc(1);
return realloc(p, 1000);
});
test_allocation_function([]() { return aligned_alloc(4, 1024); });
return make_ready_future<>();
}
// So the compiler won't optimize the call to realloc(nullptr, size)
// and call malloc directly.
void* test_nullptr = nullptr;
SEASTAR_TEST_CASE(test_realloc_nullptr) {
auto p0 = realloc(test_nullptr, 8);
BOOST_REQUIRE(p0 != nullptr);
BOOST_REQUIRE_EQUAL(realloc(p0, 0), nullptr);
p0 = realloc(test_nullptr, 0);
BOOST_REQUIRE(p0 != nullptr);
auto p1 = malloc(0);
BOOST_REQUIRE(p1 != nullptr);
free(p0);
free(p1);
return make_ready_future<>();
}
void * volatile sink;
SEASTAR_TEST_CASE(test_bad_alloc_throws) {
// test that a large allocation throws bad_alloc
auto stats = seastar::memory::stats();
// this allocation cannot be satisfied (at least when the seastar
// allocator is used, which it is for this test)
size_t size = stats.total_memory() * 2;
auto failed_allocs = [&stats]() {
return seastar::memory::stats().failed_allocations() - stats.failed_allocations();
};
// test that new throws
stats = seastar::memory::stats();
BOOST_REQUIRE_THROW(sink = operator new(size), std::bad_alloc);
BOOST_CHECK_EQUAL(failed_allocs(), 1);
// test that huge malloc returns null
stats = seastar::memory::stats();
BOOST_REQUIRE_EQUAL(malloc(size), nullptr);
BOOST_CHECK_EQUAL(failed_allocs(), 1);
// test that huge realloc on nullptr returns null
stats = seastar::memory::stats();
BOOST_REQUIRE_EQUAL(realloc(nullptr, size), nullptr);
BOOST_CHECK_EQUAL(failed_allocs(), 1);
// test that huge realloc on an existing ptr returns null
stats = seastar::memory::stats();
void *p = malloc(1);
BOOST_REQUIRE(p);
void *p2 = realloc(p, size);
BOOST_REQUIRE_EQUAL(p2, nullptr);
BOOST_CHECK_EQUAL(failed_allocs(), 1);
free(p2 ?: p);
return make_ready_future<>();
}
SEASTAR_TEST_CASE(test_diagnostics_failures) {
// test that an allocation failure is reflected in the diagnostics
auto stats = seastar::memory::stats();
size_t size = stats.total_memory() * 2; // cannot be satisfied
// we expect that the failure is immediately reflected in the diagnostics
try {
sink = operator new(size);
} catch (const std::bad_alloc&) {}
auto report = memory::generate_memory_diagnostics_report();
// +1 because we caused one additional hard failure from the allocation above
auto expected = fmt::format("Hard failures: {}", stats.failed_allocations() + 1);
if (report.find(expected) == seastar::sstring::npos) {
BOOST_FAIL(fmt::format("Did not find expected message: {} in\n{}\n", expected, report));
}
return seastar::make_ready_future();
}
template <typename Func>
SEASTAR_CONCEPT(requires requires (Func fn) { fn(); })
void check_function_allocation(const char* name, size_t expected_allocs, Func f) {
auto before = seastar::memory::stats();
f();
auto after = seastar::memory::stats();
BOOST_TEST_INFO("After function: " << name);
BOOST_REQUIRE_EQUAL(expected_allocs, after.mallocs() - before.mallocs());
}
SEASTAR_TEST_CASE(test_diagnostics_allocation) {
check_function_allocation("empty", 0, []{});
check_function_allocation("operator new", 1, []{
// note that many pairs of malloc/free-alikes can just be optimized
// away, but not operator new(size_t), per the standard
void * volatile p = operator new(1);
operator delete(p);
});
// The meat of this test. Dump the diagnostics report to the log and ensure it
// doesn't allocate. Doing it lots is important because it may alloc only occasionally:
// a real example being the optimized timestamp logging which (used to) make an allocation
// only once a second.
check_function_allocation("log_memory_diagnostics_report", 0, [&]{
for (int i = 0; i < 1000; i++) {
seastar::memory::internal::log_memory_diagnostics_report(log_level::info);
}
});
return seastar::make_ready_future();
}
#endif // #ifndef SEASTAR_DEFAULT_ALLOCATOR
SEASTAR_TEST_CASE(test_large_allocation_warning_off_by_one) {
#ifndef SEASTAR_DEFAULT_ALLOCATOR
constexpr size_t large_alloc_threshold = 1024*1024;
seastar::memory::scoped_large_allocation_warning_threshold mtg(large_alloc_threshold);
BOOST_REQUIRE(seastar::memory::get_large_allocation_warning_threshold() == large_alloc_threshold);
auto old_large_allocs_count = memory::stats().large_allocations();
volatile auto obj = (char*)malloc(large_alloc_threshold);
*obj = 'c'; // to prevent compiler from considering this a dead allocation and optimizing it out
// Verify large allocation was detected by allocator.
BOOST_REQUIRE(memory::stats().large_allocations() == old_large_allocs_count+1);
free(obj);
#endif
return make_ready_future<>();
}
|