From 69068165f49ca204025410ab02546318997edaa3 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 15 Apr 2026 16:53:36 +0200 Subject: kernel: improve bht speed using memory mapping --- .../filesystem/storage_boot_module_fixture.cpp | 105 +++++++++++++-------- .../filesystem/storage_boot_module_vfs_fixture.cpp | 7 +- 2 files changed, 70 insertions(+), 42 deletions(-) (limited to 'kernel/src/test_support') diff --git a/kernel/src/test_support/filesystem/storage_boot_module_fixture.cpp b/kernel/src/test_support/filesystem/storage_boot_module_fixture.cpp index a139f63..cd8360b 100644 --- a/kernel/src/test_support/filesystem/storage_boot_module_fixture.cpp +++ b/kernel/src/test_support/filesystem/storage_boot_module_fixture.cpp @@ -8,17 +8,72 @@ #include "kernel/test_support/boot_modules.hpp" #include "kernel/test_support/devices/storage/management.hpp" -#include -#include - #include +#include #include -#include -#include +#include #include +#include +#include +#include +#include +#include +#include namespace kernel::tests::filesystem { + storage_boot_module_fixture::mapped_image::mapped_image(std::filesystem::path path) + : file_descriptor(::open(path.c_str(), O_RDWR)) + { + if (file_descriptor < 0) + { + throw std::runtime_error{"Failed to open image file for test boot module: " + path.string()}; + } + + struct stat statistics{}; + if (::fstat(file_descriptor, &statistics) < 0) + { + throw std::runtime_error{"Failed to get statistics for image file: " + path.string()}; + } + + size = static_cast(statistics.st_size); + + mapping = static_cast(::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, file_descriptor, 0)); + if (mapping == MAP_FAILED) + { + throw std::runtime_error{"Failed to map image file for test boot module: " + path.string()}; + } + } + + storage_boot_module_fixture::mapped_image::~mapped_image() + { + if (mapping != nullptr) + { + ::munmap(mapping, size); + } + if (file_descriptor >= 0) + { + ::close(file_descriptor); + } + } + + storage_boot_module_fixture::mapped_image::mapped_image(mapped_image && other) noexcept + : file_descriptor{std::exchange(other.file_descriptor, -1)} + , mapping{std::exchange(other.mapping, nullptr)} + , size{std::exchange(other.size, 0)} + {} + + auto storage_boot_module_fixture::mapped_image::operator=(mapped_image && other) noexcept -> mapped_image & + { + if (this != &other) + { + file_descriptor = std::exchange(other.file_descriptor, -1); + mapping = std::exchange(other.mapping, nullptr); + size = std::exchange(other.size, 0); + } + return *this; + } + storage_boot_module_fixture::~storage_boot_module_fixture() { kernel::tests::devices::storage::management::deinit(); @@ -36,23 +91,22 @@ namespace kernel::tests::filesystem for (std::size_t i = 0; i < module_count; ++i) { - m_module_names.push_back("test_mod" + kstd::to_string(i)); + m_module_names.push_back(std::format("test_mod{}", i)); m_module_data.emplace_back(module_size, std::byte{static_cast(0x40 + (i % 16))}); } for (std::size_t i = 0; i < module_count; ++i) { m_registry.add_boot_module(kapi::boot_modules::boot_module{ - m_module_names[i].view(), kapi::memory::linear_address{m_module_data[i].data()}, m_module_data[i].size()}); + m_module_names[i].c_str(), kapi::memory::linear_address{m_module_data[i].data()}, m_module_data[i].size()}); } kapi::boot_modules::set_boot_module_registry(m_registry); kernel::devices::storage::management::init(); } - auto storage_boot_module_fixture::setup_modules_from_img(kstd::vector const & module_names, - kstd::vector const & img_paths) - -> void + auto storage_boot_module_fixture::setup_modules_from_img(std::vector const & module_names, + std::vector const & img_paths) -> void { m_module_names.clear(); m_module_data.clear(); @@ -72,38 +126,13 @@ namespace kernel::tests::filesystem kernel::devices::storage::management::init(); } - auto storage_boot_module_fixture::setup_module_from_img(kstd::string const & module_name, + auto storage_boot_module_fixture::setup_module_from_img(std::string const & module_name, std::filesystem::path const & img_path) -> void { - auto file = std::ifstream{img_path, std::ios::binary | std::ios::ate}; - if (!file) - { - throw std::runtime_error{"Failed to open image file for test boot module: " + img_path.string()}; - } - - auto const end_pos = file.tellg(); - if (end_pos < 0) - { - throw std::runtime_error{"Failed to determine image file size for test boot module: " + img_path.string()}; - } - - auto const size = static_cast(end_pos); - file.seekg(0, std::ios::beg); - m_module_names.push_back(module_name); - m_module_data.emplace_back(size); - - if (size > 0) - { - file.read(reinterpret_cast(m_module_data.back().data()), static_cast(size)); - if (!file) - { - throw std::runtime_error{"Failed to read image file content for test boot module: " + img_path.string()}; - } - } + auto & mapped_image = m_mapped_images.emplace_back(img_path); m_registry.add_boot_module(kapi::boot_modules::boot_module{ - m_module_names.back().view(), kapi::memory::linear_address{m_module_data.back().data()}, - m_module_data.back().size()}); + m_module_names.back().c_str(), kapi::memory::linear_address{mapped_image.mapping}, mapped_image.size}); } } // namespace kernel::tests::filesystem \ No newline at end of file diff --git a/kernel/src/test_support/filesystem/storage_boot_module_vfs_fixture.cpp b/kernel/src/test_support/filesystem/storage_boot_module_vfs_fixture.cpp index cba7278..1a71b57 100644 --- a/kernel/src/test_support/filesystem/storage_boot_module_vfs_fixture.cpp +++ b/kernel/src/test_support/filesystem/storage_boot_module_vfs_fixture.cpp @@ -3,11 +3,10 @@ #include "kernel/filesystem/vfs.hpp" #include "kernel/test_support/filesystem/vfs.hpp" -#include -#include - #include #include +#include +#include namespace kernel::tests::filesystem { @@ -24,7 +23,7 @@ namespace kernel::tests::filesystem } auto storage_boot_module_vfs_fixture::setup_modules_from_img_and_init_vfs( - kstd::vector const & module_names, kstd::vector const & img_paths) -> void + std::vector const & module_names, std::vector const & img_paths) -> void { setup_modules_from_img(module_names, img_paths); kernel::filesystem::vfs::init(); -- cgit v1.2.3