Skip to content

Commit 6641f35

Browse files
committed
[k2] add temporary stats for confdata and instance cache calls
1 parent ceabd49 commit 6641f35

5 files changed

Lines changed: 48 additions & 3 deletions

File tree

runtime-light/state/instance-state.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,8 @@ kphp::coro::task<> InstanceState::run_instance_epilogue() noexcept {
244244
while (!ignore_answer_request_await_set.empty()) {
245245
co_await ignore_answer_request_await_set.next();
246246
}
247+
248+
// TEMPORARY: log instance-cache and confdata timings
249+
kphp::log::info("instance stats (approximately): confdata -> {}ms, instance cache -> {}ms", confdata_instance_state.time_ns >> 20,
250+
instance_cache_instance_state.time_ns >> 20);
247251
}

runtime-light/stdlib/confdata/confdata-functions.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
#include <algorithm>
88
#include <cstddef>
9+
#include <memory>
910
#include <span>
1011
#include <string_view>
1112
#include <utility>
1213

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

55-
auto& confdata_key_cache{ConfdataInstanceState::get().key_cache()};
57+
auto& confdata_instance_st{ConfdataInstanceState::get()};
58+
auto& confdata_key_cache{confdata_instance_st.key_cache()};
5659
if (auto it{confdata_key_cache.find(key)}; it != confdata_key_cache.end()) {
5760
co_return it->second;
5861
}
5962

63+
k2::SystemTime start_time{};
64+
k2::system_time(std::addressof(start_time));
65+
const auto finalizer{vk::finally([&start_time, &confdata_instance_st] noexcept {
66+
k2::SystemTime end_time{};
67+
k2::system_time(std::addressof(end_time));
68+
confdata_instance_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
69+
})};
70+
6071
tl::ConfdataGet confdata_get{.key = {.value = {key.c_str(), key.size()}}};
6172
tl::storer tls{confdata_get.footprint()};
6273
confdata_get.store(tls);
@@ -93,11 +104,20 @@ kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wild
93104
co_return array<mixed>{};
94105
}
95106

96-
auto& confdata_wildcard_cache{ConfdataInstanceState::get().wildcard_cache()};
107+
auto& confdata_instance_st{ConfdataInstanceState::get()};
108+
auto& confdata_wildcard_cache{confdata_instance_st.wildcard_cache()};
97109
if (auto it{confdata_wildcard_cache.find(wildcard)}; it != confdata_wildcard_cache.end()) {
98110
co_return it->second;
99111
}
100112

113+
k2::SystemTime start_time{};
114+
k2::system_time(std::addressof(start_time));
115+
const auto finalizer{vk::finally([&start_time, &confdata_instance_st] noexcept {
116+
k2::SystemTime end_time{};
117+
k2::system_time(std::addressof(end_time));
118+
confdata_instance_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
119+
})};
120+
101121
const std::string_view wildcard_view{wildcard.c_str(), wildcard.size()};
102122

103123
const tl::ConfdataGetWildcard confdata_get_wildcard{.wildcard = {.value = wildcard_view}};

runtime-light/stdlib/confdata/confdata-state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class ConfdataInstanceState final : private vk::not_copyable {
1818
kphp::stl::unordered_map<string, array<mixed>, kphp::memory::script_allocator, hasher_type> m_wildcard_cache;
1919

2020
public:
21+
size_t time_ns{};
22+
2123
ConfdataInstanceState() noexcept = default;
2224

2325
auto& key_cache() noexcept {

runtime-light/stdlib/instance-cache/instance-cache-functions.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ kphp::coro::task<bool> f$instance_cache_store(string key, InstanceType instance,
4646
ttl = 0;
4747
}
4848

49+
auto& instance_cache_st{InstanceCacheInstanceState::get()};
50+
k2::SystemTime start_time{};
51+
k2::system_time(std::addressof(start_time));
52+
const auto finalizer{vk::finally([&start_time, &instance_cache_st] noexcept {
53+
k2::SystemTime end_time{};
54+
k2::system_time(std::addressof(end_time));
55+
instance_cache_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
56+
})};
57+
4958
auto serialized_instance{f$instance_serialize(instance)};
5059
if (!serialized_instance.has_value()) [[unlikely]] {
5160
kphp::log::warning("can't serialize instance: key -> {}", key.c_str());
@@ -78,12 +87,21 @@ kphp::coro::task<bool> f$instance_cache_store(string key, InstanceType instance,
7887

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

97+
k2::SystemTime start_time{};
98+
k2::system_time(std::addressof(start_time));
99+
const auto finalizer{vk::finally([&start_time, &instance_cache_st] noexcept {
100+
k2::SystemTime end_time{};
101+
k2::system_time(std::addressof(end_time));
102+
instance_cache_st.time_ns += (end_time.since_epoch_ns - start_time.since_epoch_ns);
103+
})};
104+
87105
tl::CacheFetch cache_fetch{.key = tl::string{.value = {key.c_str(), key.size()}}};
88106
tl::storer tls{cache_fetch.footprint()};
89107
cache_fetch.store(tls);

runtime-light/stdlib/instance-cache/instance-cache-state.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "runtime-common/core/std/containers.h"
1313

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

0 commit comments

Comments
 (0)