44
55#include " runtime-light/state/component-state.h"
66
7+ #include < charconv>
78#include < cstddef>
9+ #include < cstdint>
810#include < cstring>
911#include < iterator>
1012#include < memory>
13+ #include < optional>
1114#include < span>
1215#include < string_view>
1316#include < sys/mman.h>
1417#include < sys/stat.h>
18+ #include < system_error>
1519#include < utility>
1620
1721#include " runtime-common/core/runtime-core.h"
2024#include " runtime-light/stdlib/diagnostics/logs.h"
2125#include " runtime-light/stdlib/file/resource.h"
2226
27+ namespace {
28+
29+ std::optional<uint64_t > parse_uint64 (std::string_view value_view) noexcept {
30+ // Num of symbols in max u64 (18_446_744_073_709_551_615) is 20
31+ if (value_view.empty () || value_view.size () > 20 ) {
32+ return std::nullopt ;
33+ }
34+
35+ uint64_t res{};
36+ if (const auto [ptr, err_code]{std::from_chars (value_view.begin (), value_view.end (), /* out paremeter */ res)};
37+ err_code != std::errc{} || ptr != value_view.end ()) {
38+ return std::nullopt ;
39+ }
40+ return res;
41+ }
42+
43+ } // namespace
44+
2345void ComponentState::parse_env () noexcept {
2446 for (auto i = 0 ; i < envc; ++i) {
2547 const auto [env_key, env_value]{k2::env_fetch (i)};
@@ -92,6 +114,24 @@ void ComponentState::parse_exit_after_response_arg(std::string_view value_view)
92114 }
93115}
94116
117+ void ComponentState::parse_initial_instance_memory_size_arg (std::string_view value_view) noexcept {
118+ const auto parsed{parse_uint64 (value_view)};
119+ if (!parsed) {
120+ kphp::log::error (" couldn't parse initial instance memory size, got {}" , value_view);
121+ }
122+ initial_instance_memory_size = *parsed;
123+ kphp::log::info (" set initial instance memory size to {} bytes" , initial_instance_memory_size);
124+ }
125+
126+ void ComponentState::parse_min_instance_extra_memory_size_arg (std::string_view value_view) noexcept {
127+ const auto parsed{parse_uint64 (value_view)};
128+ if (!parsed) {
129+ kphp::log::error (" couldn't parse min instance extra memory size, got {}" , value_view);
130+ }
131+ min_instance_extra_memory_size = *parsed;
132+ kphp::log::info (" set min instance extra memory size to {} bytes" , min_instance_extra_memory_size);
133+ }
134+
95135void ComponentState::parse_args () noexcept {
96136 for (auto i = 0 ; i < argc; ++i) {
97137 const auto [arg_key, arg_value]{k2::arg_fetch (i)};
@@ -108,6 +148,10 @@ void ComponentState::parse_args() noexcept {
108148 parse_cluster_name_arg (value_view);
109149 } else if (key_view == EXIT_AFTER_RESPONSE_ARG) {
110150 parse_exit_after_response_arg (value_view);
151+ } else if (key_view == INITIAL_INSTANCE_MEMORY_SIZE_ARG) {
152+ parse_initial_instance_memory_size_arg (value_view);
153+ } else if (key_view == MIN_INSTANCE_EXTRA_MEMORY_SIZE_ARG) {
154+ parse_min_instance_extra_memory_size_arg (value_view);
111155 } else {
112156 kphp::log::warning (" unexpected argument format: {}" , key_view);
113157 }
0 commit comments