Skip to content

Commit 2305d44

Browse files
authored
[k2] improve runtime-config argument processing (#1606)
* use mmap instead of read * throw an error instead of warning in case of failure
1 parent 0150b56 commit 2305d44

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

runtime-light/state/component-state.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include <memory>
1111
#include <span>
1212
#include <string_view>
13+
#include <sys/mman.h>
1314
#include <sys/stat.h>
1415
#include <utility>
1516

16-
#include "runtime-common/core/allocator/script-malloc-interface.h"
1717
#include "runtime-common/core/runtime-core.h"
1818
#include "runtime-common/stdlib/serialization/json-functions.h"
1919
#include "runtime-light/k2-platform/k2-api.h"
@@ -42,36 +42,34 @@ void ComponentState::parse_kml_arg(std::string_view kml_dir) noexcept {
4242
}
4343

4444
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);
4848
}
4949

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);
5454
}
5555

5656
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);
6059
}
6160

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);
7064
}
7165

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()})};
7371
if (!opt_config) [[unlikely]] {
74-
return kphp::log::warning("error decoding runtime-config");
72+
kphp::log::error("error decoding runtime-config: path -> {}", value_view);
7573
}
7674
runtime_config = *std::move(opt_config);
7775
}

0 commit comments

Comments
 (0)