1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/memory.cpp

156 lines
3.3 KiB
C++
Raw Permalink Normal View History

2016-09-19 20:58:41 -04:00
/** Memory allocators.
2016-09-15 21:25:32 -04:00
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
*********************************************************************************/
2016-09-15 21:25:32 -04:00
2019-04-07 15:44:19 +02:00
#include <unordered_map>
#include <unistd.h>
2020-06-08 04:03:07 +02:00
#include <cstdlib>
#include <cerrno>
2020-06-08 04:03:07 +02:00
#include <cstring>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/mman.h>
2016-09-15 21:25:32 -04:00
2021-02-16 14:15:14 +01:00
#include <villas/log.hpp>
#include <villas/node/memory.hpp>
#include <villas/utils.hpp>
2020-03-04 13:07:20 +01:00
#include <villas/kernel/kernel.hpp>
2016-09-15 21:25:32 -04:00
2020-03-04 13:38:40 +01:00
using namespace villas;
using namespace villas::node;
using namespace villas::node::memory;
2020-03-04 13:38:40 +01:00
static std::unordered_map<void *, struct Allocation *> allocations;
2021-02-16 14:15:14 +01:00
static Logger logger;
int villas::node::memory::init(int hugepages)
2018-08-20 18:31:27 +02:00
{
int ret;
2021-02-16 14:15:14 +01:00
logger = logging.get("memory");
logger->info("Initialize memory sub-system: #hugepages={}", hugepages);
ret = mmap_init(hugepages);
if (ret < 0)
2019-10-29 09:23:36 +01:00
return ret;
2019-10-26 13:35:40 +02:00
size_t lock_sz = kernel::getHugePageSize() * hugepages;
ret = lock(lock_sz);
if (ret)
return ret;
return 0;
}
int villas::node::memory::lock(size_t sz)
{
int ret;
if (!utils::isPrivileged()) {
logger->warn("Running in an unprivileged environment. Memory is not locked to RAM!");
return 0;
}
#ifdef __linux__
#ifndef __arm__
struct rlimit l;
/* Increase ressource limit for locked memory */
ret = getrlimit(RLIMIT_MEMLOCK, &l);
if (ret)
return ret;
if (l.rlim_cur < sz) {
if (l.rlim_max < sz) {
if (getuid() != 0) {
logger->warn("Failed to increase ressource limit of locked memory. Please increase manually by running as root:");
logger->warn(" $ ulimit -Hl {}", sz);
2019-06-27 15:45:45 +02:00
return 0;
}
l.rlim_max = sz;
}
l.rlim_cur = sz;
ret = setrlimit(RLIMIT_MEMLOCK, &l);
if (ret)
return ret;
logger->debug("Increased ressource limit of locked memory to {} bytes", sz);
}
2019-06-27 15:45:45 +02:00
#endif /* __arm__ */
#ifdef _POSIX_MEMLOCK
/* Lock all current and future memory allocations */
ret = mlockall(MCL_CURRENT | MCL_FUTURE);
2019-02-12 17:29:03 +01:00
if (ret)
return -1;
#endif /* _POSIX_MEMLOCK */
#endif /* __linux__ */
return 0;
}
void * villas::node::memory::alloc(size_t len, struct Type *m)
2016-09-15 21:25:32 -04:00
{
return alloc_aligned(len, sizeof(void *), m);
2016-09-15 21:25:32 -04:00
}
void * villas::node::memory::alloc_aligned(size_t len, size_t alignment, struct Type *m)
2018-08-20 18:31:27 +02:00
{
struct Allocation *ma = m->alloc(len, alignment, m);
2019-04-07 15:13:40 +02:00
if (ma == nullptr) {
2021-02-16 14:15:14 +01:00
logger->warn("Memory allocation of type {} failed. reason={}", m->name, strerror(errno));
2019-04-07 15:13:40 +02:00
return nullptr;
}
2019-04-07 15:44:19 +02:00
allocations[ma->address] = ma;
2021-02-16 14:15:14 +01:00
logger->debug("Allocated {:#x} bytes of {:#x}-byte-aligned {} memory: {}", ma->length, ma->alignment, ma->type->name, ma->address);
2016-09-19 20:58:41 -04:00
return ma->address;
2016-09-15 21:25:32 -04:00
}
int villas::node::memory::free(void *ptr)
2016-09-15 21:25:32 -04:00
{
int ret;
/* Find corresponding memory allocation entry */
struct Allocation *ma = allocations[ptr];
if (!ma)
return -1;
2021-02-16 14:15:14 +01:00
logger->debug("Releasing {:#x} bytes of {} memory: {}", ma->length, ma->type->name, ma->address);
2019-10-26 13:07:02 +02:00
ret = ma->type->free(ma, ma->type);
if (ret)
return ret;
2016-09-15 21:25:32 -04:00
/* Remove allocation entry */
2019-04-07 15:44:19 +02:00
auto iter = allocations.find(ptr);
2019-10-26 13:28:29 +02:00
if (iter == allocations.end())
2019-04-07 15:44:19 +02:00
return -1;
2019-10-26 13:28:29 +02:00
allocations.erase(iter);
delete ma;
2018-07-09 12:54:18 +02:00
2016-09-15 21:25:32 -04:00
return 0;
}
struct Allocation * villas::node::memory::get_allocation(void *ptr)
{
2019-04-07 15:44:19 +02:00
return allocations[ptr];
2017-03-27 13:22:54 +02:00
}
struct Type *villas::node::memory::default_type = nullptr;