2016-09-19 20:58:41 -04:00
|
|
|
/* Memory allocators.
|
2016-09-15 21:25:32 -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
|
2017-03-03 20:20:13 -04:00
|
|
|
*/
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2019-04-07 15:44:19 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cerrno>
|
2020-06-08 04:03:07 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2017-05-24 14:47:24 +00:00
|
|
|
#include <unistd.h>
|
2017-07-24 19:21:57 +02:00
|
|
|
|
2019-02-12 17:27:09 +01:00
|
|
|
#include <sys/mman.h>
|
2017-04-02 00:14:17 +02:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2021-02-16 14:15:14 +01: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>
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
using namespace villas;
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
|
|
|
using namespace villas::node::memory;
|
2020-03-04 13:38:40 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static std::unordered_map<void *, struct Allocation *> allocations;
|
2021-02-16 14:15:14 +01:00
|
|
|
static Logger logger;
|
2018-07-04 16:50:36 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::memory::init(int hugepages) {
|
2018-11-23 19:42:18 +02:00
|
|
|
int ret;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
logger = logging.get("memory");
|
|
|
|
|
|
|
|
logger->info("Initialize memory sub-system: #hugepages={}", hugepages);
|
2018-07-02 19:00:55 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = mmap_init(hugepages);
|
2019-10-28 15:10:31 +01:00
|
|
|
if (ret < 0)
|
2019-10-29 09:23:36 +01:00
|
|
|
return ret;
|
2019-10-26 13:35:40 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
size_t lock_sz = kernel::getHugePageSize() * hugepages;
|
2018-07-04 12:15:24 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = lock(lock_sz);
|
2018-12-04 10:46:02 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-12-04 10:46:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-04-02 00:14:17 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::memory::lock(size_t sz) {
|
2018-12-04 10:46:02 +01:00
|
|
|
int ret;
|
2019-02-12 17:27:09 +01:00
|
|
|
|
2021-09-20 16:28:51 +02:00
|
|
|
if (!utils::isPrivileged()) {
|
|
|
|
logger->warn(
|
|
|
|
"Running in an unprivileged environment. Memory is not locked to RAM!");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-12 17:27:09 +01:00
|
|
|
|
2021-09-20 16:28:51 +02:00
|
|
|
#ifdef __linux__
|
2019-02-12 17:27:09 +01:00
|
|
|
#ifndef __arm__
|
2018-12-04 10:46:02 +01:00
|
|
|
struct rlimit l;
|
2018-07-04 12:15:24 +02:00
|
|
|
|
2019-02-12 17:27:09 +01:00
|
|
|
// Increase ressource limit for locked memory
|
2017-04-02 00:14:17 +02:00
|
|
|
ret = getrlimit(RLIMIT_MEMLOCK, &l);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (l.rlim_cur < sz) {
|
|
|
|
if (l.rlim_max < sz) {
|
2018-07-04 12:15:24 +02:00
|
|
|
if (getuid() != 0) {
|
2021-09-20 16:28:51 +02:00
|
|
|
logger->warn("Failed to increase ressource limit of locked memory. "
|
|
|
|
"Please increase manually by running as root:");
|
2021-08-10 10:12:48 -04:00
|
|
|
logger->warn(" $ ulimit -Hl {}", sz);
|
2018-07-04 12:15:24 +02:00
|
|
|
|
2019-06-27 15:45:45 +02:00
|
|
|
return 0;
|
2018-07-04 12:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
l.rlim_max = sz;
|
2018-07-04 12:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
l.rlim_cur = sz;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-02 00:14:17 +02:00
|
|
|
ret = setrlimit(RLIMIT_MEMLOCK, &l);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2022-01-14 16:14:15 -05:00
|
|
|
logger->debug("Increased ressource limit of locked memory to {} bytes", sz);
|
2017-03-05 11:04:07 -04:00
|
|
|
}
|
2019-06-27 15:45:45 +02:00
|
|
|
|
2019-04-08 14:04:07 +01:00
|
|
|
#endif // __arm__
|
2019-02-12 17:27:09 +01:00
|
|
|
#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)
|
2019-02-12 17:27:09 +01:00
|
|
|
return -1;
|
|
|
|
#endif // _POSIX_MEMLOCK
|
|
|
|
|
|
|
|
#endif // __linux__
|
2018-12-04 10:46:02 +01:00
|
|
|
|
2016-11-20 02:45:16 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void *villas::node::memory::alloc(size_t len, struct Type *m) {
|
|
|
|
return alloc_aligned(len, sizeof(void *), m);
|
2016-09-15 21:25:32 -04:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void *villas::node::memory::alloc_aligned(size_t len, size_t alignment,
|
|
|
|
struct Type *m) {
|
|
|
|
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;
|
2018-07-18 15:15:08 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-07 15:44:19 +02:00
|
|
|
allocations[ma->address] = ma;
|
2017-03-31 18:04:08 +02:00
|
|
|
|
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
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
return ma->address;
|
2016-09-15 21:25:32 -04:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::memory::free(void *ptr) {
|
2016-11-20 03:02:19 -05:00
|
|
|
int ret;
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
// Find corresponding memory allocation entry
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Allocation *ma = allocations[ptr];
|
2018-07-02 19:00:55 +02:00
|
|
|
if (!ma)
|
|
|
|
return -1;
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->debug("Releasing {:#x} bytes of {} memory: {}", ma->length,
|
|
|
|
ma->type->name, ma->address);
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2019-10-26 13:07:02 +02:00
|
|
|
ret = ma->type->free(ma, ma->type);
|
2018-07-02 19:00:55 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2018-07-02 19:00:55 +02: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;
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2019-10-26 13:28:29 +02:00
|
|
|
allocations.erase(iter);
|
2020-01-21 16:26:51 +01:00
|
|
|
delete ma;
|
2018-07-09 12:54:18 +02:00
|
|
|
|
2016-09-15 21:25:32 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
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
|
|
|
}
|
2018-10-21 12:57:02 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Type *villas::node::memory::default_type = nullptr;
|