Diagnosing Load and CPU
Goal
You measure for yourself what the load average counts, explain a situation where "the metrics are normal but it is slow" by reading CPU utilization, PSI, and the throttling counter together, and then, in such an incident, keep the harmless one alive and stop only the cause.
Why it matters
Linux's load average does not count only CPU waiting. It also counts tasks in the D state that are waiting for disk or NFS responses. So an 8-core server with a load of 24 and a CPU idle of 60% is normal behavior. Adding CPU then just spends money. Conversely, a container's cgroup throttling appears neither in load, nor in steal, nor in utilization - because a throttled task is removed from the run queue entirely. That is why you have to look separately at the counters in cpu.stat. The reason this lab even grades "if it isn't there, write that it isn't" is that in the field too, the first action is to check whether that file exists.
In the incident in step 8, two things use the CPU at the same time. The batch running at nice 19 has low priority and cannot push the api aside, but a loop that runs without rest uses up the container's CPU share and stops the whole cgroup every period. "It hit the limit" is a result; the cause is who is using that share. Killing the harmless one leaves the symptom in place and only keeps necessary work from getting done.
Steps
Work in the /root/load directory. Including the incident step, it takes a little over 60 minutes.
- Measure the number of CPUs visible in this container and the amount of CPU the cgroup actually lets you use (quota ÷ period), and write them to
/root/load/cpus.txtas two lines,online=<개수>(count) andquota=<CPU 수>(number of CPUs). If there is no quota, writequota=max; if the quota file cannot be read, writequota=unavailable. - Start 2 or more processes that keep using the CPU in the background, all pinned to the same single CPU, and write their PIDs to
/root/load/burner_pids.txt, one per line. They must stay alive through all the following steps. - After waiting 1–2 minutes, measure the 1-minute load average and write it to
/root/load/load1.txt. It must be 0.7 or more. - Write one letter for the current state of those processes to
/root/load/state.txt. - Measure, at the same moment, the cumulative CPU time (seconds) the first process has used so far and the wall-clock time (seconds) elapsed since it started, and write them to
/root/load/cpu_sec.txtas two lines,cpu_sec=<정수>andwall_sec=<정수>(integers). The CPU time must be 5 seconds or more. - Read the
someline of/proc/pressure/cputwice, 10 seconds apart, and write the second line'savg10andtotal, plus how muchtotalgrew over those 10 seconds (microseconds), to/root/load/psi.txtas three lines,avg10=<값>,total=<값>, anddelta_us=<값>(values). If the file doesn't exist, write just one line,unavailable. - In the cgroup's
cpu.stat, take thenr_throttledvalue and write it to/root/load/throttle.txtin the formnr_throttled=<값>. If it can't be read, writeunavailable. - Incident
incident ts-load-slow:tsload-apiresponses stall occasionally. Pick the cause fromrunaway-loop,nice-batch,cpu-throttle, andio-wait, and stop only what is the cause. Leave api and backup as the very same processes, and leave the burners from the earlier steps as they are. Write three lines,cause:,nr_throttled:(the value read from cpu.stat), andevidence:, to/root/incident/ts-load-slow/report.txt.
Notes
- Incident tools:
incident listshows the incidents you can raise, andincident show ts-load-slowshows the ticket again. Raising the same incident again returns it to its original state. - Report format:
cause: <코드>(code),nr_throttled: <사건 동안 cpu.stat 에서 읽은 값 그대로>(the value read from cpu.stat during the incident, as is), andevidence: <판단의 근거가 된 출력 한 줄>(one line of output your judgment was based on). - The fields of
/proc/<PID>/statare in the orderpid (comm) state ...; the 14th and 15th are utime/stime (clock ticks), and the 22nd is the start time (ticks since boot). - Measure latency with
curl -w '%{time_total}'. You only see "it spikes occasionally" by measuring several times, not once. - Common mistake 1: if you read it right after creating the load in step 3, the value is low. An exponentially decaying average reflects only 63% even after 1 minute.
- Common mistake 2: in step 8, stopping only the parent process. The workers (children) the parent started survive on their own.
Visible CPUs and usable CPUs
Measure the number of CPUs visible in this container and the amount of CPU the cgroup actually lets you use (quota ÷ period), and write them to /root/load/cpus.txt as two lines, online=<개수> (count) and quota=<CPU 수> (number of CPUs). If there is no quota, write quota=max; if the quota file cannot be read, write quota=unavailable.
The command that reports the number of CPUs shows the host's CPUs as they are. The amount this container can actually use is in the cgroup's CPU quota file as two numbers, "quota period" (for v2). The load average has meaning only when compared with the former, and throttling only when compared with the latter.
Start processes that share one CPU
Start 2 or more processes that keep using the CPU in the background, all pinned to the same single CPU, and write their PIDs to /root/load/burner_pids.txt, one per line. They must stay alive through all the following steps.
One infinite loop uses up one whole CPU. Choosing which CPUs a process may use is called CPU affinity, and there is a tool to specify it at launch. Check whether they are pinned with the allowed-CPU list in the process status file.
Confirm that the load average rises
After waiting 1–2 minutes, measure the 1-minute load average and write it to /root/load/load1.txt. It must be 0.7 or more. See how high the load average climbs when two processes share one CPU.
It is an exponentially decaying average, so it doesn't rise immediately. Wait 1–2 minutes and then read the first of the 1-, 5-, and 15-minute values that the kernel provides as a one-line file under /proc. The point is why CPU utilization (one CPU's worth) and the load average (two) differ.
Read the process state letter
Write one letter for the current state of those processes to /root/load/state.txt.
The state letter is in ps's STAT column or in the kernel's process stat file (right after the parenthesized name). Check whether it is the same letter even while a process waits for the CPU, when the two take turns on one CPU.
Measure CPU time and wall-clock time
Measure, at the same moment, the cumulative CPU time (seconds) the first process has used so far and the wall-clock time (seconds) elapsed since it started, and write them to /root/load/cpu_sec.txt as two lines, cpu_sec=<정수> and wall_sec=<정수> (integers). The CPU time must be 5 seconds or more. See what the ratio between the two is.
The cumulative CPU time is the sum of the user and kernel time fields in the process stat file, in clock ticks (check the ticks per second with getconf). The start time is in the same file as ticks since boot, so compare it with the time elapsed since boot. You can also estimate with ps's TIME and ETIME columns.
Measure the PSI pressure metric
Read the some line of /proc/pressure/cpu twice, 10 seconds apart, and measure the second line's avg10 and total, plus how much total grew over those 10 seconds (microseconds), then write them to /root/load/psi.txt as three lines, avg10=<값>, total=<값>, and delta_us=<값> (values). See whether avg10 matches the fraction of time stalled during those 10 seconds. If the file doesn't exist, write just one line, unavailable.
PSI shows not "how many are waiting" but "how long things stalled". The some line has the 10-, 60-, and 300-second averages (percentages) side by side with the cumulative time during which at least one task waited (total, in microseconds). The averages change within seconds, but the cumulative value never decreases; dividing the difference between two cumulative readings by the interval length gives a value with the same meaning as the average. If the kernel has no such file, write that it is absent.
cgroup throttling counter
In the cgroup's cpu.stat, take the nr_throttled value and write it to /root/load/throttle.txt in the form nr_throttled=<값>. If it can't be read, write unavailable.
The cgroup cumulatively counts how many periods used up the quota. Find the CPU statistics file in the container's own cgroup directory (for v2, directly under /sys/fs/cgroup). Comparing the quota you measured in step 1 with what the burners from step 2 use lets you guess whether this value should be growing right now.
Incident: there is supposedly CPU to spare, yet the api stalls occasionally
Running incident ts-load-slow makes the responses of tsload-api (http://127.0.0.1:8088/) erratic. It is slow even though the node has CPU to spare. Pick the cause from runaway-loop (a loop that runs without rest), nice-batch (a batch job with lowered priority), cpu-throttle (the cgroup CPU limit), and io-wait (waiting on disk), and stop only what is the cause. tsload-api and tsload-backup must stay alive as the very same processes from the incident (no restarts, stop signals, or nice changes), and leave the burners from the earlier steps as they are. In /root/incident/ts-load-slow/report.txt, write cause: <코드> (code); nr_throttled: <값> (value) as read from the cgroup's cpu.stat during the incident (the grader checks that it lies between the counter at the time the incident was raised and the counter now); and evidence: <관찰한 출력 한 줄> (one line of output you observed).
Direction: "the node has CPU to spare but it is slow" may mean this container has used up its share. Determine who is using that share and whether priority (nice) protects that share too. "It hit the limit" may be a result rather than the cause. Tools: curl -w (latency), the cgroup's cpu.max and cpu.stat, ps's NI, %CPU, STAT, and PPID, the service configuration and logs (/etc/tsload, /var/log/tsload). Concretely: read nr_throttled in cpu.stat twice, a few seconds apart, to see whether it is increasing, and check from the configuration and logs how each CPU-using process is supposed to behave. Count a parent process and its children (workers) separately.