Skip to content
Closed
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
16 changes: 16 additions & 0 deletions runtime-light/state/instance-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
Expand Down Expand Up @@ -134,6 +135,12 @@ kphp::coro::task<> InstanceState::init_server_instance() noexcept {

template<image_kind kind>
kphp::coro::task<> InstanceState::run_instance_prologue() noexcept {
{
k2::SystemTime start_time{};
k2::system_time(std::addressof(start_time));
start_time_ns = start_time.since_epoch_ns;
}

static_assert(kind != image_kind::invalid);
image_kind_ = kind;

Expand Down Expand Up @@ -244,4 +251,13 @@ kphp::coro::task<> InstanceState::run_instance_epilogue() noexcept {
while (!ignore_answer_request_await_set.empty()) {
co_await ignore_answer_request_await_set.next();
}

k2::SystemTime end_time{};
k2::system_time(std::addressof(end_time));

// TEMPORARY: log instance-cache and confdata timings
kphp::log::info("instance stats (approximately): confdata -> {}ms, instance cache -> {}ms, instance time -> {}ms",
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds{confdata_instance_state.time_ns}).count(),
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds{instance_cache_instance_state.time_ns}).count(),
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds{end_time.since_epoch_ns - start_time_ns}).count());
}
1 change: 1 addition & 0 deletions runtime-light/state/instance-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct InstanceState final : vk::not_copyable {
enum class shutdown_state : uint8_t { not_started, in_progress, finished };
shutdown_state shutdown_state_{shutdown_state::not_started};

uint64_t start_time_ns{};
enum image_kind image_kind_ { image_kind::invalid };
enum instance_kind instance_kind_ { instance_kind::invalid };

Expand Down
24 changes: 22 additions & 2 deletions runtime-light/stdlib/confdata/confdata-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

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

#include "common/containers/final_action.h"
#include "runtime-common/core/allocator/script-allocator.h"
#include "runtime-common/core/runtime-core.h"
#include "runtime-common/core/std/containers.h"
Expand Down Expand Up @@ -52,11 +54,20 @@ kphp::coro::task<mixed> f$confdata_get_value(string key) noexcept {
co_return mixed{};
}

auto& confdata_key_cache{ConfdataInstanceState::get().key_cache()};
auto& confdata_instance_st{ConfdataInstanceState::get()};
auto& confdata_key_cache{confdata_instance_st.key_cache()};
if (auto it{confdata_key_cache.find(key)}; it != confdata_key_cache.end()) {
co_return it->second;
}

k2::SystemTime start_time{};
k2::system_time(std::addressof(start_time));
const auto finalizer{vk::finally([&start_time, &confdata_instance_st] noexcept {
k2::SystemTime end_time{};
k2::system_time(std::addressof(end_time));
confdata_instance_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
})};

tl::ConfdataGet confdata_get{.key = {.value = {key.c_str(), key.size()}}};
tl::storer tls{confdata_get.footprint()};
confdata_get.store(tls);
Expand Down Expand Up @@ -93,11 +104,20 @@ kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wild
co_return array<mixed>{};
}

auto& confdata_wildcard_cache{ConfdataInstanceState::get().wildcard_cache()};
auto& confdata_instance_st{ConfdataInstanceState::get()};
auto& confdata_wildcard_cache{confdata_instance_st.wildcard_cache()};
if (auto it{confdata_wildcard_cache.find(wildcard)}; it != confdata_wildcard_cache.end()) {
co_return it->second;
}

k2::SystemTime start_time{};
k2::system_time(std::addressof(start_time));
const auto finalizer{vk::finally([&start_time, &confdata_instance_st] noexcept {
k2::SystemTime end_time{};
k2::system_time(std::addressof(end_time));
confdata_instance_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
})};

const std::string_view wildcard_view{wildcard.c_str(), wildcard.size()};

const tl::ConfdataGetWildcard confdata_get_wildcard{.wildcard = {.value = wildcard_view}};
Expand Down
3 changes: 3 additions & 0 deletions runtime-light/stdlib/confdata/confdata-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <cstddef>
#include <cstdint>

#include "common/mixin/not_copyable.h"
#include "runtime-common/core/allocator/script-allocator.h"
Expand All @@ -18,6 +19,8 @@ class ConfdataInstanceState final : private vk::not_copyable {
kphp::stl::unordered_map<string, array<mixed>, kphp::memory::script_allocator, hasher_type> m_wildcard_cache;

public:
uint64_t time_ns{};

ConfdataInstanceState() noexcept = default;

auto& key_cache() noexcept {
Expand Down
20 changes: 19 additions & 1 deletion runtime-light/stdlib/instance-cache/instance-cache-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ kphp::coro::task<bool> f$instance_cache_store(string key, InstanceType instance,
ttl = 0;
}

auto& instance_cache_st{InstanceCacheInstanceState::get()};
k2::SystemTime start_time{};
k2::system_time(std::addressof(start_time));
const auto finalizer{vk::finally([&start_time, &instance_cache_st] noexcept {
k2::SystemTime end_time{};
k2::system_time(std::addressof(end_time));
instance_cache_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
})};

auto serialized_instance{f$instance_serialize(instance)};
if (!serialized_instance.has_value()) [[unlikely]] {
kphp::log::warning("can't serialize instance: key -> {}", key.c_str());
Expand Down Expand Up @@ -78,12 +87,21 @@ kphp::coro::task<bool> f$instance_cache_store(string key, InstanceType instance,

template<typename InstanceType>
kphp::coro::task<InstanceType> f$instance_cache_fetch(string /*class_name*/, string key, bool /*even_if_expired*/ = false) noexcept {
auto& request_cache{InstanceCacheInstanceState::get().request_cache};
auto& instance_cache_st{InstanceCacheInstanceState::get()};
auto& request_cache{instance_cache_st.request_cache};
if (auto it{request_cache.find(key)}; it != request_cache.end()) {
auto cached_instance{from_mixed<InstanceType>(it->second, {})};
co_return std::move(cached_instance);
}

k2::SystemTime start_time{};
k2::system_time(std::addressof(start_time));
const auto finalizer{vk::finally([&start_time, &instance_cache_st] noexcept {
k2::SystemTime end_time{};
k2::system_time(std::addressof(end_time));
instance_cache_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
})};

tl::CacheFetch cache_fetch{.key = tl::string{.value = {key.c_str(), key.size()}}};
tl::storer tls{cache_fetch.footprint()};
cache_fetch.store(tls);
Expand Down
2 changes: 2 additions & 0 deletions runtime-light/stdlib/instance-cache/instance-cache-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
#pragma once

#include <cstddef>
#include <cstdint>

#include "common/mixin/not_copyable.h"
#include "runtime-common/core/allocator/script-allocator.h"
#include "runtime-common/core/runtime-core.h"
#include "runtime-common/core/std/containers.h"

struct InstanceCacheInstanceState final : private vk::not_copyable {
uint64_t time_ns{};
kphp::stl::unordered_map<string, mixed, kphp::memory::script_allocator, decltype([](const string& s) noexcept { return static_cast<size_t>(s.hash()); })>
request_cache;

Expand Down
Loading