]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/tests/unit/alloc_test.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / tests / unit / alloc_test.cc
CommitLineData
11fdf7f2
TL
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2015 Cloudius Systems, Ltd.
20 */
21
22#include <seastar/testing/test_case.hh>
23#include <seastar/core/memory.hh>
24#include <seastar/core/reactor.hh>
9f95a23c 25#include <seastar/core/temporary_buffer.hh>
11fdf7f2
TL
26#include <vector>
27
28using namespace seastar;
29
30SEASTAR_TEST_CASE(alloc_almost_all_and_realloc_it_with_a_smaller_size) {
31#ifndef SEASTAR_DEFAULT_ALLOCATOR
32 auto all = memory::stats().total_memory();
33 auto reserve = size_t(0.02 * all);
34 auto to_alloc = all - (reserve + (10 << 20));
35 auto orig_to_alloc = to_alloc;
36 auto obj = malloc(to_alloc);
37 while (!obj) {
38 to_alloc *= 0.9;
39 obj = malloc(to_alloc);
40 }
41 BOOST_REQUIRE(to_alloc > orig_to_alloc / 4);
42 BOOST_REQUIRE(obj != nullptr);
43 auto obj2 = realloc(obj, to_alloc - (1 << 20));
44 BOOST_REQUIRE(obj == obj2);
45 free(obj2);
46#endif
47 return make_ready_future<>();
48}
49
50SEASTAR_TEST_CASE(malloc_0_and_free_it) {
51#ifndef SEASTAR_DEFAULT_ALLOCATOR
52 auto obj = malloc(0);
53 BOOST_REQUIRE(obj != nullptr);
54 free(obj);
55#endif
56 return make_ready_future<>();
57}
58
59SEASTAR_TEST_CASE(test_live_objects_counter_with_cross_cpu_free) {
60 return smp::submit_to(1, [] {
61 auto ret = std::vector<std::unique_ptr<bool>>(1000000);
62 for (auto& o : ret) {
63 o = std::make_unique<bool>(false);
64 }
65 return ret;
66 }).then([] (auto&& vec) {
67 vec.clear(); // cause cross-cpu free
68 BOOST_REQUIRE(memory::stats().live_objects() < std::numeric_limits<size_t>::max() / 2);
69 });
70}
9f95a23c
TL
71
72SEASTAR_TEST_CASE(test_aligned_alloc) {
73 for (size_t align = sizeof(void*); align <= 65536; align <<= 1) {
74 for (size_t size = align; size <= align * 2; size <<= 1) {
75 void *p = aligned_alloc(align, size);
76 BOOST_REQUIRE(p != nullptr);
77 BOOST_REQUIRE((reinterpret_cast<uintptr_t>(p) % align) == 0);
78 ::memset(p, 0, size);
79 free(p);
80 }
81 }
82 return make_ready_future<>();
83}
84
85SEASTAR_TEST_CASE(test_temporary_buffer_aligned) {
86 for (size_t align = sizeof(void*); align <= 65536; align <<= 1) {
87 for (size_t size = align; size <= align * 2; size <<= 1) {
88 auto buf = temporary_buffer<char>::aligned(align, size);
89 void *p = buf.get_write();
90 BOOST_REQUIRE(p != nullptr);
91 BOOST_REQUIRE((reinterpret_cast<uintptr_t>(p) % align) == 0);
92 ::memset(p, 0, size);
93 }
94 }
95 return make_ready_future<>();
96}