Skip to content

Commit 475badf

Browse files
authored
[k2] instance_cache_store: freshness optimization (#1695)
1 parent 9e5c07f commit 475badf

6 files changed

Lines changed: 108 additions & 50 deletions

File tree

‎runtime-light/components/confdata/state/instance-state.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ InstanceState::confdata_piece::~confdata_piece() {
151151
if (m_storage.is_initialized()) {
152152
m_storage.close();
153153
}
154-
if (const auto released{k2::release_shared_memory(m_memory)}; !released) [[unlikely]] {
154+
if (const auto released{k2::shared_memory_release(m_memory)}; !released) [[unlikely]] {
155155
kphp::log::warning("failed to free confdata shared memory: error -> {}", released.error());
156156
}
157157
}
@@ -167,7 +167,7 @@ auto InstanceState::confdata_piece::create(confdata_piece_list& owner, size_t me
167167
.m_code = static_cast<int32_t>(std::to_underlying(shared_memory_size.error()))}};
168168
}
169169

170-
const auto shared_memory{k2::alloc_shared_memory(*shared_memory_size, kphp::confdata::storage::memory_alignment())};
170+
const auto shared_memory{k2::shared_memory_alloc(*shared_memory_size, kphp::confdata::storage::memory_alignment())};
171171
if (!shared_memory) [[unlikely]] {
172172
return std::unexpected{confdata_sync_error{.m_stage = confdata_sync_error::stage::shared_memory_allocation, .m_code = shared_memory.error()}};
173173
}
@@ -429,7 +429,7 @@ auto InstanceState::perform_sync(std::string_view confdata_proxy_actor) noexcept
429429
piece.storage().memory_usage().m_used);
430430
// A lease must keep resolving to its pinned piece even if another sync completes before the reader maps it.
431431
// Reject name collisions rather than replacing an allocation that an outstanding lease may still name.
432-
if (const auto published{k2::publish_shared_memory(piece.shared_memory_name(), piece.storage().memory().data(), 0, true, false)}; !published) [[unlikely]] {
432+
if (const auto published{k2::shared_memory_publish(piece.shared_memory_name(), piece.storage().memory().data(), 0, true, false)}; !published) [[unlikely]] {
433433
co_return std::unexpected{
434434
confdata_sync_error{.m_stage = confdata_sync_error::stage::shared_memory_publication, .m_code = static_cast<int32_t>(published.error())}};
435435
}

‎runtime-light/k2-platform/k2-api.h‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ using PollStatus = PollStatus;
8181

8282
using ImageInfo = ImageInfo;
8383

84+
using SharedMemoryInfo = SharedMemoryInfo;
85+
8486
using RpcKind = RpcKind;
8587

8688
using ControlFlags = ControlFlags;
@@ -129,54 +131,62 @@ inline void free_checked(void* ptr, size_t size, size_t align) noexcept {
129131
k2_free_checked(ptr, size, align);
130132
}
131133

132-
inline std::expected<void*, int32_t> alloc_shared_memory(size_t size, size_t align = k2::details::DEFAULT_MEMORY_ALIGN) noexcept {
134+
inline std::expected<void*, int32_t> shared_memory_alloc(size_t size, size_t align = k2::details::DEFAULT_MEMORY_ALIGN) noexcept {
133135
void* pointer{nullptr};
134-
if (auto error_code{k2_alloc_shared_memory(size, align, std::addressof(pointer))}; error_code != k2::errno_ok) [[unlikely]] {
136+
if (auto error_code{k2_shared_memory_alloc(size, align, std::addressof(pointer))}; error_code != k2::errno_ok) [[unlikely]] {
135137
return std::unexpected{error_code};
136138
}
137139
return {pointer};
138140
}
139141

140-
inline std::expected<void, int32_t> publish_shared_memory(std::string_view name, const void* memory, uint64_t ttl_ms, bool as_mut,
142+
inline std::expected<void, int32_t> shared_memory_publish(std::string_view name, const void* memory, uint64_t ttl_ms, bool as_mut,
141143
bool ignore_if_exist) noexcept {
142-
if (auto error_code{k2_publish_shared_memory(name.data(), name.length(), memory, ttl_ms, as_mut, ignore_if_exist)}; error_code != k2::errno_ok) [[unlikely]] {
144+
if (auto error_code{k2_shared_memory_publish(name.data(), name.length(), memory, ttl_ms, as_mut, ignore_if_exist)}; error_code != k2::errno_ok) [[unlikely]] {
143145
return std::unexpected{error_code};
144146
}
145147
return {};
146148
}
147149

148-
inline std::expected<std::span<const std::byte>, int32_t> get_shared_memory(std::string_view name) noexcept {
150+
inline std::expected<std::span<const std::byte>, int32_t> shared_memory_get(std::string_view name) noexcept {
149151
const void* pointer{nullptr};
150152
size_t size{};
151-
if (auto error_code{k2_get_shared_memory(name.data(), name.length(), std::addressof(pointer), std::addressof(size))}; error_code != k2::errno_ok)
153+
if (auto error_code{k2_shared_memory_get(name.data(), name.length(), std::addressof(pointer), std::addressof(size))}; error_code != k2::errno_ok)
152154
[[unlikely]] {
153155
return std::unexpected{error_code};
154156
}
155157
return {std::span{static_cast<const std::byte*>(pointer), size}};
156158
}
157159

158-
inline std::expected<void, int32_t> republish_shared_memory(std::string_view name, uint64_t ttl) noexcept {
159-
if (auto error_code{k2_republish_shared_memory(name.data(), name.length(), ttl)}; error_code != k2::errno_ok) [[unlikely]] {
160+
inline std::expected<void, int32_t> shared_memory_republish(std::string_view name, uint64_t ttl) noexcept {
161+
if (auto error_code{k2_shared_memory_republish(name.data(), name.length(), ttl)}; error_code != k2::errno_ok) [[unlikely]] {
160162
return std::unexpected{error_code};
161163
}
162164
return {};
163165
}
164166

165-
inline std::expected<void, int32_t> seek_ttl_to_shared_memory(std::string_view name, uint8_t percentile, uint64_t remaining_lifetime_limit) noexcept {
166-
if (auto error_code{k2_seek_ttl_to_shared_memory(name.data(), name.length(), percentile, remaining_lifetime_limit)}; error_code != k2::errno_ok)
167+
inline std::expected<void, int32_t> shared_memory_seek_ttl_to(std::string_view name, uint8_t percentile, uint64_t remaining_lifetime_limit) noexcept {
168+
if (auto error_code{k2_shared_memory_seek_ttl_to(name.data(), name.length(), percentile, remaining_lifetime_limit)}; error_code != k2::errno_ok)
167169
[[unlikely]] {
168170
return std::unexpected{error_code};
169171
}
170172
return {};
171173
}
172174

173-
inline std::expected<void, int32_t> release_shared_memory(const void* ptr) noexcept {
174-
if (auto error_code{k2_release_shared_memory(ptr)}; error_code != k2::errno_ok) [[unlikely]] {
175+
inline std::expected<void, int32_t> shared_memory_release(const void* ptr) noexcept {
176+
if (auto error_code{k2_shared_memory_release(ptr)}; error_code != k2::errno_ok) [[unlikely]] {
175177
return std::unexpected{error_code};
176178
}
177179
return {};
178180
}
179181

182+
inline std::expected<const k2::SharedMemoryInfo, int32_t> shared_memory_info(std::string_view name) noexcept {
183+
k2::SharedMemoryInfo info{};
184+
if (auto error_code{k2_shared_memory_info(name.data(), name.length(), std::addressof(info))}; error_code != k2::errno_ok) [[unlikely]] {
185+
return std::unexpected{error_code};
186+
}
187+
return info;
188+
}
189+
180190
[[noreturn]] inline void exit(int32_t exit_code) noexcept {
181191
k2_exit(exit_code);
182192
}

‎runtime-light/k2-platform/k2-header.h‎

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <time.h>
2929
#endif
3030

31-
#define K2_PLATFORM_HEADER_H_VERSION 17
31+
#define K2_PLATFORM_HEADER_H_VERSION 18
3232

3333
// Always check that enum value is a valid value!
3434

@@ -124,6 +124,18 @@ struct ImageInfo {
124124
const struct KeyValuePair* extra_info;
125125
};
126126

127+
// `stored_at` (the moment of the last publish/republish) is always treated as time origin 0,
128+
// so both `now` and `expires_at` are nanosecond offsets from it, not wall-clock timestamps.
129+
struct SharedMemoryInfo {
130+
// Nanoseconds elapsed since `stored_at`.
131+
uint64_t now;
132+
// Nanoseconds from `stored_at` to the expiration moment. `0` means the memory is immortal
133+
// (it was published/republished with a zero TTL) and never expires.
134+
uint64_t expires_at;
135+
// `true` if the memory was last published by this instance.
136+
bool is_own;
137+
};
138+
127139
/**
128140
* Symbols provided by component image
129141
* Every image should provide these symbols
@@ -199,9 +211,9 @@ void k2_free_checked(void* ptr, size_t size, size_t align);
199211

200212
/**
201213
* Shared memory provides a mechanism for instances to share data.
202-
* To use it, first allocate memory with `k2_alloc_shared_memory`, then publish
203-
* it with a unique name using `k2_publish_shared_memory`. Other instances can
204-
* then retrieve the memory by name with `k2_get_shared_memory`.
214+
* To use it, first allocate memory with `k2_shared_memory_alloc`, then publish
215+
* it with a unique name using `k2_shared_memory_publish`. Other instances can
216+
* then retrieve the memory by name with `k2_shared_memory_get`.
205217
*
206218
* Lifecycle:
207219
* - A finite TTL removes the name from shared-memory storage after the TTL.
@@ -211,21 +223,21 @@ void k2_free_checked(void* ptr, size_t size, size_t align);
211223
* storage when the last user reference is released.
212224
* - UINT64_MAX publishes a strong entry with no initial expiration. Storage
213225
* keeps it alive until it is replaced, assigned a finite expiration by
214-
* `k2_republish_shared_memory` or `k2_seek_ttl_to_shared_memory`, or the
226+
* `k2_shared_memory_republish` or `k2_shared_memory_seek_ttl_to`, or the
215227
* shared-memory subsystem stops.
216228
*
217229
* Reference counting:
218-
* - A successful `k2_publish_shared_memory` gives the calling instance one
230+
* - A successful `k2_shared_memory_publish` gives the calling instance one
219231
* user reference. For a zero TTL, storage keeps only a weak reference and
220232
* does not add a reference of its own.
221233
* - For a finite TTL or UINT64_MAX, storage keeps a strong reference until
222234
* the entry is removed from storage.
223-
* - Calling `k2_get_shared_memory` acquires a user reference if this instance
235+
* - Calling `k2_shared_memory_get` acquires a user reference if this instance
224236
* does not already hold one; repeated calls for the same pointer while it is
225237
* already held do not add another reference.
226-
* - Calling `k2_release_shared_memory` clears the user reference held by this
238+
* - Calling `k2_shared_memory_release` clears the user reference held by this
227239
* instance, if `pointer` is currently held. Unpublished memory from
228-
* `k2_alloc_shared_memory` is simply freed.
240+
* `k2_shared_memory_alloc` is simply freed.
229241
*/
230242

231243
/**
@@ -246,34 +258,34 @@ void k2_free_checked(void* ptr, size_t size, size_t align);
246258
* `ENOMEM` => Not enough memory to allocate the requested region.
247259
* `ENOSYS` => Shared memory subsystem is unavailable on this host.
248260
*/
249-
int32_t k2_alloc_shared_memory(size_t size, size_t align, void** pointer);
261+
int32_t k2_shared_memory_alloc(size_t size, size_t align, void** pointer);
250262

251263
/**
252264
* Publishes shared memory with a name and TTL, making it discoverable by other instances.
253265
*
254266
* @param `name` Name to associate with the memory region. Must be unique.
255267
* Should be valid UTF-8 and not contain null bytes.
256268
* @param `name_len` Length of the name in bytes. Must be greater than 0.
257-
* @param `memory` Pointer to memory previously allocated via `k2_alloc_shared_memory`.
269+
* @param `memory` Pointer to memory previously allocated via `k2_shared_memory_alloc`.
258270
* @param `ttl` Lifetime selector in milliseconds:
259271
* `0` publishes a weak entry with no TTL;
260272
* `1..=UINT64_MAX-1` uses a finite TTL;
261273
* `UINT64_MAX` means an initially infinite strong lifetime.
262274
* @param `as_mut` Controls whether write protection is disabled. If true, the memory is made writable.
263275
* @param `ignore_if_exist` If true, allows re-publishing memory previously allocated via
264-
* `k2_alloc_shared_memory` even if a live published memory with the same name already exists.
276+
* `k2_shared_memory_alloc` even if a live published memory with the same name already exists.
265277
*
266278
* @return `0` on success. libc-like `errno` on error.
267279
*
268280
* Possible `errno`:
269281
* `EINVAL` => `name` is NULL, `name_len` is 0, `memory` is NULL, `name` is
270282
* not valid UTF-8, or `ttl` is too small (non-zero and less than
271283
* 100 ms).
272-
* `ENOENT` => `memory` was not allocated by `k2_alloc_shared_memory`.
284+
* `ENOENT` => `memory` was not allocated by `k2_shared_memory_alloc`.
273285
* `EEXIST` => Memory with this name already exists.
274286
* `ENOSYS` => Shared memory subsystem is unavailable on this host.
275287
*/
276-
int32_t k2_publish_shared_memory(const char* name, size_t name_len, const void* memory, uint64_t ttl, bool as_mut, bool ignore_if_exist);
288+
int32_t k2_shared_memory_publish(const char* name, size_t name_len, const void* memory, uint64_t ttl, bool as_mut, bool ignore_if_exist);
277289

278290
/**
279291
* Retrieves shared memory by name and, unless already held by this
@@ -290,11 +302,11 @@ int32_t k2_publish_shared_memory(const char* name, size_t name_len, const void*
290302
* Possible `errno`:
291303
* `EINVAL` => `name` is NULL, `name_len` is 0, `pointer` is NULL, or `name`
292304
* is not valid UTF-8.
293-
* `ENOENT` => No memory found with the given name, the weak entry has no live
294-
* user references, or its finite TTL has removed it from storage.
305+
* `ENOENT` => No memory found with the given name (memory was freed).
306+
* `EKEYEXPIRED` => TTL for this name has already expired.
295307
* `ENOSYS` => Shared memory subsystem is unavailable on this host.
296308
*/
297-
int32_t k2_get_shared_memory(const char* name, size_t name_len, const void** pointer, size_t* size);
309+
int32_t k2_shared_memory_get(const char* name, size_t name_len, const void** pointer, size_t* size);
298310

299311
/**
300312
* Republishes shared memory, selecting the lifetime mode and restarting the
@@ -316,7 +328,7 @@ int32_t k2_get_shared_memory(const char* name, size_t name_len, const void** poi
316328
* user references, or its finite TTL has removed it from storage.
317329
* `ENOSYS` => Shared memory subsystem is unavailable on this host.
318330
*/
319-
int32_t k2_republish_shared_memory(const char* name, size_t name_len, uint64_t ttl);
331+
int32_t k2_shared_memory_republish(const char* name, size_t name_len, uint64_t ttl);
320332

321333
/**
322334
* Fast-forwards the expiration of published shared memory ahead of its TTL, by name.
@@ -352,13 +364,13 @@ int32_t k2_republish_shared_memory(const char* name, size_t name_len, uint64_t t
352364
* user references, or its finite TTL has removed it from storage.
353365
* `ENOSYS` => Shared memory subsystem is unavailable on this host.
354366
*/
355-
int32_t k2_seek_ttl_to_shared_memory(const char* name, size_t name_len, uint8_t percentile, uint64_t remaining_lifetime_limit);
367+
int32_t k2_shared_memory_seek_ttl_to(const char* name, size_t name_len, uint8_t percentile, uint64_t remaining_lifetime_limit);
356368

357369
/**
358-
* Frees shared memory allocated via `k2_alloc_shared_memory` that was never
370+
* Frees shared memory allocated via `k2_shared_memory_alloc` that was never
359371
* published, or releases the reference to shared memory held by this
360-
* instance, acquired via `k2_publish_shared_memory` or
361-
* `k2_get_shared_memory` (see the reference counting notes above).
372+
* instance, acquired via `k2_shared_memory_publish` or
373+
* `k2_shared_memory_get` (see the reference counting notes above).
362374
*
363375
* Releasing a reference may remove a weak entry when it was the last user
364376
* reference. For a finite-TTL entry, the allocation is physically reclaimed
@@ -367,8 +379,8 @@ int32_t k2_seek_ttl_to_shared_memory(const char* name, size_t name_len, uint8_t
367379
* released.
368380
*
369381
* @param `pointer` Pointer to shared memory, as returned by
370-
* `k2_alloc_shared_memory` or `k2_get_shared_memory`, or
371-
* passed as the `memory` argument to `k2_publish_shared_memory`.
382+
* `k2_shared_memory_alloc` or `k2_shared_memory_get`, or
383+
* passed as the `memory` argument to `k2_shared_memory_publish`.
372384
*
373385
* @return `0` on success: either the unpublished allocation was freed, or the
374386
* reference held by this instance was released. libc-like `errno`
@@ -377,10 +389,31 @@ int32_t k2_seek_ttl_to_shared_memory(const char* name, size_t name_len, uint8_t
377389
* Possible `errno`:
378390
* `EINVAL` => `pointer` is NULL.
379391
* `ENOENT` => `pointer` does not correspond to memory allocated by this
380-
* instance via `k2_alloc_shared_memory`, nor to a reference
392+
* instance via `k2_shared_memory_alloc`, nor to a reference
381393
* currently held by this instance.
382394
*/
383-
int32_t k2_release_shared_memory(const void* pointer);
395+
int32_t k2_shared_memory_release(const void* pointer);
396+
397+
/**
398+
* Retrieves timing and ownership info about published shared memory, by name, without
399+
* taking a reference to it.
400+
*
401+
* @param `name` Name of the shared memory to query.
402+
* @param `name_len` Length of the name in bytes. Must be greater than 0.
403+
* @param `info` Out parameter, filled with the current info on success. `stored_at` (the
404+
* moment of the last publish/republish) is always treated as time origin 0, so
405+
* `info->now` and `info->expires_at` are nanosecond offsets from it, not
406+
* wall-clock timestamps. `info->expires_at == 0` means the memory is immortal
407+
* (published/republished with a zero TTL or weak lifetime) and never expires.
408+
*
409+
* @return `0` on success. libc-like `errno` on error.
410+
*
411+
* Possible `errno`:
412+
* `EINVAL` => `name` is NULL, `name_len` is 0, `info` is NULL, or `name` is not valid UTF-8.
413+
* `ENOENT` => No memory found with the given name (memory was freed).
414+
* `EKEYEXPIRED` => TTL for this name has already expired.
415+
*/
416+
int32_t k2_shared_memory_info(const char* name, size_t name_len, struct SharedMemoryInfo* info);
384417

385418
/**
386419
* Immediately abort component execution.

‎runtime-light/stdlib/confdata/confdata-reader-lease.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class reader_lease final {
3030
storage::sample_id m_sample_id{storage::INVALID_SAMPLE_ID};
3131
/** Number of meaningful bytes in `m_shared_memory_name`. */
3232
uint32_t m_shared_memory_name_size{};
33-
/** Name passed to `k2_get_shared_memory`; it is not null-terminated. */
33+
/** Name passed to `k2_shared_memory_get`; it is not null-terminated. */
3434
std::array<char, MAX_SHARED_MEMORY_NAME_SIZE> m_shared_memory_name{};
3535

3636
public:

‎runtime-light/stdlib/confdata/confdata-state.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ auto instance_state::init() noexcept -> kphp::coro::task<> {
3030
co_return kphp::log::warning("failed to acquire a valid confdata reader lease");
3131
}
3232

33-
const auto shared_memory{k2::get_shared_memory(lease.shared_memory_name())};
33+
const auto shared_memory{k2::shared_memory_get(lease.shared_memory_name())};
3434
if (!shared_memory) {
3535
co_return kphp::log::warning("failed to get confdata shared memory: error -> {}", shared_memory.error());
3636
}

0 commit comments

Comments
 (0)