Skip to contentSkip to conversation

syscalls/readahead02: Wait for the readahead()

PATCH In review Cyril Hrubis 2 participants 4 messages Nov 24, 2025 – Dec 19, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/readahead02: Wait for the readahead()

The test did request readahead on a file and then immediatelly tried to access the data and measure if readahead saved I/O or not. The problem is that we need to wait a bit for the readahead to happen, especially on hardware with slower I/O speeds. So the test now waits a bit for the readahead to start and the loops for a while, with a short usleeps, until retires are reached or until page cache stops to grow.

---
 .../kernel/syscalls/readahead/readahead02.c   | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)
testcases/kernel/syscalls/readahead/readahead02.c +41 -0
index f007db187..e0f56e989 100644
@@ -39,6 +39,8 @@ static char testfile[PATH_MAX] = "testfile";
 #define MEMINFO_FNAME "/proc/meminfo"
 #define PROC_IO_FNAME "/proc/self/io"
 #define DEFAULT_FILESIZE (64 * 1024 * 1024)
+#define INITIAL_SHORT_SLEEP_US 10000
+#define SHORT_SLEEP_US 5000
 
 static size_t testfile_size = DEFAULT_FILESIZE;
 static char *opt_fsizestr;
@@ -173,8 +175,47 @@ static int read_testfile(struct tcase *tc, int do_readahead,
 
 			i++;
 			offset += readahead_length;
+			/* Wait a bit so that the readahead() has chance to start. */
+			usleep(INITIAL_SHORT_SLEEP_US);
+			/*
+			 * We assume that the worst case I/O speed is around
+			 * 5MB/s which is roughly 5 bytes per 1 us, which gives
+			 * us upper bound for retries that is
+			 * readahead_size/(5 * SHORT_SLEEP_US).
+			 *
+			 * We also monitor the cache size increases before and
+			 * after the sleep. With the same assumption about the
+			 * speed we are supposed to read at least 5 *
+			 * SHORT_SLEEP_US bytes during that time. That amound
+			 * is genreally quite close a page size so that we just
+			 * assume that we sould continue as long as the cache
+			 * increases.
+			 *
+			 * Of course all of this is inprecise on multitasking
+			 * OS however even on a system where there are several
+			 * processes figthing for I/O this loop will wait as
+			 * long a cache is increasing which will gives us high
+			 * chance of waiting for the readahead to happen.
+			 */
+			unsigned long cached_prev, cached_cur = get_cached_size();
+			int retries = readahead_length / (5 * SHORT_SLEEP_US);
+
+			tst_res(TDEBUG, "Readahead cached %lu", cached_cur);
+
+			do {
+				usleep(SHORT_SLEEP_US);
+
+				cached_prev = cached_cur;
+				cached_cur = get_cached_size();
+
+				if (cached_cur <= cached_prev)
+					break;
+			} while (retries-- > 0);
+
 		} while ((size_t)offset < fsize);
+
 		tst_res(TINFO, "readahead calls made: %zu", i);
+
 		*cached = get_cached_size();
 
 		/* offset of file shouldn't change after readahead */
Signature · 5 lines
2.51.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by Li Wang on v1 1/1
#1 lore raw
Quoted 9 lines · Cyril Hrubis

The test did request readahead on a file and then immediatelly tried to access the data and measure if readahead saved I/O or not. The problem is that we need to wait a bit for the readahead to happen, especially on hardware with slower I/O speeds. So the test now waits a bit for the readahead to start and the loops for a while, with a short usleeps, until retires are reached or until page cache stops to grow.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by Jan Stancek on v1 1/1
#2 lore raw
In reply to Cyril Hrubis
Quoted 9 lines · Cyril Hrubis

The test did request readahead on a file and then immediatelly tried to access the data and measure if readahead saved I/O or not. The problem is that we need to wait a bit for the readahead to happen, especially on hardware with slower I/O speeds. So the test now waits a bit for the readahead to start and the loops for a while, with a short usleeps, until retires are reached or until page cache stops to grow.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp