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>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, 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>
|
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-07-02 19:00:55 +02:00
|
|
|
static struct hash_table allocations = { .state = STATE_DESTROYED };
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2017-03-11 23:39:00 -03:00
|
|
|
int memory_init(int hugepages)
|
2017-03-07 08:03:14 -04:00
|
|
|
{
|
2018-07-02 19:00:55 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-07-04 12:15:24 +02:00
|
|
|
info("Initialize memory sub-system");
|
|
|
|
|
2018-07-04 16:50:36 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
if (allocations.state == STATE_DESTROYED) {
|
|
|
|
ret = hash_table_init(&allocations, 100);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-20 02:45:16 -05:00
|
|
|
#ifdef __linux__
|
2018-07-03 18:28:21 +02:00
|
|
|
int pagecnt, pagesz;
|
2017-04-02 00:14:17 +02:00
|
|
|
struct rlimit l;
|
|
|
|
|
|
|
|
pagecnt = kernel_get_nr_hugepages();
|
2018-07-16 14:42:11 +02:00
|
|
|
if (pagecnt < hugepages) {
|
2018-07-04 12:15:24 +02:00
|
|
|
if (getuid() == 0) {
|
|
|
|
kernel_set_nr_hugepages(hugepages);
|
|
|
|
debug(LOG_MEM | 2, "Increased number of reserved hugepages from %d to %d", pagecnt, hugepages);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
warn("Failed to reserved hugepages. Please re-run as super-user or reserve manually via:");
|
|
|
|
warn(" $ echo %d > /proc/sys/vm/nr_hugepages", hugepages);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-02 00:14:17 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-02 00:14:17 +02:00
|
|
|
pagesz = kernel_get_hugepage_size();
|
|
|
|
if (pagesz < 0)
|
|
|
|
return -1;
|
|
|
|
|
2018-07-04 12:15:24 +02:00
|
|
|
/* Amount of memory which should be lockable */
|
|
|
|
size_t lock = pagesz * hugepages;
|
|
|
|
|
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) {
|
|
|
|
warn("Failed to in increase ressource limit of locked memory from %zu to %zu bytes", l.rlim_cur, lock);
|
|
|
|
warn("Please re-run as super-user or raise manually via:");
|
|
|
|
warn(" $ ulimit -Hl %zu", lock);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-02 00:14:17 +02:00
|
|
|
debug(LOG_MEM | 2, "Increased ressource limit of locked memory to %d bytes", pagesz * pagecnt);
|
2017-03-05 11:04:07 -04:00
|
|
|
}
|
2016-11-20 02:45:16 -05:00
|
|
|
#endif
|
|
|
|
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)
|
2016-09-22 21:20:21 -04:00
|
|
|
{
|
2018-07-02 19:00:55 +02:00
|
|
|
struct memory_allocation *ma = m->alloc(m, len, alignment);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
hash_table_insert(&allocations, ma->address, ma);
|
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
|
|
|
}
|