From 41a86d41a9b2b937faeda6cafc225d50f266a0ad Mon Sep 17 00:00:00 2001 From: Jan Wassenberg Date: Fri, 15 Aug 2025 06:30:07 -0700 Subject: [PATCH] Fix preadv error: only enable if we have a handle PiperOrigin-RevId: 795455020 --- io/io.cc | 30 +++++++++++++++++------------- io/io.h | 2 +- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/io/io.cc b/io/io.cc index df39f7b..d19ec10 100644 --- a/io/io.cc +++ b/io/io.cc @@ -226,21 +226,26 @@ void InternalInit() { } uint64_t IOBatch::Read(const File& file) const { -#if GEMMA_IO_PREADV HWY_ASSERT(!spans_.empty()); - ssize_t bytes_read; - for (;;) { - bytes_read = - preadv(file.Handle(), reinterpret_cast(spans_.data()), - static_cast(spans_.size()), offset_); - if (bytes_read >= 0) break; - if (errno == EINTR) continue; // signal: retry - HWY_WARN("preadv failed, errno %d.", errno); - return 0; +#if GEMMA_IO_PREADV + if (file.Handle() != -1) { + ssize_t bytes_read; + for (;;) { + bytes_read = + preadv(file.Handle(), reinterpret_cast(spans_.data()), + static_cast(spans_.size()), offset_); + if (bytes_read >= 0) break; + if (errno == EINTR) continue; // signal: retry + HWY_WARN("preadv(%d) for %4zu spans from offset %12zu failed, errno %d.", + file.Handle(), spans_.size(), offset_, errno); + return 0; + } + return static_cast(bytes_read); } - return static_cast(bytes_read); -#else +#endif // GEMMA_IO_PREADV + + // preadv disabled or no handle: use normal reads (higher kernel overhead). uint64_t total = 0; uint64_t offset = offset_; for (const IOSpan& span : spans_) { @@ -249,7 +254,6 @@ uint64_t IOBatch::Read(const File& file) const { offset += span.bytes; } return total; -#endif } } // namespace gcpp diff --git a/io/io.h b/io/io.h index d9481bc..f90a636 100644 --- a/io/io.h +++ b/io/io.h @@ -68,7 +68,7 @@ class File { // modify internal state. This is only expected to be called once per file. virtual MapPtr Map() = 0; - // For use by `IOBatch::Read`. + // Returns handle for use by `IOBatch::Read`, or -1 if not supported. virtual int Handle() const { return -1; } };