AlgoViz

Crossing the kernel boundary

easy 7 min

An application executes ordinary instructions directly on the processor.

Video lesson · Chapter 1 of 8

Small writes, busy CPU

0:00 / 5:43

Theory

Read or listen first. About 1 min.

What it is

An application executes ordinary instructions directly on the processor. A privileged operation such as submitting device I/O goes through a kernel entry point. The processor changes privilege, the kernel validates the arguments and eventually returns to the caller. A mode transition can take place on the same thread. A context switch means that a different execution context is scheduled.

Why it exists

A video encoder starts writing each metadata field separately. The new version spends more CPU per frame although it emits exactly the same bytes. Count kernel entries for equal-sized frames and compare CPU time on a matched core. A scheduler counter answers a different question because it measures changes of running task.

If entries per frame remain constant, sample userspace encoding cost and compare bytes emitted.

Small writes, busy CPU

Encoder → System call entry (write buffer). System call entry → Output device (submit I/O)
Encoder → System call entry (write buffer). System call entry → Output device (submit I/O)

Inside this chapter

CPU Ring Protection Levels (Ring 0 vs Ring 3)
On x86 the kernel normally runs at ring 0 and applications at ring 3. The processor enforces privileged operations. Other architectures name their privilege levels differently.
The cost of Context Switching
Switching tasks saves and restores execution state. Disrupted cache or translation locality can cost more than saving the registers. Compare switches per completed operation at equal load.
User Mode vs. Kernel Mode transitions
A syscall enters privileged execution on behalf of the current thread. The thread can return directly without the scheduler choosing a different task.
Interrupts and Traps
An interrupt can arrive asynchronously from a device. A synchronous exception is tied to the executing instruction. A page fault is one such exception and can be handled successfully.
DMA (Direct Memory Access) and Zero-Copy
DMA lets a device transfer data to mapped memory. Zero-copy paths reduce data movement between software buffers. Page ownership, lifetimes and completion notifications still matter.

How it works

Step through the idea, or press Play.

Step 1 of 4

Describe a transfer. The driver maps the buffer for the device and supplies descriptors for the I/O.

Understand it

Worked incident

Small writes, busy CPU

ObservationKernel entries per frame (entries)Interpretation
Baseline1One buffered payload crosses the boundary once.
Incident64Sixty-four small writes transfer the same payload and repeat the entry work.
After intervention2A bounded buffer combines the fields into two writes. Confirm both frame latency and flush behavior.
Illustrative measurements from the same workload at three checkpoints.

Choose the next investigation

If entries per frame remain constant, sample userspace encoding cost and compare bytes emitted.

Summary

The whole topic in one minute.

The application chooses how often to call write. A longer slice may change scheduling but leaves the same number of entries for a frame.

If entries per frame remain constant, sample userspace encoding cost and compare bytes emitted.

Sources