Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ build
/cmake-build-*

dev
kphp
/kphp
kphp_out
26 changes: 0 additions & 26 deletions runtime-light/allocator/allocator-state.cpp

This file was deleted.

4 changes: 3 additions & 1 deletion runtime-light/allocator/allocator-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ class AllocatorState final : private vk::not_copyable {
: allocator(script_mem_size, min_extra_mem_size, oom_handling_mem_size) {}

static const AllocatorState& get() noexcept;
static AllocatorState& get_mutable() noexcept;
static AllocatorState& get_mutable() noexcept {
return const_cast<AllocatorState&>(get());
}
};
3 changes: 1 addition & 2 deletions runtime-light/allocator/allocator.cmake
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
set(RUNTIME_LIGHT_ALLOCATOR_SRC allocator/allocator-state.cpp
allocator/runtime-light-allocator.cpp)
set(RUNTIME_LIGHT_ALLOCATOR_SRC allocator/runtime-light-allocator.cpp)
54 changes: 54 additions & 0 deletions runtime-light/components/confdata/bindings/bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <functional>
#include <optional>

#include "runtime-common/core/runtime-core.h"
#include "runtime-light/allocator/allocator-state.h"
#include "runtime-light/components/confdata/state/instance-state.h"
#include "runtime-light/coroutine/coroutine-state.h"
#include "runtime-light/coroutine/io-scheduler.h"
#include "runtime-light/k2-platform/k2-api.h"
#include "runtime-light/stdlib/diagnostics/contextual-tags.h"
#include "runtime-light/stdlib/diagnostics/error-handling-state.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

namespace kphp::coro {

auto instance_state::get() noexcept -> instance_state& {
return InstanceState::get().coroutine_instance_state;
}

auto io_scheduler::get() noexcept -> io_scheduler& {
return InstanceState::get().io_scheduler;
}

} // namespace kphp::coro

namespace kphp::log {

auto contextual_tags::try_get() noexcept -> std::optional<std::reference_wrapper<contextual_tags>> {
if (k2::instance_state() != nullptr) [[likely]] {
return InstanceState::get().instance_tags;
}
return std::nullopt;
}

} // namespace kphp::log

auto AllocatorState::get() noexcept -> const AllocatorState& {
if (k2::instance_state() != nullptr) [[likely]] {
return InstanceState::get().instance_allocator_state;
}
kphp::log::error("can't find allocator state");
}

auto ErrorHandlingState::try_get() noexcept -> std::optional<std::reference_wrapper<ErrorHandlingState>> {
return std::nullopt; // confdata doesn't support PHP error handling
}

auto RuntimeContext::get() noexcept -> RuntimeContext& {
kphp::log::error("unexpected access to RuntimeContext"); // confdata doesn't have a runtime context
}
84 changes: 84 additions & 0 deletions runtime-light/components/confdata/confdata-component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <array>
#include <memory>

#include "runtime-light/components/confdata/state/component-state.h"
#include "runtime-light/components/confdata/state/image-state.h"
#include "runtime-light/components/confdata/state/instance-state.h"
#include "runtime-light/coroutine/io-scheduler.h"
#include "runtime-light/k2-platform/k2-api.h"
#include "runtime-light/k2-platform/k2-header.h"
#include "runtime-light/stdlib/confdata/confdata-constants.h"

#define VISIBILITY_DEFAULT __attribute__((visibility("default")))

VISIBILITY_DEFAULT ImageState* k2_create_image() {
k2::details::image_state_ptr = nullptr;
k2::details::component_state_ptr = nullptr;
k2::details::instance_state_ptr = nullptr;
return static_cast<ImageState*>(k2::alloc_align(sizeof(ImageState), alignof(ImageState)));
}

VISIBILITY_DEFAULT void k2_init_image() {
k2::details::image_state_ptr = k2_image_state();
k2::details::component_state_ptr = nullptr;
k2::details::instance_state_ptr = nullptr;
new (const_cast<ImageState*>(k2::image_state())) ImageState{};
}

VISIBILITY_DEFAULT ComponentState* k2_create_component() {
k2::details::image_state_ptr = k2_image_state();
k2::details::component_state_ptr = nullptr;
k2::details::instance_state_ptr = nullptr;
auto* component_state_ptr{static_cast<ComponentState*>(k2::alloc_align(sizeof(ComponentState), alignof(ComponentState)))};
return component_state_ptr;
}

VISIBILITY_DEFAULT void k2_init_component() {
k2::details::image_state_ptr = k2_image_state();
k2::details::component_state_ptr = k2_component_state();
k2::details::instance_state_ptr = nullptr;
new (const_cast<ComponentState*>(k2::component_state())) ComponentState{};
}

VISIBILITY_DEFAULT InstanceState* k2_create_instance() {
k2::details::image_state_ptr = k2_image_state();
k2::details::component_state_ptr = k2_component_state();
k2::details::instance_state_ptr = nullptr;
auto* instance_state_ptr{static_cast<InstanceState*>(k2::alloc_align(sizeof(InstanceState), alignof(InstanceState)))};
return instance_state_ptr;
}

VISIBILITY_DEFAULT void k2_init_instance() {
k2::details::image_state_ptr = k2_image_state();
k2::details::component_state_ptr = k2_component_state();
k2::details::instance_state_ptr = k2_instance_state();
new (k2::instance_state()) InstanceState{};
k2::instance_state()->init();
}

VISIBILITY_DEFAULT k2::PollStatus k2_warmup() {
return k2::PollStatus::PollFinishedOk;
}

VISIBILITY_DEFAULT k2::PollStatus k2_poll() {
k2::details::image_state_ptr = k2_image_state();
k2::details::component_state_ptr = k2_component_state();
k2::details::instance_state_ptr = k2_instance_state();
return kphp::coro::io_scheduler::get().process_events();
}

VISIBILITY_DEFAULT const ImageInfo* k2_describe() {
static constexpr std::array extra_info{ImageInfo::KeyValuePair{.key = "compiler_version", .value = K2_CONFDATA_COMPILER_VERSION}};
static constexpr ImageInfo image_info{.image_name = kphp::confdata::COMPONENT_NAME.data(),
.is_oneshot = 0,
.build_timestamp = K2_CONFDATA_BUILD_TIMESTAMP,
.header_h_version = K2_PLATFORM_HEADER_H_VERSION,
.version = "0.0.1",
.extra_info_size = extra_info.size(),
.extra_info = extra_info.data()};
return std::addressof(image_info);
}
44 changes: 44 additions & 0 deletions runtime-light/components/confdata/confdata.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# k2-confdata image: a standalone C++ component that shares the common
# runtime-light machinery but provides its own entry points and bindings
set(K2_CONFDATA_COMPONENT_SRC
${RUNTIME_LIGHT_DIR}/components/confdata/confdata-component.cpp
${RUNTIME_LIGHT_DIR}/components/confdata/bindings/bindings.cpp
${RUNTIME_LIGHT_DIR}/components/confdata/state/instance-state.cpp)

set(K2_CONFDATA_ALLOCATOR_SRC
${RUNTIME_LIGHT_DIR}/allocator/runtime-light-allocator.cpp
${RUNTIME_LIGHT_DIR}/memory-resource-impl/monotonic-light-buffer-resource.cpp)

set(K2_CONFDATA_DIAGNOSTICS_SRC
${RUNTIME_LIGHT_DIR}/stdlib/diagnostics/backtrace.cpp
${RUNTIME_LIGHT_DIR}/stdlib/diagnostics/php-assert.cpp)

set(K2_CONFDATA_MEMORY_RESOURCE_SRC
${RUNTIME_COMMON_DIR}/core/memory-resource/unsynchronized_pool_resource.cpp
${RUNTIME_COMMON_DIR}/core/memory-resource/monotonic_buffer_resource.cpp
${RUNTIME_COMMON_DIR}/core/memory-resource/details/memory_chunk_tree.cpp
${RUNTIME_COMMON_DIR}/core/memory-resource/details/memory_ordered_chunk_list.cpp)

set(K2_CONFDATA_SRC
${K2_CONFDATA_COMPONENT_SRC}
${K2_CONFDATA_ALLOCATOR_SRC}
${K2_CONFDATA_DIAGNOSTICS_SRC}
${K2_CONFDATA_MEMORY_RESOURCE_SRC}
# link the alloc-wrapper objects directly (not as an archive) so that
# __wrap_* definitions are always present regardless of link order
$<TARGET_OBJECTS:libc-alloc-wrapper-pic>)

vk_add_library_pic(k2-confdata-pic SHARED ${K2_CONFDATA_SRC})
set_target_properties(k2-confdata-pic PROPERTIES PREFIX "" OUTPUT_NAME "k2-confdata" LIBRARY_OUTPUT_DIRECTORY ${OBJS_DIR})
target_compile_options(k2-confdata-pic PUBLIC ${RUNTIME_LIGHT_COMPILE_FLAGS})
# reuse the common link flags; cmake drives the link through the compiler,
# so bare ld options need the -Wl, prefix
set(K2_CONFDATA_LINK_FLAGS ${RUNTIME_LIGHT_LINK_FLAGS})
if(NOT APPLE)
list(TRANSFORM K2_CONFDATA_LINK_FLAGS REPLACE "^--" "-Wl,--")
endif()
target_link_options(k2-confdata-pic PUBLIC ${K2_CONFDATA_LINK_FLAGS})

string(TIMESTAMP K2_CONFDATA_BUILD_TIMESTAMP "%s" UTC)
target_compile_definitions(k2-confdata-pic PRIVATE K2_CONFDATA_BUILD_TIMESTAMP=${K2_CONFDATA_BUILD_TIMESTAMP}ULL
K2_CONFDATA_COMPILER_VERSION="${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}")
24 changes: 24 additions & 0 deletions runtime-light/components/confdata/state/component-state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstddef>

#include "common/mixin/not_copyable.h"
#include "runtime-light/k2-platform/k2-api.h"

struct ComponentState final : private vk::not_copyable {
ComponentState() noexcept = default;
static auto get() noexcept -> const ComponentState&;
static auto get_mutable() noexcept -> ComponentState&;
};

inline auto ComponentState::get() noexcept -> const ComponentState& {
return *k2::component_state();
}

inline auto ComponentState::get_mutable() noexcept -> ComponentState& {
return const_cast<ComponentState&>(ComponentState::get());
}
24 changes: 24 additions & 0 deletions runtime-light/components/confdata/state/image-state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstddef>

#include "common/mixin/not_copyable.h"
#include "runtime-light/k2-platform/k2-api.h"

struct ImageState final : private vk::not_copyable {
ImageState() noexcept = default;
static auto get() noexcept -> const ImageState&;
static auto get_mutable() noexcept -> ImageState&;
};

inline auto ImageState::get() noexcept -> const ImageState& {
return *k2::image_state();
}

inline auto ImageState::get_mutable() noexcept -> ImageState& {
return const_cast<ImageState&>(ImageState::get());
}
39 changes: 39 additions & 0 deletions runtime-light/components/confdata/state/instance-state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime-light/components/confdata/state/instance-state.h"

#include <cstddef>
#include <memory>
#include <span>
#include <utility>

#include "runtime-light/coroutine/task.h"
#include "runtime-light/stdlib/diagnostics/logs.h"
#include "runtime-light/streams/stream.h"

auto InstanceState::init() noexcept -> void {
auto main_task{run()};
// initialize async stack
auto& main_task_async_stack_frame{main_task.get_handle().promise().get_async_stack_frame()};
main_task_async_stack_frame.async_stack_root = std::addressof(coroutine_instance_state.coroutine_stack_root);
coroutine_instance_state.coroutine_stack_root.top_async_stack_frame = std::addressof(main_task_async_stack_frame);
// spawn main task onto the scheduler
kphp::log::assertion(io_scheduler.spawn(std::move(main_task)));
}

auto InstanceState::run() noexcept -> kphp::coro::task<> {
auto opt_stream{co_await kphp::component::stream::accept()};
if (!opt_stream.has_value()) [[unlikely]] {
kphp::log::warning("failed to accept a stream");
co_return;
}
auto request_stream{std::move(*opt_stream)};
kphp::log::info("accepted a stream: descriptor -> {}", request_stream.descriptor());

// dummy implementation: drain the request and close
if (auto expected{co_await request_stream.read_all([](std::span<const std::byte>) noexcept {})}; !expected) [[unlikely]] {
kphp::log::warning("failed to read a request: error -> {}", expected.error());
}
}
38 changes: 38 additions & 0 deletions runtime-light/components/confdata/state/instance-state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstddef>

#include "common/mixin/not_copyable.h"
#include "runtime-light/allocator/allocator-state.h"
#include "runtime-light/coroutine/coroutine-state.h"
#include "runtime-light/coroutine/io-scheduler.h"
#include "runtime-light/coroutine/task.h"
#include "runtime-light/k2-platform/k2-api.h"
#include "runtime-light/stdlib/diagnostics/contextual-tags.h"

struct InstanceState final : vk::not_copyable {
AllocatorState instance_allocator_state{INIT_INSTANCE_ALLOCATOR_SIZE, DEFAULT_MIN_EXTRA_MEMORY_POOL_SIZE, 0};

kphp::log::contextual_tags instance_tags;

kphp::coro::instance_state coroutine_instance_state;
kphp::coro::io_scheduler io_scheduler{coroutine_instance_state};

InstanceState() noexcept = default;
static auto get() noexcept -> InstanceState&;

auto init() noexcept -> void;

private:
static constexpr auto INIT_INSTANCE_ALLOCATOR_SIZE = static_cast<size_t>(16U * 1024U * 1024U); // 16MiB

auto run() noexcept -> kphp::coro::task<>;
};

inline auto InstanceState::get() noexcept -> InstanceState& {
return *k2::instance_state();
}
Loading
Loading