Following a system call
easy 7 minA library translates a call into the platform’s kernel interface.
Video lesson · Chapter 1 of 8
When a read has nothing to return
Theory
Read or listen first. About 2 min.
What it is
A library translates a call into the platform’s kernel interface. A blocking read can put the caller to sleep until bytes are available. A nonblocking read can return EAGAIN instead. The caller must decide when to try again. A readiness interface lets a thread wait on many descriptors and make progress on the ones with work.
Why it exists
After an event-loop change a service burns one core even while receiving few messages. Trace a short interval on one representative worker. Repeated immediate EAGAIN results place the time in a busy loop. A single long read would support a different diagnosis. Bound the observation window because tracing itself can change timing.
A long blocking read with no data suggests the producer or the path to it; repeated immediate returns suggest a busy consumer.
When a read has nothing to return
Inside this chapter
- The System Call Interface internals
- A wrapper arranges a call number and arguments according to an architecture-specific ABI. The kernel validates arguments, performs the operation and reports a result or error.
- Tracing syscalls with strace
- A short syscall trace can distinguish an immediate error from an operation that blocked. Compare counts, elapsed times and the specific worker involved before drawing conclusions.
- Blocking vs. Non-blocking system calls
- A blocking call may sleep awaiting progress. Nonblocking mode reports when it cannot progress now. A partial successful return still requires the caller to handle remaining bytes.
- The vDSO (Virtual Dynamic Shared Object) optimization
- The vDSO maps kernel-supplied routines into userspace. Supported clock queries can read shared timekeeping data there and avoid a full kernel transition.
- Common syscalls: read, write, open, close, mmap
- Open returns a descriptor, read and write transfer up to the requested count, and close releases a reference. A file mapping can remain valid after the original descriptor is closed.
- seccomp: System Call Filtering
- A seccomp filter restricts the syscalls a process may make. When a deployment unexpectedly loses an operation, compare the workload’s documented requirements with its active confinement policy.
How it works
Step through the idea, or press Play.
Format in userspace. In this example a flushed stdio buffer reaches libc’s write wrapper.
Understand it
Worked incident
When a read has nothing to return
| Observation | Read attempts per message (attempts) | Interpretation |
|---|---|---|
| Baseline | 1 | A queued message is consumed in one read. |
| Incident | 40 | The loop retries the empty socket forty times before the message arrives. |
| After intervention | 2 | Wait for readiness and retry after notification. Handle a second EAGAIN because readiness may change. |
Choose the next investigation
Summary
The whole topic in one minute.
Another consumer may have drained the socket. A nonblocking handler needs a way to resume waiting without occupying a core.
A long blocking read with no data suggests the producer or the path to it; repeated immediate returns suggest a busy consumer.