Skip to content

[k2] log error on OOM in pool allocator - #1704

Open
Shamzik wants to merge 8 commits into
masterfrom
kshamazov/k2/oom_assertion
Open

Shamzik wants to merge 8 commits into
masterfrom
kshamazov/k2/oom_assertion

Conversation

@Shamzik

@Shamzik Shamzik commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

This PR replaces log::assertion with log::error handling OOM in pool allocator.

It is also rewrites kphp::log::impl::log function so it does not call pool allocator recursivly

@Shamzik Shamzik added runtime Feature related to runtime k2 Affects compiler or runtime in K2 mode labels Oct 8, 2026
@Shamzik Shamzik added this to the next milestone Oct 8, 2026
@Shamzik Shamzik self-assigned this Oct 8, 2026
@Shamzik
Shamzik requested review from LLirikkkk and apolyakov and removed request for LLirikkkk October 9, 2026 10:26
LLirikkkk
LLirikkkk previously approved these changes Oct 10, 2026
Comment thread runtime-light/stdlib/diagnostics/logs.h Outdated
kphp::stl::vector<k2::LogTaggedEntry, kphp::memory::script_allocator> tagged_entries{};
tagged_entries.reserve(tagged_entries_size);
size_t tagged_entries_size{};
static constexpr size_t TAGGED_ENTRIES_BUFFER_SIZE = 8UZ;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reserve more space for tags. Something like 20 should be ok for now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

extra_mem_size = std::bit_ceil(extra_mem_size);

auto* extra_mem{kphp::memory::platform::alloc(extra_mem_size)};
kphp::log::assertion(extra_mem != nullptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also other assertions we need to convert to error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted

auto* extra_mem{kphp::memory::platform::alloc(extra_mem_size)};
kphp::log::assertion(extra_mem != nullptr);
if (extra_mem == nullptr) [[unlikely]] {
kphp::log::error("pool_allocator: failed to get extra memory from platform");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's print memory usage statistics so we understand what's happened

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Replace OOM assertions in pool_allocator::alloc/calloc/realloc_script_memory
with raise_out_of_memory() that logs MemoryStats (current/peak usage, arena
limit, total allocations, defragmentation calls, free pieces) before exiting,
so OOM failures can be diagnosed from logs.
@Shamzik
Shamzik requested a review from apolyakov October 13, 2026 13:29
Comment thread runtime-light/stdlib/diagnostics/logs.h Outdated
kphp::stl::vector<k2::LogTaggedEntry, kphp::memory::script_allocator> tagged_entries{};
tagged_entries.reserve(tagged_entries_size);
size_t tagged_entries_size{};
static constexpr size_t TAGGED_ENTRIES_BUFFER_SIZE = 24UZ;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use braced init

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread runtime-light/stdlib/diagnostics/logs.h Outdated
return 0;
});
if (trace.has_value()) {
if (trace.has_value() && tagged_entries_size != TAGGED_ENTRIES_BUFFER_SIZE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сould we reserve one slot for the trace when it's present ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — tags are capped at TAGGED_ENTRIES_BUFFER_SIZE - 1 when a trace is present, so the trace slot is always reserved and the extra bounds check in the trace branch is gone.


auto pool_allocator::raise_out_of_memory(size_t requested_size) const noexcept -> void {
const auto& stats{memory_resource.get_memory_stats()};
kphp::log::error("pool_allocator: out of memory: failed to allocate {} bytes even after requesting extra memory, "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to mention the extra-memory request here ? It exposes an implementation detail and couples the message to the current allocation strategy

requested_size, extra_mem_size, stats.memory_used, stats.memory_limit, stats.max_memory_used, stats.real_memory_used,
stats.max_real_memory_used, stats.total_allocations, stats.total_memory_allocated, stats.defragmentation_calls, stats.huge_memory_pieces,
stats.small_memory_pieces);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this duplicates the logic in raise_out_of_memory(). Could we reuse or adapt it here ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — raise_out_of_memory() now takes a context string and is reused for the platform allocation failure too; the stats are formatted in a single place.

Shamzik and others added 3 commits October 14, 2026 18:00
When a trace is present, contextual tags are capped at
TAGGED_ENTRIES_BUFFER_SIZE - 1 so the trace is never dropped.

Co-Authored-By: Claude Code <noreply@anthropic.com>
raise_out_of_memory() now takes a context string and is reused for the
platform allocation failure in request_extra_memory(), so memory usage
statistics are logged in a single place. The message no longer mentions
the extra-memory request, decoupling it from the allocation strategy.

Co-Authored-By: Claude Code <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

k2 Affects compiler or runtime in K2 mode runtime Feature related to runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants