|
10 | 10 | #include <memory> |
11 | 11 | #include <span> |
12 | 12 | #include <string_view> |
| 13 | +#include <sys/mman.h> |
13 | 14 | #include <sys/stat.h> |
14 | 15 | #include <utility> |
15 | 16 |
|
16 | | -#include "runtime-common/core/allocator/script-malloc-interface.h" |
17 | 17 | #include "runtime-common/core/runtime-core.h" |
18 | 18 | #include "runtime-common/stdlib/serialization/json-functions.h" |
19 | 19 | #include "runtime-light/k2-platform/k2-api.h" |
@@ -42,36 +42,34 @@ void ComponentState::parse_kml_arg(std::string_view kml_dir) noexcept { |
42 | 42 | } |
43 | 43 |
|
44 | 44 | void ComponentState::parse_runtime_config_arg(std::string_view value_view) noexcept { |
45 | | - auto expected_canonicalized_path{k2::canonicalize(value_view)}; |
46 | | - if (!expected_canonicalized_path) [[unlikely]] { |
47 | | - return kphp::log::warning("error canonicalizing runtime-config path: error_code -> {}, path -> '{}'", expected_canonicalized_path.error(), value_view); |
| 45 | + auto canonicalized_path{k2::canonicalize(value_view)}; |
| 46 | + if (!canonicalized_path) [[unlikely]] { |
| 47 | + kphp::log::error("error canonicalizing runtime-config path: error_code -> {}, path -> {}", canonicalized_path.error(), value_view); |
48 | 48 | } |
49 | 49 |
|
50 | | - const auto [runtime_config_path, runtime_config_path_size]{*std::move(expected_canonicalized_path)}; |
51 | | - auto expected_runtime_config_file{kphp::fs::file::open({runtime_config_path.get(), runtime_config_path_size}, "r")}; |
52 | | - if (!expected_runtime_config_file) [[unlikely]] { |
53 | | - return kphp::log::warning("error opening runtime-config: error code -> {}", expected_runtime_config_file.error()); |
| 50 | + const auto [runtime_config_path, runtime_config_path_size]{*std::move(canonicalized_path)}; |
| 51 | + auto runtime_config_file{kphp::fs::file::open({runtime_config_path.get(), runtime_config_path_size}, "r")}; |
| 52 | + if (!runtime_config_file) [[unlikely]] { |
| 53 | + kphp::log::error("error opening runtime-config: error code -> {}, path -> {}", runtime_config_file.error(), value_view); |
54 | 54 | } |
55 | 55 |
|
56 | 56 | struct stat stat {}; |
57 | | - if (auto expected_stat_result{k2::stat({runtime_config_path.get(), runtime_config_path_size}, std::addressof(stat))}; !expected_stat_result.has_value()) |
58 | | - [[unlikely]] { |
59 | | - return kphp::log::warning("error getting runtime-config stat: error code -> {}", expected_stat_result.error()); |
| 57 | + if (auto stat_result{k2::fstat(runtime_config_file->descriptor(), std::addressof(stat))}; !stat_result.has_value()) [[unlikely]] { |
| 58 | + kphp::log::error("error getting runtime-config stat: error code -> {}, path -> {}", stat_result.error(), value_view); |
60 | 59 | } |
61 | 60 |
|
62 | | - const auto runtime_config_mem{std::unique_ptr<char, decltype(std::addressof(kphp::memory::script::free))>{ |
63 | | - reinterpret_cast<char*>(kphp::memory::script::alloc(static_cast<size_t>(stat.st_size))), kphp::memory::script::free}}; |
64 | | - kphp::log::assertion(runtime_config_mem != nullptr); |
65 | | - auto runtime_config_buf{std::span<char>{runtime_config_mem.get(), static_cast<size_t>(stat.st_size)}}; |
66 | | - if (auto expected_read{(*expected_runtime_config_file).read(std::as_writable_bytes(runtime_config_buf))}; !expected_read || *expected_read != stat.st_size) |
67 | | - [[unlikely]] { |
68 | | - return kphp::log::warning("error reading runtime-config: error code -> {}, read bytes -> {}", expected_read.error_or(k2::errno_ok), |
69 | | - expected_read.value_or(0)); |
| 61 | + auto mmap{kphp::fs::mmap::create(stat.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, runtime_config_file->descriptor(), 0)}; |
| 62 | + if (!mmap) [[unlikely]] { |
| 63 | + kphp::log::error("error mmaping runtime-config: error code -> {}, path -> {}", mmap.error(), value_view); |
70 | 64 | } |
71 | 65 |
|
72 | | - auto opt_config{json_decode({runtime_config_buf.data(), runtime_config_buf.size()})}; |
| 66 | + if (auto madvise_result{mmap->madvise(MADV_SEQUENTIAL)}; !madvise_result) [[unlikely]] { |
| 67 | + kphp::log::warning("error performing madvise on runtime-config's mmap: error code -> {}", madvise_result.error()); |
| 68 | + } |
| 69 | + |
| 70 | + auto opt_config{json_decode({reinterpret_cast<const char*>(mmap->data().data()), mmap->data().size()})}; |
73 | 71 | if (!opt_config) [[unlikely]] { |
74 | | - return kphp::log::warning("error decoding runtime-config"); |
| 72 | + kphp::log::error("error decoding runtime-config: path -> {}", value_view); |
75 | 73 | } |
76 | 74 | runtime_config = *std::move(opt_config); |
77 | 75 | } |
|
0 commit comments