357.4. kdos-oomd-0.2.0
PSI-triggered memory-pressure killer that spares the desktop
| Version | 0.2.0, release 1 |
| Source tree | src/daemons/kdos-oomd/ |
| Phase | 50_desktop (Desktop), 78 of 92 in build order |
| Licence | MIT |
| Homepage | https://github.com/kunaldawn/kdos |
| Build time | 0.1 SBU |
| Installed size | 110.0 KiB in 1 file |
| Recipe | src/daemons/kdos-oomd/kpkgbuild |
About
kdos-oomd kills the process that is starving the machine of memory before the desktop freezes. The kernel’s own out-of-memory killer fires only when an allocation fails, which on a machine with swap can be long after the session stopped responding.
It writes a trigger into the kernel’s memory-pressure file (Pressure Stall Information, PSI), /proc/pressure/memory, and sleeps until the kernel reports a stall. A stall alone kills nothing; memory must also be short:
MemAvailableat most a tenth ofMemTotal, orMemAvailableat most a quarter ofMemTotalwith swap exhausted (SwapFreeat most 2% ofSwapTotal).
It then kills one process: the largest in a box over its declared memory budget first, otherwise preferably a boxed one, and never KWin, plasmashell, the login, the audio session or the other programs the session is made of. It frees the victim’s memory at once with process_mrelease(). It scans for a victim at most once every ten seconds, counted from a firing that finds memory short. How it chooses, and its socket, are in kdos-oomd.
The port is KDOS’s own C, with no upstream source. build.sh compiles main.c with the sources of libkbase and libkproc and nothing else. The daemon reads the process table from /proc itself; libkproc names the box a process belongs to, by walking its parent chain up to podman’s conmon, and moves the /proc root for the --fixture mode. See How the libraries are built.
Why it is in KDOS
The comment above it in script/phases/50_desktop/packages.txt:
Wedge protection under memory pressure. Blocks on a /proc/pressure/memory PSI trigger and SIGKILLs + process_mrelease()s the largest attributable offender — preferring boxed apps, never the Plasma session itself — at most once per ten seconds. Same shape as kdos-powerd: root, foreground under ksvc, SO_PEERCRED socket for
kdos doctor.
Sources
None: the port builds from its own directory, /kdos/src/daemons/kdos-oomd.
Dependencies
Depends on: nothing a recipe names.
Required by (1): kde-plasma.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/50_desktop/phase.env && kpkg install kdos-oomd'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /kdos/src/daemons/kdos-oomd.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/kdos-oomd
SRC=$SRC_ROOT/kdos-oomd-0.2.0
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
LIBS="$PORT_SRC/../../libs"
gcc $CFLAGS -O2 -std=gnu11 -D_GNU_SOURCE -Wall -Wextra \
-I"$LIBS/libkbase" -I"$LIBS/libkproc" \
-o kdos-oomd "$PORT_SRC"/main.c "$LIBS"/libkbase/*.c "$LIBS"/libkproc/*.c $LDFLAGS
install -Dm755 kdos-oomd "$PKG/usr/sbin/kdos-oomd"
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/kdos-oomd, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf kdos-oomd-0.2.0-1.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/kdos-oomd, and runs the triggers its paths call for.
Other files in the port directory, read by build.sh: main.c.
Contents
Installed programs: kdos-oomd.
Notes
The daemon needs PSI in the kernel. /etc/init.d/57_oomd.sh skips it at boot when /proc/pressure/memory is not writable, and the machine then has only the kernel’s own killer. It is installed under one name only, with no client link. Its socket, /run/kdos-oomd.sock, answers ping, status and protected from root, from members of wheel and from a user with an active session at the machine, and nothing in the protocol names a process; kdos-oomd --explain <user> says which of the three admits a user.
The active session is elogind’s answer, sd_uid_get_state(), and libelogind.so.0 from elogind is opened when the first caller outside root and wheel connects rather than linked. elogind is therefore not a dependency: without it the daemon still starts and kills, and only that third tier is closed.
The protected programs are matched by process name, a list compiled into main.c, and any name that begins with pipewire is protected as well. A process name is the executable’s name cut at 15 characters, so each entry is the cut form (kwin_wayland_wr, kscreenlocker_g, polkit-kde-auth); kdos-oomd --protected prints the list. A session program renamed, or a new one added to the session, is a candidate victim until the list names it. Any other process is spared only when its oom_score_adj is -500 or lower, a value only root can set and which its children inherit.