VFS and Block Layer Latency Histograms on a VPS with eBPF

The Signal You Actually Want

Web server logs record request outcomes, not request behavior. A write that completes in 3 ms is fine; the same write that takes 900 ms is a problem, and neither outcome tells you whether the storage layer was slow or the filesystem was waiting on a lock. Measuring behavior means instrumenting the filesystem and block layers, which on Linux means eBPF probes on the virtual filesystem calls and the block request queue.

On a VPS this is especially useful because the guest cannot see the host’s storage counters. Everything you know about latency above the virtualization boundary comes from your own measurements. A small set of delta latencies, recorded per operation and per mount point, replaces guesswork with a distribution you can compare across time and across providers.

Two Hooks, Two Different Questions

The virtual filesystem probe answers “how long did this operation take from the application’s point of view”, including time spent waiting on locks, page cache, and journal commits. The block layer probe answers “how long did the device take to service this request”, excluding filesystem behavior entirely. Recording both lets you separate an application problem from a storage problem in one step.

Attach the probes to entry and exit of the relevant calls, capturing a timestamp on entry in a hash keyed by process identifier and thread, then computing the delta on exit. Store the delta in a log-scale histogram rather than a running average. Averages hide the tail, and the tail is what users experience.

Building a Latency Histogram Without Losing Data

A simple counter per bucket is enough. Choose buckets in microseconds with roughly logarithmic spacing: under 10, 10 to 50, 50 to 250, 250 to 1000, and a catch-all above that. Per-CPU maps make the increment cheap, and the reader aggregates buckets across CPUs on export. The result is small, cheap to store, and directly comparable between two machines and two weeks.

bpftrace -e 'kprobe:vfs_write { @s[tid] = nsecs; }
kretprobe:vfs_write /@s[tid]/ {
  @us = hist((nsecs - @s[tid]) / 1000); delete(@s[tid]); }'

Run the histogram for two minutes at a known workload level and compare the 99th percentile across candidates rather than the mean. Providers with aggressive burst credits often show an unremarkable mean and a tail that runs an order of magnitude worse, which is precisely the pattern you are trying to detect.

Splitting by Operation Type

Reads, writes, opens, fsyncs, and metadata lookups have different latency profiles and different sensitivity to host contention. A machine with fast sequential writes can still have terrible fsync latency if the virtual disk acknowledges writes before they are durable, or if the journal is being flushed against a throttled device. Keying the histogram by operation name keeps those distributions separate.

For database workloads, the fsync histogram matters more than the write histogram. A commit that returns quickly while the underlying fsync takes 50 ms is a durability lie, and only the fsync measurement shows it. Monitor both and compare their relationship: a widening gap between write latency and fsync latency usually means the write path is absorbing buffers while the device falls behind. On a virtual disk backed by a network volume, that gap is often the first symptom of a host-side problem that will later appear as a stalled query.

Attributing Latency to Processes and Mounts

Two workloads on the same instance share a virtual disk, and a burst of writes from one will show up as latency in the other. Recording the process name and the mount point with each sample lets you attribute the tail to a specific process instead of guessing. This is the difference between concluding “the disk is slow” and concluding “the backup job at 02:00 saturates the disk for eleven minutes”.

Mount attribution is straightforward because the path’s vfsmount is available at the probe site. Process attribution is cheap if you record it at entry rather than resolving it on exit, since the task may have exited by the time the operation completes and the process identifier may have been recycled.

Watching the Tail Over Days, Not Minutes

A single hour of measurement can miss periodic behavior entirely: cron jobs, log rotation, snapshot creation, and provider-side maintenance all produce short, regular spikes. Exporting the histogram once a minute for several days costs almost nothing and reveals patterns that no benchmark run will. Plot the 99th percentile over time and look for a regular shape rather than a noise floor.

Keep the collection footprint small by aggregating per CPU and exporting deltas rather than raw events. A collector that keeps every sample will double the memory footprint of a small instance and eventually become the problem it was meant to diagnose. One histogram per operation, exported on a fixed interval, is sufficient. Rotate the exported files on the same schedule as the rest of your logs so that a storage problem does not quietly fill the disk you are measuring.

What the Numbers Let You Decide

With per-operation p50 and p99 in hand, you can size an instance honestly: if the fsync tail at your application’s write rate is already above your service target, no amount of application tuning fixes it, and the correct response is a plan with a faster disk or a different provider. If the tail is fine and the application is still slow, the bottleneck is elsewhere and the storage upgrade is wasted money.

Run the same probe set on two candidates, then compare the percentile shape rather than the headline throughput. The storage specifications for each plan give you the device type to overlay on your measurements, and the published latency figures provide a reference distribution when you need a sanity check on a new host.

Leave a Reply