Brett 2024-09-04 02:42:45 -04:00
parent a7645d9dde
commit 82cc1aff96
5 changed files with 111 additions and 78 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake)
set(BLT_VERSION 0.19.5)
set(BLT_VERSION 0.19.6)
set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT)

View File

@ -31,6 +31,12 @@
#include "logging.h"
#include <cstdlib>
#ifdef __unix__
#include <sys/mman.h>
#endif
namespace blt
{
@ -548,7 +554,7 @@ namespace blt
if constexpr (WARN_ON_FAIL)
{
BLT_WARN_STREAM << "We failed to allocate huge pages\n";
handle_mmap_error(BLT_WARN_STREAM);
BLT_WARN_STREAM << handle_mmap_error();
BLT_WARN_STREAM << "\033[1;31mYou should attempt to enable "
"huge pages as this will allocate normal pages and double the memory usage!\033[22m\n";
}
@ -557,8 +563,7 @@ namespace blt
if (buffer == MAP_FAILED)
{
BLT_ERROR_STREAM << "Failed to allocate normal pages\n";
handle_mmap_error(BLT_ERROR_STREAM);
throw std::bad_alloc();
throw bad_alloc_t(handle_mmap_error());
}
if constexpr (WARN_ON_FAIL)
{
@ -768,7 +773,7 @@ namespace blt
if (munmap(p, BLOCK_SIZE))
{
BLT_ERROR_STREAM << "FAILED TO DEALLOCATE BLOCK\n";
handle_mmap_error(BLT_ERROR_STREAM);
throw bad_alloc_t(handle_mmap_error());
}
} else
free(p);

View File

@ -35,7 +35,7 @@
namespace blt
{
// template<typename Alloc = blt::aligned_huge_allocator>
// class atomic_bump_allocator
// {

View File

@ -23,22 +23,43 @@
#include <blt/std/types.h>
#include <cstdlib>
#ifdef __unix__
#include <sys/mman.h>
#endif
// size of 2mb in bytes
inline constexpr blt::size_t BLT_2MB_SIZE = 4096 * 512;
inline constexpr blt::size_t BLT_2MB_SIZE = 2048 * 1024;
inline constexpr blt::size_t BLT_1GB_SIZE = 1048576 * 1024;
namespace blt
{
enum class huge_page_t : blt::u64
{
BLT_2MB_PAGE,
BLT_1GB_PAGE
};
class bad_alloc_t : public std::exception
{
public:
bad_alloc_t() = default;
explicit bad_alloc_t(std::string_view str): str(str)
{}
explicit bad_alloc_t(std::string str): str(std::move(str))
{}
[[nodiscard]] const char* what() const noexcept override
{
return str.c_str();
}
private:
std::string str;
};
/**
* Logging function used for handling mmap errors. call after a failed mmap call.
*/
void handle_mmap_error(blt::logging::logger func);
std::string handle_mmap_error();
inline static constexpr blt::size_t align_size_to(blt::size_t size, blt::size_t align)
{
@ -46,16 +67,16 @@ namespace blt
return (size & MASK) + align;
}
void* allocate_2mb_huge_pages(blt::size_t bytes);
void* allocate_huge_pages(huge_page_t page_type, blt::size_t bytes);
void mmap_free(void* ptr, blt::size_t bytes);
class mmap_huge_allocator
{
public:
void* allocate(blt::size_t bytes) // NOLINT
void* allocate(blt::size_t bytes, huge_page_t page_type) // NOLINT
{
return allocate_2mb_huge_pages(bytes);
return allocate_huge_pages(page_type, bytes);
}
void deallocate(void* ptr, blt::size_t bytes) // NOLINT

View File

@ -17,77 +17,85 @@
*/
#include <blt/std/mmap.h>
#ifdef __unix__
#include <sys/mman.h>
#endif
namespace blt
{
void handle_mmap_error(blt::logging::logger func)
std::string handle_mmap_error()
{
std::string str;
#define BLT_WRITE(arg) str += arg; str += '\n';
switch (errno)
{
#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;
}
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;
}
return str;
}
void* allocate_2mb_huge_pages(blt::size_t bytes)
void* allocate_huge_pages(huge_page_t page_type, blt::size_t bytes)
{
#ifdef __unix__
auto buffer = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0);
auto type = (21 << MAP_HUGE_SHIFT);
if (page_type == huge_page_t::BLT_1GB_PAGE)
type = (30 << MAP_HUGE_SHIFT);
auto buffer = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | type | MAP_POPULATE, -1, 0);
if (buffer == MAP_FAILED)
{
BLT_WARN_STREAM << "Failed to allocate huge pages, reason:\n";
handle_mmap_error(BLT_WARN_STREAM);
throw std::bad_alloc();
throw bad_alloc_t(handle_mmap_error());
}
return buffer;
#else
@ -100,8 +108,7 @@ namespace blt
if (munmap(ptr, bytes))
{
BLT_ERROR_STREAM << "Failed to deallocate\n";
handle_mmap_error(BLT_ERROR_STREAM);
throw std::bad_alloc();
throw bad_alloc_t(handle_mmap_error());
}
}
}