Blog Io
Diagnosing pathological small-file performance on FreeBSD ZFS
Symptom
Updating the English TLDR cache created 7,447 small files from a 3.25 MiB archive and took about 81 seconds:
# time tldr --update
real 1m21s
The download completed quickly. The delay began at the extraction phase.
dozer was online, idle, and had no data, read, write, or checksum errors.
Storage was not the bottleneck
The same archive was tested on several targets:
| Target | Real time | User CPU | System CPU |
|---|---|---|---|
| malloc-backed UFS memory disk | 0.120s | 0.000s | 0.091s |
tlrc on malloc-backed UFS, including download |
1.85s | 0.06s | 0.16s |
dozer ZFS |
74.67-81.13s | 0.03-0.21s | 72.88-79.21s |
| ZFS pool on a malloc-backed memory disk | 91.98s | 0.00s | 91.59s |
RAM-backed ZFS was as slow as disk-backed ZFS, while RAM-backed UFS was fast.
This rules out the disks, pool topology, ARC size, SLOG, decompression, download
speed, and tlrc itself as primary causes.
System-call evidence
A direct truss -c profile of tlrc showed:
syscall calls cumulative time
close 7633 78.26s
openat 7584 0.95s
write 7488 0.06s
Each file close stalled for roughly 10.25 ms. tar reproduced the same delay,
so this was not specific to the Rust client.
Kernel lock evidence
lockstat identified the hot path as the global VFS vnode_list mutex:
close(2)
closefp_impl
closef
_fdrop
vn_closefile
vn_close1
vput_final
vdropl
vdbatch_process
vnode_list_mtx
About 88% of measured contention was in vdbatch_process() waiting for
vnode_list. Many individual spin intervals fell near 134 ms. The competing
kernel worker was vnlru_proc, the vnode reclamation process.
The host was running:
FreeBSD 15.1-RELEASE-p1
OpenZFS 2.4.2
This matches a reported FreeBSD 15 failure mode in which builds spend more
than 90% of their time in lock_delay with the same
vdbatch_process -> vdropl -> vput_final -> vn_close1 stack. Enabling
vfs.vnode.param.can_skip_requeue=1 reportedly restores performance
immediately:
The installed source tree contains a newer vdbatch_process() implementation
that uses mtx_trylock(&vnode_list_mtx) and skips the quasi-LRU requeue when
the global lock is contended. The running kernel exposes
vfs.vnode.param.can_skip_requeue, currently set to 0, and profiles as the
older blocking implementation.
Why were there so many vnodes?
The live counters were:
kern.maxvnodes: 1,103,460
vfs.numvnodes: 1,091,635
vfs.freevnodes: 456,120
vfs.wantfreevnodes: 275,865
kern.openfiles: 225,883
vfs.vnode.stats.created: 1,208,641,362
vfs.vnode.vnlru.recycles_free: 1,199,531,283
vfs.numvnodes was at about 99% of its configured limit. This made vnlru
walk and manipulate a global list containing roughly 1.1 million entries.
They were not all open files and they were not evidence of an unclean ZFS pool:
- 456,120 were already classified as free vnodes. These are clean, reclaimable VFS cache objects.
- Other non-free vnodes may be retained by the name cache, resident pages, current directories, executable mappings, mounts, or actual file descriptors.
- The name cache held about 807,568 entries and reported an 88% hit rate.
- There were about 225,883 live open-file table entries, far fewer than the total vnode count but still abnormally high.
Two long-running codex-tui processes explained most of the live descriptors:
| PID | Age at diagnosis | Vnode descriptors | Approx. unique files |
|---|---|---|---|
| 97438 | 2d 21h | ~110,000 | 97,256 |
| 6854 | 1d 23h | ~92,000 | 88,355 |
Together they held about 202,000 vnode descriptors to approximately 185,600
unique files, mostly beneath the code jail's filesystem. That behavior is
consistent with a file-descriptor leak or an intentionally retained but
unbounded file cache in those long-running clients. It is not an unclean
filesystem condition. Their per-process descriptor limit was 1,872,270, so
the leak could grow for a long time without hitting EMFILE.
The system-wide history also shows very high vnode churn: about 1.209 billion
vnodes created and 1.200 billion free-vnode recycles since boot. The two
clients supplied the immediate live-reference pressure; the name/vnode caches
then kept the global population close to kern.maxvnodes, exposing the VFS
locking regression during small-file workloads.
Remediation and verification
The direct runtime workaround for this kernel is:
sysctl vfs.vnode.param.can_skip_requeue=1
Re-run the archive extraction immediately afterward. The expected result is a drop from roughly 80-92 seconds toward the sub-second UFS baseline, with system CPU no longer dominating.
Safety and tradeoffs of skipping vnode requeues
This is a low-risk VFS cache-management workaround, not a ZFS durability setting. When the global vnode-list mutex is available, FreeBSD updates the quasi-LRU positions normally. When the mutex is contended, it skips that optional batch of position updates instead of making file-closing threads spin.
It does not skip file writes, flushes, closes, vnode reclamation, or ZFS transaction commits. It therefore does not weaken filesystem consistency or data durability. The tradeoff is only that vnode LRU ordering becomes less precise under contention. A vnode may consequently be reclaimed somewhat earlier or later than ideal, which could marginally reduce vnode/name-cache hit rates for some workloads. On this host that small theoretical cache-efficiency cost is preferable to the measured 80-92 seconds of global-lock spinning.
The workaround fixed the benchmark immediately. Newer installed FreeBSD source
also implements the same policy directly with mtx_trylock(), making the
nonblocking behavior strong evidence of the intended long-term fix rather than
a durability shortcut.
To persist it across reboots:
# /etc/sysctl.conf
vfs.vnode.param.can_skip_requeue=1
A secondary mitigation is to provide vnode headroom:
sysctl kern.maxvnodes=1500000
This may keep vnlru away from its thresholds, but it does not repair the
global-lock behavior and allows additional kernel memory consumption.
Increasing kern.maxvnodes also raises the derived wantfreevnodes target
(normally about one quarter of the maximum), permits the global vnode list to
grow larger, and makes full-list work potentially more expensive. If the
file-descriptor leak continues, the system will eventually approach the new
limit as well.
| Option | Primary effect | Main tradeoff |
|---|---|---|
vfs.vnode.param.can_skip_requeue=1 |
Removes the diagnosed lock bottleneck | Slightly less precise vnode LRU under contention |
Raise kern.maxvnodes |
Delays vnode-limit pressure | More kernel memory and larger vnode/cache targets |
| Restart descriptor-heavy processes | Releases about 202,000 live descriptors | Requires safely ending or resuming those sessions |
| Boot an updated kernel | Incorporates the nonblocking implementation | Upgrade and reboot required |
The preferred order is to keep can_skip_requeue=1, safely restart the
descriptor-heavy clients, monitor vnode and open-file counts, and boot an updated kernel. Raise
kern.maxvnodes only if legitimate workload demand still approaches the limit
after the descriptor leak is resolved.
The durable fixes are:
- Close or restart the descriptor-heavy
codex-tuisessions after preserving any required work, then verify thatkern.openfilesfalls substantially. - Upgrade or rebuild and boot a FreeBSD kernel containing the nonblocking vnode requeue implementation.
- Keep
vfs.vnode.param.can_skip_requeue=1as a boot-time setting if it is required by the installed kernel and survives workload testing. - Add monitoring for
kern.openfiles,vfs.numvnodes,vfs.freevnodes, and per-process descriptor counts so another leak is caught before reaching the vnode limit.
Useful checks:
sysctl kern.maxvnodes kern.openfiles \
vfs.numvnodes vfs.freevnodes vfs.wantfreevnodes \
vfs.vnode.param.can_skip_requeue
procstat -f PID | wc -l
fstat -p PID | wc -l
Do not kill the two sessions solely to clear cached vnodes: first confirm that they can be safely resumed or that their work is saved. Cached free vnodes are normal; the actionable abnormality is the approximately 202,000 live file descriptors retained by two processes.
Retained versus leaked descriptors
The kernel observations prove that the two codex-tui processes still owned
the descriptors; they do not prove why the application kept them:
- Retained means the application intentionally keeps descriptors open, perhaps as a cache. Retaining approximately 100,000 descriptors per process would still be excessive or insufficiently bounded for this workload.
- Leaked means the application finished using the files but failed to
close their descriptors because of a bug. The unused descriptors then
accumulate until the process exits or reaches
RLIMIT_NOFILE.
The observed pattern is strongly suspicious of a descriptor leak, but it should be described as "retained or leaked" until application-level evidence establishes intent. A simple confirmation is to sample the descriptor count while a session runs:
procstat -f PID | wc -l
sleep 300
procstat -f PID | wc -l
Continual growth that does not fall after file-processing tasks finish is strong evidence of a leak. Restarting the process should immediately release its live descriptors; cached/free vnodes may decline more gradually as the kernel reclaims them.
The impact can be contained per session with an inherited descriptor limit:
ulimit -n 8192
codex
This converts unbounded growth into an eventual EMFILE error; it is a guard
rail, not a fix. No environment variable controls FreeBSD's descriptor limit.
Final diagnosis
The slowdown did not occur merely because vfs.numvnodes reached the old
kern.maxvnodes value. Raising kern.maxvnodes was tested and did not improve
the benchmark because it neither shortened the existing million-entry global
vnode list nor changed the blocking requeue path. It may allow the list and its
memory use to grow further.
The failure required two conditions: unusually high vnode pressure, driven in
large part by the descriptors retained by the Codex sessions, and a FreeBSD 15
VFS implementation that blocks while performing optional quasi-LRU requeues
under the global vnode_list_mtx. can_skip_requeue=1 fixes the immediate
performance problem by avoiding that optional work when the lock is busy;
reducing the application descriptor count addresses the pressure that exposed
the kernel bug.
Finding which programs hold the most vnodes
fstat reports vnode-backed objects held by running processes. Rank processes
by the number of reported vnode references with:
fstat | awk '
NR > 1 && $5 ~ /^\// {
key = $2 " PID=" $3
count[key]++
}
END {
for (key in count)
print count[key], key
}
' | sort -nr | head -20
The first column is the number of vnode-backed entries reported for that
process. The command excludes sockets, pipes, kqueues, and other descriptors
whose MOUNT column is not a filesystem path. It includes ordinary file
descriptors plus references such as the executable, current directory, process
root, and jail root, so treat the number as vnode references rather than only
application-opened files.
Inspect all descriptors belonging to one suspicious process:
procstat -f PID
Count only its vnode-backed entries:
procstat -f PID | awk 'NR > 1 && $4 == "v" { count++ } END { print count + 0 }'
One process can open the same file more than once. To estimate the number of
unique filesystem objects held by a PID, deduplicate fstat output by mount
and inode:
fstat -p PID |
awk 'NR > 1 && $5 ~ /^\// { print $5, $6 }' |
sort -u |
wc -l
System-wide context is available from:
sysctl kern.openfiles kern.maxfiles \
vfs.numvnodes vfs.freevnodes kern.maxvnodes
kern.openfiles and per-process fstat counts are not expected to equal
vfs.numvnodes. Vnodes also belong to the name cache, resident pages,
executables, current directories, mount infrastructure, and the reusable free
vnode cache. The process ranking identifies live application pressure; it does
not account for every cached vnode in the kernel.