How to use dm-delay to emulate slower SSD on NVME device

Intro to dm-delay

Device-Mapper’s “delay” target delays reads and/or writes and maps them to different devices.

Setup dm-delay

To setup a dm-delay for extra 1ms latency:

$ cat setup_dm_delay.sh
disk=nvme2n1
size=$(blockdev --getsize /dev/$disk)
echo "0 $size delay /dev/$disk 0 1" | dmsetup create delayed2
dmsetup table delayed2

Verify the dm-delay

To ensure the dm-delay is configured successfully,

$ ls -la /dev/nvme2n1
brw-rw---- 1 root disk 259, 1 Jun 3 17:03 /dev/nvme2n1

$ dmsetup table delayed2
0 3125627568 delay 259:1 0 1

$ls -la /dev/mapper/ | grep delayed2
lrwxrwxrwx  1 root root       7 Jun  3 17:24 delayed2 -> ../dm-2

From iostat, you can verify the delayed latency on dm-2 device when you have some workload running.

06/03/2023 05:24:56 PM
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

nvme2n1           0.00     0.00    0.00 63667.20     0.00 254668.80     8.00     1.03    0.02    0.00    0.02   0.01  49.74
dm-2              0.00     0.00    0.00 63667.40     0.00 254669.60     8.00   126.13    1.98    0.00    1.98   0.02 100.02

Remove dm-delay

$ dmsetup remove delayed2