From 7ef2d4ed4e88c2b583a9474ef37b2fcd039a8202 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Tue, 4 Aug 2026 15:29:42 +0300 Subject: [PATCH 01/12] [k2] move kphp component entry point and state to components/kphp/ Prepare runtime-light for multiple component entry points: the kphp component's sources (runtime-light.cpp and state/) move to components/kphp/, making room for sibling components (e.g. confdata). Includes are updated accordingly; .gitignore's bare "kphp" pattern is anchored to the repo root so it no longer hides the new directory. --- .gitignore | 2 +- runtime-light/allocator/allocator-state.cpp | 6 +++--- .../kphp/kphp-component.cpp} | 6 +++--- runtime-light/components/kphp/kphp.cmake | 2 ++ .../{ => components/kphp}/state/component-state.cpp | 2 +- runtime-light/{ => components/kphp}/state/component-state.h | 0 runtime-light/{ => components/kphp}/state/image-state.h | 0 .../{ => components/kphp}/state/instance-state.cpp | 4 ++-- runtime-light/{ => components/kphp}/state/instance-state.h | 2 +- runtime-light/core/globals/php-script-globals.cpp | 2 +- runtime-light/core/kphp-core-impl/kphp-core-context.cpp | 2 +- runtime-light/coroutine/coroutine-state.cpp | 2 +- runtime-light/coroutine/io-scheduler.cpp | 2 +- runtime-light/runtime-light.cmake | 5 ++--- runtime-light/server/cli/cli-instance-state.cpp | 2 +- runtime-light/server/http/http-server-state.cpp | 2 +- runtime-light/server/http/init-functions.cpp | 2 +- .../server/http/multipart/details/parts-processing.cpp | 2 +- runtime-light/server/job-worker/init-functions.h | 2 +- runtime-light/server/job-worker/job-worker-server-state.cpp | 2 +- runtime-light/server/rpc/rpc-server-state.cpp | 2 +- runtime-light/state/state.cmake | 1 - runtime-light/stdlib/confdata/confdata-state.cpp | 2 +- runtime-light/stdlib/curl/curl-state.cpp | 4 ++-- runtime-light/stdlib/diagnostics/backtrace.cpp | 2 +- runtime-light/stdlib/diagnostics/contextual-tags.cpp | 2 +- runtime-light/stdlib/diagnostics/error-handling-state.cpp | 4 ++-- runtime-light/stdlib/file/file-system-state.cpp | 2 +- runtime-light/stdlib/file/resource.cpp | 2 +- runtime-light/stdlib/fork/fork-state.cpp | 2 +- runtime-light/stdlib/fork/wait-queue-state.cpp | 2 +- .../stdlib/instance-cache/instance-cache-state.cpp | 2 +- runtime-light/stdlib/job-worker/job-worker-api.cpp | 2 +- runtime-light/stdlib/job-worker/job-worker-client-state.cpp | 2 +- runtime-light/stdlib/kml/kml-state.cpp | 4 ++-- runtime-light/stdlib/math/math-state.cpp | 4 ++-- runtime-light/stdlib/math/random-state.cpp | 2 +- runtime-light/stdlib/output/output-state.cpp | 2 +- runtime-light/stdlib/rpc/rpc-client-state.cpp | 4 ++-- runtime-light/stdlib/rpc/rpc-queue-state.cpp | 2 +- runtime-light/stdlib/serialization/serialization-state.cpp | 4 ++-- runtime-light/stdlib/server/args-functions.h | 2 +- runtime-light/stdlib/server/common-functions.h | 2 +- runtime-light/stdlib/server/handler-functions.h | 2 +- runtime-light/stdlib/string/regex-state.cpp | 2 +- runtime-light/stdlib/string/string-state.cpp | 4 ++-- runtime-light/stdlib/system/system-functions.h | 6 +++--- runtime-light/stdlib/system/system-state.cpp | 2 +- runtime-light/stdlib/time/time-state.cpp | 4 ++-- runtime-light/stdlib/visitors/array-visitors.h | 2 +- runtime-light/stdlib/web-transfer-lib/web-state.cpp | 2 +- 51 files changed, 65 insertions(+), 65 deletions(-) rename runtime-light/{runtime-light.cpp => components/kphp/kphp-component.cpp} (94%) create mode 100644 runtime-light/components/kphp/kphp.cmake rename runtime-light/{ => components/kphp}/state/component-state.cpp (98%) rename runtime-light/{ => components/kphp}/state/component-state.h (100%) rename runtime-light/{ => components/kphp}/state/image-state.h (100%) rename runtime-light/{ => components/kphp}/state/instance-state.cpp (98%) rename runtime-light/{ => components/kphp}/state/instance-state.h (98%) delete mode 100644 runtime-light/state/state.cmake diff --git a/.gitignore b/.gitignore index 51a74d35cc..3f3d9e916a 100644 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,5 @@ build /cmake-build-* dev -kphp +/kphp kphp_out diff --git a/runtime-light/allocator/allocator-state.cpp b/runtime-light/allocator/allocator-state.cpp index 88d8c1d43b..de644ebce9 100644 --- a/runtime-light/allocator/allocator-state.cpp +++ b/runtime-light/allocator/allocator-state.cpp @@ -4,10 +4,10 @@ #include "runtime-light/allocator/allocator-state.h" +#include "runtime-light/components/kphp/state/component-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.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" #include "runtime-light/stdlib/diagnostics/logs.h" const AllocatorState& AllocatorState::get() noexcept { diff --git a/runtime-light/runtime-light.cpp b/runtime-light/components/kphp/kphp-component.cpp similarity index 94% rename from runtime-light/runtime-light.cpp rename to runtime-light/components/kphp/kphp-component.cpp index 3afbcb8332..ea7968aca4 100644 --- a/runtime-light/runtime-light.cpp +++ b/runtime-light/components/kphp/kphp-component.cpp @@ -4,13 +4,13 @@ #include +#include "runtime-light/components/kphp/state/component-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/core/globals/php-init-scripts.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/state/component-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/diagnostics/logs.h" #define VISIBILITY_DEFAULT __attribute__((visibility("default"))) diff --git a/runtime-light/components/kphp/kphp.cmake b/runtime-light/components/kphp/kphp.cmake new file mode 100644 index 0000000000..8a41e81a00 --- /dev/null +++ b/runtime-light/components/kphp/kphp.cmake @@ -0,0 +1,2 @@ +prepend(RUNTIME_LIGHT_KPHP_COMPONENT_SRC components/kphp/ kphp-component.cpp + state/component-state.cpp state/instance-state.cpp) diff --git a/runtime-light/state/component-state.cpp b/runtime-light/components/kphp/state/component-state.cpp similarity index 98% rename from runtime-light/state/component-state.cpp rename to runtime-light/components/kphp/state/component-state.cpp index 5463088f88..9cd5b6e9da 100644 --- a/runtime-light/state/component-state.cpp +++ b/runtime-light/components/kphp/state/component-state.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2024 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime-light/state/component-state.h" +#include "runtime-light/components/kphp/state/component-state.h" #include #include diff --git a/runtime-light/state/component-state.h b/runtime-light/components/kphp/state/component-state.h similarity index 100% rename from runtime-light/state/component-state.h rename to runtime-light/components/kphp/state/component-state.h diff --git a/runtime-light/state/image-state.h b/runtime-light/components/kphp/state/image-state.h similarity index 100% rename from runtime-light/state/image-state.h rename to runtime-light/components/kphp/state/image-state.h diff --git a/runtime-light/state/instance-state.cpp b/runtime-light/components/kphp/state/instance-state.cpp similarity index 98% rename from runtime-light/state/instance-state.cpp rename to runtime-light/components/kphp/state/instance-state.cpp index 4bb90f6e55..e544ef9cb3 100644 --- a/runtime-light/state/instance-state.cpp +++ b/runtime-light/components/kphp/state/instance-state.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2024 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" #include #include @@ -15,6 +15,7 @@ #include "runtime-common/core/allocator/script-allocator.h" #include "runtime-common/core/runtime-core.h" #include "runtime-common/core/std/containers.h" +#include "runtime-light/components/kphp/state/component-state.h" #include "runtime-light/core/globals/php-init-scripts.h" #include "runtime-light/core/globals/php-script-globals.h" #include "runtime-light/coroutine/await-set.h" @@ -24,7 +25,6 @@ #include "runtime-light/server/http/http-server-state.h" #include "runtime-light/server/http/init-functions.h" #include "runtime-light/server/rpc/init-functions.h" -#include "runtime-light/state/component-state.h" #include "runtime-light/stdlib/component/component-api.h" #include "runtime-light/stdlib/diagnostics/logs.h" #include "runtime-light/stdlib/fork/fork-functions.h" diff --git a/runtime-light/state/instance-state.h b/runtime-light/components/kphp/state/instance-state.h similarity index 98% rename from runtime-light/state/instance-state.h rename to runtime-light/components/kphp/state/instance-state.h index f48a39f9c8..639dfceefc 100644 --- a/runtime-light/state/instance-state.h +++ b/runtime-light/components/kphp/state/instance-state.h @@ -11,6 +11,7 @@ #include "runtime-common/core/runtime-core.h" #include "runtime-common/core/std/containers.h" #include "runtime-light/allocator/allocator-state.h" +#include "runtime-light/components/kphp/state/component-state.h" #include "runtime-light/core/globals/php-script-globals.h" #include "runtime-light/coroutine/coroutine-state.h" #include "runtime-light/coroutine/io-scheduler.h" @@ -20,7 +21,6 @@ #include "runtime-light/server/http/http-server-state.h" #include "runtime-light/server/job-worker/job-worker-server-state.h" #include "runtime-light/server/rpc/rpc-server-state.h" -#include "runtime-light/state/component-state.h" #include "runtime-light/stdlib/confdata/confdata-state.h" #include "runtime-light/stdlib/curl/curl-state.h" #include "runtime-light/stdlib/diagnostics/contextual-tags.h" diff --git a/runtime-light/core/globals/php-script-globals.cpp b/runtime-light/core/globals/php-script-globals.cpp index 98ca963a4b..6f009471f3 100644 --- a/runtime-light/core/globals/php-script-globals.cpp +++ b/runtime-light/core/globals/php-script-globals.cpp @@ -4,7 +4,7 @@ #include "php-script-globals.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/stdlib/diagnostics/logs.h" PhpScriptMutableGlobals& PhpScriptMutableGlobals::current() noexcept { diff --git a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp index ce79f0ee45..14d3d0ecaf 100644 --- a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp +++ b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp @@ -3,8 +3,8 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #include "runtime-common/core/runtime-core.h" +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/k2-platform/k2-api.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/diagnostics/logs.h" namespace { diff --git a/runtime-light/coroutine/coroutine-state.cpp b/runtime-light/coroutine/coroutine-state.cpp index b7dc08d01d..2c8b4e925d 100644 --- a/runtime-light/coroutine/coroutine-state.cpp +++ b/runtime-light/coroutine/coroutine-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/coroutine/coroutine-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" CoroutineInstanceState& CoroutineInstanceState::get() noexcept { return InstanceState::get().coroutine_instance_state; diff --git a/runtime-light/coroutine/io-scheduler.cpp b/runtime-light/coroutine/io-scheduler.cpp index ea22b37b76..f396e55295 100644 --- a/runtime-light/coroutine/io-scheduler.cpp +++ b/runtime-light/coroutine/io-scheduler.cpp @@ -4,7 +4,7 @@ #include "runtime-light/coroutine/io-scheduler.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" namespace kphp::coro { diff --git a/runtime-light/runtime-light.cmake b/runtime-light/runtime-light.cmake index e5df2f323d..63307a80e4 100644 --- a/runtime-light/runtime-light.cmake +++ b/runtime-light/runtime-light.cmake @@ -16,12 +16,12 @@ set(RUNTIME_LIGHT_LINK_FLAGS -stdlib=libc++ -static-libstdc++ -static-libgcc ${R # ================================================================================================= include(${RUNTIME_LIGHT_DIR}/allocator/allocator.cmake) +include(${RUNTIME_LIGHT_DIR}/components/kphp/kphp.cmake) include(${RUNTIME_LIGHT_DIR}/core/core.cmake) include(${RUNTIME_LIGHT_DIR}/coroutine/coroutine.cmake) include(${RUNTIME_LIGHT_DIR}/server/server.cmake) include(${RUNTIME_LIGHT_DIR}/stdlib/stdlib.cmake) include(${RUNTIME_LIGHT_DIR}/tl/tl.cmake) -include(${RUNTIME_LIGHT_DIR}/state/state.cmake) include(${RUNTIME_LIGHT_DIR}/memory-resource-impl/memory-resource-impl.cmake) set(RUNTIME_LIGHT_SRC @@ -32,12 +32,11 @@ set(RUNTIME_LIGHT_SRC ${RUNTIME_LIGHT_SERVER_SRC} ${RUNTIME_LIGHT_ALLOCATOR_SRC} ${RUNTIME_LIGHT_COROUTINE_SRC} - ${RUNTIME_LIGHT_STATE_SRC} ${RUNTIME_LIGHT_STREAMS_SRC} ${RUNTIME_LIGHT_TL_SRC} ${RUNTIME_LIGHT_UTILS_SRC} ${RUNTIME_LIGHT_MEMORY_RESOURCE_IMPL_SRC} - runtime-light.cpp) + ${RUNTIME_LIGHT_KPHP_COMPONENT_SRC}) set(RUNTIME_SOURCES_FOR_COMP "${RUNTIME_LIGHT_SRC}") configure_file(${BASE_DIR}/compiler/runtime_sources.h.in ${AUTO_DIR}/compiler/runtime_sources.h) diff --git a/runtime-light/server/cli/cli-instance-state.cpp b/runtime-light/server/cli/cli-instance-state.cpp index cbccadfb38..8f00e23fab 100644 --- a/runtime-light/server/cli/cli-instance-state.cpp +++ b/runtime-light/server/cli/cli-instance-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/server/cli/cli-instance-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" CLIInstanceInstance& CLIInstanceInstance::get() noexcept { return InstanceState::get().cli_instance_instate; diff --git a/runtime-light/server/http/http-server-state.cpp b/runtime-light/server/http/http-server-state.cpp index 42bca3cb04..c6b7f6162a 100644 --- a/runtime-light/server/http/http-server-state.cpp +++ b/runtime-light/server/http/http-server-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/server/http/http-server-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" HttpServerInstanceState& HttpServerInstanceState::get() noexcept { return InstanceState::get().http_server_instance_state; diff --git a/runtime-light/server/http/init-functions.cpp b/runtime-light/server/http/init-functions.cpp index 7ded360ceb..1c0a83eb9d 100644 --- a/runtime-light/server/http/init-functions.cpp +++ b/runtime-light/server/http/init-functions.cpp @@ -24,12 +24,12 @@ #include "runtime-common/core/runtime-core.h" #include "runtime-common/core/std/containers.h" #include "runtime-common/stdlib/server/url-functions.h" +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/core/globals/php-script-globals.h" #include "runtime-light/coroutine/task.h" #include "runtime-light/k2-platform/k2-api.h" #include "runtime-light/server/http/http-server-state.h" #include "runtime-light/server/http/multipart/multipart.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/component/component-api.h" #include "runtime-light/stdlib/diagnostics/logs.h" #include "runtime-light/stdlib/output/output-state.h" diff --git a/runtime-light/server/http/multipart/details/parts-processing.cpp b/runtime-light/server/http/multipart/details/parts-processing.cpp index 867432a70f..18b1c24672 100644 --- a/runtime-light/server/http/multipart/details/parts-processing.cpp +++ b/runtime-light/server/http/multipart/details/parts-processing.cpp @@ -20,10 +20,10 @@ #include "runtime-common/core/runtime-core.h" #include "runtime-common/core/std/containers.h" #include "runtime-common/stdlib/server/url-functions.h" +#include "runtime-light/components/kphp/state/component-state.h" #include "runtime-light/k2-platform/k2-api.h" #include "runtime-light/server/http/http-server-state.h" #include "runtime-light/server/http/multipart/details/parts-parsing.h" -#include "runtime-light/state/component-state.h" #include "runtime-light/stdlib/diagnostics/logs.h" #include "runtime-light/stdlib/file/resource.h" #include "runtime-light/stdlib/math/random-functions.h" diff --git a/runtime-light/server/job-worker/init-functions.h b/runtime-light/server/job-worker/init-functions.h index fbb8cae3f7..d676a17708 100644 --- a/runtime-light/server/job-worker/init-functions.h +++ b/runtime-light/server/job-worker/init-functions.h @@ -4,9 +4,9 @@ #pragma once +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/core/globals/php-script-globals.h" #include "runtime-light/server/job-worker/job-worker-server-state.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/tl/tl-functions.h" inline void init_job_server(tl::K2InvokeJobWorker invoke_jw) noexcept { diff --git a/runtime-light/server/job-worker/job-worker-server-state.cpp b/runtime-light/server/job-worker/job-worker-server-state.cpp index 74188133cd..706748d5c1 100644 --- a/runtime-light/server/job-worker/job-worker-server-state.cpp +++ b/runtime-light/server/job-worker/job-worker-server-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/server/job-worker/job-worker-server-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" JobWorkerServerInstanceState& JobWorkerServerInstanceState::get() noexcept { return InstanceState::get().job_worker_server_instance_state; diff --git a/runtime-light/server/rpc/rpc-server-state.cpp b/runtime-light/server/rpc/rpc-server-state.cpp index 75e8ba268e..6debd9a782 100644 --- a/runtime-light/server/rpc/rpc-server-state.cpp +++ b/runtime-light/server/rpc/rpc-server-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/server/rpc/rpc-server-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" RpcServerInstanceState& RpcServerInstanceState::get() noexcept { return InstanceState::get().rpc_server_instance_state; diff --git a/runtime-light/state/state.cmake b/runtime-light/state/state.cmake deleted file mode 100644 index a8b292b562..0000000000 --- a/runtime-light/state/state.cmake +++ /dev/null @@ -1 +0,0 @@ -prepend(RUNTIME_LIGHT_STATE_SRC state/ component-state.cpp instance-state.cpp) diff --git a/runtime-light/stdlib/confdata/confdata-state.cpp b/runtime-light/stdlib/confdata/confdata-state.cpp index e69d9fa5dc..33e34502aa 100644 --- a/runtime-light/stdlib/confdata/confdata-state.cpp +++ b/runtime-light/stdlib/confdata/confdata-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/confdata/confdata-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" ConfdataInstanceState& ConfdataInstanceState::get() noexcept { return InstanceState::get().confdata_instance_state; diff --git a/runtime-light/stdlib/curl/curl-state.cpp b/runtime-light/stdlib/curl/curl-state.cpp index 019d97fdc6..6802f2e9fe 100644 --- a/runtime-light/stdlib/curl/curl-state.cpp +++ b/runtime-light/stdlib/curl/curl-state.cpp @@ -4,8 +4,8 @@ #include "runtime-light/stdlib/curl/curl-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" CurlInstanceState& CurlInstanceState::get() noexcept { return InstanceState::get().curl_instance_state; diff --git a/runtime-light/stdlib/diagnostics/backtrace.cpp b/runtime-light/stdlib/diagnostics/backtrace.cpp index c12092a187..13db879b81 100644 --- a/runtime-light/stdlib/diagnostics/backtrace.cpp +++ b/runtime-light/stdlib/diagnostics/backtrace.cpp @@ -6,10 +6,10 @@ #include #include +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/coroutine/async-stack.h" #include "runtime-light/coroutine/coroutine-state.h" #include "runtime-light/k2-platform/k2-api.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/diagnostics/backtrace.h" #include "runtime-light/stdlib/diagnostics/logs.h" diff --git a/runtime-light/stdlib/diagnostics/contextual-tags.cpp b/runtime-light/stdlib/diagnostics/contextual-tags.cpp index a662e7b2bd..6e9745cb74 100644 --- a/runtime-light/stdlib/diagnostics/contextual-tags.cpp +++ b/runtime-light/stdlib/diagnostics/contextual-tags.cpp @@ -8,8 +8,8 @@ #include #include +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/k2-platform/k2-api.h" -#include "runtime-light/state/instance-state.h" namespace kphp::log { diff --git a/runtime-light/stdlib/diagnostics/error-handling-state.cpp b/runtime-light/stdlib/diagnostics/error-handling-state.cpp index 78829795b3..93a1654189 100644 --- a/runtime-light/stdlib/diagnostics/error-handling-state.cpp +++ b/runtime-light/stdlib/diagnostics/error-handling-state.cpp @@ -9,8 +9,8 @@ #include #include "runtime-common/core/runtime-core.h" -#include "runtime-light/state/component-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/component-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" ErrorHandlingState::ErrorHandlingState() noexcept { const auto& component_st{ComponentState::get()}; diff --git a/runtime-light/stdlib/file/file-system-state.cpp b/runtime-light/stdlib/file/file-system-state.cpp index e3c31c1749..b592ea3c9c 100644 --- a/runtime-light/stdlib/file/file-system-state.cpp +++ b/runtime-light/stdlib/file/file-system-state.cpp @@ -8,7 +8,7 @@ #include "common/php-functions.h" #include "runtime-common/core/runtime-core.h" -#include "runtime-light/state/image-state.h" +#include "runtime-light/components/kphp/state/image-state.h" namespace { diff --git a/runtime-light/stdlib/file/resource.cpp b/runtime-light/stdlib/file/resource.cpp index f7a8327d75..7ddfad6b6f 100644 --- a/runtime-light/stdlib/file/resource.cpp +++ b/runtime-light/stdlib/file/resource.cpp @@ -7,7 +7,7 @@ #include #include -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" namespace kphp::fs { diff --git a/runtime-light/stdlib/fork/fork-state.cpp b/runtime-light/stdlib/fork/fork-state.cpp index 4655aa3455..ba402a8da8 100644 --- a/runtime-light/stdlib/fork/fork-state.cpp +++ b/runtime-light/stdlib/fork/fork-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/fork/fork-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" ForkInstanceState& ForkInstanceState::get() noexcept { return InstanceState::get().fork_instance_state; diff --git a/runtime-light/stdlib/fork/wait-queue-state.cpp b/runtime-light/stdlib/fork/wait-queue-state.cpp index 4b51ea7f2c..d89304b401 100644 --- a/runtime-light/stdlib/fork/wait-queue-state.cpp +++ b/runtime-light/stdlib/fork/wait-queue-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/fork/wait-queue-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" WaitQueueInstanceState& WaitQueueInstanceState::get() noexcept { return InstanceState::get().wait_queue_instance_state; diff --git a/runtime-light/stdlib/instance-cache/instance-cache-state.cpp b/runtime-light/stdlib/instance-cache/instance-cache-state.cpp index 15c84e80a2..785095f879 100644 --- a/runtime-light/stdlib/instance-cache/instance-cache-state.cpp +++ b/runtime-light/stdlib/instance-cache/instance-cache-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/instance-cache/instance-cache-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" InstanceCacheInstanceState& InstanceCacheInstanceState::get() noexcept { return InstanceState::get().instance_cache_instance_state; diff --git a/runtime-light/stdlib/job-worker/job-worker-api.cpp b/runtime-light/stdlib/job-worker/job-worker-api.cpp index ec36599895..ebf43d1bdb 100644 --- a/runtime-light/stdlib/job-worker/job-worker-api.cpp +++ b/runtime-light/stdlib/job-worker/job-worker-api.cpp @@ -11,11 +11,11 @@ #include #include "runtime-common/core/runtime-core.h" +#include "runtime-light/components/kphp/state/instance-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/server/job-worker/job-worker-server-state.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/component/component-api.h" #include "runtime-light/stdlib/fork/fork-functions.h" #include "runtime-light/stdlib/fork/fork-state.h" diff --git a/runtime-light/stdlib/job-worker/job-worker-client-state.cpp b/runtime-light/stdlib/job-worker/job-worker-client-state.cpp index 3b7921304c..f505f5f229 100644 --- a/runtime-light/stdlib/job-worker/job-worker-client-state.cpp +++ b/runtime-light/stdlib/job-worker/job-worker-client-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/job-worker/job-worker-client-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" JobWorkerClientInstanceState& JobWorkerClientInstanceState::get() noexcept { return InstanceState::get().job_worker_client_instance_state; diff --git a/runtime-light/stdlib/kml/kml-state.cpp b/runtime-light/stdlib/kml/kml-state.cpp index 96f061ad86..faaa72a993 100644 --- a/runtime-light/stdlib/kml/kml-state.cpp +++ b/runtime-light/stdlib/kml/kml-state.cpp @@ -4,8 +4,8 @@ #include "runtime-light/stdlib/kml/kml-state.h" -#include "runtime-light/state/component-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/component-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" template<> const KmlComponentState& KmlComponentState::get() noexcept { diff --git a/runtime-light/stdlib/math/math-state.cpp b/runtime-light/stdlib/math/math-state.cpp index 130ff5bae7..b3d1dbf486 100644 --- a/runtime-light/stdlib/math/math-state.cpp +++ b/runtime-light/stdlib/math/math-state.cpp @@ -4,8 +4,8 @@ #include "runtime-light/stdlib/math/math-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" MathInstanceState& MathInstanceState::get() noexcept { return InstanceState::get().math_instance_state; diff --git a/runtime-light/stdlib/math/random-state.cpp b/runtime-light/stdlib/math/random-state.cpp index 09388d3910..256874cdbb 100644 --- a/runtime-light/stdlib/math/random-state.cpp +++ b/runtime-light/stdlib/math/random-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/math/random-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" RandomInstanceState& RandomInstanceState::get() noexcept { return InstanceState::get().random_instance_state; diff --git a/runtime-light/stdlib/output/output-state.cpp b/runtime-light/stdlib/output/output-state.cpp index c03f5165ff..b3f36b3d44 100644 --- a/runtime-light/stdlib/output/output-state.cpp +++ b/runtime-light/stdlib/output/output-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/output/output-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" OutputInstanceState& OutputInstanceState::get() noexcept { return InstanceState::get().output_instance_state; diff --git a/runtime-light/stdlib/rpc/rpc-client-state.cpp b/runtime-light/stdlib/rpc/rpc-client-state.cpp index e4ebc32e9a..ffdeef5c0c 100644 --- a/runtime-light/stdlib/rpc/rpc-client-state.cpp +++ b/runtime-light/stdlib/rpc/rpc-client-state.cpp @@ -4,8 +4,8 @@ #include "runtime-light/stdlib/rpc/rpc-client-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" RpcClientInstanceState& RpcClientInstanceState::get() noexcept { return InstanceState::get().rpc_client_instance_state; diff --git a/runtime-light/stdlib/rpc/rpc-queue-state.cpp b/runtime-light/stdlib/rpc/rpc-queue-state.cpp index a346863a01..fa48499498 100644 --- a/runtime-light/stdlib/rpc/rpc-queue-state.cpp +++ b/runtime-light/stdlib/rpc/rpc-queue-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/rpc/rpc-queue-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" RpcQueueInstanceState& RpcQueueInstanceState::get() noexcept { return InstanceState::get().rpc_queue_instance_state; diff --git a/runtime-light/stdlib/serialization/serialization-state.cpp b/runtime-light/stdlib/serialization/serialization-state.cpp index 29f6632898..f819e19d57 100644 --- a/runtime-light/stdlib/serialization/serialization-state.cpp +++ b/runtime-light/stdlib/serialization/serialization-state.cpp @@ -4,8 +4,8 @@ #include "runtime-light/stdlib/serialization/serialization-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" SerializationInstanceState& SerializationInstanceState::get() noexcept { return InstanceState::get().serialization_instance_state; diff --git a/runtime-light/stdlib/server/args-functions.h b/runtime-light/stdlib/server/args-functions.h index bf9c93b9f3..bd4544950d 100644 --- a/runtime-light/stdlib/server/args-functions.h +++ b/runtime-light/stdlib/server/args-functions.h @@ -5,7 +5,7 @@ #pragma once #include "runtime-common/core/runtime-core.h" -#include "runtime-light/state/component-state.h" +#include "runtime-light/components/kphp/state/component-state.h" inline Optional f$ini_get(const string& key) noexcept { const auto& component_st{ComponentState::get()}; diff --git a/runtime-light/stdlib/server/common-functions.h b/runtime-light/stdlib/server/common-functions.h index e5716cab42..f622501743 100644 --- a/runtime-light/stdlib/server/common-functions.h +++ b/runtime-light/stdlib/server/common-functions.h @@ -7,9 +7,9 @@ #include #include "runtime-common/core/runtime-core.h" +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/coroutine/task.h" #include "runtime-light/server/http/http-server-state.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/diagnostics/logs.h" #include "runtime-light/stdlib/fork/fork-functions.h" #include "runtime-light/stdlib/system/system-functions.h" diff --git a/runtime-light/stdlib/server/handler-functions.h b/runtime-light/stdlib/server/handler-functions.h index 49c562c045..41c9981b0c 100644 --- a/runtime-light/stdlib/server/handler-functions.h +++ b/runtime-light/stdlib/server/handler-functions.h @@ -8,9 +8,9 @@ #include #include +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/coroutine/task.h" #include "runtime-light/coroutine/type-traits.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/diagnostics/logs.h" template diff --git a/runtime-light/stdlib/string/regex-state.cpp b/runtime-light/stdlib/string/regex-state.cpp index 39e14265af..ac870d0f47 100644 --- a/runtime-light/stdlib/string/regex-state.cpp +++ b/runtime-light/stdlib/string/regex-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/string/regex-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" RegexInstanceState& RegexInstanceState::get() noexcept { return InstanceState::get().regex_instance_state; diff --git a/runtime-light/stdlib/string/string-state.cpp b/runtime-light/stdlib/string/string-state.cpp index 5d45cb722b..2172403bb4 100644 --- a/runtime-light/stdlib/string/string-state.cpp +++ b/runtime-light/stdlib/string/string-state.cpp @@ -4,8 +4,8 @@ #include "runtime-light/stdlib/string/string-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" StringInstanceState& StringInstanceState::get() noexcept { return InstanceState::get().string_instance_state; diff --git a/runtime-light/stdlib/system/system-functions.h b/runtime-light/stdlib/system/system-functions.h index 1b1845ce11..09cad7370a 100644 --- a/runtime-light/stdlib/system/system-functions.h +++ b/runtime-light/stdlib/system/system-functions.h @@ -26,13 +26,13 @@ #include "runtime-common/core/allocator/script-malloc-interface.h" #include "runtime-common/core/runtime-core.h" #include "runtime-common/stdlib/serialization/json-functions.h" +#include "runtime-light/components/kphp/state/component-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/core/globals/php-script-globals.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/state/component-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" #include "runtime-light/stdlib/diagnostics/contextual-tags.h" #include "runtime-light/stdlib/diagnostics/logs.h" #include "runtime-light/stdlib/fork/fork-functions.h" diff --git a/runtime-light/stdlib/system/system-state.cpp b/runtime-light/stdlib/system/system-state.cpp index 34269aeadf..81a1ee0540 100644 --- a/runtime-light/stdlib/system/system-state.cpp +++ b/runtime-light/stdlib/system/system-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/system/system-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" SystemInstanceState& SystemInstanceState::get() noexcept { return InstanceState::get().system_instance_state; diff --git a/runtime-light/stdlib/time/time-state.cpp b/runtime-light/stdlib/time/time-state.cpp index 637f3929d1..42e0792662 100644 --- a/runtime-light/stdlib/time/time-state.cpp +++ b/runtime-light/stdlib/time/time-state.cpp @@ -4,8 +4,8 @@ #include "runtime-light/stdlib/time/time-state.h" -#include "runtime-light/state/image-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" TimeInstanceState& TimeInstanceState::get() noexcept { return InstanceState::get().time_instance_state; diff --git a/runtime-light/stdlib/visitors/array-visitors.h b/runtime-light/stdlib/visitors/array-visitors.h index c6f7b85121..e0cde9b84f 100644 --- a/runtime-light/stdlib/visitors/array-visitors.h +++ b/runtime-light/stdlib/visitors/array-visitors.h @@ -5,7 +5,7 @@ #pragma once #include "runtime-common/core/runtime-core.h" -#include "runtime-light/state/image-state.h" +#include "runtime-light/components/kphp/state/image-state.h" #include "runtime-light/stdlib/visitors/shape-visitors.h" class ToArrayVisitor { diff --git a/runtime-light/stdlib/web-transfer-lib/web-state.cpp b/runtime-light/stdlib/web-transfer-lib/web-state.cpp index 96d1fdb523..237306607b 100644 --- a/runtime-light/stdlib/web-transfer-lib/web-state.cpp +++ b/runtime-light/stdlib/web-transfer-lib/web-state.cpp @@ -4,7 +4,7 @@ #include "runtime-light/stdlib/web-transfer-lib/web-state.h" -#include "runtime-light/state/instance-state.h" +#include "runtime-light/components/kphp/state/instance-state.h" WebInstanceState& WebInstanceState::get() noexcept { return InstanceState::get().web_instance_state; From f04c0213f49b3abd62408aa1c5278786f26376f2 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Tue, 4 Aug 2026 18:20:27 +0300 Subject: [PATCH 02/12] [k2] introduce per-component state bindings Common runtime code no longer depends on the concrete kphp InstanceState: state accessors (kphp::coro::instance_state::get, io_scheduler::get, AllocatorState::get, ErrorHandlingState, RuntimeContext::get, contextual_tags::try_get) are resolved via link-time bindings implemented in components/kphp/bindings/bindings.cpp. CoroutineInstanceState is moved into kphp::coro as instance_state, io_scheduler receives it via its constructor, and async_backtrace reaches the stack root through the binding instead of the concrete state layout. --- runtime-light/allocator/allocator-state.cpp | 26 ------ runtime-light/allocator/allocator-state.h | 4 +- runtime-light/allocator/allocator.cmake | 3 +- .../components/kphp/bindings/bindings.cpp | 82 +++++++++++++++++++ runtime-light/components/kphp/kphp.cmake | 3 +- .../components/kphp/state/instance-state.h | 4 +- .../core/kphp-core-impl/kphp-core-context.cpp | 10 --- runtime-light/coroutine/await-set.h | 4 +- runtime-light/coroutine/coroutine-state.cpp | 11 --- runtime-light/coroutine/coroutine-state.h | 10 ++- runtime-light/coroutine/coroutine.cmake | 2 - .../coroutine/detail/task-self-deleting.h | 2 +- runtime-light/coroutine/event.h | 2 +- runtime-light/coroutine/io-scheduler.cpp | 15 ---- runtime-light/coroutine/io-scheduler.h | 13 +-- runtime-light/runtime-light.cmake | 3 - .../stdlib/diagnostics/backtrace.cpp | 22 ++--- .../stdlib/diagnostics/contextual-tags.cpp | 23 ------ .../diagnostics/error-handling-state.cpp | 35 -------- runtime-light/stdlib/stdlib.cmake | 2 - 20 files changed, 120 insertions(+), 156 deletions(-) delete mode 100644 runtime-light/allocator/allocator-state.cpp create mode 100644 runtime-light/components/kphp/bindings/bindings.cpp delete mode 100644 runtime-light/coroutine/coroutine-state.cpp delete mode 100644 runtime-light/coroutine/coroutine.cmake delete mode 100644 runtime-light/coroutine/io-scheduler.cpp delete mode 100644 runtime-light/stdlib/diagnostics/contextual-tags.cpp delete mode 100644 runtime-light/stdlib/diagnostics/error-handling-state.cpp diff --git a/runtime-light/allocator/allocator-state.cpp b/runtime-light/allocator/allocator-state.cpp deleted file mode 100644 index de644ebce9..0000000000 --- a/runtime-light/allocator/allocator-state.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/allocator/allocator-state.h" - -#include "runtime-light/components/kphp/state/component-state.h" -#include "runtime-light/components/kphp/state/image-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" -#include "runtime-light/k2-platform/k2-api.h" -#include "runtime-light/stdlib/diagnostics/logs.h" - -const AllocatorState& AllocatorState::get() noexcept { - if (const auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { - return instance_state_ptr->instance_allocator_state; - } else if (const auto* component_state_ptr{k2::component_state()}; component_state_ptr != nullptr) { - return component_state_ptr->component_allocator_state; - } else if (const auto* image_state_ptr{k2::image_state()}; image_state_ptr != nullptr) { - return image_state_ptr->image_allocator_state; - } - kphp::log::error("can't find allocator state"); -} - -AllocatorState& AllocatorState::get_mutable() noexcept { - return const_cast(AllocatorState::get()); -} diff --git a/runtime-light/allocator/allocator-state.h b/runtime-light/allocator/allocator-state.h index bf0f15d423..a5cc2e79e7 100644 --- a/runtime-light/allocator/allocator-state.h +++ b/runtime-light/allocator/allocator-state.h @@ -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(get()); + } }; diff --git a/runtime-light/allocator/allocator.cmake b/runtime-light/allocator/allocator.cmake index 78a9747af4..85880d002e 100644 --- a/runtime-light/allocator/allocator.cmake +++ b/runtime-light/allocator/allocator.cmake @@ -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) diff --git a/runtime-light/components/kphp/bindings/bindings.cpp b/runtime-light/components/kphp/bindings/bindings.cpp new file mode 100644 index 0000000000..fb8ae235f2 --- /dev/null +++ b/runtime-light/components/kphp/bindings/bindings.cpp @@ -0,0 +1,82 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2026 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include +#include +#include + +#include "runtime-common/core/runtime-core.h" +#include "runtime-light/allocator/allocator-state.h" +#include "runtime-light/components/kphp/state/component-state.h" +#include "runtime-light/components/kphp/state/image-state.h" +#include "runtime-light/components/kphp/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" + +const AllocatorState& AllocatorState::get() noexcept { + if (const auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { + return instance_state_ptr->instance_allocator_state; + } else if (const auto* component_state_ptr{k2::component_state()}; component_state_ptr != nullptr) { + return component_state_ptr->component_allocator_state; + } else if (const auto* image_state_ptr{k2::image_state()}; image_state_ptr != nullptr) { + return image_state_ptr->image_allocator_state; + } + kphp::log::error("can't find allocator state"); +} + +RuntimeContext& RuntimeContext::get() noexcept { + if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { + return instance_state_ptr->runtime_context; + } + kphp::log::error("unexpected access to RuntimeContext"); +} + +ErrorHandlingState::ErrorHandlingState() noexcept { + const auto& component_st{ComponentState::get()}; + const auto& default_level_str{component_st.ini_opts.get_value(string{INI_ERROR_REPORTING_KEY.data(), INI_ERROR_REPORTING_KEY.size()})}; + if (default_level_str.empty() || !default_level_str.is_int()) { + return; + } + + const int64_t default_level{default_level_str.to_int()}; + minimum_log_level = static_cast(SUPPORTED_ERROR_LEVELS & default_level) ? default_level : minimum_log_level; +} + +ErrorHandlingState& ErrorHandlingState::get() noexcept { + return InstanceState::get().error_handling_instance_state; +} + +std::optional> ErrorHandlingState::try_get() noexcept { + if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { + return instance_state_ptr->error_handling_instance_state; + } + return std::nullopt; +} + +namespace kphp::coro { + +instance_state& instance_state::get() noexcept { + return InstanceState::get().coroutine_instance_state; +} + +io_scheduler& io_scheduler::get() noexcept { + return InstanceState::get().io_scheduler; +} + +} // namespace kphp::coro + +namespace kphp::log { + +std::optional> contextual_tags::try_get() noexcept { + if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { + return instance_state_ptr->instance_tags; + } + return std::nullopt; +} + +} // namespace kphp::log diff --git a/runtime-light/components/kphp/kphp.cmake b/runtime-light/components/kphp/kphp.cmake index 8a41e81a00..3e786ff62e 100644 --- a/runtime-light/components/kphp/kphp.cmake +++ b/runtime-light/components/kphp/kphp.cmake @@ -1,2 +1,3 @@ prepend(RUNTIME_LIGHT_KPHP_COMPONENT_SRC components/kphp/ kphp-component.cpp - state/component-state.cpp state/instance-state.cpp) + bindings/bindings.cpp state/component-state.cpp + state/instance-state.cpp) diff --git a/runtime-light/components/kphp/state/instance-state.h b/runtime-light/components/kphp/state/instance-state.h index 639dfceefc..65ba170383 100644 --- a/runtime-light/components/kphp/state/instance-state.h +++ b/runtime-light/components/kphp/state/instance-state.h @@ -91,9 +91,9 @@ struct InstanceState final : vk::not_copyable { AllocatorState instance_allocator_state{ComponentState::get().initial_instance_memory_size, ComponentState::get().min_instance_extra_memory_size, 0}; kphp::log::contextual_tags instance_tags; - kphp::coro::io_scheduler io_scheduler; - CoroutineInstanceState coroutine_instance_state; + kphp::coro::instance_state coroutine_instance_state; + kphp::coro::io_scheduler io_scheduler{coroutine_instance_state}; ForkInstanceState fork_instance_state; WaitQueueInstanceState wait_queue_instance_state; RpcQueueInstanceState rpc_queue_instance_state; diff --git a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp index 14d3d0ecaf..4be268c33d 100644 --- a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp +++ b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp @@ -3,9 +3,6 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #include "runtime-common/core/runtime-core.h" -#include "runtime-light/components/kphp/state/instance-state.h" -#include "runtime-light/k2-platform/k2-api.h" -#include "runtime-light/stdlib/diagnostics/logs.h" namespace { @@ -14,13 +11,6 @@ constexpr string_size_type initial_maximum_string_buffer_length = (1 << 24); } // namespace -RuntimeContext& RuntimeContext::get() noexcept { - if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { - return instance_state_ptr->runtime_context; - } - kphp::log::error("unexpected access to RuntimeContext"); -} - void RuntimeContext::init() noexcept { init_string_buffer_lib(initial_minimum_string_buffer_length, initial_maximum_string_buffer_length); } diff --git a/runtime-light/coroutine/await-set.h b/runtime-light/coroutine/await-set.h index 1fbf78de2e..3117d46cc7 100644 --- a/runtime-light/coroutine/await-set.h +++ b/runtime-light/coroutine/await-set.h @@ -26,7 +26,7 @@ class await_set { public: await_set() noexcept : m_await_broker(std::make_unique>()), - m_coroutine_stack_root(CoroutineInstanceState::get().coroutine_stack_root) {} + m_coroutine_stack_root(kphp::coro::instance_state::get().coroutine_stack_root) {} await_set(await_set&& other) noexcept : m_await_broker(std::move(other.m_await_broker)), @@ -40,6 +40,8 @@ class await_set { return *this; } + ~await_set() noexcept = default; + await_set(const await_set&) = delete; await_set& operator=(const await_set&) = delete; diff --git a/runtime-light/coroutine/coroutine-state.cpp b/runtime-light/coroutine/coroutine-state.cpp deleted file mode 100644 index 2c8b4e925d..0000000000 --- a/runtime-light/coroutine/coroutine-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/coroutine/coroutine-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -CoroutineInstanceState& CoroutineInstanceState::get() noexcept { - return InstanceState::get().coroutine_instance_state; -} diff --git a/runtime-light/coroutine/coroutine-state.h b/runtime-light/coroutine/coroutine-state.h index b6d260c278..169ff00993 100644 --- a/runtime-light/coroutine/coroutine-state.h +++ b/runtime-light/coroutine/coroutine-state.h @@ -8,11 +8,15 @@ #include "runtime-light/coroutine/async-stack.h" -struct CoroutineInstanceState final : private vk::not_copyable { +namespace kphp::coro { - CoroutineInstanceState() noexcept = default; +struct instance_state final : private vk::not_copyable { - static CoroutineInstanceState& get() noexcept; + instance_state() noexcept = default; + + static instance_state& get() noexcept; kphp::coro::async_stack_root coroutine_stack_root; }; + +} // namespace kphp::coro diff --git a/runtime-light/coroutine/coroutine.cmake b/runtime-light/coroutine/coroutine.cmake deleted file mode 100644 index 83fc6e2858..0000000000 --- a/runtime-light/coroutine/coroutine.cmake +++ /dev/null @@ -1,2 +0,0 @@ -prepend(RUNTIME_LIGHT_COROUTINE_SRC coroutine/ coroutine-state.cpp - io-scheduler.cpp) diff --git a/runtime-light/coroutine/detail/task-self-deleting.h b/runtime-light/coroutine/detail/task-self-deleting.h index 73dcc51e59..e6908f575b 100644 --- a/runtime-light/coroutine/detail/task-self-deleting.h +++ b/runtime-light/coroutine/detail/task-self-deleting.h @@ -77,7 +77,7 @@ class task_self_deleting { // initialize task_self_deleting's frame as top async stack frame auto& async_stack_frame{m_promise.get_async_stack_frame()}; async_stack_frame.return_address = STACK_RETURN_ADDRESS; - async_stack_frame.async_stack_root = std::addressof(CoroutineInstanceState::get().coroutine_stack_root); + async_stack_frame.async_stack_root = std::addressof(kphp::coro::instance_state::get().coroutine_stack_root); } ~task_self_deleting() noexcept = default; diff --git a/runtime-light/coroutine/event.h b/runtime-light/coroutine/event.h index 905345c97a..4be3984ac8 100644 --- a/runtime-light/coroutine/event.h +++ b/runtime-light/coroutine/event.h @@ -41,7 +41,7 @@ class event { explicit awaiter(event_controller& event_controller) noexcept : m_controller(event_controller), - m_async_stack_root(CoroutineInstanceState::get().coroutine_stack_root) {} + m_async_stack_root(kphp::coro::instance_state::get().coroutine_stack_root) {} awaiter(const awaiter&) = delete; awaiter(awaiter&&) = delete; diff --git a/runtime-light/coroutine/io-scheduler.cpp b/runtime-light/coroutine/io-scheduler.cpp deleted file mode 100644 index f396e55295..0000000000 --- a/runtime-light/coroutine/io-scheduler.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/coroutine/io-scheduler.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -namespace kphp::coro { - -io_scheduler& io_scheduler::get() noexcept { - return InstanceState::get().io_scheduler; -} - -} // namespace kphp::coro diff --git a/runtime-light/coroutine/io-scheduler.h b/runtime-light/coroutine/io-scheduler.h index 406016184e..781173699f 100644 --- a/runtime-light/coroutine/io-scheduler.h +++ b/runtime-light/coroutine/io-scheduler.h @@ -54,7 +54,7 @@ class io_scheduler { kphp::coro::detail::poll_info::parked_polls m_parked_polls; kphp::coro::detail::poll_info::scheduled_coroutines m_scheduled_coroutines; - CoroutineInstanceState& m_coroutine_instance_state{CoroutineInstanceState::get()}; + kphp::coro::instance_state& m_coro_instance_state; auto make_cancellation_handler(kphp::coro::detail::poll_info& poll_info) noexcept; auto make_timeout_task(std::chrono::milliseconds timeout) noexcept -> kphp::coro::task; @@ -69,7 +69,8 @@ class io_scheduler { auto remove_timer_token(kphp::coro::detail::poll_info::timed_events::iterator pos) noexcept -> void; public: - io_scheduler() noexcept = default; + explicit io_scheduler(kphp::coro::instance_state& coroutine_instance_state) noexcept + : m_coro_instance_state(coroutine_instance_state) {} ~io_scheduler() = default; io_scheduler(const io_scheduler&) = delete; @@ -427,7 +428,7 @@ inline auto io_scheduler::process_events() noexcept -> k2::PollStatus { */ scheduled_coroutines.pop_front(); kphp::log::assertion(static_cast(coroutine)); - kphp::coro::resume(coroutine, m_coroutine_instance_state.coroutine_stack_root); + kphp::coro::resume(coroutine, m_coro_instance_state.coroutine_stack_root); } } @@ -452,7 +453,7 @@ auto io_scheduler::start(coroutine_type coroutine) noexcept -> bool { if (!handle || handle.done()) [[unlikely]] { return false; } - kphp::coro::resume(handle, m_coroutine_instance_state.coroutine_stack_root); + kphp::coro::resume(handle, m_coro_instance_state.coroutine_stack_root); return true; } @@ -466,7 +467,7 @@ inline auto io_scheduler::schedule() noexcept { explicit schedule_operation(io_scheduler& scheduler) noexcept : m_scheduler(scheduler), - m_async_stack_frame(m_scheduler.m_coroutine_instance_state.coroutine_stack_root.top_async_stack_frame) {} + m_async_stack_frame(m_scheduler.m_coro_instance_state.coroutine_stack_root.top_async_stack_frame) {} public: schedule_operation(const schedule_operation&) = delete; @@ -508,7 +509,7 @@ inline auto io_scheduler::schedule() noexcept { auto await_resume() noexcept -> void { m_schedule_pos = std::monostate{}; - m_scheduler.m_coroutine_instance_state.coroutine_stack_root.top_async_stack_frame = m_async_stack_frame; + m_scheduler.m_coro_instance_state.coroutine_stack_root.top_async_stack_frame = m_async_stack_frame; } }; return schedule_operation{*this}; diff --git a/runtime-light/runtime-light.cmake b/runtime-light/runtime-light.cmake index 63307a80e4..014c58b35d 100644 --- a/runtime-light/runtime-light.cmake +++ b/runtime-light/runtime-light.cmake @@ -18,7 +18,6 @@ set(RUNTIME_LIGHT_LINK_FLAGS -stdlib=libc++ -static-libstdc++ -static-libgcc ${R include(${RUNTIME_LIGHT_DIR}/allocator/allocator.cmake) include(${RUNTIME_LIGHT_DIR}/components/kphp/kphp.cmake) include(${RUNTIME_LIGHT_DIR}/core/core.cmake) -include(${RUNTIME_LIGHT_DIR}/coroutine/coroutine.cmake) include(${RUNTIME_LIGHT_DIR}/server/server.cmake) include(${RUNTIME_LIGHT_DIR}/stdlib/stdlib.cmake) include(${RUNTIME_LIGHT_DIR}/tl/tl.cmake) @@ -26,12 +25,10 @@ include(${RUNTIME_LIGHT_DIR}/memory-resource-impl/memory-resource-impl.cmake) set(RUNTIME_LIGHT_SRC ${RUNTIME_LIGHT_CORE_SRC} - ${RUNTIME_LIGHT_COROUTINE_SRC} ${RUNTIME_LIGHT_STDLIB_SRC} ${RUNTIME_LIGHT_SCHEDULER_SRC} ${RUNTIME_LIGHT_SERVER_SRC} ${RUNTIME_LIGHT_ALLOCATOR_SRC} - ${RUNTIME_LIGHT_COROUTINE_SRC} ${RUNTIME_LIGHT_STREAMS_SRC} ${RUNTIME_LIGHT_TL_SRC} ${RUNTIME_LIGHT_UTILS_SRC} diff --git a/runtime-light/stdlib/diagnostics/backtrace.cpp b/runtime-light/stdlib/diagnostics/backtrace.cpp index 13db879b81..5d11fb0771 100644 --- a/runtime-light/stdlib/diagnostics/backtrace.cpp +++ b/runtime-light/stdlib/diagnostics/backtrace.cpp @@ -6,7 +6,6 @@ #include #include -#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/coroutine/async-stack.h" #include "runtime-light/coroutine/coroutine-state.h" #include "runtime-light/k2-platform/k2-api.h" @@ -44,18 +43,19 @@ size_t async_frames(std::span addresses, kphp::coro::async_stack_frame* t namespace kphp::diagnostic::impl { size_t async_backtrace(std::span addresses) noexcept { - if (const auto* instance_state{k2::instance_state()}; instance_state != nullptr) [[likely]] { - const auto& async_stack_root{instance_state->coroutine_instance_state.coroutine_stack_root}; + if (k2::instance_state() == nullptr) [[unlikely]] { + return 0; + } + + const auto& async_stack_root{kphp::coro::instance_state::get().coroutine_stack_root}; - auto* const start_sync_frame{reinterpret_cast(STACK_FRAME_ADDRESS)}; - auto* const stop_sync_frame{async_stack_root.stop_sync_stack_frame}; + auto* const start_sync_frame{reinterpret_cast(STACK_FRAME_ADDRESS)}; + auto* const stop_sync_frame{async_stack_root.stop_sync_stack_frame}; - size_t frames_count{sync_frames(addresses, start_sync_frame, stop_sync_frame)}; - if (frames_count < addresses.size()) { - frames_count += async_frames(addresses.subspan(frames_count), async_stack_root.top_async_stack_frame); - } - return frames_count; + size_t frames_count{sync_frames(addresses, start_sync_frame, stop_sync_frame)}; + if (frames_count < addresses.size()) { + frames_count += async_frames(addresses.subspan(frames_count), async_stack_root.top_async_stack_frame); } - return 0; + return frames_count; } } // namespace kphp::diagnostic::impl diff --git a/runtime-light/stdlib/diagnostics/contextual-tags.cpp b/runtime-light/stdlib/diagnostics/contextual-tags.cpp deleted file mode 100644 index 6e9745cb74..0000000000 --- a/runtime-light/stdlib/diagnostics/contextual-tags.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/diagnostics/contextual-tags.h" - -#include -#include -#include - -#include "runtime-light/components/kphp/state/instance-state.h" -#include "runtime-light/k2-platform/k2-api.h" - -namespace kphp::log { - -std::optional> contextual_tags::try_get() noexcept { - if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { - return instance_state_ptr->instance_tags; - } - return std::nullopt; -} - -} // namespace kphp::log diff --git a/runtime-light/stdlib/diagnostics/error-handling-state.cpp b/runtime-light/stdlib/diagnostics/error-handling-state.cpp deleted file mode 100644 index 93a1654189..0000000000 --- a/runtime-light/stdlib/diagnostics/error-handling-state.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/diagnostics/error-handling-state.h" - -#include -#include -#include - -#include "runtime-common/core/runtime-core.h" -#include "runtime-light/components/kphp/state/component-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" - -ErrorHandlingState::ErrorHandlingState() noexcept { - const auto& component_st{ComponentState::get()}; - const auto& default_level_str{component_st.ini_opts.get_value(string{INI_ERROR_REPORTING_KEY.data(), INI_ERROR_REPORTING_KEY.size()})}; - if (default_level_str.empty() || !default_level_str.is_int()) { - return; - } - - const int64_t default_level{default_level_str.to_int()}; - minimum_log_level = static_cast(SUPPORTED_ERROR_LEVELS & default_level) ? default_level : minimum_log_level; -} - -ErrorHandlingState& ErrorHandlingState::get() noexcept { - return InstanceState::get().error_handling_instance_state; -} - -std::optional> ErrorHandlingState::try_get() noexcept { - if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { - return instance_state_ptr->error_handling_instance_state; - } - return std::nullopt; -} diff --git a/runtime-light/stdlib/stdlib.cmake b/runtime-light/stdlib/stdlib.cmake index 92f67cd6b4..6f2682c46d 100644 --- a/runtime-light/stdlib/stdlib.cmake +++ b/runtime-light/stdlib/stdlib.cmake @@ -7,8 +7,6 @@ prepend( curl/curl-state.cpp web-transfer-lib/web-state.cpp diagnostics/backtrace.cpp - diagnostics/contextual-tags.cpp - diagnostics/error-handling-state.cpp diagnostics/php-assert.cpp file/file-system-state.cpp file/file-system-functions.cpp From dc8cf8b3b8e6df67aa202fd769757bf077cfffe2 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Tue, 4 Aug 2026 19:20:07 +0300 Subject: [PATCH 03/12] [k2] add confdata component skeleton Introduce components/confdata/: a standalone C++ component image that mirrors the kphp component structure (state/, bindings/, entry points) and links the common runtime-light machinery (coroutine scheduler, script allocator, streams, logging) via the per-component bindings introduced earlier. The dummy instance accepts a stream, drains the request and closes it; k2_describe() is hand-written with the "confdata" image name. The k2-confdata.so image is produced by cmake with the same link recipe the compiler uses for k2 images, and confdata headers are excluded from the merged runtime headers. --- .../components/confdata/bindings/bindings.cpp | 54 +++++++++++++ .../confdata/confdata-component.cpp | 80 +++++++++++++++++++ .../components/confdata/confdata.cmake | 31 +++++++ .../confdata/state/component-state.h | 24 ++++++ .../components/confdata/state/image-state.h | 24 ++++++ .../confdata/state/instance-state.cpp | 39 +++++++++ .../confdata/state/instance-state.h | 38 +++++++++ runtime-light/runtime-light.cmake | 6 ++ 8 files changed, 296 insertions(+) create mode 100644 runtime-light/components/confdata/bindings/bindings.cpp create mode 100644 runtime-light/components/confdata/confdata-component.cpp create mode 100644 runtime-light/components/confdata/confdata.cmake create mode 100644 runtime-light/components/confdata/state/component-state.h create mode 100644 runtime-light/components/confdata/state/image-state.h create mode 100644 runtime-light/components/confdata/state/instance-state.cpp create mode 100644 runtime-light/components/confdata/state/instance-state.h diff --git a/runtime-light/components/confdata/bindings/bindings.cpp b/runtime-light/components/confdata/bindings/bindings.cpp new file mode 100644 index 0000000000..f78fdb2576 --- /dev/null +++ b/runtime-light/components/confdata/bindings/bindings.cpp @@ -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 +#include + +#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" + +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> { + 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 +} + +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> { + if (k2::instance_state() != nullptr) [[likely]] { + return InstanceState::get().instance_tags; + } + return std::nullopt; +} + +} // namespace kphp::log diff --git a/runtime-light/components/confdata/confdata-component.cpp b/runtime-light/components/confdata/confdata-component.cpp new file mode 100644 index 0000000000..b85454db93 --- /dev/null +++ b/runtime-light/components/confdata/confdata-component.cpp @@ -0,0 +1,80 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2026 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include +#include + +#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(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(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(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(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(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_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 = "cxx"}}; + 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); +} diff --git a/runtime-light/components/confdata/confdata.cmake b/runtime-light/components/confdata/confdata.cmake new file mode 100644 index 0000000000..ecf76ae9c5 --- /dev/null +++ b/runtime-light/components/confdata/confdata.cmake @@ -0,0 +1,31 @@ +# 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_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 + ${RUNTIME_LIGHT_DIR}/allocator/runtime-light-allocator.cpp + ${RUNTIME_LIGHT_DIR}/stdlib/diagnostics/backtrace.cpp + ${RUNTIME_LIGHT_DIR}/stdlib/diagnostics/php-assert.cpp + ${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 + # link the alloc-wrapper objects directly (not as an archive) so that + # __wrap_* definitions are always present regardless of link order + $) + +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}) +target_link_options(k2-confdata-pic PUBLIC -stdlib=libc++ -static-libstdc++ -static-libgcc) +if(APPLE) + target_link_options(k2-confdata-pic PUBLIC -undefined dynamic_lookup) +else() + # k2 platform symbols are resolved by k2-node during dlopen + target_link_options(k2-confdata-pic PUBLIC -Wl,--allow-shlib-undefined -Wl,--wrap,malloc -Wl,--wrap,free -Wl,--wrap,calloc -Wl,--wrap,realloc + -Wl,--wrap,strdup) +endif() + +string(TIMESTAMP K2_CONFDATA_BUILD_TIMESTAMP "%s" UTC) +target_compile_definitions(k2-confdata-pic PRIVATE K2_CONFDATA_BUILD_TIMESTAMP=${K2_CONFDATA_BUILD_TIMESTAMP}ULL) diff --git a/runtime-light/components/confdata/state/component-state.h b/runtime-light/components/confdata/state/component-state.h new file mode 100644 index 0000000000..c5e3059aa6 --- /dev/null +++ b/runtime-light/components/confdata/state/component-state.h @@ -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 + +#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::get()); +} diff --git a/runtime-light/components/confdata/state/image-state.h b/runtime-light/components/confdata/state/image-state.h new file mode 100644 index 0000000000..333d031425 --- /dev/null +++ b/runtime-light/components/confdata/state/image-state.h @@ -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 + +#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::get()); +} diff --git a/runtime-light/components/confdata/state/instance-state.cpp b/runtime-light/components/confdata/state/instance-state.cpp new file mode 100644 index 0000000000..1aaa3c42f4 --- /dev/null +++ b/runtime-light/components/confdata/state/instance-state.cpp @@ -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 +#include +#include +#include + +#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) noexcept {})}; !expected) [[unlikely]] { + kphp::log::warning("failed to read a request: error -> {}", expected.error()); + } +} diff --git a/runtime-light/components/confdata/state/instance-state.h b/runtime-light/components/confdata/state/instance-state.h new file mode 100644 index 0000000000..4a7243baea --- /dev/null +++ b/runtime-light/components/confdata/state/instance-state.h @@ -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 + +#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(16U * 1024U * 1024U); // 16MiB + + auto run() noexcept -> kphp::coro::task<>; +}; + +inline auto InstanceState::get() noexcept -> InstanceState& { + return *k2::instance_state(); +} diff --git a/runtime-light/runtime-light.cmake b/runtime-light/runtime-light.cmake index 014c58b35d..a0fe34d878 100644 --- a/runtime-light/runtime-light.cmake +++ b/runtime-light/runtime-light.cmake @@ -66,10 +66,16 @@ combine_static_runtime_library(kphp-light-runtime-pic k2kphp-rt) # ================================================================================================= +include(${RUNTIME_LIGHT_DIR}/components/confdata/confdata.cmake) + +# ================================================================================================= + file( GLOB_RECURSE KPHP_RUNTIME_ALL_HEADERS RELATIVE ${BASE_DIR} CONFIGURE_DEPENDS "${RUNTIME_LIGHT_DIR}/*.h") +# confdata component has its own state types and is not a part of the kphp runtime +list(FILTER KPHP_RUNTIME_ALL_HEADERS EXCLUDE REGEX "^runtime-light/components/confdata/") file( GLOB_RECURSE KPHP_RUNTIME_COMMON_ALL_HEADERS RELATIVE ${BASE_DIR} From 37118d4399cb5aaa58a6d2e3cdd5d93d1f9c504d Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 00:35:37 +0300 Subject: [PATCH 04/12] [k2] move all state accessors to per-component bindings Every *::get / *::try_get / *::get_mutable definition (and PhpScriptMutableGlobals::current) now lives in components/kphp/bindings/bindings.cpp, so common runtime-light code no longer includes components/kphp/state headers from its .cpp files and stays fully agnostic of where the state actually lives. The 22 state .cpp files that contained nothing but accessors are removed; file-system-state.cpp keeps its constructor and php-script-globals.cpp keeps its logic. Direct InstanceState accesses in http and job-worker init code now go through PhpScriptMutableGlobals::current(). Another component can adopt a runtime-light .cpp incrementally by adding the corresponding binding to its own bindings.cpp. --- .../components/confdata/bindings/bindings.cpp | 30 +-- .../components/kphp/bindings/bindings.cpp | 205 ++++++++++++++++-- .../core/globals/php-script-globals.cpp | 7 +- .../server/cli/cli-instance-state.cpp | 11 - .../server/http/http-server-state.cpp | 11 - runtime-light/server/http/init-functions.cpp | 3 +- .../server/job-worker/init-functions.h | 3 +- .../job-worker/job-worker-server-state.cpp | 11 - runtime-light/server/rpc/rpc-server-state.cpp | 11 - runtime-light/server/server.cmake | 6 +- .../stdlib/confdata/confdata-state.cpp | 11 - runtime-light/stdlib/curl/curl-state.cpp | 16 -- .../stdlib/file/file-system-state.cpp | 5 - runtime-light/stdlib/fork/fork-state.cpp | 11 - .../stdlib/fork/wait-queue-state.cpp | 11 - .../instance-cache/instance-cache-state.cpp | 11 - .../stdlib/job-worker/job-worker-api.cpp | 1 - .../job-worker/job-worker-client-state.cpp | 11 - runtime-light/stdlib/kml/kml-state.cpp | 23 -- runtime-light/stdlib/math/math-state.cpp | 16 -- runtime-light/stdlib/math/random-state.cpp | 11 - runtime-light/stdlib/output/output-state.cpp | 11 - runtime-light/stdlib/rpc/rpc-client-state.cpp | 20 -- runtime-light/stdlib/rpc/rpc-queue-state.cpp | 11 - .../serialization/serialization-state.cpp | 16 -- runtime-light/stdlib/stdlib.cmake | 18 -- runtime-light/stdlib/string/regex-state.cpp | 19 -- runtime-light/stdlib/string/string-state.cpp | 16 -- runtime-light/stdlib/system/system-state.cpp | 11 - runtime-light/stdlib/time/time-state.cpp | 20 -- .../stdlib/web-transfer-lib/web-state.cpp | 11 - 31 files changed, 208 insertions(+), 370 deletions(-) delete mode 100644 runtime-light/server/cli/cli-instance-state.cpp delete mode 100644 runtime-light/server/http/http-server-state.cpp delete mode 100644 runtime-light/server/job-worker/job-worker-server-state.cpp delete mode 100644 runtime-light/server/rpc/rpc-server-state.cpp delete mode 100644 runtime-light/stdlib/confdata/confdata-state.cpp delete mode 100644 runtime-light/stdlib/curl/curl-state.cpp delete mode 100644 runtime-light/stdlib/fork/fork-state.cpp delete mode 100644 runtime-light/stdlib/fork/wait-queue-state.cpp delete mode 100644 runtime-light/stdlib/instance-cache/instance-cache-state.cpp delete mode 100644 runtime-light/stdlib/job-worker/job-worker-client-state.cpp delete mode 100644 runtime-light/stdlib/kml/kml-state.cpp delete mode 100644 runtime-light/stdlib/math/math-state.cpp delete mode 100644 runtime-light/stdlib/math/random-state.cpp delete mode 100644 runtime-light/stdlib/output/output-state.cpp delete mode 100644 runtime-light/stdlib/rpc/rpc-client-state.cpp delete mode 100644 runtime-light/stdlib/rpc/rpc-queue-state.cpp delete mode 100644 runtime-light/stdlib/serialization/serialization-state.cpp delete mode 100644 runtime-light/stdlib/string/regex-state.cpp delete mode 100644 runtime-light/stdlib/string/string-state.cpp delete mode 100644 runtime-light/stdlib/system/system-state.cpp delete mode 100644 runtime-light/stdlib/time/time-state.cpp delete mode 100644 runtime-light/stdlib/web-transfer-lib/web-state.cpp diff --git a/runtime-light/components/confdata/bindings/bindings.cpp b/runtime-light/components/confdata/bindings/bindings.cpp index f78fdb2576..5d3ae4366a 100644 --- a/runtime-light/components/confdata/bindings/bindings.cpp +++ b/runtime-light/components/confdata/bindings/bindings.cpp @@ -15,21 +15,6 @@ #include "runtime-light/stdlib/diagnostics/error-handling-state.h" #include "runtime-light/stdlib/diagnostics/logs.h" -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> { - 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 -} - namespace kphp::coro { auto instance_state::get() noexcept -> instance_state& { @@ -52,3 +37,18 @@ auto contextual_tags::try_get() noexcept -> std::optional 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> { + 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 +} diff --git a/runtime-light/components/kphp/bindings/bindings.cpp b/runtime-light/components/kphp/bindings/bindings.cpp index fb8ae235f2..5f1b40b704 100644 --- a/runtime-light/components/kphp/bindings/bindings.cpp +++ b/runtime-light/components/kphp/bindings/bindings.cpp @@ -11,14 +11,61 @@ #include "runtime-light/components/kphp/state/component-state.h" #include "runtime-light/components/kphp/state/image-state.h" #include "runtime-light/components/kphp/state/instance-state.h" +#include "runtime-light/core/globals/php-script-globals.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/server/cli/cli-instance-state.h" +#include "runtime-light/server/http/http-server-state.h" +#include "runtime-light/server/job-worker/job-worker-server-state.h" +#include "runtime-light/server/rpc/rpc-server-state.h" +#include "runtime-light/stdlib/confdata/confdata-state.h" +#include "runtime-light/stdlib/curl/curl-state.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" +#include "runtime-light/stdlib/file/file-system-state.h" +#include "runtime-light/stdlib/fork/fork-state.h" +#include "runtime-light/stdlib/fork/wait-queue-state.h" +#include "runtime-light/stdlib/instance-cache/instance-cache-state.h" +#include "runtime-light/stdlib/job-worker/job-worker-client-state.h" +#include "runtime-light/stdlib/kml/kml-state.h" +#include "runtime-light/stdlib/math/math-state.h" +#include "runtime-light/stdlib/math/random-state.h" +#include "runtime-light/stdlib/output/output-state.h" +#include "runtime-light/stdlib/rpc/rpc-client-state.h" +#include "runtime-light/stdlib/rpc/rpc-queue-state.h" +#include "runtime-light/stdlib/serialization/serialization-state.h" +#include "runtime-light/stdlib/string/regex-state.h" +#include "runtime-light/stdlib/string/string-state.h" +#include "runtime-light/stdlib/system/system-state.h" +#include "runtime-light/stdlib/time/time-state.h" +#include "runtime-light/stdlib/web-transfer-lib/web-state.h" -const AllocatorState& AllocatorState::get() noexcept { +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> { + if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { + return instance_state_ptr->instance_tags; + } + return std::nullopt; +} + +} // namespace kphp::log + +auto AllocatorState::get() noexcept -> const AllocatorState& { if (const auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { return instance_state_ptr->instance_allocator_state; } else if (const auto* component_state_ptr{k2::component_state()}; component_state_ptr != nullptr) { @@ -29,13 +76,17 @@ const AllocatorState& AllocatorState::get() noexcept { kphp::log::error("can't find allocator state"); } -RuntimeContext& RuntimeContext::get() noexcept { +auto RuntimeContext::get() noexcept -> RuntimeContext& { if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { return instance_state_ptr->runtime_context; } kphp::log::error("unexpected access to RuntimeContext"); } +auto PhpScriptMutableGlobals::current() noexcept -> PhpScriptMutableGlobals& { + return InstanceState::get().php_script_mutable_globals_singleton; +} + ErrorHandlingState::ErrorHandlingState() noexcept { const auto& component_st{ComponentState::get()}; const auto& default_level_str{component_st.ini_opts.get_value(string{INI_ERROR_REPORTING_KEY.data(), INI_ERROR_REPORTING_KEY.size()})}; @@ -47,36 +98,156 @@ ErrorHandlingState::ErrorHandlingState() noexcept { minimum_log_level = static_cast(SUPPORTED_ERROR_LEVELS & default_level) ? default_level : minimum_log_level; } -ErrorHandlingState& ErrorHandlingState::get() noexcept { +auto ErrorHandlingState::get() noexcept -> ErrorHandlingState& { return InstanceState::get().error_handling_instance_state; } -std::optional> ErrorHandlingState::try_get() noexcept { +auto ErrorHandlingState::try_get() noexcept -> std::optional> { if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { return instance_state_ptr->error_handling_instance_state; } return std::nullopt; } -namespace kphp::coro { +auto CLIInstanceInstance::get() noexcept -> CLIInstanceInstance& { + return InstanceState::get().cli_instance_instate; +} -instance_state& instance_state::get() noexcept { - return InstanceState::get().coroutine_instance_state; +auto HttpServerInstanceState::get() noexcept -> HttpServerInstanceState& { + return InstanceState::get().http_server_instance_state; } -io_scheduler& io_scheduler::get() noexcept { - return InstanceState::get().io_scheduler; +auto JobWorkerServerInstanceState::get() noexcept -> JobWorkerServerInstanceState& { + return InstanceState::get().job_worker_server_instance_state; } -} // namespace kphp::coro +auto RpcServerInstanceState::get() noexcept -> RpcServerInstanceState& { + return InstanceState::get().rpc_server_instance_state; +} -namespace kphp::log { +auto ConfdataInstanceState::get() noexcept -> ConfdataInstanceState& { + return InstanceState::get().confdata_instance_state; +} -std::optional> contextual_tags::try_get() noexcept { - if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { - return instance_state_ptr->instance_tags; - } - return std::nullopt; +auto CurlInstanceState::get() noexcept -> CurlInstanceState& { + return InstanceState::get().curl_instance_state; } -} // namespace kphp::log +auto CurlImageState::get() noexcept -> const CurlImageState& { + return ImageState::get().curl_image_state; +} + +auto FileSystemImageState::get() noexcept -> const FileSystemImageState& { + return ImageState::get().file_system_image_state; +} + +auto ForkInstanceState::get() noexcept -> ForkInstanceState& { + return InstanceState::get().fork_instance_state; +} + +auto InstanceCacheInstanceState::get() noexcept -> InstanceCacheInstanceState& { + return InstanceState::get().instance_cache_instance_state; +} + +auto JobWorkerClientInstanceState::get() noexcept -> JobWorkerClientInstanceState& { + return InstanceState::get().job_worker_client_instance_state; +} + +template<> +auto KmlComponentState::get() noexcept -> const KmlComponentState& { + return ComponentState::get().kml_component_state; +} + +template<> +auto KmlComponentState::get_mutable() noexcept -> KmlComponentState& { + return const_cast(KmlComponentState::get()); +} + +template<> +auto KmlInstanceState::get() noexcept -> KmlInstanceState& { + return InstanceState::get().kml_instance_state; +} + +auto MathInstanceState::get() noexcept -> MathInstanceState& { + return InstanceState::get().math_instance_state; +} + +auto MathImageState::get() noexcept -> const MathImageState& { + return ImageState::get().math_image_state; +} + +auto OutputInstanceState::get() noexcept -> OutputInstanceState& { + return InstanceState::get().output_instance_state; +} + +auto RandomInstanceState::get() noexcept -> RandomInstanceState& { + return InstanceState::get().random_instance_state; +} + +auto RegexInstanceState::get() noexcept -> RegexInstanceState& { + return InstanceState::get().regex_instance_state; +} + +auto RegexImageState::get() noexcept -> const RegexImageState& { + return ImageState::get().regex_image_state; +} + +auto RegexImageState::get_mutable() noexcept -> RegexImageState& { + return ImageState::get_mutable().regex_image_state; +} + +auto RpcClientInstanceState::get() noexcept -> RpcClientInstanceState& { + return InstanceState::get().rpc_client_instance_state; +} + +auto RpcImageState::get() noexcept -> const RpcImageState& { + return ImageState::get().rpc_image_state; +} + +auto RpcImageState::get_mutable() noexcept -> RpcImageState& { + return ImageState::get_mutable().rpc_image_state; +} + +auto RpcQueueInstanceState::get() noexcept -> RpcQueueInstanceState& { + return InstanceState::get().rpc_queue_instance_state; +} + +auto SerializationInstanceState::get() noexcept -> SerializationInstanceState& { + return InstanceState::get().serialization_instance_state; +} + +auto SerializationImageState::get() noexcept -> const SerializationImageState& { + return ImageState::get().serialization_image_state; +} + +auto StringInstanceState::get() noexcept -> StringInstanceState& { + return InstanceState::get().string_instance_state; +} + +auto StringImageState::get() noexcept -> const StringImageState& { + return ImageState::get().string_image_state; +} + +auto SystemInstanceState::get() noexcept -> SystemInstanceState& { + return InstanceState::get().system_instance_state; +} + +auto TimeInstanceState::get() noexcept -> TimeInstanceState& { + return InstanceState::get().time_instance_state; +} + +auto TimeImageState::get() noexcept -> const TimeImageState& { + return ImageState::get().time_image_state; +} + +auto TimeImageState::get_mutable() noexcept -> TimeImageState& { + return ImageState::get_mutable().time_image_state; +} + +auto WaitQueueInstanceState::get() noexcept -> WaitQueueInstanceState& { + return InstanceState::get().wait_queue_instance_state; +} + +auto WebInstanceState::get() noexcept -> WebInstanceState& { + return InstanceState::get().web_instance_state; +} diff --git a/runtime-light/core/globals/php-script-globals.cpp b/runtime-light/core/globals/php-script-globals.cpp index 6f009471f3..298b304de9 100644 --- a/runtime-light/core/globals/php-script-globals.cpp +++ b/runtime-light/core/globals/php-script-globals.cpp @@ -4,13 +4,10 @@ #include "php-script-globals.h" -#include "runtime-light/components/kphp/state/instance-state.h" +#include "common/php-functions.h" +#include "runtime-common/core/allocator/runtime-allocator.h" #include "runtime-light/stdlib/diagnostics/logs.h" -PhpScriptMutableGlobals& PhpScriptMutableGlobals::current() noexcept { - return InstanceState::get().php_script_mutable_globals_singleton; -} - void PhpScriptMutableGlobals::once_alloc_linear_mem(unsigned int n_bytes) { kphp::log::assertion(g_linear_mem == nullptr); g_linear_mem = static_cast(RuntimeAllocator::get().alloc0_global_memory(n_bytes)); diff --git a/runtime-light/server/cli/cli-instance-state.cpp b/runtime-light/server/cli/cli-instance-state.cpp deleted file mode 100644 index 8f00e23fab..0000000000 --- a/runtime-light/server/cli/cli-instance-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/server/cli/cli-instance-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -CLIInstanceInstance& CLIInstanceInstance::get() noexcept { - return InstanceState::get().cli_instance_instate; -} diff --git a/runtime-light/server/http/http-server-state.cpp b/runtime-light/server/http/http-server-state.cpp deleted file mode 100644 index c6b7f6162a..0000000000 --- a/runtime-light/server/http/http-server-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/server/http/http-server-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -HttpServerInstanceState& HttpServerInstanceState::get() noexcept { - return InstanceState::get().http_server_instance_state; -} diff --git a/runtime-light/server/http/init-functions.cpp b/runtime-light/server/http/init-functions.cpp index 1c0a83eb9d..0855f881f1 100644 --- a/runtime-light/server/http/init-functions.cpp +++ b/runtime-light/server/http/init-functions.cpp @@ -24,7 +24,6 @@ #include "runtime-common/core/runtime-core.h" #include "runtime-common/core/std/containers.h" #include "runtime-common/stdlib/server/url-functions.h" -#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/core/globals/php-script-globals.h" #include "runtime-light/coroutine/task.h" #include "runtime-light/k2-platform/k2-api.h" @@ -247,7 +246,7 @@ void init_server(kphp::component::stream&& request_stream, kphp::stl::vector #include "runtime-common/core/runtime-core.h" -#include "runtime-light/components/kphp/state/instance-state.h" #include "runtime-light/coroutine/io-scheduler.h" #include "runtime-light/coroutine/task.h" #include "runtime-light/k2-platform/k2-api.h" diff --git a/runtime-light/stdlib/job-worker/job-worker-client-state.cpp b/runtime-light/stdlib/job-worker/job-worker-client-state.cpp deleted file mode 100644 index f505f5f229..0000000000 --- a/runtime-light/stdlib/job-worker/job-worker-client-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/job-worker/job-worker-client-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -JobWorkerClientInstanceState& JobWorkerClientInstanceState::get() noexcept { - return InstanceState::get().job_worker_client_instance_state; -} diff --git a/runtime-light/stdlib/kml/kml-state.cpp b/runtime-light/stdlib/kml/kml-state.cpp deleted file mode 100644 index faaa72a993..0000000000 --- a/runtime-light/stdlib/kml/kml-state.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/kml/kml-state.h" - -#include "runtime-light/components/kphp/state/component-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" - -template<> -const KmlComponentState& KmlComponentState::get() noexcept { - return ComponentState::get().kml_component_state; -} - -template<> -KmlComponentState& KmlComponentState::get_mutable() noexcept { - return const_cast(KmlComponentState::get()); -} - -template<> -KmlInstanceState& KmlInstanceState::get() noexcept { - return InstanceState::get().kml_instance_state; -} diff --git a/runtime-light/stdlib/math/math-state.cpp b/runtime-light/stdlib/math/math-state.cpp deleted file mode 100644 index b3d1dbf486..0000000000 --- a/runtime-light/stdlib/math/math-state.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/math/math-state.h" - -#include "runtime-light/components/kphp/state/image-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" - -MathInstanceState& MathInstanceState::get() noexcept { - return InstanceState::get().math_instance_state; -} - -const MathImageState& MathImageState::get() noexcept { - return ImageState::get().math_image_state; -} diff --git a/runtime-light/stdlib/math/random-state.cpp b/runtime-light/stdlib/math/random-state.cpp deleted file mode 100644 index 256874cdbb..0000000000 --- a/runtime-light/stdlib/math/random-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/math/random-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -RandomInstanceState& RandomInstanceState::get() noexcept { - return InstanceState::get().random_instance_state; -} diff --git a/runtime-light/stdlib/output/output-state.cpp b/runtime-light/stdlib/output/output-state.cpp deleted file mode 100644 index b3f36b3d44..0000000000 --- a/runtime-light/stdlib/output/output-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/output/output-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -OutputInstanceState& OutputInstanceState::get() noexcept { - return InstanceState::get().output_instance_state; -} diff --git a/runtime-light/stdlib/rpc/rpc-client-state.cpp b/runtime-light/stdlib/rpc/rpc-client-state.cpp deleted file mode 100644 index ffdeef5c0c..0000000000 --- a/runtime-light/stdlib/rpc/rpc-client-state.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/rpc/rpc-client-state.h" - -#include "runtime-light/components/kphp/state/image-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" - -RpcClientInstanceState& RpcClientInstanceState::get() noexcept { - return InstanceState::get().rpc_client_instance_state; -} - -const RpcImageState& RpcImageState::get() noexcept { - return ImageState::get().rpc_image_state; -} - -RpcImageState& RpcImageState::get_mutable() noexcept { - return ImageState::get_mutable().rpc_image_state; -} diff --git a/runtime-light/stdlib/rpc/rpc-queue-state.cpp b/runtime-light/stdlib/rpc/rpc-queue-state.cpp deleted file mode 100644 index fa48499498..0000000000 --- a/runtime-light/stdlib/rpc/rpc-queue-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/rpc/rpc-queue-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -RpcQueueInstanceState& RpcQueueInstanceState::get() noexcept { - return InstanceState::get().rpc_queue_instance_state; -} diff --git a/runtime-light/stdlib/serialization/serialization-state.cpp b/runtime-light/stdlib/serialization/serialization-state.cpp deleted file mode 100644 index f819e19d57..0000000000 --- a/runtime-light/stdlib/serialization/serialization-state.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/serialization/serialization-state.h" - -#include "runtime-light/components/kphp/state/image-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" - -SerializationInstanceState& SerializationInstanceState::get() noexcept { - return InstanceState::get().serialization_instance_state; -} - -const SerializationImageState& SerializationImageState::get() noexcept { - return ImageState::get().serialization_image_state; -} diff --git a/runtime-light/stdlib/stdlib.cmake b/runtime-light/stdlib/stdlib.cmake index 6f2682c46d..8776da4dc4 100644 --- a/runtime-light/stdlib/stdlib.cmake +++ b/runtime-light/stdlib/stdlib.cmake @@ -2,48 +2,30 @@ prepend( RUNTIME_LIGHT_STDLIB_SRC stdlib/ confdata/confdata-functions.cpp - confdata/confdata-state.cpp crypto/crypto-functions.cpp - curl/curl-state.cpp - web-transfer-lib/web-state.cpp diagnostics/backtrace.cpp diagnostics/php-assert.cpp file/file-system-state.cpp file/file-system-functions.cpp file/resource.cpp - fork/fork-state.cpp fork/fork-storage.cpp - fork/wait-queue-state.cpp - instance-cache/instance-cache-state.cpp job-worker/job-worker-api.cpp - job-worker/job-worker-client-state.cpp - kml/kml-state.cpp - math/random-state.cpp - math/math-state.cpp output/output-buffer.cpp - output/output-state.cpp output/print-functions.cpp rpc/rpc-api.cpp - rpc/rpc-client-state.cpp rpc/rpc-extra-headers.cpp rpc/rpc-extra-info.cpp - rpc/rpc-queue-state.cpp rpc/rpc-tl-builtins.cpp rpc/rpc-tl-error.cpp rpc/rpc-tl-query.cpp rpc/rpc-tl-request.cpp - serialization/serialization-state.cpp server/http-functions.cpp string/regex-functions.cpp - string/regex-state.cpp - string/string-state.cpp system/system-functions.cpp - system/system-state.cpp time/date-interval.cpp time/date-time.cpp time/date-time-immutable.cpp time/time-functions.cpp - time/time-state.cpp time/timelib-functions.cpp zlib/zlib-functions.cpp zstd/zstd-functions.cpp) diff --git a/runtime-light/stdlib/string/regex-state.cpp b/runtime-light/stdlib/string/regex-state.cpp deleted file mode 100644 index ac870d0f47..0000000000 --- a/runtime-light/stdlib/string/regex-state.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/string/regex-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -RegexInstanceState& RegexInstanceState::get() noexcept { - return InstanceState::get().regex_instance_state; -} - -const RegexImageState& RegexImageState::get() noexcept { - return ImageState::get().regex_image_state; -} - -RegexImageState& RegexImageState::get_mutable() noexcept { - return ImageState::get_mutable().regex_image_state; -} diff --git a/runtime-light/stdlib/string/string-state.cpp b/runtime-light/stdlib/string/string-state.cpp deleted file mode 100644 index 2172403bb4..0000000000 --- a/runtime-light/stdlib/string/string-state.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/string/string-state.h" - -#include "runtime-light/components/kphp/state/image-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" - -StringInstanceState& StringInstanceState::get() noexcept { - return InstanceState::get().string_instance_state; -} - -const StringImageState& StringImageState::get() noexcept { - return ImageState::get().string_image_state; -} diff --git a/runtime-light/stdlib/system/system-state.cpp b/runtime-light/stdlib/system/system-state.cpp deleted file mode 100644 index 81a1ee0540..0000000000 --- a/runtime-light/stdlib/system/system-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/system/system-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -SystemInstanceState& SystemInstanceState::get() noexcept { - return InstanceState::get().system_instance_state; -} diff --git a/runtime-light/stdlib/time/time-state.cpp b/runtime-light/stdlib/time/time-state.cpp deleted file mode 100644 index 42e0792662..0000000000 --- a/runtime-light/stdlib/time/time-state.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/time/time-state.h" - -#include "runtime-light/components/kphp/state/image-state.h" -#include "runtime-light/components/kphp/state/instance-state.h" - -TimeInstanceState& TimeInstanceState::get() noexcept { - return InstanceState::get().time_instance_state; -} - -const TimeImageState& TimeImageState::get() noexcept { - return ImageState::get().time_image_state; -} - -TimeImageState& TimeImageState::get_mutable() noexcept { - return ImageState::get_mutable().time_image_state; -} diff --git a/runtime-light/stdlib/web-transfer-lib/web-state.cpp b/runtime-light/stdlib/web-transfer-lib/web-state.cpp deleted file mode 100644 index 237306607b..0000000000 --- a/runtime-light/stdlib/web-transfer-lib/web-state.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2025 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime-light/stdlib/web-transfer-lib/web-state.h" - -#include "runtime-light/components/kphp/state/instance-state.h" - -WebInstanceState& WebInstanceState::get() noexcept { - return InstanceState::get().web_instance_state; -} From 3cd991b9451d96ac22b77de3c2cf7cefce2eb740 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 00:50:25 +0300 Subject: [PATCH 05/12] [k2] report true compiler version for confdata component --- runtime-light/components/confdata/confdata-component.cpp | 2 +- runtime-light/components/confdata/confdata.cmake | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime-light/components/confdata/confdata-component.cpp b/runtime-light/components/confdata/confdata-component.cpp index b85454db93..72f218af3c 100644 --- a/runtime-light/components/confdata/confdata-component.cpp +++ b/runtime-light/components/confdata/confdata-component.cpp @@ -68,7 +68,7 @@ VISIBILITY_DEFAULT k2::PollStatus k2_poll() { } VISIBILITY_DEFAULT const ImageInfo* k2_describe() { - static constexpr std::array extra_info{ImageInfo::KeyValuePair{.key = "compiler_version", .value = "cxx"}}; + 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, diff --git a/runtime-light/components/confdata/confdata.cmake b/runtime-light/components/confdata/confdata.cmake index ecf76ae9c5..3e2c190400 100644 --- a/runtime-light/components/confdata/confdata.cmake +++ b/runtime-light/components/confdata/confdata.cmake @@ -28,4 +28,5 @@ else() endif() string(TIMESTAMP K2_CONFDATA_BUILD_TIMESTAMP "%s" UTC) -target_compile_definitions(k2-confdata-pic PRIVATE K2_CONFDATA_BUILD_TIMESTAMP=${K2_CONFDATA_BUILD_TIMESTAMP}ULL) +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}") From 51e7642903b5f38cb597180c620d772573e98a51 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 01:16:32 +0300 Subject: [PATCH 06/12] a bit of confdata cmake refactoring --- .../components/confdata/confdata.cmake | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/runtime-light/components/confdata/confdata.cmake b/runtime-light/components/confdata/confdata.cmake index 3e2c190400..4a27789c8d 100644 --- a/runtime-light/components/confdata/confdata.cmake +++ b/runtime-light/components/confdata/confdata.cmake @@ -1,16 +1,29 @@ # 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_SRC +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 + ${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 + ${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 + ${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 $) From c584cbcd50cf2a6149cf8f956739ee3cc6dd559c Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 01:52:11 +0300 Subject: [PATCH 07/12] add trivial warmup for confdata --- runtime-light/components/confdata/confdata-component.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/runtime-light/components/confdata/confdata-component.cpp b/runtime-light/components/confdata/confdata-component.cpp index 72f218af3c..4e04ac4a6f 100644 --- a/runtime-light/components/confdata/confdata-component.cpp +++ b/runtime-light/components/confdata/confdata-component.cpp @@ -60,6 +60,10 @@ VISIBILITY_DEFAULT void k2_init_instance() { 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(); From e1daa0959b2ef513fd11e5b22ca82f2f7a28cd8f Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 17:13:50 +0300 Subject: [PATCH 08/12] [k2] reuse RUNTIME_LIGHT_LINK_FLAGS for confdata --- runtime-light/components/confdata/confdata.cmake | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/runtime-light/components/confdata/confdata.cmake b/runtime-light/components/confdata/confdata.cmake index 4a27789c8d..a6f2478d31 100644 --- a/runtime-light/components/confdata/confdata.cmake +++ b/runtime-light/components/confdata/confdata.cmake @@ -31,14 +31,13 @@ set(K2_CONFDATA_SRC 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}) -target_link_options(k2-confdata-pic PUBLIC -stdlib=libc++ -static-libstdc++ -static-libgcc) -if(APPLE) - target_link_options(k2-confdata-pic PUBLIC -undefined dynamic_lookup) -else() - # k2 platform symbols are resolved by k2-node during dlopen - target_link_options(k2-confdata-pic PUBLIC -Wl,--allow-shlib-undefined -Wl,--wrap,malloc -Wl,--wrap,free -Wl,--wrap,calloc -Wl,--wrap,realloc - -Wl,--wrap,strdup) +# 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 From 352b2057202d9866bcc573d7818f0101ebe35318 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 17:32:14 +0300 Subject: [PATCH 09/12] [k2] use -flto in confdata --- .../components/confdata/confdata.cmake | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/runtime-light/components/confdata/confdata.cmake b/runtime-light/components/confdata/confdata.cmake index a6f2478d31..a6315a9c78 100644 --- a/runtime-light/components/confdata/confdata.cmake +++ b/runtime-light/components/confdata/confdata.cmake @@ -39,6 +39,23 @@ if(NOT APPLE) endif() target_link_options(k2-confdata-pic PUBLIC ${K2_CONFDATA_LINK_FLAGS}) +include(CheckIPOSupported) +check_ipo_supported(RESULT K2_CONFDATA_IPO_SUPPORTED OUTPUT K2_CONFDATA_IPO_ERROR) +if(NOT APPLE) + # GNU ld can't read LLVM bitcode objects; LTO requires a linker with + # LLVM plugin support — mold, or the version-matched lld + string(REGEX MATCH "^[0-9]+" K2_CONFDATA_COMPILER_VERSION_MAJOR "${CMAKE_CXX_COMPILER_VERSION}") + find_program(K2_CONFDATA_LINKER NAMES mold ld.lld-${K2_CONFDATA_COMPILER_VERSION_MAJOR}) +endif() +if(K2_CONFDATA_IPO_SUPPORTED AND (APPLE OR K2_CONFDATA_LINKER)) + set_target_properties(k2-confdata-pic PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) + if(K2_CONFDATA_LINKER) + target_link_options(k2-confdata-pic PRIVATE -fuse-ld=${K2_CONFDATA_LINKER}) + endif() +else() + message(WARNING "k2-confdata: IPO/LTO is disabled: ${K2_CONFDATA_IPO_ERROR}") +endif() + 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}") From 66a39db9a0f1b596647a6ef46d4837d95ed87727 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 17:34:31 +0300 Subject: [PATCH 10/12] [k2] remove unsafe logs from kphp-component.cpp --- runtime-light/components/kphp/kphp-component.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/runtime-light/components/kphp/kphp-component.cpp b/runtime-light/components/kphp/kphp-component.cpp index ea7968aca4..07cd13f9fd 100644 --- a/runtime-light/components/kphp/kphp-component.cpp +++ b/runtime-light/components/kphp/kphp-component.cpp @@ -34,9 +34,7 @@ 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; - kphp::log::debug("start component state creation, requested {} bytes", sizeof(ComponentState)); auto* component_state_ptr{static_cast(k2::alloc_align(sizeof(ComponentState), alignof(ComponentState)))}; - kphp::log::debug("finish component state creation"); return component_state_ptr; } @@ -44,18 +42,14 @@ 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; - kphp::log::debug("start component state init"); new (const_cast(k2::component_state())) ComponentState{}; - kphp::log::debug("finish component state init"); } 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; - kphp::log::debug("start instance state creation, requested {} bytes", sizeof(InstanceState)); auto* instance_state_ptr{static_cast(k2::alloc_align(sizeof(InstanceState), alignof(InstanceState)))}; - kphp::log::debug("finish instance state creation"); return instance_state_ptr; } @@ -63,16 +57,12 @@ 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(); - kphp::log::debug("start instance state init"); new (k2::instance_state()) InstanceState{}; k2::instance_state()->init_script_execution(); - kphp::log::debug("finish instance state init"); } VISIBILITY_DEFAULT k2::PollStatus k2_warmup() { - kphp::log::debug("start instance warmup"); const auto warmup_status{k2::PollStatus::PollFinishedOk}; - kphp::log::debug("finish instance warmup"); return warmup_status; } @@ -80,8 +70,8 @@ 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(); - kphp::log::debug("k2_poll started"); + kphp::log::trace("k2_poll started"); const auto poll_status{kphp::coro::io_scheduler::get().process_events()}; - kphp::log::debug("k2_poll finished: {}", std::to_underlying(poll_status)); + kphp::log::trace("k2_poll finished: {}", std::to_underlying(poll_status)); return poll_status; } From 534f8b3b4f8eb1e25d6f100253ecb3903d6df72e Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Wed, 5 Aug 2026 17:39:59 +0300 Subject: [PATCH 11/12] Revert "[k2] use -flto in confdata" This reverts commit ee03e3fef40ee237a1685b690b02081bab8c5901. --- .../components/confdata/confdata.cmake | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/runtime-light/components/confdata/confdata.cmake b/runtime-light/components/confdata/confdata.cmake index a6315a9c78..a6f2478d31 100644 --- a/runtime-light/components/confdata/confdata.cmake +++ b/runtime-light/components/confdata/confdata.cmake @@ -39,23 +39,6 @@ if(NOT APPLE) endif() target_link_options(k2-confdata-pic PUBLIC ${K2_CONFDATA_LINK_FLAGS}) -include(CheckIPOSupported) -check_ipo_supported(RESULT K2_CONFDATA_IPO_SUPPORTED OUTPUT K2_CONFDATA_IPO_ERROR) -if(NOT APPLE) - # GNU ld can't read LLVM bitcode objects; LTO requires a linker with - # LLVM plugin support — mold, or the version-matched lld - string(REGEX MATCH "^[0-9]+" K2_CONFDATA_COMPILER_VERSION_MAJOR "${CMAKE_CXX_COMPILER_VERSION}") - find_program(K2_CONFDATA_LINKER NAMES mold ld.lld-${K2_CONFDATA_COMPILER_VERSION_MAJOR}) -endif() -if(K2_CONFDATA_IPO_SUPPORTED AND (APPLE OR K2_CONFDATA_LINKER)) - set_target_properties(k2-confdata-pic PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) - if(K2_CONFDATA_LINKER) - target_link_options(k2-confdata-pic PRIVATE -fuse-ld=${K2_CONFDATA_LINKER}) - endif() -else() - message(WARNING "k2-confdata: IPO/LTO is disabled: ${K2_CONFDATA_IPO_ERROR}") -endif() - 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}") From 0c28fca968adb9affd4c5007450cccc079dd6603 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Thu, 6 Aug 2026 19:00:58 +0300 Subject: [PATCH 12/12] nfc --- runtime-light/components/confdata/confdata-component.cpp | 6 ++---- runtime-light/components/kphp/kphp-component.cpp | 9 +++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/runtime-light/components/confdata/confdata-component.cpp b/runtime-light/components/confdata/confdata-component.cpp index 4e04ac4a6f..d846ce020c 100644 --- a/runtime-light/components/confdata/confdata-component.cpp +++ b/runtime-light/components/confdata/confdata-component.cpp @@ -33,8 +33,7 @@ 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(k2::alloc_align(sizeof(ComponentState), alignof(ComponentState)))}; - return component_state_ptr; + return static_cast(k2::alloc_align(sizeof(ComponentState), alignof(ComponentState))); } VISIBILITY_DEFAULT void k2_init_component() { @@ -48,8 +47,7 @@ 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(k2::alloc_align(sizeof(InstanceState), alignof(InstanceState)))}; - return instance_state_ptr; + return static_cast(k2::alloc_align(sizeof(InstanceState), alignof(InstanceState))); } VISIBILITY_DEFAULT void k2_init_instance() { diff --git a/runtime-light/components/kphp/kphp-component.cpp b/runtime-light/components/kphp/kphp-component.cpp index 07cd13f9fd..c10c005230 100644 --- a/runtime-light/components/kphp/kphp-component.cpp +++ b/runtime-light/components/kphp/kphp-component.cpp @@ -34,8 +34,7 @@ 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(k2::alloc_align(sizeof(ComponentState), alignof(ComponentState)))}; - return component_state_ptr; + return static_cast(k2::alloc_align(sizeof(ComponentState), alignof(ComponentState))); } VISIBILITY_DEFAULT void k2_init_component() { @@ -49,8 +48,7 @@ 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(k2::alloc_align(sizeof(InstanceState), alignof(InstanceState)))}; - return instance_state_ptr; + return static_cast(k2::alloc_align(sizeof(InstanceState), alignof(InstanceState))); } VISIBILITY_DEFAULT void k2_init_instance() { @@ -62,8 +60,7 @@ VISIBILITY_DEFAULT void k2_init_instance() { } VISIBILITY_DEFAULT k2::PollStatus k2_warmup() { - const auto warmup_status{k2::PollStatus::PollFinishedOk}; - return warmup_status; + return k2::PollStatus::PollFinishedOk; } VISIBILITY_DEFAULT k2::PollStatus k2_poll() {