From a0a855463d34c9b8b30e825125fa8dc2bc9c52de Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 18 Aug 2024 14:00:45 -0400 Subject: [PATCH] mmap atomic --- CMakeLists.txt | 2 +- include/blt/std/allocator.h | 76 +--------------------------- include/blt/std/atomic_allocator.h | 43 ++++++++++++++++ include/blt/std/mmap.h | 51 +++++++++++++++++++ libraries/parallel-hashmap | 2 +- src/blt/std/mmap.cpp | 81 ++++++++++++++++++++++++++++++ 6 files changed, 179 insertions(+), 76 deletions(-) create mode 100644 include/blt/std/atomic_allocator.h create mode 100644 include/blt/std/mmap.h create mode 100644 src/blt/std/mmap.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3188a72..fc25eb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.18.35) +set(BLT_VERSION 0.18.36) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/std/allocator.h b/include/blt/std/allocator.h index 255d775..4b54218 100644 --- a/include/blt/std/allocator.h +++ b/include/blt/std/allocator.h @@ -25,18 +25,11 @@ #include #include #include -// TODO: remove -//#include + #include #include #include #include "logging.h" #include - - #ifdef __unix__ - - #include - - #endif namespace blt { @@ -543,77 +536,12 @@ namespace blt } }; - // size of 2mb in bytes - inline constexpr blt::size_t BLT_2MB_SIZE = 4096 * 512; - - /** - * Logging function used for handling mmap errors. call after a failed mmap call. - * @param LOG_FUNC function to log with, must be a BLT_*_STREAM - */ - template - static void handle_mmap_error(LOG_FUNC func = BLT_ERROR_STREAM) - { -#define BLT_WRITE(arg) func << arg << '\n'; - switch (errno) - { - case EACCES: - BLT_WRITE("fd not set to open!"); - break; - case EAGAIN: - BLT_WRITE("The file has been locked, or too much memory has been locked"); - break; - case EBADF: - BLT_WRITE("fd is not a valid file descriptor"); - break; - case EEXIST: - BLT_WRITE("MAP_FIXED_NOREPLACE was specified in flags, and the range covered " - "by addr and length clashes with an existing mapping."); - break; - case EINVAL: - BLT_WRITE("We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary)."); - BLT_WRITE("Or length was 0"); - BLT_WRITE("Or flags contained none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE."); - break; - case ENFILE: - BLT_WRITE("The system-wide limit on the total number of open files has been reached."); - break; - case ENODEV: - BLT_WRITE("The underlying filesystem of the specified file does not support memory mapping."); - break; - case ENOMEM: - BLT_WRITE("No memory is available."); - BLT_WRITE("Or The process's maximum number of mappings would have been exceeded. " - "This error can also occur for munmap(), when unmapping a region in the middle of an existing mapping, " - "since this results in two smaller mappings on either side of the region being unmapped."); - BLT_WRITE("Or The process's RLIMIT_DATA limit, described in getrlimit(2), would have been exceeded."); - BLT_WRITE("Or We don't like addr, because it exceeds the virtual address space of the CPU."); - break; - case EOVERFLOW: - BLT_WRITE("On 32-bit architecture together with the large file extension (i.e., using 64-bit off_t): " - "the number of pages used for length plus number of " - "pages used for offset would overflow unsigned long (32 bits)."); - break; - case EPERM: - BLT_WRITE("The prot argument asks for PROT_EXEC but the mapped area " - "belongs to a file on a filesystem that was mounted no-exec."); - BLT_WRITE("Or The operation_t was prevented by a file seal"); - BLT_WRITE("Or The MAP_HUGETLB flag was specified, but the caller " - "was not privileged (did not have the CAP_IPC_LOCK capability) " - "and is not a member of the sysctl_hugetlb_shm_group group; " - "see the description of /proc/sys/vm/sysctl_hugetlb_shm_group"); - break; - case ETXTBSY: - BLT_WRITE("MAP_DENYWRITE was set but the object specified by fd is open for writing."); - break; - } - } - template static inline T* allocate_huge_page(blt::size_t BLOCK_SIZE, blt::size_t HUGE_PAGE_SIZE = BLT_2MB_SIZE) { BLT_ASSERT((BLOCK_SIZE & (HUGE_PAGE_SIZE - 1)) == 0 && "Must be multiple of the huge page size!"); T* buffer = static_cast(mmap(nullptr, BLOCK_SIZE, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0)); + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0)); // if we fail to allocate a huge page we can try to allocate normally if (buffer == MAP_FAILED) { diff --git a/include/blt/std/atomic_allocator.h b/include/blt/std/atomic_allocator.h new file mode 100644 index 0000000..5406074 --- /dev/null +++ b/include/blt/std/atomic_allocator.h @@ -0,0 +1,43 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * 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 + * (at your option) 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 . + */ + +#ifndef BLT_ATOMIC_ALLOCATOR_H +#define BLT_ATOMIC_ALLOCATOR_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "logging.h" +#include + + +namespace blt +{ + + + +} + +#endif //BLT_ATOMIC_ALLOCATOR_H diff --git a/include/blt/std/mmap.h b/include/blt/std/mmap.h new file mode 100644 index 0000000..a8cc4a6 --- /dev/null +++ b/include/blt/std/mmap.h @@ -0,0 +1,51 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * 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 + * (at your option) 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 . + */ + +#ifndef BLT_MMAP_H +#define BLT_MMAP_H + +#include +#include + +#ifdef __unix__ + + #include + +#endif + +// size of 2mb in bytes +inline constexpr blt::size_t BLT_2MB_SIZE = 4096 * 512; + +namespace blt +{ + + /** + * Logging function used for handling mmap errors. call after a failed mmap call. + */ + void handle_mmap_error(blt::logging::logger func); + + inline static constexpr blt::size_t align_to_size(blt::size_t in, blt::size_t align) + { + return 0; + } + + void* allocate_2mb_huge_pages(blt::size_t bytes); + +} + +#endif //BLT_MMAP_H diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index d88c5e1..8a889d3 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit d88c5e15079047777b418132ece5879e7c9aaa2b +Subproject commit 8a889d3699b3c09ade435641fb034427f3fd12b6 diff --git a/src/blt/std/mmap.cpp b/src/blt/std/mmap.cpp new file mode 100644 index 0000000..7f045fc --- /dev/null +++ b/src/blt/std/mmap.cpp @@ -0,0 +1,81 @@ +/* + * + * Copyright (C) 2024 Brett Terpstra + * + * 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 + * (at your option) 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 + +namespace blt +{ + + void handle_mmap_error(blt::logging::logger func) + { + { +#define BLT_WRITE(arg) func << arg << '\n'; + switch (errno) + { + case EACCES: + BLT_WRITE("fd not set to open!"); + break; + case EAGAIN: + BLT_WRITE("The file has been locked, or too much memory has been locked"); + break; + case EBADF: + BLT_WRITE("fd is not a valid file descriptor"); + break; + case EEXIST: + BLT_WRITE("MAP_FIXED_NOREPLACE was specified in flags, and the range covered " + "by addr and length clashes with an existing mapping."); + break; + case EINVAL: + BLT_WRITE("We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary)."); + BLT_WRITE("Or length was 0"); + BLT_WRITE("Or flags contained none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE."); + break; + case ENFILE: + BLT_WRITE("The system-wide limit on the total number of open files has been reached."); + break; + case ENODEV: + BLT_WRITE("The underlying filesystem of the specified file does not support memory mapping."); + break; + case ENOMEM: + BLT_WRITE("No memory is available."); + BLT_WRITE("Or The process's maximum number of mappings would have been exceeded. " + "This error can also occur for munmap(), when unmapping a region in the middle of an existing mapping, " + "since this results in two smaller mappings on either side of the region being unmapped."); + BLT_WRITE("Or The process's RLIMIT_DATA limit, described in getrlimit(2), would have been exceeded."); + BLT_WRITE("Or We don't like addr, because it exceeds the virtual address space of the CPU."); + break; + case EOVERFLOW: + BLT_WRITE("On 32-bit architecture together with the large file extension (i.e., using 64-bit off_t): " + "the number of pages used for length plus number of " + "pages used for offset would overflow unsigned long (32 bits)."); + break; + case EPERM: + BLT_WRITE("The prot argument asks for PROT_EXEC but the mapped area " + "belongs to a file on a filesystem that was mounted no-exec."); + BLT_WRITE("Or The operation_t was prevented by a file seal"); + BLT_WRITE("Or The MAP_HUGETLB flag was specified, but the caller " + "was not privileged (did not have the CAP_IPC_LOCK capability) " + "and is not a member of the sysctl_hugetlb_shm_group group; " + "see the description of /proc/sys/vm/sysctl_hugetlb_shm_group"); + break; + case ETXTBSY: + BLT_WRITE("MAP_DENYWRITE was set but the object specified by fd is open for writing."); + break; + } + } + } +} \ No newline at end of file