/** Hugepage memory allocator. * * @author Steffen Vogel * @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC * @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 . *********************************************************************************/ #include #include #include #include #include #include #include #include /* Required to allocate hugepages on Apple OS X */ #ifdef __MACH__ #include #elif defined(__linux__) #include #endif #include #include #include #define HUGEPAGESIZE (1 << 22) /* 2 MiB */ /** Allocate memory backed by hugepages with malloc() like interface */ static struct memory_allocation * memory_hugepage_alloc(struct memory_type *m, size_t len, size_t alignment) { int prot = PROT_READ | PROT_WRITE; int flags = MAP_PRIVATE | MAP_ANONYMOUS; #ifdef __MACH__ flags |= VM_FLAGS_SUPERPAGE_SIZE_2MB; #elif defined(__linux__) flags |= MAP_HUGETLB; if (getuid() == 0) flags |= MAP_LOCKED; #endif struct memory_allocation *ma = alloc(sizeof(struct memory_allocation)); if (!ma) return NULL; /** We must make sure that len is a multiple of the hugepage size * * See: https://lkml.org/lkml/2014/10/22/925 */ ma->length = ALIGN(len, HUGEPAGESIZE); ma->alignment = alignment; ma->type = m; ma->address = mmap(NULL, ma->length, prot, flags, -1, 0); if (ma->address == MAP_FAILED) { free(ma); return NULL; } return ma; } static int memory_hugepage_free(struct memory_type *m, struct memory_allocation *ma) { int ret; ret = munmap(ma->address, ma->length); if (ret) return ret; return 0; } struct memory_type memory_hugepage = { .name = "mmap_hugepages", .flags = MEMORY_MMAP | MEMORY_HUGEPAGE, .alloc = memory_hugepage_alloc, .free = memory_hugepage_free, .alignment = 21 /* 2 MiB hugepage */ };