Day 9: Disks – block layer, iostat in depth and the process in state D
Goal: know the path of a request through the block layer, interpret
iostat -xconfidently, understand the%utiltrap, measure disk latency per device and per request and trace a hanging process down to the slow device · Duration: about 3.5 hours · Prerequisite: Day 0–8
Where we start
/daten is empty, no test service is running, and there
are no device mapper devices.
# on perf1
ls /datenExpected:
lost+found
# on perf1
systemctl list-units --failed --no-legendExpected: no output.
dmsetup ls lists devices of the device mapper (more on
that shortly):
# on perf1
sudo dmsetup lsExpected:
No devices found
Introduction
Yesterday you measured how long an application waits for the file system. Today you go one layer deeper, to the disk. Only the requests that the page cache could not catch arrive here – reads of uncached data, written-back dirty pages, journal writes and flushes.
Disks are the resource whose metrics are most often misleading.
%util at 100 % sounds like “full” – and with modern devices
it often is not. A latency of 10 ms can be normal for a hard disk and an
alarm signal for an SSD. And sometimes the problem is not the disk you
see but a layer in between that presents itself as a device of its
own.
At the end of the day you trace a case that exists in every data
center: processes hang in state D, a simple sync takes ten
seconds, and the cause is a single slow device.
Today you learn:
- the path of a request through the block layer and what queues and I/O schedulers do,
- to read a disk’s settings under
/sys/block/…/queue/, - to interpret
iostat -xcompletely – and why%utilis deceptive with modern disks, - to measure latency distributions per disk with
biolatency, - to see individual block requests – and what to do when the standard tools fail on your kernel,
- what the device mapper is and why stacked devices change troubleshooting,
- to trace a process in state D down to the device that causes it.
Background
The picture for today: the parcel center
Picture the disk as a parcel center. Every block request is a truck that delivers or picks up something.
- Merging: Two small shipments to the same address go
on one truck (
rqm/s). - Sorting system: The I/O scheduler may change the
order – or, with
none, wave everything through in order of arrival. - Loading docks: The device has many gates at the
same time (
queue_depth, 128 on perf1). An old hard disk has only one gate. await: the time from entering the yard to leaving – waiting in the yard included.aqu-sz: how many trucks on average stand in the yard or load at gates.%util: the share of time in which at least one gate was occupied. With one gate, 100 % means: full. With 128 gates, 100 % only means: somebody was always there.- Device mapper: a transshipment depot between customer and center. Every truck is registered there and sent on; every station counts it. If the depot is slow, the center behind it looks completely relaxed.
- State D: A driver waits at the dock for the signature and cannot leave until it comes.
The terms
| Term | Meaning | in the picture |
|---|---|---|
| Block request (bio/request) | “read/write N sectors from sector S on device M:m” | a truck |
Merging (rqm/s) |
adjacent requests are combined | two shipments on one truck |
| I/O scheduler | orders requests (none, mq-deadline,
bfq, kyber) |
sorting system |
queue_depth |
this many requests the device accepts at the same time | number of gates |
await |
latency including the queue | entering to leaving |
aqu-sz |
average number of outstanding requests | trucks in the yard |
%util |
share of time with at least one outstanding request | at least one gate occupied |
| Device mapper | virtual devices that forward to others (LVM, dm-crypt …) | transshipment depot |
Remember:
%utilsays whether work was done, not how much more is possible. Saturation shows inawaitandaqu-szrising while throughput does not.
The block layer
If the file system cannot serve a request from the cache, it creates a block request (bio): “Read 8 sectors from sector 18,439,288 of device 8:16.” It travels through the block layer:
- Merging: If a request lies right next to another
one that is already waiting, both are combined – two small ones become
one large one.
iostatcounts that asrqm/s. - Scheduler: An I/O scheduler can change the order,
for example move reads ahead of writes. Linux has
none,mq-deadline,bfqandkyber. - Hardware queue: The request goes to the driver and to the device. Modern devices have queues of their own and process many requests at the same time – NVMe SSDs up to 64,000 per queue.
- Completion: The device reports “done”, the kernel wakes up the waiting process.
The times
iostat shows await: the time from handing
the request to the block layer until completion – including the time a
request spent in the queues. Typical orders of magnitude for a 4 KiB
access:
| Device | Latency |
|---|---|
| NVMe SSD | 0.02–0.1 ms |
| SATA SSD | 0.1–0.3 ms |
| virtual disk (depending on the storage below) | 0.2–2 ms |
| hard disk (7,200 rpm) | 5–15 ms |
| network storage | 0.5 ms to many ms |
So a latency is only “high” or “low” in comparison with the device – and with your baseline from Day 0.
The %util trap
%util is the share of time in which at least
one request was in flight. For a hard disk that can only
process one request at a time, that is a good utilization metric: at 100
% it is full. For devices that process many requests in
parallel – SSDs, RAID arrays, virtual disks on large storage –,
100 % only says: “There was always something to do.” Whether the device
could manage ten times as much, the number does not say. Saturation
there is rather shown by aqu-sz together with rising
await.
Stacked devices: the device mapper
Between the file system and the real disk there are often further
layers that present themselves as a block device: LVM (logical volumes),
encryption (dm-crypt/LUKS), software RAID (md), multipath, thin
provisioning. Most of them are built by the kernel’s device
mapper: it accepts requests to a virtual device
(/dev/dm-0, with names under /dev/mapper/) and
forwards them to one or more devices below according to a table.
For troubleshooting that means: a request appears on several devices, once per layer. If one layer is slow, you see it in the difference of the latencies from top to bottom. Device mapper devices are also usually operated without the usual request queue (bio-based) – which escapes some tools, as you will see today.
State D once more
A process waiting for a block request is in state D. It cannot be
ended until the request comes back. If a device hangs completely – a
network disk is gone, a controller does not respond –, processes stay in
D for any length of time, and after 120 seconds the kernel writes the
message task … blocked for more than 120 seconds to the
log. A global sync waits for all file
systems – one slow device slows it down for everyone.
Drill 1: A disk’s settings
Where you are: on perf1.
What you have afterwards: read the queue settings of
sdband changed the scheduler once.
Every setting is a small file under
/sys/block/sdb/queue/. You read them one by one.
Step 1 – the scheduler.
# on perf1
cat /sys/block/sdb/queue/schedulerExpected:
[none] mq-deadline
none is active (in square brackets): requests go to the
device without reordering. Ubuntu chooses none for devices
with several queues such as virtio-SCSI and NVMe;
mq-deadline is available as an option.
Step 2 – how many may queue up?
# on perf1
cat /sys/block/sdb/queue/nr_requestsExpected:
256
This many requests may queue up in the block layer per queue – the size of the yard.
Step 3 – hard disk or not?
# on perf1
cat /sys/block/sdb/queue/rotationalExpected:
1
The kernel thinks sdb is a hard disk (1 =
rotating). That is not true (Day 0); QEMU reports it that way. It
influences the scheduler and readahead, among other things.
Step 4 – readahead.
# on perf1
cat /sys/block/sdb/queue/read_ahead_kbExpected:
128
This many KiB the kernel reads ahead during sequential reading (Day 6).
Step 5 – the largest request.
# on perf1
cat /sys/block/sdb/queue/max_sectors_kbExpected:
1280
No request that goes to the device gets larger than 1,280 KiB.
Step 6 – how many gates? This file is under
device/, not under queue/:
# on perf1
cat /sys/block/sdb/device/queue_depthExpected:
128
The device accepts 128 requests at the same time.
Step 7 – change the scheduler. While running,
without a reboot. tee writes into the file with root rights
and shows what it wrote:
# on perf1
echo mq-deadline | sudo tee /sys/block/sdb/queue/schedulerExpected:
mq-deadline
# on perf1
cat /sys/block/sdb/queue/schedulerExpected:
none [mq-deadline]
Step 8 – what mq-deadline lets you set.
# on perf1
ls /sys/block/sdb/queue/iosched/Expected:
async_depth
fifo_batch
front_merges
prio_aging_expire
read_expire
write_expire
writes_starved
With mq-deadline a directory iosched/
appears with its settings – for example read_expire, the
deadline by which a read request is served at the latest.
Step 9 – back to none.
# on perf1
echo none | sudo tee /sys/block/sdb/queue/schedulerExpected:
none
Whether another scheduler helps is decided by a measurement with the real load – Day 15.
Remember: A disk’s settings are files under
/sys/block/GERÄT/queue/(GERÄT= device). Read withcat, change withecho … | sudo tee– that applies until the next reboot.
Save your progress? Sign up for free
Drill 2: The %util trap
Where you are: on perf1, two terminals.
What you have afterwards: measured “almost 100 % utilized” twice – once with 2,650 and once with 30,600 requests per second.
Step 1 – one request at a time. In the first terminal change to the directory:
# on perf1, terminal 1
cd /datenThen read at random for 40 seconds, always only one request at a time (one gate occupied):
# on perf1, terminal 1
fio --name=t --filename=/daten/fio-test --size=1G --rw=randread --bs=4k --direct=1 \
--ioengine=libaio --iodepth=1 --runtime=40 --time_based > /dev/null| Option | Meaning |
|---|---|
--filename=/daten/fio-test --size=1G |
test file, 1 GiB |
--rw=randread --bs=4k |
random reads, 4 KiB each |
--direct=1 |
bypass the page cache so that the disk works |
--ioengine=libaio --iodepth=1 |
asynchronous, but at most one request in flight |
--runtime=40 --time_based |
40 seconds |
> /dev/null |
throw away fio’s long summary – you measure with
iostat |
After about ten seconds, in the second terminal:
# on perf1, terminal 2
iostat -sxz 1 2Expected (excerpt – the line sdb in the
second report):
sdb 2650.00 10600.00 0.00 0.32 4.00 0.86 81.00
The columns are as on Day 8: tps, kB/s,
rqm/s, await, areq-sz,
aqu-sz, %util.
Step 2 – 32 at a time. Wait until fio has finished
and restart it with --iodepth=32:
# on perf1, terminal 1
fio --name=t --filename=/daten/fio-test --size=1G --rw=randread --bs=4k --direct=1 \
--ioengine=libaio --iodepth=32 --runtime=40 --time_based > /dev/nullAfter about ten seconds:
# on perf1, terminal 2
iostat -sxz 1 2Expected (excerpt – the line sdb in the
second report):
sdb 30632.00 122528.00 0.00 0.65 4.00 19.98 99.10
Side by side:
| iodepth 1 | iodepth 32 | |
|---|---|---|
tps (IOPS) |
2,650 | 30,632 |
await |
0.32 ms | 0.65 ms |
aqu-sz |
0.86 | 19.98 |
%util |
81 % | 99.1 % |
With one request at a time the disk is 81 % “utilized”. Whoever
concludes that much more is not possible is wrong: with 32 simultaneous
requests it manages 11.5 times as much – with only 18
percentage points more %util. The latency only doubled. The
meaningful numbers are tps, await and
aqu-sz: as long as await hardly grows with
increasing parallelism, the device has reserves.
Remember: 81 %
%utilwith one request and 99 % with 32 – with eleven times as much work. For devices with many gates look atawaitandaqu-sz, not at%util.
Save your progress? Sign up for free
Drill 3: Latency per disk
Where you are: on perf1, two terminals.
What you have afterwards: a latency histogram per disk.
Step 1 – load.
# on perf1, terminal 1
fio --name=t --filename=/daten/fio-test --size=1G --rw=randread --bs=4k --direct=1 \
--ioengine=libaio --iodepth=1 --runtime=90 --time_based > /dev/nullStep 2 – histogram. After about five seconds:
# on perf1, terminal 2
sudo biolatency-bpfcc -D 5 1biolatencymeasures the time of every block request from submission to the device until completion.-Dseparates by disk,5 1means five seconds, once. The tool needs about 15 seconds to start.
Expected (shortened):
disk = sdb
usecs : count distribution
128 -> 255 : 2575 |************ |
256 -> 511 : 8437 |****************************************|
512 -> 1023 : 786 |*** |
1024 -> 2047 : 92 | |
2048 -> 4095 : 35 | |
4096 -> 8191 : 3 | |
8192 -> 16383 : 1 | |
disk = sda
usecs : count distribution
256 -> 511 : 22 |**************** |
512 -> 1023 : 53 |****************************************|
1024 -> 2047 : 28 |********************* |
On sdb most requests are between 256 and 511 µs, but
there are outliers up to 16 ms – the p99 outliers from Day 0. On
sda something happens on the side: the system’s journal and
logs. Unlike iostat, the histogram shows the
distribution: two disks with the same average can have
quite different outliers.
From building this course: Half an hour after these measurements, the same disk with the same command delivered only 83 to 133 IOPS at 7 to 12 ms – twenty to thirty times slower. Nothing else was running in the VM. Six minutes later everything was normal again. The cause was below the VM: something else was loading the shared storage on the Proxmox host. In the VM you only see something like that in the latencies – and only if you have a baseline for comparison. The sar recording (
sar -d -p) captured the period to the minute.
Remember: An average hides outliers. The histogram per disk shows whether the slowest 1 % are at 2 ms or at 16 ms.
Save your progress? Sign up for free
Drill 4: Individual requests – when tools do not match the kernel
Where you are: on perf1, two terminals, in
~/tag09.What you have afterwards: seen that two well-known tools fail on kernel 6.8, and a script of your own that shows every block request.
Step 1 – biosnoop from bcc. Restart fio in terminal 1 as in Drill 3, then:
# on perf1, terminal 2
sudo timeout -s INT 40 biosnoop-bpfccExpected (excerpt – the first lines):
TIME(s) COMM PID DISK T SECTOR BYTES LAT(ms)
0.000000 ? 0 <unknown> R 0 4096 0.30
0.000520 ? 0 <unknown> R 0 4096 0.38
0.000971 ? 0 <unknown> R 0 4096 0.39
0.001358 ? 0 <unknown> R 0 4096 0.28
0.001633 ? 0 <unknown> R 0 4096 0.22
The latencies are right, but process (?), disk
(<unknown>) and sector (0) are missing.
bcc 0.29 reads these fields from kernel structures whose layout has
changed.
Step 2 – biosnoop from bpftrace. bpftrace ships its
own versions (/usr/sbin/*.bt):
# on perf1, terminal 2
sudo timeout -s INT 15 biosnoop.btExpected (excerpt – the first lines):
/usr/sbin/biosnoop.bt:21-23: WARNING: blk_account_io_start is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
/usr/sbin/biosnoop.bt:23-24: WARNING: __blk_account_io_start is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
/usr/sbin/biosnoop.bt:30-32: WARNING: blk_account_io_done is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
/usr/sbin/biosnoop.bt:32-33: WARNING: __blk_account_io_done is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
Attaching 6 probes...
cannot attach kprobe, Invalid argument
This script attaches to kernel functions (kprobes) that the compiler built into other functions in the new kernel (inlined) – they no longer exist as functions of their own.
Step 3 – find stable tracepoints. The kernel offers
tracepoints for block requests: built-in measuring
points with documented fields that are meant to stay stable across
versions. bpftrace -l lists them; the asterisk is a
wildcard:
# on perf1
sudo bpftrace -l "tracepoint:block:block_rq_*"Expected (excerpt):
tracepoint:block:block_rq_complete
tracepoint:block:block_rq_error
tracepoint:block:block_rq_insert
tracepoint:block:block_rq_issue
tracepoint:block:block_rq_merge
tracepoint:block:block_rq_remap
tracepoint:block:block_rq_requeue
block_rq_issue fires when a request goes to the device,
block_rq_complete when it is done.
Step 4 – a tool of your own.
# on perf1
mkdir -p ~/tag09# on perf1
nano ~/tag09/bioschau.btContent (nano as on Day 0: Ctrl+O and Enter saves, Ctrl+X exits):
// Jede Blockanfrage mit Prozess, Gerät, Art, Größe und Latenz (über stabile Tracepoints)
tracepoint:block:block_rq_issue
{
@start[args.dev, args.sector] = nsecs;
@wer[args.dev, args.sector] = comm;
}
tracepoint:block:block_rq_complete
/@start[args.dev, args.sector]/
{
$us = (nsecs - @start[args.dev, args.sector]) / 1000;
printf("%-16s %3d:%-3d %-4s %8d %7d %8d\n",
@wer[args.dev, args.sector], args.dev >> 20, args.dev & 0xfffff,
args.rwbs, args.sector, args.nr_sector * 512, $us);
delete(@start[args.dev, args.sector]);
delete(@wer[args.dev, args.sector]);
}
END { clear(@start); clear(@wer); }- On submission the script remembers the time
(
nsecs, nanoseconds) and the process name (comm) in two maps, each under the key of device and sector – that way it finds the request again on completion. - On completion the filter
/@start[…]/checks whether it knows the start (otherwise the request was already running before the script started). Then it calculates the latency in microseconds. args.devis the device number in the internal form: the upper part (>> 20) the major number, the lower part (& 0xfffff) the minor number – together the8:16fromlsblk.args.rwbsis the type:Rread,Wwrite, plus letters such asS(synchronous),M(metadata),F(flush).deleteremoves the entries;ENDclears the maps on exit so that bpftrace does not print them.
Step 5 – try it out. A short load in the first terminal:
# on perf1, terminal 1
fio --name=t --filename=/daten/fio-test --size=1G --rw=randread --bs=4k --direct=1 \
--ioengine=libaio --iodepth=1 --runtime=20 --time_based > /dev/nullAfter three seconds, watch for three seconds:
# on perf1, terminal 2
sudo timeout -s INT 3 bpftrace ~/tag09/bioschau.btExpected (excerpt – the first lines):
Attaching 3 probes...
fio 8:16 R 18439288 4096 261
fio 8:16 R 19659144 4096 317
fio 8:16 R 19831496 4096 262
fio 8:16 R 19098152 4096 263
fio 8:16 R 18590456 4096 462
fio 8:16 R 20198568 4096 245
Process, device, type, sector, size in bytes and latency in
microseconds – everything biosnoop should show. The lesson:
tracepoints before kprobes. Tools built on tracepoints
survive kernel updates; those that attach to internal functions break
sooner or later.
Note: the process name is the one that submitted the
request. With direct reads like here that is the application.
Written-back dirty pages, on the other hand, are submitted by a kernel
thread (kworker) – then it appears here, not the
application that wrote.
Which fields a tracepoint has is shown by
sudo bpftrace -lv tracepoint:block:block_rq_issue. If they
are named differently on your kernel, adjust the script accordingly.
Step 6 – clean up when fio has finished:
# on perf1
rm -f /daten/fio-testRemember: Tracepoints are measuring points with a guarantee, kprobes attach to functions that may be named differently tomorrow. Scripts of your own on tracepoints survive kernel updates.
Save your progress? Sign up for free
Diagnosis task
The backup that copies documents to an archive drive at night has been running far too long for days. Since yesterday colleagues have also been reporting: “When I install a package, it hangs for minutes at ‘Processing triggers’.” According to the vendor, everything is fine with the archive drive itself.
Setup. You build yourself an archive drive that behaves like a tired device. The commands are needed for this task but are only explained in the solution. Type them one by one.
# on perf1
truncate -s 2G /daten/archiv.img# on perf1
sudo losetup --find --show /daten/archiv.imgExpected:
/dev/loop0
The name of the new device. If yours shows a different number, use it in all following commands.
# on perf1
sudo blockdev --getsz /dev/loop0Expected:
4194304
You need this number in the next line.
# on perf1
sudo dmsetup create archiv --table "0 4194304 delay /dev/loop0 0 200"# on perf1
sudo mkfs.ext4 -q -L archiv /dev/mapper/archiv# on perf1
sudo mkdir -p /mnt/archiv# on perf1
sudo mount /dev/mapper/archiv /mnt/archiv# on perf1
sudo chown ubuntu:ubuntu /mnt/archivThe backup service is a small script. The loop in it runs in the service, not in your shell:
# on perf1
nano ~/tag09/sicherung.sh#!/bin/sh
# Sicherung: kopiert immer wieder die Dokumentation aufs Archiv und synchronisiert es
while true; do
cp -r /usr/share/doc /mnt/archiv/doc-$(date +%s)
sync -f /mnt/archiv
done# on perf1
sudo systemd-run --unit=sicherung -p User=ubuntu sh /home/ubuntu/tag09/sicherung.shWait 40 seconds.
Task (25 minutes): Why does “everything that synchronizes” hang? Which device is to blame, and on which layer is the problem?
Solution path
Step 1 – reproduce the symptom. Package
installations call sync at the end. So:
# on perf1
time syncExpected (excerpt – the line real):
real 0m12.656s
Almost 13 seconds for a sync; normally it takes
hundredths of a second. The duration varies: in the lab
sync took between 1 and 13 seconds depending on the moment
– depending on how much the backup service had just made dirty. Repeat
the command a few times.
Step 2 – who is waiting? ps shows
state, ID, wait function and name for every process:
# on perf1
ps -eo state,pid,wchan:22,comm-eall processes,-othe columns.wchanis the kernel function in which a sleeping process waits;:22makes the column 22 characters wide.
Run the command while a time sync is running in a second
terminal.
Expected (excerpt – look for the lines that start
with D):
D 72394 - kworker/u8:1+flush-252:0
D 77713 wb_wait_for_completion sync
D 77718 wb_wait_for_completion sync
Three processes in D – drivers stuck at the dock: two
sync (yours and the backup service’s), waiting in
wb_wait_for_completion for the write-back, and a kernel
thread called flush-252:0. At other
moments you see the journal thread jbd2/dm-0-8 instead of
the flush thread, and sync in
jbd2_log_wait_commit – the same story, one level deeper.
That is the thread that writes back dirty pages for the device with the
number 252:0. The trail leads to a specific device.
Step 3 – which device is 252:0, and how slow is it?
# on perf1
iostat -sxzN 2 3-Nshows device mapper devices with their names instead ofdm-0;2 3three reports two seconds apart.
Expected (excerpt – the last report):
Device tps kB/s rqm/s await areq-sz aqu-sz %util
archiv 964.00 6002.00 0.00 216.34 6.23 208.56 94.30
loop0 964.00 6002.00 0.00 2.84 6.23 2.75 5.20
sdb 17.00 8720.00 7.00 1.59 512.94 0.03 0.95
Three devices, stacked on top of each other, with the same number of requests:
| Device | await |
aqu-sz |
Interpretation |
|---|---|---|---|
archiv |
216 ms | 209 | this is where the trucks pile up |
loop0 below |
2.8 ms | 3 | fast |
sdb at the bottom |
1.6 ms | 0.03 | hardly busy |
The latency arises between archiv and loop0
– in the transshipment depot. The disks below are innocent – exactly
what the vendor said.
Step 4 – how is the stack built?
# on perf1
lsblk -o NAME,MAJ:MIN,TYPE,MOUNTPOINTS /dev/loop0Expected:
NAME MAJ:MIN TYPE MOUNTPOINTS
loop0 7:0 loop
└─archiv 252:0 dm /mnt/archiv
archiv has the number 252:0 (the device
from flush-252:0), is a device mapper device
(dm) and is mounted under /mnt/archiv.
# on perf1
sudo dmsetup tableExpected:
archiv: 0 4194304 delay 7:0 0 200
| Field | Meaning |
|---|---|
0 4194304 |
sectors 0 to 4,194,304 of the virtual device |
delay |
the target: pass requests on with a delay |
7:0 0 |
to device 7:0 (loop0), from sector 0 |
200 |
delay in milliseconds |
The delay target exists for testing – in this lab it
plays a device as slow as a network disk over a bad line or a dying disk
that retries every request several times.
Now you can read the setup: truncate creates a 2 GiB
file without allocating space, losetup --find --show turns
it into the block device /dev/loop0,
blockdev --getsz returns its size in sectors, and
dmsetup create archiv --table … builds the delayed device
/dev/mapper/archiv from the table line. On it there is an
ext4 file system and the backup service.
Step 5 – why does sync hang for
everyone? Where exactly does a sync wait? First
the number of the youngest sync process (-n =
newest, -x = exact name):
# on perf1
pgrep -n -x syncExpected:
77644
You will see a different number. Use it below.
/proc/PID/stack shows which kernel functions the process is
currently in:
# on perf1
sudo cat /proc/PID/stackExpected:
[<0>] wb_wait_for_completion+0x89/0xc0
[<0>] sync_inodes_sb+0xd6/0x2c0
[<0>] sync_filesystem+0x70/0xb0
[<0>] __x64_sys_syncfs+0x49/0xb0
[<0>] x64_sys_call+0x17b2/0x25a0
[<0>] do_syscall_64+0x87/0x180
[<0>] entry_SYSCALL_64_after_hwframe+0x78/0x80
Read from bottom to top: the system call syncfs – that
is sync -f of the backup service –, then
sync_inodes_sb (all files of this file system), then the
waiting until the flush thread has written the dirty pages
to the slow device (wb_wait_for_completion). A few moments
later the same process can be waiting in the journal
(jbd2_log_wait_commit). A global sync – which
every package tool calls – waits for all file systems
and therefore for this one too. A single slow device slows down
operations on the whole machine.
A detail for tool experts. Your
bioschau.bt from Drill 4 shows only loop0 with
fast times in this case:
kworker/3:3 7:0 W 518424 12288 737
kworker/3:3 7:0 W 518448 4096 727
The request tracepoints only fire for devices with a request queue.
The device mapper device is bio-based and creates none; the 200 ms delay
arises before loop0 and is invisible to
the script. iostat, on the other hand, reads the counters
of every device from /proc/diskstats and sees it. No tool
sees everything – that is why it pays to go through the layers from top
to bottom.
Step 6 – fix. In real life this would now mean:
check the line, replace the device, switch the path. Here you replace
the delay with a direct pass-through (linear) while
running. First load the new table:
# on perf1
sudo dmsetup reload archiv --table "0 4194304 linear /dev/loop0 0"4194304is the size fromblockdev --getszearlier.
Then activate it – without unmounting the file system. This is exactly how LVM swaps paths while running:
# on perf1
sudo dmsetup resume archiv# on perf1
sudo dmsetup table archivExpected:
0 4194304 linear 7:0 0
Step 7 – check. Wait five seconds, then:
# on perf1
time syncExpected:
real 0m0.057s
user 0m0.001s
sys 0m0.006s
# on perf1
iostat -sxzN 2 2Expected (excerpt – the second report):
Device tps kB/s rqm/s await areq-sz aqu-sz %util
archiv 3004.00 24024.00 0.00 5.14 8.00 15.45 15.95
loop0 119.50 24024.00 2885.50 4.67 201.04 0.58 15.90
sdb 31.00 21930.00 4.50 1.77 707.42 0.06 1.75
sync takes 57 ms again. archiv responds in
5.1 ms and handles three times as many requests. Notice:
loop0 now merges the many small requests into a few large
ones (rqm/s 2,886, areq-sz 201 KiB) – before,
they arrived one by one and spaced apart.
Step 8 – clean up. Take it apart from top to bottom:
# on perf1
sudo systemctl stop sicherung# on perf1
sudo systemctl reset-failed sicherungIf it reports not loaded, there is nothing to do.
# on perf1
sudo umount /mnt/archiv# on perf1
sudo dmsetup remove archiv# on perf1
sudo losetup -d /dev/loop0# on perf1
rm -f /daten/archiv.imgumount unmounts, dmsetup remove removes the
device mapper device, losetup -d detaches the file from the
loop device. For losetup -d, use the device that
losetup --find --show reported during setup.
# on perf1
sudo dmsetup lsExpected:
No devices found
# on perf1
ls /datenExpected:
lost+found
Remember: With stacked devices compare the latencies from top to bottom. Where
awaitjumps, the slow layer sits – not necessarily the disk the vendor checks.
If it doesn’t work
umount: /mnt/archiv: target is busy.: the backup service is still running or your terminal is in the directory. Stop the service, runcd, try again.device-mapper: remove ioctl on archiv failed: Device or resource busy: the file system is still mounted.umountfirst.
Challenge
Measure %util, await and
aqu-sz for iodepth 1, 4, 16 and 64 (as on Day 1, Drill 1)
and enter them in a table. From which depth is the disk
saturated – and how do you recognize that even though
%util is already high at depth 1?
Solution path: %util rises above 80 %
at depth 1 already and then stays just below 100 %. You recognize
saturation by throughput hardly increasing from some depth on while
await rises significantly – on Day 1 that was the case
between 16 and 64 (24,600 → 35,000 IOPS, but 0.63 → 1.81 ms). From
there, additional requests only wait in the queue (aqu-sz
grows with the depth) without the device managing more. Your numbers
depend on the storage below your VM; the pattern is the same.
Save your progress? Sign up for free
Checkpoint
# on perf1
sudo dmsetup lsExpected: No devices found.
# on perf1
losetup -lExpected: no line with archiv.img – in the lab no output
at all. On systems with snap packages you see their loop devices here –
they belong there.
# on perf1
findmnt /mnt/archivExpected: no output – nothing is mounted anymore.
# on perf1
cat /sys/block/sdb/queue/schedulerExpected: [none] mq-deadline.
# on perf1
ls ~/tag09/bioschau.btExpected: /home/ubuntu/tag09/bioschau.bt – you need it
again on Day 14.
Test yourself
iostatshows%util99 %. When is the disk really at its limit with that, and when not?- Which two columns show saturation for devices with many parallel queues?
- Why does
biosnoopfrom bcc show?and<unknown>on Ubuntu 24.04, and how did you work around it? - A process hangs in state D, a kernel thread is called
flush-252:0. How do you find the device? archivhas 216 msawait,loop0below it 3 ms. Where is the problem?
Answers:
- For a hard disk with one request at a time, yes. For SSDs, RAID and virtual disks not necessarily – 100 % only means “always at least one request outstanding”.
awaitandaqu-sz: if they rise withouttpsgrowing along, the device is saturated.- bcc 0.29 reads fields from kernel structures that have changed. A
bpftrace script of your own on the stable tracepoints
block_rq_issue/block_rq_completeshows everything. 252:0is the device number;lsblk -o NAME,MAJ:MINoriostat -Nshow the name.- Between the two – in the device mapper layer
archiv(here thedelaytarget), not on the disk below.
Interview Note
“iostat shows 100 % %util. Is the disk at
its limit?” – For a single hard disk yes, for SSDs, RAID and virtual
disks not necessarily. Whoever says that, names await and
aqu-sz as better signs and can explain how to find the slow
layer in stacked devices (LVM, dm-crypt, multipath) – by comparing the
latencies from top to bottom –, shows real understanding. Whoever also
knows that tracing tools based on kprobes break with kernel updates
while tracepoints are stable clearly stands out.
Enterprise Note
In larger environments there is a chain below almost every disk: file
system, LVM, dm-crypt, multipath, SAN or Ceph. Monitoring on
%util alone is worthless there; alerts belong on
await (or better latency percentiles) per device and on the
number of processes in state D. The kernel message
blocked for more than 120 seconds should always trigger an
alert in log monitoring. And because virtual disks share their
performance with others, diagnosis needs a baseline and the recording –
like the short slump that occurred in this course’s lab and remained
visible only in sar -d.
Summary
- A block request is merged (
rqm/s), queued, sorted by the scheduler (none,mq-deadline) and handed to the device’s queues. Settings under/sys/block/X/queue/. await= latency including waiting time,aqu-sz= queue,%util= share of time with at least one request – for parallel devices not a measure of “full” (81 % at 2,650 IOPS, 99 % at 30,632 IOPS).biolatency -Dshows the distribution per disk.biosnoop(bcc) andbiosnoop.btfail on kernel 6.8; a bpftrace script of your own on the tracepointsblock_rq_issue/block_rq_completeworks. Tracepoints before kprobes.- The device mapper stacks devices; you find the slow
layer via
iostat -Nand by comparing the latencies. Bio-based devices are invisible to request tracepoints. - Diagnosis today: a device with 200 ms delay – recognized by
processes in D,
flush-252:0, 216 msawaitonarchivcompared with 3 ms onloop0; a globalsyncneeded up to 13 instead of 0.06 seconds.
Commands at a glance
| Command | What it does | Parameters |
|---|---|---|
cat /sys/block/sdb/queue/scheduler (likewise
nr_requests, rotational,
read_ahead_kb, max_sectors_kb) |
queue settings | – |
cat /sys/block/sdb/device/queue_depth |
simultaneous requests of the device | – |
echo mq-deadline \| sudo tee /sys/block/sdb/queue/scheduler |
change the scheduler | none, mq-deadline, bfq,
kyber |
iostat -sxzN 2 3 |
disk statistics with device mapper names | -N names |
biolatency-bpfcc -D 5 1 |
latency histogram per disk | -D per disk |
bpftrace -l "tracepoint:block:block_rq_*" |
list tracepoints | -l list, -lv with fields |
bpftrace ~/tag09/bioschau.bt |
every block request with latency | see Drill 4 |
ps -eo state,pid,wchan:22,comm |
state and wait function of every process | wchan wait function |
sudo cat /proc/PID/stack |
kernel stack of a waiting process | – |
lsblk -o NAME,MAJ:MIN,TYPE,MOUNTPOINTS |
show the device stack | – |
dmsetup ls / table /
create NAME --table … / reload /
resume / remove |
manage device mapper devices | --table table line |
losetup --find --show DATEI /
losetup -d GERÄT |
set up a file as a block device / detach it (DATEI =
file, GERÄT = device) |
– |
truncate -s 2G DATEI |
create a file without allocating space | -s size |
blockdev --getsz GERÄT |
size in sectors | – |
time sync |
duration of a global synchronization | – |