AltraMayor / AltraMayor/f3

f3read requires O_DIRECT on Cygwin to avoid reading cached data

Aperta
#232 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C
Stelle
3.4k
Fork
177
Merge medio
2g 15h
PR unite (30g)
2

Descrizione

The method used to avoid that `f3read` reads previously cached data does not work on Cygwin. This may result in false positive tests. Here is the local patch used for the new Cygwin package [f3-8.0-2](https://cygwin.com/packages/summary/f3.html):

```patch
From c85e6f20d1de2c5cab358929e490d760e50e41d1 Mon Sep 17 00:00:00 2001
From: Christian Franke
Date: Mon, 27 Jan 2025 12:04:17 +0100
Subject: [PATCH] f3read: ensure unbuffered reads also on Cygwin

Use O_DIRECT because POSIX_FADV_DONTNEED etc. have not the desired
effect.
---
f3read.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/f3read.c b/f3read.c
index 7537b2e..fd670c5 100644
--- a/f3read.c
+++ b/f3read.c
@@ -1,5 +1,8 @@
#define _POSIX_C_SOURCE 200112L
#define _XOPEN_SOURCE 600
+#ifdef __CYGWIN__
+#define _BSD_SOURCE /* required for O_DIRECT */
+#endif

#include
#include
@@ -243,16 +246,18 @@ static void validate_file(const char *path, int number, struct flow *fw,
printf("Validating file %s ... ", filename);
fflush(stdout);
#ifdef __CYGWIN__
- /* We don't need write access, but some kernels require that
- * the file descriptor passed to fdatasync(2) to be writable.
+ /* On Cygwin, the fdatasync() and posix_fadvise() calls below do
+ * not have the desired effect. Use O_DIRECT instead to avoid
+ * read caching.
*/
- fd = open(full_fn, O_RDWR);
+ fd = open(full_fn, O_RDONLY | O_DIRECT);
#else
fd = open(full_fn, O_RDONLY);
#endif
if (fd < 0)
err(errno, "Can't open file %s", full_fn);

+#ifndef __CYGWIN__
/* If the kernel follows our advice, f3read won't ever read from cache
* even when testing small memory cards without a remount, and
* we should have a better reading-speed measurement.
@@ -262,6 +267,7 @@ static void validate_file(const char *path, int number, struct flow *fw,

/* Help the kernel to help us. */
assert(!posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL));
+#endif

saved_errno = 0;
expected_offset = (uint64_t)number * GIGABYTES;
--
2.45.1
```

BTW: It may make sense to use the (non-POSIX) flag O_DIRECT on all platforms supporting it (Linux, *BSD, ...).

[POSIX 2024](https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_fadvise.html) says about `posix_fadvise()`: _The implementation **may** use this information to optimize handling of the specified data._

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.