Lesson 11 / Unit 3: Agent Workers
Memory Accounting
RSS said 404 MB per call and pointed us at memory-optimised machines. PSS said 122 MB. Why a forkserver pool fools RSS, and a script that measures it right.
Each job process on our Python agent worker showed roughly 400 MB of RSS. One process per concurrent call, 150 concurrent calls: 60 GB. That meant several machines, and it meant shopping for memory-optimised ones.
The real figure was 122 MB per call. It changed which instance family we bought.
The numbers below are saved case-study results. The original raw run bundle is not included here, and this review did not rerun the fleet experiment.
Shared Memory
RSS charges every process for every resident page it can see, including pages it shares with its siblings. In a forked pool some pages can be shared, so summing RSS counts the same memory once per process.
The Linux Python worker configuration in this case used a forkserver. One parent imports the heavy libraries once. Every job process is then forked from it and shares those pages copy-on-write (Python’s multiprocessing start methods describe the forkserver model). Look at any one job process and RSS reports the full weight of those libraries. Sum RSS across a pool of forty and you have counted the same memory forty times.
That is where “roughly 400 MB per process” came from. Most of it was the parent’s pages, seen through each child.
Every job process maps the same library pages from the parent. RSS charges each process for all of them, so we read 404 MB per call; PSS splits them between the sharers and gives 122 MB.
Proportional Memory
PSS divides each shared page by the number of processes sharing it. Add up PSS across the pool and every page is counted exactly once, which makes it the number that answers “what does one more call cost?”
The kernel exposes it per process in /proc/PID/smaps_rollup, right next to the Rss line. The kernel’s proc documentation defines it as “the count of pages it has in memory, where each page is divided by the number of processes sharing it.” smaps_rollup sums the per-mapping values from smaps into one block, so you read one line per process instead of parsing hundreds of mappings.
Process Sampling
Walk the tree from the worker’s main PID, read Pss and Rss from each process, and add them up. The forkserver and every job process are descendants of the worker, so a recursive child walk catches all of them.
#!/usr/bin/env bash# usage: pss-tree.sh <worker pid> (run as the worker's user, or with sudo)root=$1descendants() { local p; for p in $(pgrep -P "$1"); do echo "$p"; descendants "$p"; done; }for pid in "$root" $(descendants "$root"); do fds=$(ls "/proc/$pid/fd" 2>/dev/null | wc -l) awk -v fds="$fds" '/^Rss:/ {r=$2} /^Pss:/ {p=$2} END {print r, p, fds}' "/proc/$pid/smaps_rollup"done | awk '{n++; rss+=$1; pss+=$2; fds+=$3} END {printf "%d processes RSS %.0f MiB PSS %.0f MiB fds %d\n", n, rss/1024, pss/1024, fds}'The kernel reports these values in KiB, so dividing by 1024 yields MiB. The older saved table uses the label MB; preserve that provenance when comparing results. Processes can exit during sampling, so treat failed reads as missing data, not zero. The script also counts open file descriptors per process, because you will want that number too (more on it below). Run it on an idle worker and you get the baseline. Run it under load and the gap between the RSS and PSS columns shows you how much of the pool is shared.
Resource Slopes
One reading mixes the fixed baseline with the marginal cost of calls. Sample repeatedly while the call count changes and fit a line: the slope is the per-call cost and the intercept is the baseline.
We sampled every ten seconds through a ramp to 40 concurrent calls, logged active_calls,pss_mb pairs from our own call counter and the script above, and fitted them by least squares against active call count. Python’s standard library does the fit:
import csv, statistics
calls, pss_mb = zip(*((float(c), float(m)) for c, m in csv.reader(open("samples.csv"))))fit = statistics.linear_regression(calls, pss_mb)print(f"{fit.slope:.0f} MB per call, {fit.intercept:.0f} MB baseline")Here is what came out:
| Metric | Value |
|---|---|
| Idle baseline | 11 processes, PSS 1,549 MB |
| Per concurrent call, PSS | 122 MB |
| Per concurrent call, RSS | 404 MB |
| Per concurrent call, file descriptors | 38.4 |
The same slope method applies to every resource in a voice fleet. Per-call resource accounting puts the memory figure next to ports and descriptors.
Instance Sizing
At 122 MB per call, 40 concurrent calls add roughly 4.9 GB, plus the 1.5 GB idle baseline, against the memory budget. Memory stopped being the constraint. CPU became it.
So the machine to buy was compute-optimised rather than memory-optimised. The RSS figure pointed at the opposite family, and at several more machines than we needed. That is the real cost of the wrong metric: it does not stay in a report, it turns into a purchase order. The per-call slopes feed straight into the capacity model and the cost per minute later on.
Descriptor Limits
The descriptor slope mattered as much as memory. At 38.4 per call, a hundred concurrent calls want roughly 3,840 descriptors, and our effective soft limit was 1,024; inspect yours with /proc/PID/limits.
First inspect each process, not only the tree total: LimitNOFILE is a per-process limit. In the affected service the effective limit needed raising. The directive is LimitNOFILE= in the [Service] section (see systemd.exec). Size each process limit from that process’s descriptor use and headroom. Do not apply the sum across independent job processes to one process’s limit.
Now the limit has a measurement behind it.
Before you size a fleet
Run the script on an idle worker, then sample it through a ramp and fit the slope. If your per-call figure came from RSS on a forked pool, throw it away and measure again with PSS.
Next: the job process that stayed resident at 439 MB after the caller hung up, and why the leak showed up on a different machine.
Common questions
How much memory does a LiveKit agent call use?
On the Python worker we measured, one concurrent call cost 122 MB of PSS on top of an idle baseline of 1,549 MB across 11 processes. Reading RSS instead gave 404 MB per call, more than three times too high, because RSS counts pages the job processes share with the forkserver parent.
What is the difference between PSS and RSS?
RSS counts every resident page a process can see, including pages shared with other processes. PSS divides each shared page by the number of processes sharing it, so summing PSS across a process pool counts shared memory once.
How do I measure PSS for a whole process tree on Linux?
Read the Pss line from /proc/PID/smaps_rollup for the parent process and every descendant, then add them up. Sample that total against active call count over a ramp and take the slope to get the marginal cost per call.
We do this work for teams shipping voice agents: Self-Hosted Voice Agent .
Edit this lesson on GitHub ↗Discussion
Questions, corrections and war stories welcome. Sign in with GitHub to comment.