mmap
parent
a7645d9dde
commit
82cc1aff96
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
include(cmake/color.cmake)
|
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_TEST_VERSION 0.0.1)
|
||||||
|
|
||||||
set(BLT_TARGET BLT)
|
set(BLT_TARGET BLT)
|
||||||
|
|
|
@ -31,6 +31,12 @@
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#ifdef __unix__
|
||||||
|
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace blt
|
namespace blt
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -548,7 +554,7 @@ namespace blt
|
||||||
if constexpr (WARN_ON_FAIL)
|
if constexpr (WARN_ON_FAIL)
|
||||||
{
|
{
|
||||||
BLT_WARN_STREAM << "We failed to allocate huge pages\n";
|
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 "
|
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";
|
"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)
|
if (buffer == MAP_FAILED)
|
||||||
{
|
{
|
||||||
BLT_ERROR_STREAM << "Failed to allocate normal pages\n";
|
BLT_ERROR_STREAM << "Failed to allocate normal pages\n";
|
||||||
handle_mmap_error(BLT_ERROR_STREAM);
|
throw bad_alloc_t(handle_mmap_error());
|
||||||
throw std::bad_alloc();
|
|
||||||
}
|
}
|
||||||
if constexpr (WARN_ON_FAIL)
|
if constexpr (WARN_ON_FAIL)
|
||||||
{
|
{
|
||||||
|
@ -768,7 +773,7 @@ namespace blt
|
||||||
if (munmap(p, BLOCK_SIZE))
|
if (munmap(p, BLOCK_SIZE))
|
||||||
{
|
{
|
||||||
BLT_ERROR_STREAM << "FAILED TO DEALLOCATE BLOCK\n";
|
BLT_ERROR_STREAM << "FAILED TO DEALLOCATE BLOCK\n";
|
||||||
handle_mmap_error(BLT_ERROR_STREAM);
|
throw bad_alloc_t(handle_mmap_error());
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
free(p);
|
free(p);
|
||||||
|
|
|
@ -23,22 +23,43 @@
|
||||||
#include <blt/std/types.h>
|
#include <blt/std/types.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
#ifdef __unix__
|
|
||||||
|
|
||||||
#include <sys/mman.h>
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// size of 2mb in bytes
|
// 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
|
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.
|
* 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)
|
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;
|
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);
|
void mmap_free(void* ptr, blt::size_t bytes);
|
||||||
|
|
||||||
class mmap_huge_allocator
|
class mmap_huge_allocator
|
||||||
{
|
{
|
||||||
public:
|
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
|
void deallocate(void* ptr, blt::size_t bytes) // NOLINT
|
||||||
|
|
|
@ -17,77 +17,85 @@
|
||||||
*/
|
*/
|
||||||
#include <blt/std/mmap.h>
|
#include <blt/std/mmap.h>
|
||||||
|
|
||||||
|
#ifdef __unix__
|
||||||
|
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace blt
|
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';
|
case EACCES:
|
||||||
switch (errno)
|
BLT_WRITE("fd not set to open!");
|
||||||
{
|
break;
|
||||||
case EACCES:
|
case EAGAIN:
|
||||||
BLT_WRITE("fd not set to open!");
|
BLT_WRITE("The file has been locked, or too much memory has been locked");
|
||||||
break;
|
break;
|
||||||
case EAGAIN:
|
case EBADF:
|
||||||
BLT_WRITE("The file has been locked, or too much memory has been locked");
|
BLT_WRITE("fd is not a valid file descriptor");
|
||||||
break;
|
break;
|
||||||
case EBADF:
|
case EEXIST:
|
||||||
BLT_WRITE("fd is not a valid file descriptor");
|
BLT_WRITE("MAP_FIXED_NOREPLACE was specified in flags, and the range covered "
|
||||||
break;
|
"by addr and length clashes with an existing mapping.");
|
||||||
case EEXIST:
|
break;
|
||||||
BLT_WRITE("MAP_FIXED_NOREPLACE was specified in flags, and the range covered "
|
case EINVAL:
|
||||||
"by addr and length clashes with an existing mapping.");
|
BLT_WRITE("We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary).");
|
||||||
break;
|
BLT_WRITE("Or length was 0");
|
||||||
case EINVAL:
|
BLT_WRITE("Or flags contained none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE.");
|
||||||
BLT_WRITE("We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary).");
|
break;
|
||||||
BLT_WRITE("Or length was 0");
|
case ENFILE:
|
||||||
BLT_WRITE("Or flags contained none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE.");
|
BLT_WRITE("The system-wide limit on the total number of open files has been reached.");
|
||||||
break;
|
break;
|
||||||
case ENFILE:
|
case ENODEV:
|
||||||
BLT_WRITE("The system-wide limit on the total number of open files has been reached.");
|
BLT_WRITE("The underlying filesystem of the specified file does not support memory mapping.");
|
||||||
break;
|
break;
|
||||||
case ENODEV:
|
case ENOMEM:
|
||||||
BLT_WRITE("The underlying filesystem of the specified file does not support memory mapping.");
|
BLT_WRITE("No memory is available.");
|
||||||
break;
|
BLT_WRITE("Or The process's maximum number of mappings would have been exceeded. "
|
||||||
case ENOMEM:
|
"This error can also occur for munmap(), when unmapping a region in the middle of an existing mapping, "
|
||||||
BLT_WRITE("No memory is available.");
|
"since this results in two smaller mappings on either side of the region being unmapped.");
|
||||||
BLT_WRITE("Or The process's maximum number of mappings would have been exceeded. "
|
BLT_WRITE("Or The process's RLIMIT_DATA limit, described in getrlimit(2), would have been exceeded.");
|
||||||
"This error can also occur for munmap(), when unmapping a region in the middle of an existing mapping, "
|
BLT_WRITE("Or We don't like addr, because it exceeds the virtual address space of the CPU.");
|
||||||
"since this results in two smaller mappings on either side of the region being unmapped.");
|
break;
|
||||||
BLT_WRITE("Or The process's RLIMIT_DATA limit, described in getrlimit(2), would have been exceeded.");
|
case EOVERFLOW:
|
||||||
BLT_WRITE("Or We don't like addr, because it exceeds the virtual address space of the CPU.");
|
BLT_WRITE("On 32-bit architecture together with the large file extension (i.e., using 64-bit off_t): "
|
||||||
break;
|
"the number of pages used for length plus number of "
|
||||||
case EOVERFLOW:
|
"pages used for offset would overflow unsigned long (32 bits).");
|
||||||
BLT_WRITE("On 32-bit architecture together with the large file extension (i.e., using 64-bit off_t): "
|
break;
|
||||||
"the number of pages used for length plus number of "
|
case EPERM:
|
||||||
"pages used for offset would overflow unsigned long (32 bits).");
|
BLT_WRITE("The prot argument asks for PROT_EXEC but the mapped area "
|
||||||
break;
|
"belongs to a file on a filesystem that was mounted no-exec.");
|
||||||
case EPERM:
|
BLT_WRITE("Or The operation_t was prevented by a file seal");
|
||||||
BLT_WRITE("The prot argument asks for PROT_EXEC but the mapped area "
|
BLT_WRITE("Or The MAP_HUGETLB flag was specified, but the caller "
|
||||||
"belongs to a file on a filesystem that was mounted no-exec.");
|
"was not privileged (did not have the CAP_IPC_LOCK capability) "
|
||||||
BLT_WRITE("Or The operation_t was prevented by a file seal");
|
"and is not a member of the sysctl_hugetlb_shm_group group; "
|
||||||
BLT_WRITE("Or The MAP_HUGETLB flag was specified, but the caller "
|
"see the description of /proc/sys/vm/sysctl_hugetlb_shm_group");
|
||||||
"was not privileged (did not have the CAP_IPC_LOCK capability) "
|
break;
|
||||||
"and is not a member of the sysctl_hugetlb_shm_group group; "
|
case ETXTBSY:
|
||||||
"see the description of /proc/sys/vm/sysctl_hugetlb_shm_group");
|
BLT_WRITE("MAP_DENYWRITE was set but the object specified by fd is open for writing.");
|
||||||
break;
|
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__
|
#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)
|
if (buffer == MAP_FAILED)
|
||||||
{
|
{
|
||||||
BLT_WARN_STREAM << "Failed to allocate huge pages, reason:\n";
|
throw bad_alloc_t(handle_mmap_error());
|
||||||
handle_mmap_error(BLT_WARN_STREAM);
|
|
||||||
throw std::bad_alloc();
|
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
#else
|
#else
|
||||||
|
@ -100,8 +108,7 @@ namespace blt
|
||||||
if (munmap(ptr, bytes))
|
if (munmap(ptr, bytes))
|
||||||
{
|
{
|
||||||
BLT_ERROR_STREAM << "Failed to deallocate\n";
|
BLT_ERROR_STREAM << "Failed to deallocate\n";
|
||||||
handle_mmap_error(BLT_ERROR_STREAM);
|
throw bad_alloc_t(handle_mmap_error());
|
||||||
throw std::bad_alloc();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue