2019-10-26 13:35:40 +02:00
|
|
|
/** mmap memory allocator.
|
2018-07-02 14:17:50 +02: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
|
2018-07-02 14:17:50 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cerrno>
|
2018-07-02 14:17:50 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
/* Required to allocate hugepages on Apple OS X */
|
|
|
|
#ifdef __MACH__
|
|
|
|
#include <mach/vm_statistics.h>
|
2019-04-05 02:22:53 +02:00
|
|
|
#endif /* __MACH__ */
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2018-07-02 19:00:55 +02:00
|
|
|
#include <villas/memory.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2020-07-04 16:22:10 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/log.hpp>
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
using namespace villas;
|
2020-03-04 13:38:40 +01:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2018-11-23 19:42:18 +02:00
|
|
|
static size_t pgsz = -1;
|
|
|
|
static size_t hugepgsz = -1;
|
2021-02-16 14:15:14 +01:00
|
|
|
static Logger logger;
|
2018-11-23 19:42:18 +02:00
|
|
|
|
2019-10-28 15:10:31 +01:00
|
|
|
int memory_mmap_init(int hugepages)
|
2018-11-23 19:42:18 +02:00
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
logger = logging.get("memory:mmap");
|
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
pgsz = kernel::get_page_size();
|
2018-11-23 19:42:18 +02:00
|
|
|
if (pgsz < 0)
|
|
|
|
return -1;
|
|
|
|
|
2019-10-28 15:10:31 +01:00
|
|
|
if (hugepages > 0) {
|
2020-03-04 13:38:40 +01:00
|
|
|
hugepgsz = kernel::get_hugepage_size();
|
2019-10-29 23:25:44 +01:00
|
|
|
if (hugepgsz < 0) {
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("Failed to determine hugepage size.");
|
2019-10-29 23:25:44 +01:00
|
|
|
|
|
|
|
memory_default = &memory_mmap;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-23 19:42:18 +02:00
|
|
|
|
2018-12-04 10:46:02 +01:00
|
|
|
#if defined(__linux__) && defined(__x86_64__)
|
2019-10-29 23:25:44 +01:00
|
|
|
int ret, pagecnt;
|
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
pagecnt = kernel::get_nr_hugepages();
|
2019-10-29 23:25:44 +01:00
|
|
|
if (pagecnt < hugepages) {
|
|
|
|
if (getuid() == 0) {
|
2020-03-04 13:38:40 +01:00
|
|
|
ret = kernel::set_nr_hugepages(hugepages);
|
2019-10-29 23:25:44 +01:00
|
|
|
if (ret) {
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("Failed to increase number of reserved hugepages");
|
2019-10-29 23:25:44 +01:00
|
|
|
memory_default = &memory_mmap;
|
|
|
|
}
|
2019-10-28 15:10:31 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->debug("Increased number of reserved hugepages from {} to {}", pagecnt, hugepages);
|
2019-10-29 23:25:44 +01:00
|
|
|
memory_default = &memory_mmap_hugetlb;
|
2019-10-29 09:20:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("Failed to reserved hugepages. Please reserve manually by running as root:");
|
|
|
|
logger->warn(" $ echo {} > /proc/sys/vm/nr_hugepages", hugepages);
|
2019-10-29 23:25:44 +01:00
|
|
|
memory_default = &memory_mmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memory_default = &memory_mmap_hugetlb;
|
2020-07-05 13:24:23 +01:00
|
|
|
#else
|
|
|
|
memory_default = &memory_mmap;
|
2018-12-04 10:46:02 +01:00
|
|
|
#endif
|
2019-10-29 09:20:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn("Hugepage allocator disabled.");
|
2019-10-29 23:25:44 +01:00
|
|
|
memory_default = &memory_mmap;
|
2019-10-28 15:10:31 +01:00
|
|
|
}
|
2019-10-29 09:20:57 +01:00
|
|
|
|
2018-11-23 19:42:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-26 13:35:40 +02:00
|
|
|
/** Allocate memory backed by mmaps with malloc() like interface */
|
|
|
|
static struct memory_allocation * memory_mmap_alloc(size_t len, size_t alignment, struct memory_type *m)
|
2018-07-02 14:17:50 +02:00
|
|
|
{
|
2019-02-12 17:27:09 +01:00
|
|
|
int flags, fd;
|
2018-11-23 19:42:18 +02:00
|
|
|
size_t sz;
|
2018-10-21 12:57:38 +01:00
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
auto *ma = new struct memory_allocation;
|
2018-11-23 19:42:18 +02:00
|
|
|
if (!ma)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2018-11-23 19:42:18 +02:00
|
|
|
|
2019-10-26 13:35:40 +02:00
|
|
|
if (m->flags & (int) MemoryFlags::HUGEPAGE) {
|
2018-11-23 19:42:18 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB;
|
|
|
|
#else
|
|
|
|
flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
|
|
|
#endif
|
2018-07-02 14:17:50 +02:00
|
|
|
|
|
|
|
#ifdef __MACH__
|
2018-11-23 19:42:18 +02:00
|
|
|
fd = VM_FLAGS_SUPERPAGE_SIZE_2MB;
|
|
|
|
#else
|
|
|
|
fd = -1;
|
2018-07-02 14:17:50 +02:00
|
|
|
#endif
|
2018-11-23 19:42:18 +02:00
|
|
|
sz = hugepgsz;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
|
|
|
fd = -1;
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2018-11-23 19:42:18 +02:00
|
|
|
sz = pgsz;
|
|
|
|
}
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2019-10-26 13:35:40 +02:00
|
|
|
/** We must make sure that len is a multiple of the page size
|
2018-07-02 14:17:50 +02:00
|
|
|
*
|
|
|
|
* See: https://lkml.org/lkml/2014/10/22/925
|
|
|
|
*/
|
2018-11-23 19:42:18 +02:00
|
|
|
ma->length = ALIGN(len, sz);
|
|
|
|
ma->alignment = ALIGN(alignment, sz);
|
2018-07-02 19:00:55 +02:00
|
|
|
ma->type = m;
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
ma->address = mmap(nullptr, ma->length, PROT_READ | PROT_WRITE, flags, fd, 0);
|
2018-07-02 19:00:55 +02:00
|
|
|
if (ma->address == MAP_FAILED) {
|
2020-01-21 16:26:51 +01:00
|
|
|
delete ma;
|
2019-10-26 13:35:40 +02:00
|
|
|
return nullptr;
|
2018-10-21 12:57:38 +01:00
|
|
|
}
|
2018-08-13 15:26:24 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
return ma;
|
|
|
|
}
|
|
|
|
|
2019-10-26 13:35:40 +02:00
|
|
|
static int memory_mmap_free(struct memory_allocation *ma, struct memory_type *m)
|
2018-07-02 19:00:55 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = munmap(ma->address, ma->length);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 13:35:40 +02:00
|
|
|
struct memory_type memory_mmap = {
|
|
|
|
.name = "mmap",
|
|
|
|
.flags = (int) MemoryFlags::MMAP,
|
|
|
|
.alignment = 12, /* 4k page */
|
|
|
|
.alloc = memory_mmap_alloc,
|
|
|
|
.free = memory_mmap_free
|
|
|
|
};
|
|
|
|
|
|
|
|
struct memory_type memory_mmap_hugetlb = {
|
2019-10-29 09:21:13 +01:00
|
|
|
.name = "mmap_hugetlb",
|
2019-06-23 16:13:23 +02:00
|
|
|
.flags = (int) MemoryFlags::MMAP | (int) MemoryFlags::HUGEPAGE,
|
2019-04-07 15:13:40 +02:00
|
|
|
.alignment = 21, /* 2 MiB hugepage */
|
2019-10-26 13:35:40 +02:00
|
|
|
.alloc = memory_mmap_alloc,
|
|
|
|
.free = memory_mmap_free
|
2018-07-02 14:17:50 +02:00
|
|
|
};
|