6.1810 2026 Lecture 16: OS network latency Reading: "Tails of the tail: hardware, OS, and application-level sources of tail latency" (SoCC'14) this lecture: O/S net latency important metric for cloud providers: tail latency hard to get good tail latency under high load sources of tail latency application: concurrency model, single/multi-queue os: threads, interrupts hardware: NUMA [active area of research] Linux network software structure +----------------------------------+ user | application | | read() write() | +------^-------------------+-------+ ------------------------- | ----------------- | ----------------- | copy, wakeup | copy kernel +------+-------+ | | socket queue | | +------^-------+ | | enqueue | +------+-------------------v-------+ +--------->| interrupt handler, TCP/UDP/IP | | +------^-------------------+-------+ | | | | +------+-------+ +------v-------+ | | rx ring | | tx ring | (in RAM) | +------^-------+ +------+-------+ ------- | --------------- | ----------------- | ----------------- | interrupt | DMA | DMA h/w | +------+-------------------v-------+ +----------+ NIC | +------^-------------------+-------+ | | rx packets tx packets NIC DMAs received packet into rx ring NIC interrupts kernel kernel processes packet (TCP, UDP, etc.) enqueues packet buffer (a pointer, no copy) on destination socket queue wakes up application's read read copies data from kernel buffer to user memory application sends packet write copies data from user memory to a kernel buffer inserts packet into tx ring NIC sends packet how does network software structure affect performance? let's focus on high-performance network servers e.g., memcached, an in-memory key/value storage server high request rate short requests / responses set/get take ~1usec (see 3.1.2) (the application isn't the bottleneck) lots of clients, lots of potential parallelism performance metrics low latency (under low load and high utilization) avg latency tail latency latency under low load sum of a sequence of steps to process packet network speed-of-light and switch round-trip time interrupt queue operations sleep/wakeup system calls inter-core data movement RAM fetches what are the relevant h/w limits? (tail paper's testbed, Sec 3.3) 2 x 6-core 2.27 GHz Xeon L5640 (12 cores, 2 NUMA nodes), 10 Gb/s NIC 10 gigabit ethernet with memcached's 64-byte keys, 1024-byte values: ~1 million responses/second fill the link (~10,000 bits per response) one core would have to do ~1 million req/s (1 usec/request) to fill the link memcached's measured performance 125,000 requests/second per core (Sec 3.2) measured: peak throughput on a single core = 8 usec average/request need most of the machine's cores to fill the link application-level processing is only 1-2 usec (Sec 3.1.2) the rest is the kernel: interrupts, TCP/UDP, syscalls, wakeups latency under high load cloud providers want to run machines at high load to save money if cloud provider runs machines at 10% load that is expensive you need 10x the machines compared to running at 100% load latency under high load is largely determined by wait time -- queuing bursty arrivals increase queuing time a number of client requests arrive together bursty service times increase queuing time the work varies for requests, or the work is interrupted and then later resumed structural problems can increase queuing time e.g., load imbalance in sharding connections across cores core reading from low-traffic connection low latency core reading from high-traffic connection high latency latency under high load is hard to reason about it isn't the average req latency that determines perf; it is the unlucky ones how do we measure latency under high load? queuing makes latency a distribution, not one number most requests are fast; a few wait behind bursts, daemons, interrupts average hides this => look at the tail (99%, 99.9%) tail latency https://cacm.acm.org/research/the-tail-at-scale/ (cacm 2013) famous Google paper; changed how people measure latency focus on latency at tail Memcached on Linux see Fig 5(b) "Standard linux": 1 core, 80% load typical (median) response is ~100 usec (where curve crosses y = 0.5) but 1 in 1000 takes >= 5 msec 999 out of 1000 requests take < 5 msec and 1 >= 5ms 99.9% latency is 5 msec; curve crosses y = 10^-3 the tail is ~50x the median! user requests may fan out to many machines facebook uses many memcached machines if each user req fans out to 100 memcached servers, ~10% of user requests wait >= 5 msec (1 - 0.999^100) important that 99.9% tail latency is low demo: tail latency in xv6 udpecho server on xv6 (net lab), 1 core; each request ~2 msec of work host sends Poisson (random) arrivals, open loop; plots CCDF like the paper CCDF = complimentary cumulative distribution function ./lat-noload.sh: one request at a time, no queuing mean latency ~2 msec; also measures peak throughput (~480 req/s) ./tail-util.sh: load 0.5 vs. 0.8 of peak avg p99 p99 vs. no load no load ~2 msec ~4 msec 1x 0.5 ~3.5 msec ~10 msec ~2.5x 0.8 ~8 msec ~30 msec ~7.5x even at 0.5 load, the tail grows ~2.5x requests wait behind random bursts of arrivals queuing theory: wait grows like 1/(1 - load) e.g., M/M/1: p99 ~= 4.6/(1 - load) service times (9x at 0.5, 23x at 0.8) datacenter operators often run machines at low load to protect tail latency to provision for peak, not average, load load follows daily/weekly cycles and spikes (events, launches) to achieve high utilization fill spare capacity with batch (non-latency-sensitive) work ./tail-bg.sh: a daemon adds ~5% load (26 msec of CPU every ~500 msec) p99 ~10 -> ~30 msec at load 0.5 adding background work needs a priority scheduler (rt: p99 back to ~10 msec) with rt, even a 20% daemon barely matters (p99 ~10-12 msec) ./tail-queue.sh: 2 cores, load 0.8 p99: 1 core ~30 msec (tail-util.sh), shared queue ~13-16 msec, queue per core ~26-28 msec with multiple cores, a shared queue gives a lower tail than a queue per core single-queue systems: idle cores absorb bursts multi-queue: an idle core can't take requests from other cores' queues paper measures tail latency w. 3 Linux applications structure chosen by programmer for: convenience (e.g., threads) throughput/scalability event-based instead of threads connections partitioned/sharded over cores the structures represent common ways server applications are written application structures null-rpc multithreaded, one thread per TCP connection conn1 conn2 conn3 one TCP socket per connection [sock] [sock] [sock] | | | thread1 thread2 thread3 each thread blocks in read() | | | packet arrives => thread runnable +--------+--------+ | OS run queue (runnable threads) <= one shared queue / \ core0 core1 any core runs any runnable thread simple to program: blocking read/write per connection (like Apache) memcached one thread per core, each an event loop no thread per connection: few threads, few context switches UDP: 1 socket one UDP socket [ req req req ... ] <= one shared queue / \ recv recv all workers read the same socket | | worker0 worker1 core0 core1 TCP: connections partitioned over the cores (main thread accepts, hands each conn to one worker) each worker owns its connections: no sharing => good for scalability conn conn conn conn [s] [s] [s] [s] \ | / | epoll epoll <= one queue per worker | | worker0 worker1 core0 core1 nginx one worker thread per core calling epoll event loop to avoid a thread per connection listening socket [ new connections ] / \ accept accept any worker may accept a new conn | | [s][s] [s][s] ...but the conn then stays with it | | epoll epoll <= one queue per worker | | worker0 worker1 core0 core1 long TCP connections: like memcached TCP (multi-queue) short connections: new conns spread across workers => ~single queue application structure impacts tail latency structure wasn't chosen for tail latency single queue: any core can serve any request tail improves with more cores (memcached UDP Fig 7(d)) multi-queue: each request can only be served by its connection's core n cores behave like n copies of 1 core: same tail as 1 core (Fig 7(b), 7(c)) the scalable choice (partitioning connections) hurts the tail threads: OS scheduler decides order, not FIFO (Fig 6) sources of tail latency back-ground processes (fig 5) process must wait until the background process finishes its time slice time slice is a few msec small of extra background work has a big impact on tail one time-slice delay hits all reqs arriving in that interval (green vs. dedicated core) null rpc seems more impacted than memcached; maybe non-fifo effect nice: helps null-rpc but not memcached null rpc benefits because it is runs one OS thread per connection CFS makes those threads more important than the background threads memcached has one event-loop thread nice only gives longer time slices; server still can't preempt daemons so it must still wait sometimes until background processes finish their time slice scheduling (priority, FIFO, non-FIFO) strict priority helps all applications w. background processes (fig 5) FIFO is good for null-rpc; the only one affected by OS scheduling (fig 6) (but head of line blocking, which doesn't show up in these applications) multicore (fig 7) helps null rpc, memcached UDP, nginx short single queue systems; idle cores absorb bursts doesn't help for memcached TCP and nginx long multi-queue systems: one queue per core an idle core can't take requests from other cores' queues interrupt handling (fig 8) by default interrupts spread over all cores (irqbalance) interrupts preempt app threads: varying service time later request's network processing runs before earlier req finishes: not FIFO fix: dedicate cores to interrupt handling how many? 1 interrupt core and 3 app cores 1 core can handle 350K packet interrupts/s each thread can process 120K req/s 350K / 120K ~= 3 NUMA multiple processors (sockets), each with its own memory memcached w. 8 threads across 2 sockets: tail barely better than 1 core (fig 9 "NUMA Unaware" vs. fig 7(d) "1 CPU Measured"), ~10x worse than NUMA aware memcached is memory intensive Linux allocates a process's memory from one node half the threads make cross-node memory references, which are expensive fix: 2 memcached instances, each bound to its node's memory (numactl) summary high-performance networking is hard! low latency and high utilization are hard to achieve important metric: tail latency to capture latency under high load much of the tail comes from the OS e.g., 99.9% latency 5 msec -> <1 msec with real-time priority (Fig 5(b)) much progress/research after 2014 map NICs into user space to avoid syscall overhead dedicate cores to applications rather than time share dedicate cores to network processing (kernel-bypass)