Fix preadv error: only enable if we have a handle

PiperOrigin-RevId: 795455020
This commit is contained in:
Jan Wassenberg 2025-08-15 06:30:07 -07:00 committed by Copybara-Service
parent 78573b6718
commit 41a86d41a9
2 changed files with 18 additions and 14 deletions

View File

@ -226,9 +226,10 @@ void InternalInit() {
} }
uint64_t IOBatch::Read(const File& file) const { uint64_t IOBatch::Read(const File& file) const {
#if GEMMA_IO_PREADV
HWY_ASSERT(!spans_.empty()); HWY_ASSERT(!spans_.empty());
#if GEMMA_IO_PREADV
if (file.Handle() != -1) {
ssize_t bytes_read; ssize_t bytes_read;
for (;;) { for (;;) {
bytes_read = bytes_read =
@ -236,11 +237,15 @@ uint64_t IOBatch::Read(const File& file) const {
static_cast<int>(spans_.size()), offset_); static_cast<int>(spans_.size()), offset_);
if (bytes_read >= 0) break; if (bytes_read >= 0) break;
if (errno == EINTR) continue; // signal: retry if (errno == EINTR) continue; // signal: retry
HWY_WARN("preadv failed, errno %d.", errno); HWY_WARN("preadv(%d) for %4zu spans from offset %12zu failed, errno %d.",
file.Handle(), spans_.size(), offset_, errno);
return 0; return 0;
} }
return static_cast<uint64_t>(bytes_read); return static_cast<uint64_t>(bytes_read);
#else }
#endif // GEMMA_IO_PREADV
// preadv disabled or no handle: use normal reads (higher kernel overhead).
uint64_t total = 0; uint64_t total = 0;
uint64_t offset = offset_; uint64_t offset = offset_;
for (const IOSpan& span : spans_) { for (const IOSpan& span : spans_) {
@ -249,7 +254,6 @@ uint64_t IOBatch::Read(const File& file) const {
offset += span.bytes; offset += span.bytes;
} }
return total; return total;
#endif
} }
} // namespace gcpp } // namespace gcpp

View File

@ -68,7 +68,7 @@ class File {
// modify internal state. This is only expected to be called once per file. // modify internal state. This is only expected to be called once per file.
virtual MapPtr Map() = 0; 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; } virtual int Handle() const { return -1; }
}; };