2023-09-04 12:21:37 +02:00
|
|
|
/* Unit tests for memory pool.
|
2016-10-30 19:40:50 -04:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-10-30 19:40:50 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <criterion/parameterized.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
2023-06-23 11:25:20 +02:00
|
|
|
#include <vector>
|
2016-10-30 19:40:50 -04:00
|
|
|
|
2021-09-20 16:28:51 +02:00
|
|
|
#include <villas/log.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/pool.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2021-09-20 16:28:51 +02:00
|
|
|
|
|
|
|
using namespace villas;
|
2016-10-30 19:40:50 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
|
|
|
|
2018-08-13 14:58:52 +02:00
|
|
|
extern void init_memory();
|
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
struct param {
|
|
|
|
int thread_count;
|
|
|
|
int pool_size;
|
|
|
|
size_t block_size;
|
2021-08-10 10:12:48 -04:00
|
|
|
struct memory::Type *mt;
|
2016-10-30 19:40:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
ParameterizedTestParameters(pool, basic) {
|
2023-06-23 11:25:20 +02:00
|
|
|
static std::vector<struct param> params;
|
|
|
|
|
|
|
|
params.clear();
|
|
|
|
params.push_back({1, 4096, 150, &memory::heap});
|
|
|
|
params.push_back({1, 128, 8, &memory::mmap});
|
|
|
|
params.push_back({1, 4, 8192, &memory::mmap_hugetlb});
|
|
|
|
params.push_back({1, 1 << 13, 4, &memory::mmap_hugetlb});
|
|
|
|
|
|
|
|
return cr_make_param_array(struct param, params.data(), params.size());
|
2016-10-30 19:40:50 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
// cppcheck-suppress unknownMacro
|
2018-08-13 14:58:52 +02:00
|
|
|
ParameterizedTest(struct param *p, pool, basic, .init = init_memory) {
|
2016-10-30 19:40:50 -04:00
|
|
|
int ret;
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Pool pool;
|
2016-10-30 19:40:50 -04:00
|
|
|
void *ptr, *ptrs[p->pool_size];
|
|
|
|
|
2023-06-23 11:25:20 +02:00
|
|
|
logging.setLevel("trace");
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!utils::isPrivileged() && p->mt == &memory::mmap_hugetlb)
|
2021-09-20 16:28:51 +02:00
|
|
|
cr_skip_test("Skipping memory_mmap_hugetlb tests allocatpr because we are "
|
|
|
|
"running in an unprivileged environment.");
|
|
|
|
|
2019-10-26 13:28:29 +02:00
|
|
|
ret = pool_init(&pool, p->pool_size, p->block_size, p->mt);
|
2016-10-30 19:40:50 -04:00
|
|
|
cr_assert_eq(ret, 0, "Failed to create pool");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
ptr = pool_get(&pool);
|
2018-08-27 11:21:57 +02:00
|
|
|
cr_assert_neq(ptr, nullptr);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
memset(ptr, 1, p->block_size); // check that we dont get a seg fault
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:43:22 -04:00
|
|
|
int i;
|
|
|
|
for (i = 1; i < p->pool_size; i++) {
|
2016-10-30 19:40:50 -04:00
|
|
|
ptrs[i] = pool_get(&pool);
|
2016-10-30 19:43:22 -04:00
|
|
|
|
2018-08-27 11:21:57 +02:00
|
|
|
if (ptrs[i] == nullptr)
|
2016-10-30 19:43:22 -04:00
|
|
|
break;
|
2016-10-30 19:40:50 -04:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:43:22 -04:00
|
|
|
if (i < p->pool_size)
|
2018-08-27 11:21:57 +02:00
|
|
|
cr_assert_neq(ptrs[i], nullptr);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
ptr = pool_get(&pool);
|
2018-08-27 11:21:57 +02:00
|
|
|
cr_assert_eq(ptr, nullptr);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
ret = pool_destroy(&pool);
|
|
|
|
cr_assert_eq(ret, 0, "Failed to destroy pool");
|
2017-03-28 10:28:20 +02:00
|
|
|
}
|