File: alloc_test.cc

package info (click to toggle)
seastar 25.05.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 7,256 kB
  • sloc: cpp: 89,250; python: 5,066; ansic: 3,452; sh: 1,272; xml: 177; makefile: 9
file content (684 lines) | stat: -rw-r--r-- 22,033 bytes parent folder | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
 * 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/core/memory.hh>
#include <seastar/core/shard_id.hh>
#include <seastar/core/smp.hh>
#include <seastar/core/temporary_buffer.hh>
#include <seastar/testing/perf_tests.hh>
#include <seastar/testing/test_case.hh>
#include <seastar/testing/thread_test_case.hh>
#include <seastar/util/log.hh>
#include <seastar/util/memory_diagnostics.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(new_0) {

    {
        // new must always return a non-null pointer, even for 0 size
        auto obj = operator new(0);
        BOOST_REQUIRE(obj != nullptr);
        operator delete(obj);
    }

    {
        // same test but with a zero length array
        auto obj = new char[0];
        BOOST_REQUIRE(obj != nullptr);
        delete [] obj;
    }

    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<>();
}

#ifdef __cpp_sized_deallocation
SEASTAR_TEST_CASE(test_sized_delete) {
    for (size_t size = 0; size <= 65536; size++) {
        void *p0 = operator new(size), *p1 = operator new(size);
        BOOST_REQUIRE(p0 != nullptr);
        BOOST_REQUIRE(p1 != nullptr);
        ::memset(p0, 1, size);
        ::memset(p1, 2, size);
        perf_tests::do_not_optimize(p0);
        perf_tests::do_not_optimize(p1);
        operator delete(p0, size);
        operator delete(p1, size);
    }
    return make_ready_future<>();
}
#endif

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<>();
}

SEASTAR_THREAD_TEST_CASE(test_cross_thread_realloc) {
    // Tests that realloc seems to do the right thing with various sizes of
    // buffer, including cases where the initial allocation is on another
    // shard.
    // Needs at least 2 shards to usefully test the cross-shard aspect but
    // still passes when only 1 shard is used.
    auto do_xshard_realloc = [](bool cross_shard, size_t initial_size, size_t realloc_size) {
        BOOST_TEST_CONTEXT("cross_shard=" << cross_shard << ", initial="
                << initial_size << ", realloc_size=" << realloc_size) {

            auto other_shard = (this_shard_id() + cross_shard) % smp::count;

            char *p = static_cast<char *>(malloc(initial_size));

            // write some sentinels and check them after realloc
            // x start of region
            // y end of realloc'd region (if it falls within the initial size)
            // z end of initial region
            if (initial_size > 0) {
                p[0] = 'x';
                p[initial_size - 1] = 'z';
                if (realloc_size > 0 && realloc_size <= initial_size) {
                    p[realloc_size - 1] = 'y';
                }
            }
            smp::submit_to(other_shard, [=] {
                char* p2 = static_cast<char *>(realloc(p, realloc_size));
                if (initial_size > 0 && realloc_size > 0) {
                    BOOST_REQUIRE_EQUAL(p2[0], 'x');
                    if (realloc_size <= initial_size) {
                        BOOST_REQUIRE_EQUAL(p2[realloc_size - 1], 'y');
                    }
                    if (realloc_size > initial_size) {
                        BOOST_REQUIRE_EQUAL(p2[initial_size - 1], 'z');
                    }
                }
                free(p2);
            }).get();
        }
    };

    for (auto& cross_shard : {false, true}) {
        do_xshard_realloc(cross_shard, 0, 0);
        do_xshard_realloc(cross_shard, 0, 1);
        do_xshard_realloc(cross_shard, 1, 0);
        do_xshard_realloc(cross_shard, 50, 100);
        do_xshard_realloc(cross_shard, 100, 50);
        do_xshard_realloc(cross_shard, 100000, 500000);
        do_xshard_realloc(cross_shard, 500000, 100000);
    }
}


#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<>();
}

SEASTAR_TEST_CASE(test_enable_abort_on_oom) {
    bool original = seastar::memory::is_abort_on_allocation_failure();

    seastar::memory::set_abort_on_allocation_failure(false);
    BOOST_CHECK(!seastar::memory::is_abort_on_allocation_failure());

    seastar::memory::set_abort_on_allocation_failure(true);
    BOOST_CHECK(seastar::memory::is_abort_on_allocation_failure());

    seastar::memory::set_abort_on_allocation_failure(original);

    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 new[] throws
    stats = seastar::memory::stats();
    BOOST_REQUIRE_THROW(sink = new char[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>
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();
}

#ifdef SEASTAR_HEAPPROF

// small wrapper to disincentivize gcc from unrolling the loop
[[gnu::noinline]]
char* malloc_wrapper(size_t size) {
    auto ret = static_cast<char*>(malloc(size));
    *ret = 'c'; // to prevent compiler from considering this a dead allocation and optimizing it out
    return ret;
}

namespace seastar::memory {
std::ostream& operator<<(std::ostream& os, const allocation_site& site) {
    os << "allocation_site[count: " << site.count << ", size: " << site.size << "]";
    return os;
}
}

SEASTAR_TEST_CASE(test_sampled_profile_collection_small)
{
    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 0);
    }

    std::size_t count = 100;
    std::vector<volatile char*> ptrs(count);

    seastar::memory::set_heap_profiling_sampling_rate(100);

#ifdef __clang__
    #pragma nounroll
#endif
    for (std::size_t i = 0; i < count / 2; ++i) {
        ptrs[i] = malloc_wrapper(10);
    }

#ifdef __clang__
    #pragma nounroll
#endif
    for (std::size_t i = count / 2; i < count; ++i) {
        ptrs[i] = malloc_wrapper(10);
    }

    auto get_samples = []() {
        auto stats0 = seastar::memory::sampled_memory_profile();
        auto stats1 = seastar::memory::sampled_memory_profile();

        // two back-to-back copies of the sample should have the same value
        BOOST_CHECK_EQUAL(stats0, stats1);

        // check that we get the same value from the raw array iterface
        std::vector<seastar::memory::allocation_site> stats2(stats0.size());
        auto sz2 = seastar::memory::sampled_memory_profile(stats2.data(), stats2.size());
        BOOST_CHECK_EQUAL(stats0.size(), sz2);
        BOOST_CHECK_EQUAL(stats0, stats2);

        // check with +1 size, we expect to still only get size elements
        std::vector<seastar::memory::allocation_site> stats3(stats0.size() + 1);
        auto sz3 = seastar::memory::sampled_memory_profile(stats3.data(), stats3.size());
        BOOST_CHECK_EQUAL(stats0.size(), sz3);
        stats3.resize(sz3);
        BOOST_CHECK_EQUAL(stats0, stats3);

        return stats0;
    };

    // NB: the test framework allocates
    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = get_samples();
        BOOST_REQUIRE_EQUAL(stats.size(), 2);
        BOOST_REQUIRE_EQUAL(stats[0].size, stats[0].count * 100);
    }

    seastar::memory::set_heap_profiling_sampling_rate(100);

    for (auto ptr : ptrs) {
        free((void*)ptr);
    }

    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = get_samples();
        BOOST_REQUIRE_EQUAL(stats.size(), 0);
    }

    return seastar::make_ready_future();
}

SEASTAR_TEST_CASE(test_sampled_profile_collection_large)
{
    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 0);
    }

    std::size_t count = 100;
    std::vector<volatile char*> ptrs(count);

    seastar::memory::set_heap_profiling_sampling_rate(1000000);

#ifdef __clang__
    #pragma nounroll
#endif
    for (std::size_t i = 0; i < count / 2; ++i) {
        ptrs[i] = malloc_wrapper(100000);
    }

#ifdef __clang__
    #pragma nounroll
#endif
    for (std::size_t i = count / 2; i < count; ++i) {
        ptrs[i] = malloc_wrapper(100000);
    }

    // NB: the test framework allocate
    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 2);
        BOOST_REQUIRE_EQUAL(stats[0].size, stats[0].count * 1000000);
    }

    seastar::memory::set_heap_profiling_sampling_rate(1000000);

    for (auto ptr : ptrs) {
        free((void*)ptr);
    }

    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = seastar::memory::sampled_memory_profile();
        // NOTE this is because right now the tracking structure doesn't delete call sites ever
        BOOST_REQUIRE_EQUAL(stats.size(), 0);
    }

    return seastar::make_ready_future();
}

SEASTAR_TEST_CASE(test_sampled_profile_collection_max_sites)
{
    std::size_t count = 1010;
    std::vector<volatile char*> ptrs(count);

    seastar::memory::set_heap_profiling_sampling_rate(100);

    #pragma GCC unroll 1010
    for (std::size_t i = 0; i < count; ++i) {
        volatile char* ptr = static_cast<char*>(malloc(1000));
        *ptr = 'c'; // to prevent compiler from considering this a dead allocation and optimizing it out
        ptrs[i] = ptr;
    }

    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 1000);
    }

    for (auto ptr : ptrs) {
        free((void*)ptr);
    }

    return seastar::make_ready_future();
}

SEASTAR_TEST_CASE(test_change_sample_rate)
{
    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 0);
    }

    std::size_t sample_rate = 100;
    std::size_t count = 10000;
    std::vector<volatile char*> ptrs(count);

    seastar::memory::set_heap_profiling_sampling_rate(sample_rate);

#ifdef __clang__
    #pragma nounroll
#endif
    for (std::size_t i = 0; i < count; ++i) {
        ptrs[i] = malloc_wrapper(10);
    }

    // NB: the test framework allocates
    seastar::memory::set_heap_profiling_sampling_rate(0);

    size_t last_alloc_size = 0;
    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 1);
        last_alloc_size = stats[0].size;
        BOOST_REQUIRE_EQUAL(stats[0].size, stats[0].count * sample_rate);
    }

    seastar::memory::set_heap_profiling_sampling_rate(sample_rate);

    size_t free_iter = 0;
    // free some of the allocations to check size changes
    for (size_t i = 0; i < count / 4; ++i, ++free_iter) {
        free((void*)ptrs[free_iter]);
    }

    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 1);
        BOOST_REQUIRE_EQUAL(stats[0].size, stats[0].count * sample_rate);
        BOOST_REQUIRE_NE(stats[0].size, last_alloc_size);
        BOOST_REQUIRE_GT(stats[0].size, 0);
        last_alloc_size = stats[0].size;
    }

    // now increase the sampling rate with outstanding allocations from the old rate
    seastar::memory::set_heap_profiling_sampling_rate(sample_rate * 100);

    for (size_t i = 0; i < count / 4; ++i, ++free_iter) {
        free((void*)ptrs[free_iter]);
    }

    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 1);
        BOOST_REQUIRE_LT(stats[0].size, last_alloc_size); // should not have underflowed
    }

    seastar::memory::set_heap_profiling_sampling_rate(sample_rate);

    // free the rest
    for (size_t i = 0; i < count / 2; ++i, ++free_iter) {
        free((void*)ptrs[free_iter]);
    }

    seastar::memory::set_heap_profiling_sampling_rate(0);

    {
        auto stats = seastar::memory::sampled_memory_profile();
        BOOST_REQUIRE_EQUAL(stats.size(), 0);
    }

    return seastar::make_ready_future();
}


#endif // SEASTAR_HEAPPROF

#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<>();
}