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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions runtime-light/coroutine/detail/await-set.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "runtime-light/coroutine/async-stack.h"
#include "runtime-light/coroutine/type-traits.h"
#include "runtime-light/coroutine/void-value.h"
#include "runtime-light/stdlib/cpu-info/cpu-info-state.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

namespace kphp::coro {
Expand Down Expand Up @@ -58,15 +59,18 @@ class await_broker {

template<typename... Args>
void* operator new(size_t n, [[maybe_unused]] Args&&... args) noexcept {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc(n);
}

template<typename... Args>
auto operator new(size_t n, std::align_val_t al, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc_aligned(n, al);
}

void operator delete(void* ptr, [[maybe_unused]] size_t n) noexcept {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_free_cycles)};
kphp::memory::script::free(ptr);
}

Expand Down
4 changes: 4 additions & 0 deletions runtime-light/coroutine/detail/task-self-deleting.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "runtime-light/coroutine/async-stack.h"
#include "runtime-light/coroutine/concepts.h"
#include "runtime-light/coroutine/coroutine-state.h"
#include "runtime-light/stdlib/cpu-info/cpu-info-state.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

namespace kphp::coro::detail {
Expand All @@ -32,15 +33,18 @@ struct promise_self_deleting : kphp::coro::async_stack_element {

template<typename... Args>
auto operator new(size_t n, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc(n);
}

template<typename... Args>
auto operator new(size_t n, std::align_val_t al, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc_aligned(n, al);
}

auto operator delete(void* ptr, [[maybe_unused]] size_t n) noexcept -> void {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_free_cycles)};
kphp::memory::script::free(ptr);
}

Expand Down
4 changes: 4 additions & 0 deletions runtime-light/coroutine/detail/when-all.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "runtime-light/coroutine/concepts.h"
#include "runtime-light/coroutine/type-traits.h"
#include "runtime-light/coroutine/void-value.h"
#include "runtime-light/stdlib/cpu-info/cpu-info-state.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

namespace kphp::coro::detail::when_all {
Expand Down Expand Up @@ -152,15 +153,18 @@ class when_all_task_promise_base : public kphp::coro::async_stack_element {

template<typename... Args>
auto operator new(size_t n, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc(n);
}

template<typename... Args>
auto operator new(size_t n, std::align_val_t al, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc_aligned(n, al);
}

auto operator delete(void* ptr, [[maybe_unused]] size_t n) noexcept -> void {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_free_cycles)};
kphp::memory::script::free(ptr);
}

Expand Down
4 changes: 4 additions & 0 deletions runtime-light/coroutine/detail/when-any.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "runtime-light/coroutine/type-traits.h"
#include "runtime-light/coroutine/void-value.h"
#include "runtime-light/metaprogramming/type-functions.h"
#include "runtime-light/stdlib/cpu-info/cpu-info-state.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

namespace kphp::coro::detail::when_any {
Expand Down Expand Up @@ -162,15 +163,18 @@ class when_any_task_promise_base : public kphp::coro::async_stack_element {

template<typename... Args>
auto operator new(size_t n, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc(n);
}

template<typename... Args>
auto operator new(size_t n, std::align_val_t al, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc_aligned(n, al);
}

auto operator delete(void* ptr, [[maybe_unused]] size_t n) noexcept -> void {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_free_cycles)};
kphp::memory::script::free(ptr);
}

Expand Down
4 changes: 4 additions & 0 deletions runtime-light/coroutine/shared-task.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "runtime-common/core/allocator/script-malloc-interface.h"
#include "runtime-light/coroutine/async-stack.h"
#include "runtime-light/coroutine/void-value.h"
#include "runtime-light/stdlib/cpu-info/cpu-info-state.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

namespace kphp::coro {
Expand Down Expand Up @@ -145,15 +146,18 @@ struct promise_base : kphp::coro::async_stack_element {

template<typename... Args>
auto operator new(size_t n, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc(n);
}

template<typename... Args>
auto operator new(size_t n, std::align_val_t al, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc_aligned(n, al);
}

auto operator delete(void* ptr, [[maybe_unused]] size_t n) noexcept -> void {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_free_cycles)};
kphp::memory::script::free(ptr);
}

Expand Down
4 changes: 4 additions & 0 deletions runtime-light/coroutine/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "common/containers/final_action.h"
#include "runtime-common/core/allocator/script-malloc-interface.h"
#include "runtime-light/coroutine/async-stack.h"
#include "runtime-light/stdlib/cpu-info/cpu-info-state.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

namespace kphp::coro {
Expand Down Expand Up @@ -66,15 +67,18 @@ struct promise_base : kphp::coro::async_stack_element {

template<typename... Args>
auto operator new(size_t n, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc(n);
}

template<typename... Args>
auto operator new(size_t n, std::align_val_t al, [[maybe_unused]] Args&&... args) noexcept -> void* {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_alloc_cycles)};
return kphp::memory::script::alloc_aligned(n, al);
}

auto operator delete(void* ptr, [[maybe_unused]] size_t n) noexcept -> void {
auto writer{CpuInfoInstanceState::write_cycles(CpuInfoInstanceState::get().coro_free_cycles)};
kphp::memory::script::free(ptr);
}

Expand Down
21 changes: 21 additions & 0 deletions runtime-light/runtime-light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ VISIBILITY_DEFAULT void k2_init_instance() {
k2::details::instance_state_ptr = k2_instance_state();
kphp::log::debug("start instance state init");
new (k2::instance_state()) InstanceState{};

auto& cpu_info_instance_state{CpuInfoInstanceState::get()};
cpu_info_instance_state.init();
cpu_info_instance_state.total_cycles = -CpuInfoInstanceState::rdtsc();

k2::instance_state()->init_script_execution();
kphp::log::debug("finish instance state init");
}
Expand All @@ -75,6 +80,22 @@ VISIBILITY_DEFAULT k2::PollStatus k2_poll() {
k2::details::instance_state_ptr = k2_instance_state();
kphp::log::debug("k2_poll started");
const auto poll_status{kphp::coro::io_scheduler::get().process_events()};

if (poll_status == k2::PollStatus::PollFinishedOk) {
auto& cpu_info_instance_state{CpuInfoInstanceState::get()};
cpu_info_instance_state.total_cycles += CpuInfoInstanceState::rdtsc();

uint64_t coro_alloc_cycles{cpu_info_instance_state.coro_alloc_cycles};
uint64_t coro_free_cycles{cpu_info_instance_state.coro_free_cycles};
uint64_t coro_alloc_free_cycles{coro_alloc_cycles + coro_free_cycles};

kphp::log::info("\ntotal cpu cycles -> {}\n"
"coro alloc cycles -> {} ({}%)\n"
"coro free cycles -> {} ({}%)\n"
"coro alloc+free cycles -> {} ({}%)\n",
cpu_info_instance_state.total_cycles, coro_alloc_cycles, cpu_info_instance_state.get_percent(coro_alloc_cycles), coro_free_cycles,
cpu_info_instance_state.get_percent(coro_free_cycles), coro_alloc_free_cycles, cpu_info_instance_state.get_percent(coro_alloc_free_cycles));
}
kphp::log::debug("k2_poll finished: {}", std::to_underlying(poll_status));
return poll_status;
}
2 changes: 2 additions & 0 deletions runtime-light/state/instance-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#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/cpu-info/cpu-info-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"
Expand Down Expand Up @@ -98,6 +99,7 @@ struct InstanceState final : vk::not_copyable {
WaitQueueInstanceState wait_queue_instance_state;
RpcQueueInstanceState rpc_queue_instance_state;
PhpScriptMutableGlobals php_script_mutable_globals_singleton;
CpuInfoInstanceState cpu_info_instance_state;

RuntimeContext runtime_context;
CLIInstanceInstance cli_instance_instate;
Expand Down
11 changes: 11 additions & 0 deletions runtime-light/stdlib/cpu-info/cpu-info-state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime-light/stdlib/cpu-info/cpu-info-state.h"

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

CpuInfoInstanceState& CpuInfoInstanceState::get() noexcept {
return InstanceState::get().cpu_info_instance_state;
}
44 changes: 44 additions & 0 deletions runtime-light/stdlib/cpu-info/cpu-info-state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2026 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstdint>
#include <x86intrin.h>

#include "common/containers/final_action.h"
#include "runtime-light/stdlib/diagnostics/logs.h"

class CpuInfoInstanceState {
public:
CpuInfoInstanceState() noexcept = default;

static CpuInfoInstanceState& get() noexcept;

[[gnu::always_inline]] static uint64_t rdtsc() noexcept {
return __rdtsc();
}

static auto write_cycles(uint64_t& dist) noexcept {
uint64_t start{CpuInfoInstanceState::rdtsc()};
return vk::final_action([start, &dist]() noexcept { dist += CpuInfoInstanceState::rdtsc() - start; });
}

double get_percent(uint64_t cycles) const noexcept {
if (cycles > total_cycles) {
kphp::log::warning("CpuInfo. Something wrong: cycles {} > total_cycles {}", cycles, total_cycles);
}
return 100.0 * cycles / total_cycles;
}

void init() noexcept {
total_cycles = 0;
coro_alloc_cycles = 0;
coro_free_cycles = 0;
}

uint64_t total_cycles;
uint64_t coro_alloc_cycles;
uint64_t coro_free_cycles;
};
1 change: 1 addition & 0 deletions runtime-light/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ prepend(
stdlib/
confdata/confdata-functions.cpp
confdata/confdata-state.cpp
cpu-info/cpu-info-state.cpp
crypto/crypto-functions.cpp
curl/curl-state.cpp
web-transfer-lib/web-state.cpp
Expand Down
Loading