Skip to content

Commit ccd5a17

Browse files
authored
Revert "[k2] make init instance memory size adjustable (#1610)" (#1614)
This reverts commit 11d3605.
1 parent beeb24d commit ccd5a17

11 files changed

Lines changed: 45 additions & 107 deletions

File tree

runtime-common/core/allocator/runtime-allocator.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct RuntimeAllocator final : vk::not_copyable {
1313
static RuntimeAllocator& get() noexcept;
1414

1515
RuntimeAllocator() = default;
16-
RuntimeAllocator(size_t script_mem_size, size_t min_extra_mem_size, size_t oom_handling_mem_size);
16+
RuntimeAllocator(size_t script_mem_size, size_t oom_handling_mem_size);
1717

1818
void init(void* buffer, size_t script_mem_size, size_t oom_handling_mem_size);
1919
void free();
@@ -28,12 +28,5 @@ struct RuntimeAllocator final : vk::not_copyable {
2828
void* realloc_global_memory(void* mem, size_t new_size, size_t old_size) noexcept;
2929
void free_global_memory(void* mem, size_t size) noexcept;
3030

31-
private:
32-
void request_extra_memory(size_t requested_size, size_t min_extra_mem_size) noexcept;
33-
34-
public:
3531
memory_resource::unsynchronized_pool_resource memory_resource;
36-
37-
private:
38-
size_t min_extra_mem_size{0};
3932
};

runtime-common/core/memory-resource/unsynchronized_pool_resource.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace memory_resource {
1212

13+
constexpr size_t unsynchronized_pool_resource::MAX_CHUNK_BLOCK_SIZE_;
14+
1315
void unsynchronized_pool_resource::init(void* buffer, size_t buffer_size, size_t oom_handling_buffer_size) noexcept {
1416
monotonic_buffer_resource::init(buffer, buffer_size);
1517

runtime-common/core/memory-resource/unsynchronized_pool_resource.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class unsynchronized_pool_resource : private monotonic_buffer_resource {
3030
void* allocate(size_t size) noexcept {
3131
void* mem = nullptr;
3232
const auto aligned_size = details::align_for_chunk(size);
33-
if (aligned_size < MAX_CHUNK_BLOCK_SIZE) {
33+
if (aligned_size < MAX_CHUNK_BLOCK_SIZE_) {
3434
mem = try_allocate_small_piece(aligned_size);
3535
if (!mem) {
3636
mem = allocate_small_piece_from_fallback_resource(aligned_size);
@@ -123,7 +123,7 @@ class unsynchronized_pool_resource : private monotonic_buffer_resource {
123123
void put_memory_back(void* mem, size_t size) noexcept {
124124
const bool from_extra_pool = (extra_memory_head_ != &extra_memory_tail_) && is_memory_from_extra_pool(mem, size);
125125
if (from_extra_pool || !monotonic_buffer_resource::put_memory_back(mem, size)) {
126-
if (size < MAX_CHUNK_BLOCK_SIZE) {
126+
if (size < MAX_CHUNK_BLOCK_SIZE_) {
127127
size_t chunk_id = details::get_chunk_id(size);
128128
free_chunks_[chunk_id].put_mem(mem);
129129
++stats_.small_memory_pieces;
@@ -134,18 +134,15 @@ class unsynchronized_pool_resource : private monotonic_buffer_resource {
134134
}
135135
}
136136

137-
public:
138-
static constexpr size_t MAX_CHUNK_BLOCK_SIZE{static_cast<size_t>(16U * 1024U)};
139-
140-
private:
141137
details::memory_chunk_tree huge_pieces_;
142138
monotonic_buffer_resource fallback_resource_;
143139
size_t oom_handling_memory_size_{0};
144140

145141
extra_memory_pool* extra_memory_head_{nullptr};
146142
extra_memory_pool extra_memory_tail_{sizeof(extra_memory_pool)};
147143

148-
std::array<details::memory_chunk_list, details::get_chunk_id(MAX_CHUNK_BLOCK_SIZE)> free_chunks_;
144+
static constexpr size_t MAX_CHUNK_BLOCK_SIZE_{static_cast<size_t>(16U * 1024U)};
145+
std::array<details::memory_chunk_list, details::get_chunk_id(MAX_CHUNK_BLOCK_SIZE_)> free_chunks_;
149146
};
150147

151148
} // namespace memory_resource

runtime-light/allocator/allocator-state.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "runtime-common/core/allocator/runtime-allocator.h"
1212
#include "runtime-light/stdlib/diagnostics/logs.h"
1313

14-
inline constexpr auto DEFAULT_MIN_EXTRA_MEMORY_POOL_SIZE{static_cast<size_t>(64 * 1024U * 1024U)};
15-
1614
class AllocatorState final : private vk::not_copyable {
1715
uint32_t m_libc_alloc_allowed{};
1816

@@ -29,8 +27,8 @@ class AllocatorState final : private vk::not_copyable {
2927
return m_libc_alloc_allowed != 0;
3028
}
3129

32-
AllocatorState(size_t script_mem_size, size_t min_extra_mem_size, size_t oom_handling_mem_size) noexcept
33-
: allocator(script_mem_size, min_extra_mem_size, oom_handling_mem_size) {}
30+
AllocatorState(size_t script_mem_size, size_t oom_handling_mem_size) noexcept
31+
: allocator(script_mem_size, oom_handling_mem_size) {}
3432

3533
static const AllocatorState& get() noexcept;
3634
static AllocatorState& get_mutable() noexcept;

runtime-light/allocator/runtime-light-allocator.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@
1010
#include "runtime-light/k2-platform/k2-api.h"
1111
#include "runtime-light/stdlib/diagnostics/logs.h"
1212

13+
namespace {
14+
// TODO: make it depend on max chunk size, e.g. MIN_EXTRA_MEM_SIZE = f(MAX_CHUNK_SIZE);
15+
constexpr auto MIN_EXTRA_MEM_SIZE = static_cast<size_t>(1024U * 1024U); // extra mem size should be greater than max chunk block size
16+
constexpr auto EXTRA_MEMORY_MULTIPLIER = 2;
17+
18+
void request_extra_memory(size_t requested_size) noexcept {
19+
const size_t extra_mem_size{std::max(MIN_EXTRA_MEM_SIZE, EXTRA_MEMORY_MULTIPLIER * requested_size)};
20+
auto& allocator{RuntimeAllocator::get()};
21+
auto* extra_mem{allocator.alloc_global_memory(extra_mem_size)};
22+
allocator.memory_resource.add_extra_memory(new (extra_mem) memory_resource::extra_memory_pool{extra_mem_size});
23+
}
24+
25+
} // namespace
26+
1327
RuntimeAllocator& RuntimeAllocator::get() noexcept {
1428
return AllocatorState::get_mutable().allocator;
1529
}
1630

17-
RuntimeAllocator::RuntimeAllocator(size_t script_mem_size, size_t min_extra_mem_size, size_t oom_handling_mem_size)
18-
: min_extra_mem_size(min_extra_mem_size) {
31+
RuntimeAllocator::RuntimeAllocator(size_t script_mem_size, size_t oom_handling_mem_size) {
1932
kphp::log::debug("create runtime allocator -> {:p}: script memory -> {}, oom handling size -> {}", reinterpret_cast<void*>(this), script_mem_size,
2033
oom_handling_mem_size);
2134
void* buffer{alloc_global_memory(script_mem_size)};
@@ -44,7 +57,7 @@ void* RuntimeAllocator::alloc_script_memory(size_t size) noexcept {
4457
kphp::log::assertion(size != 0);
4558
void* mem{memory_resource.allocate(size)};
4659
if (mem == nullptr) [[unlikely]] {
47-
request_extra_memory(size, min_extra_mem_size);
60+
request_extra_memory(size);
4861
mem = memory_resource.allocate(size);
4962
kphp::log::assertion(mem != nullptr);
5063
}
@@ -55,7 +68,7 @@ void* RuntimeAllocator::alloc0_script_memory(size_t size) noexcept {
5568
kphp::log::assertion(size != 0);
5669
void* mem{memory_resource.allocate0(size)};
5770
if (mem == nullptr) [[unlikely]] {
58-
request_extra_memory(size, min_extra_mem_size);
71+
request_extra_memory(size);
5972
mem = memory_resource.allocate0(size);
6073
kphp::log::assertion(mem != nullptr);
6174
}
@@ -66,7 +79,7 @@ void* RuntimeAllocator::realloc_script_memory(void* old_mem, size_t new_size, si
6679
kphp::log::assertion(new_size > old_size);
6780
void* new_mem{memory_resource.reallocate(old_mem, new_size, old_size)};
6881
if (new_mem == nullptr) [[unlikely]] {
69-
request_extra_memory(new_size * 2, min_extra_mem_size);
82+
request_extra_memory(new_size * 2);
7083
new_mem = memory_resource.reallocate(old_mem, new_size, old_size);
7184
kphp::log::assertion(new_mem != nullptr);
7285
}
@@ -100,20 +113,3 @@ void* RuntimeAllocator::realloc_global_memory(void* old_mem, size_t new_size, si
100113
void RuntimeAllocator::free_global_memory(void* mem, size_t /*unused*/) noexcept {
101114
k2::free(mem);
102115
}
103-
104-
void RuntimeAllocator::request_extra_memory(size_t requested_size, size_t min_extra_mem_size) noexcept {
105-
// Extra mem size have to be greater than max chunk block
106-
min_extra_mem_size = std::max(min_extra_mem_size, memory_resource::unsynchronized_pool_resource::MAX_CHUNK_BLOCK_SIZE);
107-
108-
// If the requested size is greater than or equal to half of min_extra_mem_size, it’s more efficient to allocate a multiple of the requested size.
109-
size_t extra_mem_size{std::max(min_extra_mem_size, 2 * requested_size)};
110-
111-
// Take into account internal layout of `memory_resource::extra_memory_pool`
112-
extra_mem_size += sizeof(memory_resource::extra_memory_pool);
113-
114-
kphp::log::debug("requested extra memory pool with size {} bytes, will be allocated {} bytes", requested_size, extra_mem_size);
115-
116-
auto* extra_mem{alloc_global_memory(extra_mem_size)};
117-
kphp::log::assertion(extra_mem != nullptr);
118-
memory_resource.add_extra_memory(new (extra_mem) memory_resource::extra_memory_pool{extra_mem_size});
119-
}

runtime-light/runtime-light.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
#define VISIBILITY_DEFAULT __attribute__((visibility("default")))
1616

1717
VISIBILITY_DEFAULT ImageState* k2_create_image() {
18-
kphp::log::debug("start image state creation, requested {} bytes", sizeof(ImageState));
18+
kphp::log::debug("start image state creation");
1919
auto* image_state_ptr{static_cast<ImageState*>(k2::alloc(sizeof(ImageState)))};
20-
kphp::log::assertion(image_state_ptr != nullptr); // Not enough memory for image state
20+
if (image_state_ptr == nullptr) [[unlikely]] {
21+
kphp::log::error("can't allocate enough memory for image state");
22+
}
2123
kphp::log::debug("finish image state creation");
2224
return image_state_ptr;
2325
}
@@ -30,9 +32,11 @@ VISIBILITY_DEFAULT void k2_init_image() {
3032
}
3133

3234
VISIBILITY_DEFAULT ComponentState* k2_create_component() {
33-
kphp::log::debug("start component state creation, requested {} bytes", sizeof(ComponentState));
35+
kphp::log::debug("start component state creation");
3436
auto* component_state_ptr{static_cast<ComponentState*>(k2::alloc(sizeof(ComponentState)))};
35-
kphp::log::assertion(component_state_ptr != nullptr); // Not enough memory for component state
37+
if (component_state_ptr == nullptr) [[unlikely]] {
38+
kphp::log::error("can't allocate enough memory for component state");
39+
}
3640
kphp::log::debug("finish component state creation");
3741
return component_state_ptr;
3842
}
@@ -44,9 +48,11 @@ VISIBILITY_DEFAULT void k2_init_component() {
4448
}
4549

4650
VISIBILITY_DEFAULT InstanceState* k2_create_instance() {
47-
kphp::log::debug("start instance state creation, requested {} bytes", sizeof(InstanceState));
51+
kphp::log::debug("start instance state creation");
4852
auto* instance_state_ptr{static_cast<InstanceState*>(k2::alloc(sizeof(InstanceState)))};
49-
kphp::log::assertion(instance_state_ptr != nullptr); // Not enough memory for instance state
53+
if (instance_state_ptr == nullptr) [[unlikely]] {
54+
kphp::log::error("can't allocate enough memory for instance state");
55+
}
5056
kphp::log::debug("finish instance state creation");
5157
return instance_state_ptr;
5258
}

runtime-light/state/component-state.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44

55
#include "runtime-light/state/component-state.h"
66

7-
#include <charconv>
87
#include <cstddef>
9-
#include <cstdint>
108
#include <cstring>
119
#include <iterator>
1210
#include <memory>
13-
#include <optional>
1411
#include <span>
1512
#include <string_view>
1613
#include <sys/mman.h>
1714
#include <sys/stat.h>
18-
#include <system_error>
1915
#include <utility>
2016

2117
#include "runtime-common/core/runtime-core.h"
@@ -24,24 +20,6 @@
2420
#include "runtime-light/stdlib/diagnostics/logs.h"
2521
#include "runtime-light/stdlib/file/resource.h"
2622

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-
4523
void ComponentState::parse_env() noexcept {
4624
for (auto i = 0; i < envc; ++i) {
4725
const auto [env_key, env_value]{k2::env_fetch(i)};
@@ -114,24 +92,6 @@ void ComponentState::parse_exit_after_response_arg(std::string_view value_view)
11492
}
11593
}
11694

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-
13595
void ComponentState::parse_args() noexcept {
13696
for (auto i = 0; i < argc; ++i) {
13797
const auto [arg_key, arg_value]{k2::arg_fetch(i)};
@@ -148,10 +108,6 @@ void ComponentState::parse_args() noexcept {
148108
parse_cluster_name_arg(value_view);
149109
} else if (key_view == EXIT_AFTER_RESPONSE_ARG) {
150110
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);
155111
} else {
156112
kphp::log::warning("unexpected argument format: {}", key_view);
157113
}

runtime-light/state/component-state.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "runtime-light/stdlib/kml/kml-state.h"
2020

2121
struct ComponentState final : private vk::not_copyable {
22-
AllocatorState component_allocator_state{INIT_COMPONENT_ALLOCATOR_SIZE, DEFAULT_MIN_EXTRA_MEMORY_POOL_SIZE, 0};
22+
AllocatorState component_allocator_state{INIT_COMPONENT_ALLOCATOR_SIZE, 0};
2323
KmlComponentState kml_component_state; // This member does not hold any KPHP types, so setting a reference counter is unnecessary.
2424

2525
const uint32_t argc{k2::args_count()};
@@ -29,8 +29,6 @@ struct ComponentState final : private vk::not_copyable {
2929
mixed runtime_config;
3030
string cluster_name{DEFAULT_CLUSTER_NAME.data(), DEFAULT_CLUSTER_NAME.size()};
3131
bool exit_after_response{};
32-
uint64_t initial_instance_memory_size{INIT_INSTANCE_ALLOCATOR_SIZE};
33-
uint64_t min_instance_extra_memory_size{DEFAULT_MIN_EXTRA_MEMORY_POOL_SIZE};
3432

3533
ComponentState() noexcept {
3634
parse_env();
@@ -61,10 +59,7 @@ struct ComponentState final : private vk::not_copyable {
6159
static constexpr std::string_view CLUSTER_NAME_ARG = "cluster-name";
6260
static constexpr std::string_view DEFAULT_CLUSTER_NAME = "default";
6361
static constexpr std::string_view EXIT_AFTER_RESPONSE_ARG = "exit-after-response";
64-
static constexpr std::string_view INITIAL_INSTANCE_MEMORY_SIZE_ARG = "initial-instance-memory-size";
65-
static constexpr std::string_view MIN_INSTANCE_EXTRA_MEMORY_SIZE_ARG = "min-instance-extra-memory-size";
66-
static constexpr auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast<size_t>(1024U * 1024U); // 1MB
67-
static constexpr auto INIT_INSTANCE_ALLOCATOR_SIZE = static_cast<size_t>(64U * 1024U * 1024U); // 64MB
62+
static constexpr auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast<size_t>(1024U * 1024U); // 1MB
6863

6964
void parse_env() noexcept;
7065

@@ -79,8 +74,4 @@ struct ComponentState final : private vk::not_copyable {
7974
void parse_cluster_name_arg(std::string_view) noexcept;
8075

8176
void parse_exit_after_response_arg(std::string_view) noexcept;
82-
83-
void parse_initial_instance_memory_size_arg(std::string_view) noexcept;
84-
85-
void parse_min_instance_extra_memory_size_arg(std::string_view) noexcept;
8677
};

runtime-light/state/image-state.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "runtime-light/stdlib/visitors/shape-visitors.h"
3030

3131
struct ImageState final : private vk::not_copyable {
32-
AllocatorState image_allocator_state{INIT_IMAGE_ALLOCATOR_SIZE, DEFAULT_MIN_EXTRA_MEMORY_POOL_SIZE, 0};
32+
AllocatorState image_allocator_state{INIT_IMAGE_ALLOCATOR_SIZE, 0};
3333

3434
uint32_t pid{k2::getpid()};
3535
uid_t uid{k2::getuid()};

runtime-light/state/instance-state.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct InstanceState final : vk::not_copyable {
8888
return instance_kind_;
8989
}
9090

91-
AllocatorState instance_allocator_state{ComponentState::get().initial_instance_memory_size, ComponentState::get().min_instance_extra_memory_size, 0};
91+
AllocatorState instance_allocator_state{INIT_INSTANCE_ALLOCATOR_SIZE, 0};
9292

9393
kphp::log::contextual_tags instance_tags;
9494
kphp::coro::io_scheduler io_scheduler;
@@ -135,4 +135,6 @@ struct InstanceState final : vk::not_copyable {
135135

136136
enum image_kind image_kind_ { image_kind::invalid };
137137
enum instance_kind instance_kind_ { instance_kind::invalid };
138+
139+
static constexpr auto INIT_INSTANCE_ALLOCATOR_SIZE = static_cast<size_t>(128U * 1024U * 1024U);
138140
};

0 commit comments

Comments
 (0)