Skip to content

Commit

Permalink
K2: update header to version 11 (#1137)
Browse files Browse the repository at this point in the history
* new k2 namespace with wrappers of raw K2 primitives
* rename ComponentState -> InstanceState
* dummy support of new ComponentState
* new initiallization scheme for ImageState, ComponentState, InstanceState

---------

Co-authored-by: Vadim Sadokhov <[email protected]>
  • Loading branch information
apolyakov and astrophysik authored Nov 8, 2024
1 parent 1c42ba8 commit 38acbe3
Show file tree
Hide file tree
Showing 93 changed files with 1,351 additions and 1,210 deletions.
21 changes: 11 additions & 10 deletions compiler/code-gen/files/init-scripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,19 @@ void StaticInit::compile(CodeGenerator &W) const {

struct RunInterruptedFunction {
FunctionPtr function;
RunInterruptedFunction(FunctionPtr function) : function(function) {}
RunInterruptedFunction(FunctionPtr function)
: function(function) {}

void compile(CodeGenerator &W) const {
std::string await_prefix = function->is_interruptible ? "co_await " : "";
std::string component_kind = G->is_output_mode_k2_cli() ? "ComponentKind::CLI"
: G->is_output_mode_k2_server() ? "ComponentKind::Server"
: G->is_output_mode_k2_oneshot() ? "ComponentKind::Oneshot"
: G->is_output_mode_k2_multishot() ? "ComponentKind::Multishot"
: "ComponentKind::Invalid";

std::string script_start = "co_await get_component_context()->run_component_prologue<" + component_kind + ">();";
std::string script_finish = "co_await get_component_context()->run_component_epilogue();";
std::string image_kind = G->is_output_mode_k2_cli() ? "ImageKind::CLI"
: G->is_output_mode_k2_server() ? "ImageKind::Server"
: G->is_output_mode_k2_oneshot() ? "ImageKind::Oneshot"
: G->is_output_mode_k2_multishot() ? "ImageKind::Multishot"
: "ImageKind::Invalid";

std::string script_start = "co_await InstanceState::get().run_instance_prologue<" + image_kind + ">();";
std::string script_finish = "co_await InstanceState::get().run_instance_epilogue();";
FunctionSignatureGenerator(W) << "task_t<void> " << FunctionName(function) << "$run() " << BEGIN << script_start << NL << await_prefix
<< FunctionName(function) << "();" << NL << script_finish << NL << "co_return;" << END;
W << NL;
Expand Down Expand Up @@ -285,7 +286,7 @@ void ComponentInfoFile::compile(CodeGenerator &W) const {
auto now = std::chrono::system_clock::now();
W << OpenFile("image_info.cpp");
W << ExternInclude(G->settings().runtime_headers.get());
W << "const ImageInfo *vk_k2_describe() " << BEGIN << "static ImageInfo imageInfo {\"" << G->settings().k2_component_name.get() << "\"" << ","
W << "const ImageInfo *k2_describe() " << BEGIN << "static ImageInfo imageInfo {\"" << G->settings().k2_component_name.get() << "\"" << ","
<< std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() << ","
<< "K2_PLATFORM_HEADER_H_VERSION, "
<< "{}," // todo:k2 add commit hash
Expand Down
135 changes: 60 additions & 75 deletions runtime-light/allocator/runtime-light-allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,141 +2,126 @@
// Copyright (c) 2024 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <algorithm>
#include <cstddef>
#include <cstring>

#include "runtime-common/core/utils/kphp-assert-core.h"
#include "runtime-light/component/component.h"
#include "runtime-light/k2-platform/k2-api.h"
#include "runtime-light/state/component-state.h"
#include "runtime-light/state/image-state.h"
#include "runtime-light/state/instance-state.h"

namespace {
// TODO: make it depend on max chunk size, e.g. MIN_EXTRA_MEM_SIZE = f(MAX_CHUNK_SIZE);
constexpr auto MIN_EXTRA_MEM_SIZE = static_cast<size_t>(32U * 1024U); // extra mem size should be greater than max chunk block size

bool is_script_allocator_available() {
return get_component_context() != nullptr;
}
constexpr auto EXTRA_MEMORY_MULTIPLIER = 2;

void request_extra_memory(size_t requested_size) {
const size_t extra_mem_size = std::max(MIN_EXTRA_MEM_SIZE, requested_size);
auto &rt_alloc = RuntimeAllocator::get();
auto *extra_mem = rt_alloc.alloc_global_memory(extra_mem_size);
rt_alloc.memory_resource.add_extra_memory(new (extra_mem) memory_resource::extra_memory_pool{extra_mem_size});
const size_t extra_mem_size{std::max(MIN_EXTRA_MEM_SIZE, EXTRA_MEMORY_MULTIPLIER * requested_size)};
auto &allocator{RuntimeAllocator::get()};
auto *extra_mem{allocator.alloc_global_memory(extra_mem_size)};
allocator.memory_resource.add_extra_memory(new (extra_mem) memory_resource::extra_memory_pool{extra_mem_size});
}

} // namespace

RuntimeAllocator &RuntimeAllocator::get() noexcept {
if (k2::instance_state() != nullptr) [[likely]] {
return InstanceState::get().allocator;
} else if (k2::component_state() != nullptr) [[likely]] {
return ComponentState::get_mutable().allocator;
} else if (k2::image_state() != nullptr) [[unlikely]] {
return ImageState::get_mutable().allocator;
} else {
php_critical_error("can't find available allocator");
}
}

RuntimeAllocator::RuntimeAllocator(size_t script_mem_size, size_t oom_handling_mem_size) {
void *buffer = alloc_global_memory(script_mem_size);
void *buffer{alloc_global_memory(script_mem_size)};
php_assert(buffer != nullptr);
memory_resource.init(buffer, script_mem_size, oom_handling_mem_size);
}

RuntimeAllocator &RuntimeAllocator::get() noexcept {
return get_component_context()->runtime_allocator;
}

void RuntimeAllocator::init(void *buffer, size_t script_mem_size, size_t oom_handling_mem_size) {
php_assert(buffer != nullptr);
memory_resource.init(buffer, script_mem_size, oom_handling_mem_size);
}

void RuntimeAllocator::free() {
const Allocator &pt_ctx = get_platform_context()->allocator;

auto *extra_memory = memory_resource.get_extra_memory_head();
auto *extra_memory{memory_resource.get_extra_memory_head()};
while (extra_memory->get_pool_payload_size() != 0) {
auto *releasing_extra_memory = extra_memory;
auto *extra_memory_to_release{extra_memory};
extra_memory = extra_memory->next_in_chain;
pt_ctx.free(releasing_extra_memory);
k2::free(extra_memory_to_release);
}
pt_ctx.free(memory_resource.memory_begin());
k2::free(memory_resource.memory_begin());
}

void *RuntimeAllocator::alloc_script_memory(size_t size) noexcept {
php_assert(size);
if (unlikely(!is_script_allocator_available())) {
return alloc_global_memory(size);
}

ComponentState &rt_ctx = *get_component_context();

void *ptr = rt_ctx.runtime_allocator.memory_resource.allocate(size);
if (ptr == nullptr) {
php_assert(size > 0);
void *mem{memory_resource.allocate(size)};
if (mem == nullptr) [[unlikely]] {
request_extra_memory(size);
ptr = rt_ctx.runtime_allocator.memory_resource.allocate(size);
php_assert(ptr != nullptr);
mem = memory_resource.allocate(size);
php_assert(mem != nullptr);
}
return ptr;
return mem;
}

void *RuntimeAllocator::alloc0_script_memory(size_t size) noexcept {
php_assert(size);
if (unlikely(!is_script_allocator_available())) {
return alloc0_global_memory(size);
}

ComponentState &rt_ctx = *get_component_context();
void *ptr = rt_ctx.runtime_allocator.memory_resource.allocate0(size);
if (ptr == nullptr) {
php_assert(size > 0);
void *mem{memory_resource.allocate0(size)};
if (mem == nullptr) [[unlikely]] {
request_extra_memory(size);
ptr = rt_ctx.runtime_allocator.memory_resource.allocate0(size);
php_assert(ptr != nullptr);
mem = memory_resource.allocate0(size);
php_assert(mem != nullptr);
}
return ptr;
return mem;
}

void *RuntimeAllocator::realloc_script_memory(void *mem, size_t new_size, size_t old_size) noexcept {
void *RuntimeAllocator::realloc_script_memory(void *old_mem, size_t new_size, size_t old_size) noexcept {
php_assert(new_size > old_size);
if (unlikely(!is_script_allocator_available())) {
return realloc_global_memory(mem, new_size, old_size);
}

ComponentState &rt_ctx = *get_component_context();
void *ptr = rt_ctx.runtime_allocator.memory_resource.reallocate(mem, new_size, old_size);
if (ptr == nullptr) {
void *new_mem{memory_resource.reallocate(old_mem, new_size, old_size)};
if (new_mem == nullptr) [[unlikely]] {
request_extra_memory(new_size * 2);
ptr = rt_ctx.runtime_allocator.memory_resource.reallocate(mem, new_size, old_size);
php_assert(ptr != nullptr);
new_mem = memory_resource.reallocate(old_mem, new_size, old_size);
php_assert(new_mem != nullptr);
}
return ptr;
return new_mem;
}

void RuntimeAllocator::free_script_memory(void *mem, size_t size) noexcept {
php_assert(size);
if (unlikely(!is_script_allocator_available())) {
free_global_memory(mem, size);
return;
}

ComponentState &rt_ctx = *get_component_context();
rt_ctx.runtime_allocator.memory_resource.deallocate(mem, size);
php_assert(size > 0);
memory_resource.deallocate(mem, size);
}

void *RuntimeAllocator::alloc_global_memory(size_t size) noexcept {
void *ptr = get_platform_context()->allocator.alloc(size);
if (unlikely(ptr == nullptr)) {
void *mem{k2::alloc(size)};
if (unlikely(mem == nullptr)) [[unlikely]] {
critical_error_handler();
}
return ptr;
return mem;
}

void *RuntimeAllocator::alloc0_global_memory(size_t size) noexcept {
void *ptr = get_platform_context()->allocator.alloc(size);
if (unlikely(ptr == nullptr)) {
void *mem{k2::alloc(size)};
if (unlikely(mem == nullptr)) [[unlikely]] {
critical_error_handler();
}
std::memset(ptr, 0, size);
return ptr;
std::memset(mem, 0, size);
return mem;
}

void *RuntimeAllocator::realloc_global_memory(void *mem, size_t new_size, size_t) noexcept {
void *ptr = get_platform_context()->allocator.realloc(mem, new_size);
if (unlikely(ptr == nullptr)) {
void *RuntimeAllocator::realloc_global_memory(void *old_mem, size_t new_size, size_t /*unused*/) noexcept {
void *mem{k2::realloc(old_mem, new_size)};
if (unlikely(mem == nullptr)) [[unlikely]] {
critical_error_handler();
}
return ptr;
return mem;
}

void RuntimeAllocator::free_global_memory(void *mem, size_t) noexcept {
get_platform_context()->allocator.free(mem);
void RuntimeAllocator::free_global_memory(void *mem, size_t /*unused*/) noexcept {
k2::free(mem);
}
1 change: 0 additions & 1 deletion runtime-light/component/component.cmake

This file was deleted.

135 changes: 0 additions & 135 deletions runtime-light/component/component.h

This file was deleted.

15 changes: 0 additions & 15 deletions runtime-light/component/image.h

This file was deleted.

Loading

0 comments on commit 38acbe3

Please sign in to comment.