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>
|
2019-01-13 00:42:39 +01:00
|
|
|
* @copyright 2014-2019, 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-05-05 19:24:16 +00:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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/>.
|
2017-03-03 20:20:13 -04:00
|
|
|
*********************************************************************************/
|
2016-09-15 21:25:32 -04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2017-05-24 14:47:24 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2017-07-24 19:21:57 +02:00
|
|
|
#include <strings.h>
|
|
|
|
|
2017-04-02 00:14:17 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2019-02-12 17:27:09 +01:00
|
|
|
#include <sys/mman.h>
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/log.h>
|
|
|
|
#include <villas/memory.h>
|
|
|
|
#include <villas/utils.h>
|
2018-07-02 19:00:55 +02:00
|
|
|
#include <villas/hash_table.h>
|
2018-07-02 14:17:50 +02:00
|
|
|
#include <villas/kernel/kernel.h>
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
static struct hash_table allocations = { .state = STATE_DESTROYED };
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
__attribute__((constructor))
|
|
|
|
static void init_allocations()
|
2017-03-07 08:03:14 -04:00
|
|
|
{
|
2018-08-20 18:31:27 +02:00
|
|
|
hash_table_init(&allocations, 100);
|
|
|
|
}
|
2018-07-04 12:15:24 +02:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
__attribute__((destructor))
|
|
|
|
static void destroy_allocations()
|
|
|
|
{
|
|
|
|
/** @todo: Release remaining allocations? */
|
|
|
|
hash_table_destroy(&allocations, NULL, false);
|
|
|
|
}
|
2018-07-04 16:50:36 +02:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
int memory_init(int hugepages)
|
|
|
|
{
|
2018-11-23 19:42:18 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
info("Initialize memory sub-system: #hugepages=%d", hugepages);
|
2018-07-02 19:00:55 +02:00
|
|
|
|
2018-12-04 10:46:02 +01:00
|
|
|
ret = memory_hugepage_init(hugepages);
|
2018-11-23 19:42:18 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-12-04 10:46:02 +01:00
|
|
|
size_t lock = kernel_get_hugepage_size() * hugepages;
|
2018-07-04 12:15:24 +02:00
|
|
|
|
2018-12-04 10:46:02 +01:00
|
|
|
ret = memory_lock(lock);
|
|
|
|
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
|
|
|
|
2018-12-04 10:46:02 +01:00
|
|
|
int memory_lock(size_t lock)
|
|
|
|
{
|
|
|
|
int ret;
|
2019-02-12 17:27:09 +01:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
|
|
|
#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
|
|
|
|
2018-07-04 12:15:24 +02:00
|
|
|
if (l.rlim_cur < lock) {
|
|
|
|
if (l.rlim_max < lock) {
|
|
|
|
if (getuid() != 0) {
|
2019-02-12 17:27:09 +01:00
|
|
|
warning("Failed to in increase ressource limit of locked memory. Please increase manually by running as root:");
|
2018-10-21 21:36:08 +01:00
|
|
|
warning(" $ ulimit -Hl %zu", lock);
|
2018-07-04 12:15:24 +02:00
|
|
|
|
2019-02-12 17:27:09 +01:00
|
|
|
goto out;
|
2018-07-04 12:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l.rlim_max = lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
l.rlim_cur = lock;
|
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
|
|
|
|
2018-12-02 03:04:36 +01:00
|
|
|
debug(LOG_MEM | 2, "Increased ressource limit of locked memory to %zd bytes", lock);
|
2017-03-05 11:04:07 -04:00
|
|
|
}
|
2019-02-12 17:27:09 +01:00
|
|
|
#endif /* __arm__ */
|
2019-03-09 12:44:50 +01:00
|
|
|
out:
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
void * memory_alloc(struct memory_type *m, size_t len)
|
2016-09-15 21:25:32 -04:00
|
|
|
{
|
2018-07-02 19:00:55 +02:00
|
|
|
return memory_alloc_aligned(m, len, sizeof(void *));
|
2016-09-15 21:25:32 -04:00
|
|
|
}
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
void * memory_alloc_aligned(struct memory_type *m, size_t len, size_t alignment)
|
2018-08-20 18:31:27 +02:00
|
|
|
{
|
2018-07-18 15:15:08 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
struct memory_allocation *ma = m->alloc(m, len, alignment);
|
2018-08-20 18:31:27 +02:00
|
|
|
if (ma == NULL) {
|
2018-12-04 10:46:02 +01:00
|
|
|
warning("Memory allocation of type %s failed. reason=%s", m->name, strerror(errno) );
|
2018-08-20 18:31:27 +02:00
|
|
|
return NULL;
|
2018-07-18 15:15:08 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-07-18 15:15:08 +02:00
|
|
|
ret = hash_table_insert(&allocations, ma->address, ma);
|
2018-08-20 18:31:27 +02:00
|
|
|
if (ret) {
|
2018-12-04 10:46:02 +01:00
|
|
|
warning("Inserting into hash table failed!");
|
2018-08-20 18:31:27 +02:00
|
|
|
return NULL;
|
2018-07-18 15:15:08 +02:00
|
|
|
}
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
debug(LOG_MEM | 5, "Allocated %#zx bytes of %#zx-byte-aligned %s memory: %p", 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
|
|
|
}
|
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
int memory_free(void *ptr)
|
2016-09-15 21:25:32 -04:00
|
|
|
{
|
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 */
|
|
|
|
struct memory_allocation *ma = (struct memory_allocation *) hash_table_lookup(&allocations, ptr);
|
|
|
|
if (!ma)
|
|
|
|
return -1;
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2018-07-16 21:16:00 +02:00
|
|
|
debug(LOG_MEM | 5, "Releasing %#zx bytes of %s memory: %p", ma->length, ma->type->name, ma->address);
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = ma->type->free(ma->type, ma);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
/* Remove allocation entry */
|
2018-07-09 12:54:18 +02:00
|
|
|
ret = hash_table_delete(&allocations, ptr);
|
2018-07-02 19:00:55 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-03-31 18:04:08 +02:00
|
|
|
|
2018-07-09 12:54:18 +02:00
|
|
|
free(ma);
|
|
|
|
|
2016-09-15 21:25:32 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-04 15:15:24 +02:00
|
|
|
struct memory_allocation * memory_get_allocation(void *ptr)
|
2017-03-28 11:30:57 +02:00
|
|
|
{
|
2018-07-04 15:15:24 +02:00
|
|
|
struct memory_allocation *ma = (struct memory_allocation *) hash_table_lookup(&allocations, ptr);
|
|
|
|
return ma;
|
2017-03-27 13:22:54 +02:00
|
|
|
}
|
2018-10-21 12:57:02 +01:00
|
|
|
|
|
|
|
struct memory_type * memory_type_lookup(enum memory_type_flags flags)
|
|
|
|
{
|
|
|
|
if (flags & MEMORY_HUGEPAGE)
|
|
|
|
return &memory_hugepage;
|
|
|
|
else if (flags & MEMORY_HEAP)
|
|
|
|
return &memory_heap;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|