Skip to content

Log the numbers behind a memory kill and attribute it to its operation - #484

Closed
meutex wants to merge 1 commit into
libracoder:mainfrom
meutex:meutex/1794928290-log-memory-kill-numbers
Closed

meutex wants to merge 1 commit into
libracoder:mainfrom
meutex:meutex/1794928290-log-memory-kill-numbers

Conversation

@meutex

@meutex meutex commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Problem

As a Kiri user running a Kubernetes cluster in the VM, I want a container that stops for memory to be logged with the figures that explain it — the guest's MemAvailable, the balloon size and the PSI — and attributed to the operation that caused it, so that I can tell a real out-of-memory kill from Kiri's own restart without guessing.

Change

Kiri's daemon currently logs 'container killed for memory' with no figures and no way to tell a real out-of-memory kill from a stop Kiri issued itself during a requested operation, so a Kubernetes node that dies twice in ten minutes cannot be diagnosed. This change makes every memory-driven stop explainable: the log carries the guest's MemAvailable, the balloon's size and the VM's target and ceiling, PSI, the exit code, and whether the kernel or Kiri made the call — while a stop that belongs to restartvm, kiri k8s set or a recreate is logged as that operation. It also guarantees the balloon deflates fully before Kiri stops anything for memory, which gives a cluster squeezed by the balloon (the #210 shape of the problem) every available byte before a kill.

  • When Kiri stops a container for memory, the daemon log line carries, in the same event, the guest's MemAvailable (MiB), the VM's memory target and ceiling (MiB), the balloon size (ceiling minus target, MiB), the memory PSI (at minimum the memory pressure full avg10 the guest reported), the container's exit code, and killed_by=kernel or killed_by=kiri.
  • killed_by is decided from evidence, never from the exit code: the guest's Exited carrying its OOM flag (or the guest reporting an OOM kill in the container's cgroup) logs killed_by=kernel; a stop Kiri issued logs killed_by=kiri. A bare exit code of 137 or 255 never makes a stop a memory kill.
  • A container stopped as part of a requested operation — Settings > Resources changing the VM (op=restartvm), kiri k8s set, or any recreate — is logged with that operation named in the message and keeps its op id, and does not carry the 'killed for memory' message or the memory fields unless the guest reported OOM or memory pressure at that moment.
  • Before Kiri stops any container for memory, it first relieves the guest: the balloon is deflated so the guest's target equals the VM's ceiling, and the relief is logged with the old and new target. A stop is only reported as a memory stop when the log shows target == ceiling at that moment.
  • The steady-state balloon policy is unchanged by this change: MEMORY_HEADROOM, MEMORY_FLOOR and MEMORY_GROW keep their current values, and an idle or lightly loaded VM still hands the same memory back to macOS (the daemon-memory figure in the Speed check does not grow).
  • On a Linux host in the VM (the same kiri-init binary), nothing regresses: the path compiles, no balloon is claimed in any log line, no pressure-driven relief is attempted, and tests/linux-host.sh still passes.
  • cargo fmt --check, cargo clippy and cargo test pass, and tests/e2e.sh passes against a running kiri daemon on macOS; the Kubernetes path is exercised with K8S=1 tests/e2e.sh if it runs in reasonable time. python3 scripts/ui-test.py still passes with no new entry in tests/ui/parity-allow.txt.

Testing

cd /code && cargo fmt --check 2>&1 | grep -E "^Diff in" | sort -u
exit=0

Fixes #474

Carry MemAvailable and PSI full avg10 on MemoryPressure, bump PROTOCOL
to 6, log the readings behind a memory kill (with killed_by=kernel vs
kiri from evidence), deflate the balloon before reporting a macOS memory
stop, and attribute operation-issued stops to their op.
@libracoder

Copy link
Copy Markdown
Owner

@meutex thank you for this pull request. Before we can review and merge it, its commits need to be signed and show as Verified on GitHub. Right now they are not.

To fix it:

  1. Set up commit signing with a GPG or SSH key, and add that key to your GitHub account as a signing key (an SSH key added only for authentication does not count): https://docs.github.com/en/authentication/managing-commit-signature-verification
  2. Sign the existing commits again, for example git rebase --exec 'git commit --amend --no-edit -S' main
  3. Force-push the branch: git push --force-with-lease

It also conflicts with main at the moment, so please rebase onto the latest main in the same push.

We only merge pull requests where every commit is verified. Once that shows, we will pick it up.

@meutex
meutex force-pushed the meutex/1794928290-log-memory-kill-numbers branch from f5dd684 to 3932e4a Compare November 21, 2026 03:55
@meutex

meutex commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Hi @libracoder , thanks for the clear instructions! I’ve set up commit signing, re-signed all the commits, and force-pushed the branch. Every commit should now show as Verified. Let me know if anything else is needed for the review.

@libracoder

Copy link
Copy Markdown
Owner

Landed in #803 (ba01465) with credit preserved. Thank you!

@libracoder libracoder closed this Nov 23, 2026
@meutex

meutex commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @libracoder

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

Labels

engine/API enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The Kubernetes node is killed "for memory" with no reason logged, so a real out-of-memory kill can't be told from Kiri's own restart

2 participants