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,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<const iovec*>(spans_.data()),
static_cast<int>(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<const iovec*>(spans_.data()),
static_cast<int>(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<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 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

View File

@ -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; }
};