2019-10-26 13:35:40 +02:00
|
|
|
/* mmap memory allocator.
|
2018-07-02 14:17:50 +02: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
|
2018-07-02 14:17:50 +02:00
|
|
|
*/
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <cstdlib>
|
2018-07-02 14:17:50 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/resource.h>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <sys/time.h>
|
2018-07-02 14:17:50 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
// Required to allocate hugepages on Apple OS X
|
|
|
|
#ifdef __MACH__
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <mach/vm_statistics.h>
|
2019-04-05 02:22:53 +02:00
|
|
|
#endif // __MACH__
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/log.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
using namespace villas;
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node::memory;
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
static size_t pgsz = -1;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
static size_t hugepgsz = -1;
|
2019-06-04 16:55:38 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
static Logger logger;
|
2018-11-23 19:42:18 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::memory::mmap_init(int hugepages) {
|
2024-07-29 12:35:42 +02:00
|
|
|
logger = Log::get("memory:mmap");
|
2021-02-16 14:15:14 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
pgsz = kernel::getPageSize();
|
|
|
|
if (pgsz < 0)
|
|
|
|
return -1;
|
2018-11-23 19:42:18 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (hugepages == 0) {
|
|
|
|
logger->warn("Hugepage allocator disabled.");
|
2019-10-29 23:25:44 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
default_type = &mmap;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (!utils::isPrivileged()) {
|
|
|
|
logger->warn(
|
|
|
|
"Running in an unprivileged environment. Hugepages are not used!");
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
default_type = &mmap;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
hugepgsz = kernel::getHugePageSize();
|
|
|
|
if (hugepgsz < 0) {
|
|
|
|
logger->warn("Failed to determine hugepage size.");
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2018-11-23 19:42:18 +02:00
|
|
|
|
2018-12-04 10:46:02 +01:00
|
|
|
#if defined(__linux__) && defined(__x86_64__)
|
2023-09-07 11:46:39 +02:00
|
|
|
int ret, pagecnt;
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
pagecnt = kernel::getNrHugepages();
|
|
|
|
if (pagecnt < hugepages) {
|
|
|
|
ret = kernel::setNrHugepages(hugepages);
|
|
|
|
if (ret) {
|
|
|
|
logger->warn("Failed to reserved hugepages. Please reserve manually by "
|
|
|
|
"running as root:");
|
|
|
|
logger->warn(" $ echo {} > /proc/sys/vm/nr_hugepages", hugepages);
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->debug("Increased number of reserved hugepages from {} to {}",
|
|
|
|
pagecnt, hugepages);
|
|
|
|
}
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
default_type = &mmap_hugetlb;
|
2020-07-05 13:24:23 +01:00
|
|
|
#else
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->debug("Hugepages not supported on this system. Falling back to "
|
|
|
|
"standard mmap() allocator.");
|
2021-09-20 16:28:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
default_type = &mmap;
|
2018-12-04 10:46:02 +01:00
|
|
|
#endif
|
2019-10-29 09:20:57 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2018-11-23 19:42:18 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 13:35:40 +02:00
|
|
|
// Allocate memory backed by mmaps with malloc() like interface
|
2023-09-07 11:46:39 +02:00
|
|
|
static struct Allocation *mmap_alloc(size_t len, size_t alignment,
|
|
|
|
struct Type *m) {
|
|
|
|
int flags, fd;
|
|
|
|
size_t sz;
|
2018-10-21 12:57:38 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
auto *ma = new struct Allocation;
|
|
|
|
if (!ma)
|
|
|
|
throw MemoryAllocationError();
|
2018-11-23 19:42:18 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (m->flags & (int)Flags::HUGEPAGE) {
|
2018-11-23 19:42:18 +02:00
|
|
|
#ifdef __linux__
|
2023-09-07 11:46:39 +02:00
|
|
|
flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB;
|
2018-11-23 19:42:18 +02:00
|
|
|
#else
|
2023-09-07 11:46:39 +02:00
|
|
|
flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
2018-11-23 19:42:18 +02:00
|
|
|
#endif
|
2018-07-02 14:17:50 +02:00
|
|
|
|
|
|
|
#ifdef __MACH__
|
2023-09-07 11:46:39 +02:00
|
|
|
fd = VM_FLAGS_SUPERPAGE_SIZE_2MB;
|
2018-11-23 19:42:18 +02:00
|
|
|
#else
|
2023-09-07 11:46:39 +02:00
|
|
|
fd = -1;
|
2018-07-02 14:17:50 +02:00
|
|
|
#endif
|
2023-09-07 11:46:39 +02:00
|
|
|
sz = hugepgsz;
|
|
|
|
} else {
|
|
|
|
flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
|
|
|
fd = -1;
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
sz = pgsz;
|
|
|
|
}
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* We must make sure that len is a multiple of the page size
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* See: https://lkml.org/lkml/2014/10/22/925
|
2024-02-29 22:28:17 +01:00
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
ma->length = ALIGN(len, sz);
|
|
|
|
ma->alignment = ALIGN(alignment, sz);
|
|
|
|
ma->type = m;
|
|
|
|
|
|
|
|
ma->address =
|
|
|
|
::mmap(nullptr, ma->length, PROT_READ | PROT_WRITE, flags, fd, 0);
|
|
|
|
if (ma->address == MAP_FAILED) {
|
|
|
|
delete ma;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ma;
|
2018-07-02 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
static int mmap_free(struct Allocation *ma, struct Type *m) {
|
|
|
|
int ret;
|
2018-07-02 19:00:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = munmap(ma->address, ma->length);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-07-02 19:00:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
struct Type memory::mmap = {.name = "mmap",
|
|
|
|
.flags = (int)Flags::MMAP,
|
|
|
|
.alignment = 12, // 4k page
|
|
|
|
.alloc = mmap_alloc,
|
|
|
|
.free = mmap_free};
|
|
|
|
|
|
|
|
struct Type memory::mmap_hugetlb = {.name = "mmap_hugetlb",
|
|
|
|
.flags =
|
|
|
|
(int)Flags::MMAP | (int)Flags::HUGEPAGE,
|
|
|
|
.alignment = 21, // 2 MiB hugepage
|
|
|
|
.alloc = mmap_alloc,
|
|
|
|
.free = mmap_free};
|