core21 min studyLesson 131 in Engineering

Cross a privilege boundary without changing tasks

Separate processor privilege, process identity, and scheduling while following a request into and out of the kernel.

On this page

What you’ll understand

  • Distinguish a change of processor privilege from a scheduler context switch.
  • Classify execution, blocking, and runnable delay on one request timeline.
  • Explain why a library API call need not correspond to one kernel entry.

A service reads a request, obtains a timestamp, updates an in-memory structure, and writes a reply. Some of those operations may require kernel execution; others can complete entirely in user space. Understanding the boundary lets you ask a sharper performance question than whether an operation is expensive. Identify whose thread is executing, what privilege it has, and whether it is running or waiting. These three questions describe different aspects of the same timeline.

Privilege governs permitted operations, not task identity

A processor's privilege mechanisms restrict operations such as configuring address translation or directly controlling devices. Ordinary application instructions execute with user privileges; the operating system establishes controlled entry points for requesting protected services. A transition through an allowed entry point does not grant an application unrestricted access to kernel code. The kernel still interprets arguments, checks access, and decides whether to perform an operation. Hardware privilege is also distinct from an operating-system account: an application running under a privileged account still normally executes its application instructions in user mode.

Imagine thread A requesting an operation that completes immediately. The processor can enter the kernel, execute on behalf of A, and return to A's next user instruction without selecting thread B. That is a privilege transition with no scheduler switch to a different task. Conversely, a scheduling decision can switch between two tasks while execution is already inside the kernel. Keep separate counts for boundary crossings and task switches. Calling both events a context switch hides which mechanism a trace actually measured and encourages unjustified performance comparisons.

Ntransitions=Nuser→kernel+Nkernel→userN_{\mathrm{transitions}}=N_{\mathrm{user\to kernel}}+N_{\mathrm{kernel\to user}}
The N quantities count observed privilege-boundary events and are dimensionless. An uninterrupted, completed user-to-kernel-to-user visit contributes two transitions, but not necessarily any scheduler task switch; exceptional termination can leave a visit without a return.↔ Scroll wide equations horizontally.

Follow time through running and waiting states

A blocking request adds another distinction. When an operation cannot proceed, the calling thread may sleep and another runnable thread may be selected. After the dependency completes, the original thread can become runnable without immediately receiving a processor. The eventual return to user space may therefore include kernel CPU service, off-CPU blocked time, and runnable delay. A long elapsed system-call duration is not evidence that the CPU executed kernel instructions throughout that interval. This matters when a slow storage dependency is mistakenly treated as a processor bottleneck.

Build a timeline with mutually exclusive categories for the thread being investigated. While it runs, label user execution or kernel execution; while it does not run, distinguish waiting for a condition from waiting for scheduling. Define how interruptions are charged before adding the intervals. A hardware interrupt can consume a CPU while the application's thread identity remains associated with that CPU, but its handler is not the application making progress. Accounting tools may present these categories differently, so compare definitions before comparing their percentages or drawing conclusions about work that should be optimized.

Twall=Tuser+Tkernel+Tblocked+Trunnable+TotherT_{\mathrm{wall}}=T_{\mathrm{user}}+T_{\mathrm{kernel}}+T_{\mathrm{blocked}}+T_{\mathrm{runnable}}+T_{\mathrm{other}}
All T terms have units of seconds over one explicitly partitioned request interval. Other covers interruptions or unattributed intervals excluded from the first four categories; categories must be disjoint, and this identity is accounting rather than a scheduling model.↔ Scroll wide equations horizontally.

Observe the implementation behind the API

A library call is a source-level interface, whereas a system call is an interface to the kernel. A buffered library write may only copy data into a user-space buffer until a later flush. Some time-query APIs can use a kernel-provided vDSO mapping to read suitable information without a fresh kernel entry. Other wrappers may perform several operations or choose a fallback path. The mapping depends on the function, architecture, library, and running kernel. Trace the actual execution when the distinction matters rather than counting function names in source code as system calls.

Use the queue lab to separate waiting work from the mechanism that admits it. Compare two separate fixed-rate runs. For each run, press Reset, re-set both rates, predict the backlog direction, then press Play or Step. First use arrival rate 4 and service rate 3 to inspect growth; reset again and set arrival rate 2 and service rate 3 to inspect a fresh draining run. The second run starts from four jobs, not the first run's final backlog. This fluid model starts with four jobs and does not represent privilege modes, interrupts, or a scheduler. Its useful connection is narrower: an observed delay can increase because work waits longer, even when a single operation's CPU service is unchanged. Test that hypothesis with CPU-time and off-CPU measurements instead of inferring it from elapsed latency alone.

Account for a traced request

A fictional 10 ms request contains 4 ms of user execution, 1 ms of kernel execution, 3 ms blocked on a device, and 2 ms runnable but not running. It makes six completed kernel visits; two visits cause A to sleep and another task to run. Ignore interrupts and migration.

  1. Add disjoint intervals: 4 + 1 + 3 + 2 = 10 ms elapsed. The thread receives 5 ms of CPU execution; its remaining 5 ms are off CPU.

  2. Six completed user-to-kernel-to-user visits contribute twelve privilege transitions. The four visits that do not sleep can return directly to the same thread.

  3. For the two sleeps, the simplified trace includes at least a switch away and a later switch back for each visit, giving four task-switch events involving A. Other tasks can switch among themselves, so these observations do not determine the machine-wide total.

The result

The request has twelve observed privilege transitions and four specified switches involving A. Those counts describe different events; neither makes the 3 ms device wait into kernel CPU work.

Explore the model

Interactive lab

Interactive lab · queue

Waiting can change without a new boundary cost

For each separate fixed-rate run: Reset, re-set both arrival rate λ and service rate μ, predict, then Play or Step. Compare λ=4, μ=3 with a fresh λ=2, μ=3 run, each starting from four jobs. Editing a rate retains elapsed time; it is not a live rate switch. The fluid model does not show privilege transitions, CPU dispatch, or measured system-call costs.

Waiting can change without a new boundary costFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forSeparate time spent executing a protected operation from time spent waiting for resources or a processor.

Check your understanding

A thread enters the kernel for a call that immediately succeeds and then continues its own user instructions. What is necessarily true?

Try it yourself

An 8 ms operation has 2 ms of user CPU, 1 ms of kernel CPU, and 5 ms of measured off-CPU time. Can the measurements tell you how much of the off-CPU time was runnable delay?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Ask whether the reported categories distinguish waiting for a dependency from waiting for a processor.

Compare with the solution

CPU time totals 3 ms, and the remaining 5 ms is off CPU. Without state-transition or equivalent scheduling evidence, that 5 ms cannot be split between blocked time and runnable delay. The same elapsed and CPU totals permit several different timelines and therefore different remedies.

Take it with you

  • A privilege transition and a scheduler task switch are separate events.
  • Elapsed latency includes waiting that does not consume the thread's CPU time.
  • Library calls, kernel entries, and account privileges are different interfaces and properties.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.