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

165 lines
3.7 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
*
2016-09-19 20:58:41 -04:00
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
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>
2017-12-09 02:19:28 +08:00
#include <villas/memory.h>
#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;
2019-04-07 15:44:19 +02:00
static std::unordered_map<void *, struct memory_allocation *> allocations;
2021-02-16 14:15:14 +01:00
static Logger logger;
2018-08-20 18:31:27 +02:00
int memory_init(int hugepages)
{
int ret;
2021-02-16 14:15:14 +01:00
logger = logging.get("memory");
logger->info("Initialize memory sub-system: #hugepages={}", hugepages);
ret = memory_mmap_init(hugepages);
if (ret < 0)
2019-10-29 09:23:36 +01:00
return ret;
2019-10-26 13:35:40 +02:00
2020-03-04 13:38:40 +01:00
size_t lock = kernel::get_hugepage_size() * hugepages;
ret = memory_lock(lock);
if (ret)
return ret;
return 0;
}
int memory_lock(size_t lock)
{
int ret;
#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 < lock) {
if (l.rlim_max < lock) {
if (getuid() != 0) {
2021-02-16 14:15:14 +01:00
logger->warn("Failed to in increase ressource limit of locked memory. Please increase manually by running as root:");
logger->warn(" $ ulimit -Hl {}", lock);
2019-06-27 15:45:45 +02:00
return 0;
}
l.rlim_max = lock;
}
l.rlim_cur = lock;
ret = setrlimit(RLIMIT_MEMLOCK, &l);
if (ret)
return ret;
2021-02-16 14:15:14 +01:00
logger->debug("Increased ressource limit of locked memory to {} bytes", lock);
}
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;
}
2019-10-26 13:07:02 +02:00
void * memory_alloc(size_t len, struct memory_type *m)
2016-09-15 21:25:32 -04:00
{
2019-10-26 13:07:02 +02:00
return memory_alloc_aligned(len, sizeof(void *), m);
2016-09-15 21:25:32 -04:00
}
2019-10-26 13:07:02 +02:00
void * memory_alloc_aligned(size_t len, size_t alignment, struct memory_type *m)
2018-08-20 18:31:27 +02:00
{
2019-10-26 13:07:02 +02:00
struct memory_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 memory_free(void *ptr)
2016-09-15 21:25:32 -04:00
{
int ret;
/* Find corresponding memory allocation entry */
2019-04-07 15:44:19 +02:00
struct memory_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 memory_allocation * memory_get_allocation(void *ptr)
{
2019-04-07 15:44:19 +02:00
return allocations[ptr];
2017-03-27 13:22:54 +02:00
}
2019-10-26 13:28:29 +02:00
struct memory_type *memory_default = nullptr;