public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/readahead02: Wait for the readahead()
@ 2025-11-24 13:13 Cyril Hrubis
  2025-11-26  7:37 ` Li Wang via ltp
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Cyril Hrubis @ 2025-11-24 13:13 UTC (permalink / raw)
  To: ltp; +Cc: Amir Goldstein

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.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 .../kernel/syscalls/readahead/readahead02.c   | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/testcases/kernel/syscalls/readahead/readahead02.c b/testcases/kernel/syscalls/readahead/readahead02.c
index f007db187..e0f56e989 100644
--- a/testcases/kernel/syscalls/readahead/readahead02.c
+++ b/testcases/kernel/syscalls/readahead/readahead02.c
@@ -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 */
-- 
2.51.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-12-19 10:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-24 13:13 [LTP] [PATCH] syscalls/readahead02: Wait for the readahead() Cyril Hrubis
2025-11-26  7:37 ` Li Wang via ltp
2025-11-26  7:44 ` Jan Stancek via ltp
2025-12-19 10:52 ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox