2023-09-04 12:21:37 +02:00
|
|
|
/* Unit tests for memory management.
|
2016-10-30 18:39:22 -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 18:39:22 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <criterion/theories.h>
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cerrno>
|
2016-10-30 18:39:22 -04:00
|
|
|
|
2018-10-20 14:24:51 +02:00
|
|
|
#include <villas/log.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
2021-09-20 16:28:51 +02:00
|
|
|
#include <villas/utils.hpp>
|
|
|
|
|
|
|
|
using namespace villas;
|
2016-10-30 18:39:22 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
|
|
|
|
2018-08-13 15:24:04 +02:00
|
|
|
extern void init_memory();
|
|
|
|
|
|
|
|
#define PAGESIZE (1 << 12)
|
|
|
|
#define HUGEPAGESIZE (1 << 21)
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2016-10-30 18:39:22 -04:00
|
|
|
TheoryDataPoints(memory, aligned) = {
|
2018-08-13 15:24:04 +02:00
|
|
|
DataPoints(size_t, 1, 32, 55, 1 << 10, PAGESIZE, HUGEPAGESIZE),
|
|
|
|
DataPoints(size_t, 1, 8, PAGESIZE, PAGESIZE),
|
2021-08-10 10:12:48 -04:00
|
|
|
DataPoints(struct memory::Type *, &memory::heap, &memory::mmap_hugetlb,
|
|
|
|
&memory::mmap_hugetlb)};
|
2016-10-30 18:39:22 -04:00
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
// cppcheck-suppress unknownMacro
|
2021-08-10 10:12:48 -04:00
|
|
|
Theory((size_t len, size_t align, struct memory::Type *mt), memory, aligned,
|
|
|
|
.init = init_memory) {
|
2016-10-30 18:39:22 -04:00
|
|
|
int ret;
|
|
|
|
void *ptr;
|
2023-09-07 11:46:39 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!utils::isPrivileged() && 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.");
|
2023-09-07 11:46:39 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ptr = memory::alloc_aligned(len, align, mt);
|
2018-08-13 15:24:04 +02:00
|
|
|
cr_assert_not_null(ptr, "Failed to allocate memory");
|
2023-09-07 11:46:39 +02:00
|
|
|
|
2018-08-13 15:24:04 +02:00
|
|
|
cr_assert(IS_ALIGNED(ptr, align),
|
|
|
|
"Memory at %p is not alligned to %#zx byte bounary", ptr, align);
|
2023-09-07 11:46:39 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (mt == &memory::mmap_hugetlb) {
|
2018-08-13 15:24:04 +02:00
|
|
|
cr_assert(IS_ALIGNED(ptr, HUGEPAGESIZE),
|
|
|
|
"Memory at %p is not alligned to %#x byte bounary", ptr,
|
|
|
|
HUGEPAGESIZE);
|
2016-10-30 18:39:22 -04:00
|
|
|
}
|
2023-09-07 11:46:39 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::free(ptr);
|
2016-10-30 18:39:22 -04:00
|
|
|
cr_assert_eq(ret, 0, "Failed to release memory: ret=%d, ptr=%p, len=%zu: %s",
|
|
|
|
ret, ptr, len, strerror(errno));
|
2017-03-28 10:28:20 +02:00
|
|
|
}
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2018-08-13 14:58:52 +02:00
|
|
|
Test(memory, manager, .init = init_memory) {
|
2017-03-31 18:28:51 +02:00
|
|
|
size_t total_size;
|
|
|
|
size_t max_block;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-31 18:28:51 +02:00
|
|
|
int ret;
|
|
|
|
void *p, *p1, *p2, *p3;
|
2021-08-10 10:12:48 -04:00
|
|
|
struct memory::Type *m;
|
2017-03-31 18:28:51 +02:00
|
|
|
|
|
|
|
total_size = 1 << 10;
|
2021-08-10 10:12:48 -04:00
|
|
|
max_block =
|
|
|
|
total_size - sizeof(struct memory::Type) - sizeof(struct memory::Block);
|
2018-07-02 19:00:55 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
p = memory::alloc(total_size, &memory::heap);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(p);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
m = memory::managed(p, total_size);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(m);
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
p1 = memory::alloc(16, m);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(p1);
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
p2 = memory::alloc(32, m);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(p2);
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::free(p1);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
p1 = memory::alloc_aligned(128, 128, m);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(p1);
|
|
|
|
cr_assert(IS_ALIGNED(p1, 128));
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
p3 = memory::alloc_aligned(128, 256, m);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(p3);
|
|
|
|
cr_assert(IS_ALIGNED(p3, 256));
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::free(p2);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::free(p1);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::free(p3);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
p1 = memory::alloc(max_block, m);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(p1);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::free(p1);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 13:10:33 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::free(p);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
}
|