From 78aaacdc1fb8c53a3a337c090e36c53b354a64ee Mon Sep 17 00:00:00 2001 From: Slaapliedje <25948081+slaapliedje@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:19:40 -0700 Subject: [PATCH] Best practice: IDE reads stall every task below priority 12, with two V4 tests Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NWALVGtmg35zN3v1pCUHCA --- .../disk-reads-stall-lower-priorities.md | 98 +++++++++++++ tools/v4/tests/ide-read-rate/test.c | 99 +++++++++++++ tools/v4/tests/ide-read-stall/test.c | 136 ++++++++++++++++++ 3 files changed, 333 insertions(+) create mode 100644 hardware/best-practices/disk-reads-stall-lower-priorities.md create mode 100644 tools/v4/tests/ide-read-rate/test.c create mode 100644 tools/v4/tests/ide-read-stall/test.c diff --git a/hardware/best-practices/disk-reads-stall-lower-priorities.md b/hardware/best-practices/disk-reads-stall-lower-priorities.md new file mode 100644 index 0000000..5e38879 --- /dev/null +++ b/hardware/best-practices/disk-reads-stall-lower-priorities.md @@ -0,0 +1,98 @@ +--- +title: IDE reads stop every task below priority 12 - read in small, paced pieces +type: best-practice +owner: slaapliedje +status: draft +origin: ai-assisted +basis: [measured, other] +components: [hardware, devs] +applies_to: + core: "Core 12846" + os: [amigaos-3.9] + mode: [os-friendly] + board: [v4-standalone] +tags: [ide, cf, scsi.device, lide, pio, streaming, latency, music, file-io, task-priority] +related: + - hardware/pitfalls/ide-speed-cf-write-corruption.md +sources: + - "VCMI port, V4 Standalone, Core 12846, CoffinOS R65, 2026-11-26: CF card SanDisk SDCFXS-128G on the IDE port (scsi.device 48.13 in ROM, the lide.device Fast-IDE backend), SFS volume, files read cold through dos.library Read()" + - "tools/v4/tests/ide-read-rate and tools/v4/tests/ide-read-stall (this knowledge base), run on the machine above on 2026-11-26; the boot-default rows come from diskprobe and wakeprobe, the programs these tests were made from" + - "pcsamp (CIA-B PC sampler, 1 kHz) during VCMI AI turns, with the samples outside the program mapped to ROM modules by dumping $F00000-$F3FFFF and disassembling it" +--- + +## Goal + +Smooth frames, music and input while a program reads files from the V4's IDE +drive. + +## Technique + +Measure your own drive with `tools/v4/tests/ide-read-rate` and +`tools/v4/tests/ide-read-stall` (set `AKB_READ_FILES` to cold files first). + + +- **A read holds up every task below priority 12 for its whole transfer.** + The priority of the reading task does not matter. The data is moved by the + CPU (`move16` and `move.l` loops in `scsi.device`, around `$F1D184` in ROM + 48.13) with no DMA, and the CPU is busy for the whole transfer: a counting + task at priority -1 got 0–1 % of its idle rate while a file was read (M). A + task at priority 5 woke up to 4.3 ms late during 64 KB reads, one piece's + transfer time, and a task at priority 12 was not delayed (M). This fits the + copy running in scsi.device's `lide ata task`, which runs at priority 11. +- **The stall per read is its size divided by the drive speed.** On the CF + card above: 4 ms per 64 KB at 16 MB/s, 20 ms per 64 KB at the 3.2 MB/s the + card read after boot. A 60 Hz frame is 16.7 ms. +- **DO set the IDE speed** after checking writes on the card (see + [ide-speed-cf-write-corruption.md](../pitfalls/ide-speed-cf-write-corruption.md)). + It divides every stall by the speed gained: 5x on this card. +- **DO stream large files at the rate you use them**, in pieces small enough + for your frame budget, instead of loading them at full speed. 16-bit + stereo music at 44.1 kHz uses 176 KB/s: one 64 KB piece every 0.37 s, kept + a few seconds ahead of playback. +- **DO keep archive files open and read each entry with one `Read()`.** An + open, a seek and several small reads per entry each pay the drive's + per-request cost. +- **DON'T expect a higher priority for the drawing task to help** unless it + is above the driver: everything at priority 11 or lower waits. + +```c +/* Read the next piece of a stream only when the buffer runs low: one short + stall every few hundred ms instead of seconds of stalls when the file opens */ +#include + +#define PIECE (64 * 1024) + +LONG feedStream(BPTR fh, UBYTE *buffer, LONG loaded, LONG total, LONG playedBytes, LONG aheadBytes) +{ + LONG got; + if (loaded >= total || loaded - playedBytes > aheadBytes) + return loaded; /* enough buffered: no read, no stall */ + got = Read(fh, buffer + loaded, total - loaded < PIECE ? total - loaded : PIECE); + return got > 0 ? loaded + got : total; +} +``` + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| Read rate after boot, nothing setting the IDE speed | 2.8 MB/s (4 KB pieces), 3.2 MB/s (64 KB), 3.2 MB/s (1 MB); CPU left 0 % | Core 12846, V4 Standalone | diskprobe (as `ide-read-rate`, with the piece size as a parameter), cold files of 2–3.5 MB, about 7 MB per run | +| Read rate at `ApolloControl ID=2` | 16.1–16.3 MB/s (64 KB pieces); CPU left 0–1 % | Same | Same, before and after a reboot with ID=2 in User-Startup | +| Read rate and CPU left at ID=2, published test | 16.1 MB/s, CPU left 0 % | Same | `tools/v4/tests/ide-read-rate`, three cold files, 2 MB | +| Task at priority -1 while reading at ID=2 | 4 wake-ups/s instead of 860, up to 224 ms late | Same | wakeprobe: 1 ms timer.device requests, reader at priority 0 | +| Task at priority 5 | 259 wake-ups/s instead of 862, mean 2.9 ms and max 4.3 ms late | Same | Same | +| Task at priority 12 | 858 wake-ups/s instead of 862, max 0.24 ms late | Same | Same | +| Same, published test | Priority 5: 293 wake-ups/s instead of 864, mean 2.4 ms, max 4.1 ms late; priority 12: 863 instead of 864, max 0.23 ms | Same | `tools/v4/tests/ide-read-stall`, four cold files, 14 MB/s while reading | +| VCMI music read whole on each AI-turn theme change | One 10 s window: 12.8 MB read in 4.1 s of read time, 4.7 frames/s | Same, boot-default IDE speed | Per-file read accounting in the game, frame counter per 10 s | +| Same music read 4 s ahead of playback in 64 KB pieces | 1.7 MB per 10 s, 0.56 s of read time | Same, boot-default IDE speed | Same | +| VCMI archive entries opened, sought and read in 10 KB pieces | 1480 entries of H3sprite.lod in 10 s: 979 KB in 4.6 s of read time, about 3 ms each | Same, boot-default IDE speed | Same; the whole-entry reads that replaced it were not timed separately | + +## When not to use + +- Small files and loading screens: when nothing on screen has to move, read + at full speed and get it over with. +- Programs that take over the machine and drive the IDE port themselves are + outside these measurements. +- Measured on one CF card with SFS. Other cards, other filesystems and the + secondary SD slot (sagasd.device) were not measured; the SD card driver + may behave differently. diff --git a/tools/v4/tests/ide-read-rate/test.c b/tools/v4/tests/ide-read-rate/test.c new file mode 100644 index 0000000..d41bb53 --- /dev/null +++ b/tools/v4/tests/ide-read-rate/test.c @@ -0,0 +1,99 @@ +/* ide-read-rate: read rate of whole files through dos.library Read(), and the + * CPU left to other tasks while reading + * (hardware/best-practices/disk-reads-stall-lower-priorities.md). + * + * Reads each file named in the environment variable AKB_READ_FILES (space + * separated, e.g. SetEnv AKB_READ_FILES "Work:a.bin Work:b.bin") once, in + * 64 KB pieces. Use files of a few MB that were not read since boot, so that + * the filesystem cache does not answer. A task at priority -1 counts loop + * passes meanwhile: its rate while reading, against its idle rate, is the CPU + * the reads leave. It is below this task, so it cannot starve anything. + * v4:timeout 120 */ +#include "../akbtest.h" +#include +#include +#include +#include +#include +#include + +#define PIECE (64 * 1024) + +struct Device *TimerBase; +static ULONG eclockHz; +static volatile ULONG counter; +static volatile int stopCounter, counterDone; +static UBYTE piece[PIECE]; + +static double seconds(void) +{ + struct EClockVal v; + ReadEClock(&v); + return ((double)v.ev_hi * 4294967296.0 + v.ev_lo) / eclockHz; +} + +static void counterMain(void) +{ + while (!stopCounter) + counter++; + counterDone = 1; +} + +int main(void) +{ + static char files[1024]; + struct timerequest tr; + struct EClockVal v; + double t0, idleRate, readSecs = 0, bytes = 0, counts = 0; + char *name; + ULONG c0; + + AKB_BEGIN("ide-read-rate"); + akb_machine(); + if (GetVar((CONST_STRPTR)"AKB_READ_FILES", (STRPTR)files, sizeof(files), 0) <= 0) { + AKB_FAIL("files", "set AKB_READ_FILES to cold files of a few MB on the drive under test"); + return AKB_END(); + } + if (OpenDevice((CONST_STRPTR)TIMERNAME, UNIT_ECLOCK, (struct IORequest *)&tr, 0)) { + AKB_FAIL("timer", "timer.device did not open"); + return AKB_END(); + } + TimerBase = tr.tr_node.io_Device; + eclockHz = ReadEClock(&v); + if (!CreateNewProcTags(NP_Entry, (ULONG)counterMain, NP_Name, (ULONG)"ide-read-rate counter", + NP_Priority, -1, NP_StackSize, 4096, TAG_DONE)) { + AKB_FAIL("counter", "task did not start"); + CloseDevice((struct IORequest *)&tr); + return AKB_END(); + } + Delay(10); + c0 = counter; t0 = seconds(); Delay(100); + idleRate = (counter - c0) / (seconds() - t0); + + for (name = strtok(files, " "); name; name = strtok(NULL, " ")) { + BPTR fh = Open((CONST_STRPTR)name, MODE_OLDFILE); + LONG got; + if (!fh) { + AKB_FAIL("open", "%s", name); + continue; + } + c0 = counter; t0 = seconds(); + while ((got = Read(fh, piece, PIECE)) > 0) + bytes += got; + readSecs += seconds() - t0; + counts += counter - c0; + Close(fh); + } + stopCounter = 1; + while (!counterDone) + Delay(1); + CloseDevice((struct IORequest *)&tr); + + AKB_CHECK("files-read", bytes > 0 && readSecs > 0, "nothing read"); + if (bytes > 0 && readSecs > 0) { + AKB_VALUE("read-kb", bytes / 1024, "KB"); + AKB_VALUE("read-rate", bytes / 1024 / readSecs, "KB/s"); + AKB_VALUE("cpu-left", 100.0 * counts / (idleRate * readSecs), "%"); + } + return AKB_END(); +} diff --git a/tools/v4/tests/ide-read-stall/test.c b/tools/v4/tests/ide-read-stall/test.c new file mode 100644 index 0000000..acd8523 --- /dev/null +++ b/tools/v4/tests/ide-read-stall/test.c @@ -0,0 +1,136 @@ +/* ide-read-stall: does a file read hold up tasks of a higher priority than the + * reader? (hardware/best-practices/disk-reads-stall-lower-priorities.md) + * + * A task at priority 5, then one at priority 12, sleeps in 1 ms timer.device + * requests and records how late each wake-up is: first idle, then while this + * task (priority 0) reads the files named in AKB_READ_FILES in 64 KB pieces. + * The first half of the files is read for priority 5, the second half for 12; + * use cold files of a few MB (see ide-read-rate). The sleeping task cannot + * starve anything, and it ends itself after 30 s. + * v4:timeout 120 */ +#include "../akbtest.h" +#include +#include +#include +#include +#include +#include + +#define PIECE (64 * 1024) + +struct Device *TimerBase; +static ULONG eclockHz; +static volatile int phase, wakerDone; /* phase 0 idle, 1 reading, 2 stop */ +static volatile ULONG wakes[2], lateMax[2]; +static volatile double lateSum[2]; +static UBYTE piece[PIECE]; + +static double seconds(void) +{ + struct EClockVal v; + ReadEClock(&v); + return ((double)v.ev_hi * 4294967296.0 + v.ev_lo) / eclockHz; +} + +static void wakerMain(void) +{ + struct MsgPort *port = CreateMsgPort(); + struct timerequest *tr = port ? (struct timerequest *)CreateIORequest(port, sizeof(*tr)) : NULL; + double start = seconds(); + if (tr && OpenDevice((CONST_STRPTR)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)tr, 0) == 0) { + while (phase < 2 && seconds() - start < 30.0) { + int p = phase; + double t0 = seconds(), late; + tr->tr_node.io_Command = TR_ADDREQUEST; + tr->tr_time.tv_secs = 0; + tr->tr_time.tv_micro = 1000; + DoIO((struct IORequest *)tr); + late = (seconds() - t0) * 1e6 - 1000.0; + wakes[p]++; + lateSum[p] += late; + if (late > lateMax[p]) + lateMax[p] = (ULONG)late; + } + CloseDevice((struct IORequest *)tr); + } + if (tr) DeleteIORequest((struct IORequest *)tr); + if (port) DeleteMsgPort(port); + wakerDone = 1; +} + +static void measure(int priority, char **names, int count) +{ + char key[40]; + double t0, idleSecs, readSecs = 0, bytes = 0; + int i; + phase = 0; wakerDone = 0; + wakes[0] = wakes[1] = lateMax[0] = lateMax[1] = 0; + lateSum[0] = lateSum[1] = 0; + if (!CreateNewProcTags(NP_Entry, (ULONG)wakerMain, NP_Name, (ULONG)"ide-read-stall waker", + NP_Priority, priority, NP_StackSize, 4096, TAG_DONE)) { + AKB_FAIL("waker", "task at priority %d did not start", priority); + return; + } + t0 = seconds(); Delay(100); idleSecs = seconds() - t0; + phase = 1; + for (i = 0; i < count; i++) { + BPTR fh = Open((CONST_STRPTR)names[i], MODE_OLDFILE); + LONG got; + if (!fh) { + AKB_FAIL("open", "%s", names[i]); + continue; + } + t0 = seconds(); + while ((got = Read(fh, piece, PIECE)) > 0) + bytes += got; + readSecs += seconds() - t0; + Close(fh); + } + phase = 2; + while (!wakerDone) + Delay(1); + if (readSecs <= 0) { + AKB_FAIL("files-read", "nothing read at priority %d", priority); + return; + } + sprintf(key, "pri%d-idle-wakes", priority); AKB_VALUE(key, wakes[0] / idleSecs, "/s"); + sprintf(key, "pri%d-idle-late-max", priority); AKB_VALUE(key, lateMax[0], "us"); + sprintf(key, "pri%d-reading-wakes", priority); AKB_VALUE(key, wakes[1] / readSecs, "/s"); + sprintf(key, "pri%d-reading-late-mean", priority); AKB_VALUE(key, wakes[1] && lateSum[1] > 0 ? lateSum[1] / wakes[1] : 0, "us"); + sprintf(key, "pri%d-reading-late-max", priority); AKB_VALUE(key, lateMax[1], "us"); + sprintf(key, "pri%d-read-rate", priority); AKB_VALUE(key, bytes / 1024 / readSecs, "KB/s"); +} + +int main(void) +{ + static char files[1024]; + char *names[32]; + struct timerequest tr; + struct EClockVal v; + int n = 0; + char *name; + + AKB_BEGIN("ide-read-stall"); + akb_machine(); + if (GetVar((CONST_STRPTR)"AKB_READ_FILES", (STRPTR)files, sizeof(files), 0) <= 0) { + AKB_FAIL("files", "set AKB_READ_FILES to cold files of a few MB on the drive under test"); + return AKB_END(); + } + for (name = strtok(files, " "); name && n < 32; name = strtok(NULL, " ")) + names[n++] = name; + if (n < 2) { + AKB_FAIL("files", "need at least two files, one set per priority"); + return AKB_END(); + } + if (OpenDevice((CONST_STRPTR)TIMERNAME, UNIT_ECLOCK, (struct IORequest *)&tr, 0)) { + AKB_FAIL("timer", "timer.device did not open"); + return AKB_END(); + } + TimerBase = tr.tr_node.io_Device; + eclockHz = ReadEClock(&v); + measure(5, names, n / 2); + measure(12, names + n / 2, n - n / 2); + CloseDevice((struct IORequest *)&tr); + AKB_PASS("measured"); + return AKB_END(); +}