35constexpr int kDefaultBlockSize = 4096;
39 :
RRawFile(url, options), fFileDes(-1)
51 return std::make_unique<RRawFileUnix>(fUrl, fOptions);
55 return kFeatureHasSize | kFeatureHasMmap;
62 int res = fstat64(fFileDes, &info);
65 int res = fstat(fFileDes, &info);
68 throw std::runtime_error(
"Cannot call fstat on '" + fUrl +
"', error: " + std::string(strerror(errno)));
74 static std::uint64_t szPageBitmap = sysconf(_SC_PAGESIZE) - 1;
75 mapdOffset = offset & ~szPageBitmap;
76 nbytes += offset & szPageBitmap;
78 void *result = mmap(
nullptr, nbytes, PROT_READ, MAP_PRIVATE, fFileDes, mapdOffset);
79 if (result == MAP_FAILED)
80 throw std::runtime_error(std::string(
"Cannot perform memory mapping: ") + strerror(errno));
87 fFileDes = open64(GetLocation(fUrl).c_str(), O_RDONLY);
89 fFileDes = open(GetLocation(fUrl).c_str(), O_RDONLY);
92 throw std::runtime_error(
"Cannot open '" + fUrl +
"', error: " + std::string(strerror(errno)));
95 if (fOptions.fBlockSize >= 0)
100 int res = fstat64(fFileDes, &info);
103 int res = fstat(fFileDes, &info);
106 throw std::runtime_error(
"Cannot call fstat on '" + fUrl +
"', error: " + std::string(strerror(errno)));
108 if (info.st_blksize > 0) {
109 fOptions.fBlockSize = info.st_blksize;
111 fOptions.fBlockSize = kDefaultBlockSize;
118 thread_local bool uring_failed =
false;
122 std::vector<RIoUring::RReadEvent> reads;
124 for (std::size_t i = 0; i < nReq; ++i) {
133 for (std::size_t i = 0; i < nReq; ++i) {
134 ioVec[i].
fOutBytes = reads.at(i).fOutBytes;
138 catch(
const std::runtime_error &
e) {
139 Warning(
"RIoUring",
"io_uring is unexpectedly not available because:\n%s",
e.what());
141 "io_uring setup failed, falling back to blocking I/O in ReadV");
151 size_t total_bytes = 0;
154 ssize_t res = pread64(fFileDes, buffer, nbytes, offset);
156 ssize_t res = pread(fFileDes, buffer, nbytes, offset);
161 throw std::runtime_error(
"Cannot read from '" + fUrl +
"', error: " + std::string(strerror(errno)));
162 }
else if (res == 0) {
165 R__ASSERT(
static_cast<size_t>(res) <= nbytes);
166 buffer =
reinterpret_cast<unsigned char *
>(buffer) + res;
176 int rv = munmap(region, nbytes);
178 throw std::runtime_error(std::string(
"Cannot remove memory mapping: ") + strerror(errno));
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
void SubmitReadsAndWait(RReadEvent *readEvents, unsigned int nReads)
Submit a number of read events and wait for completion.
void UnmapImpl(void *region, size_t nbytes) final
Derived classes with mmap support must be able to unmap the memory area handed out by Map()
std::unique_ptr< RRawFile > Clone() const final
Create a new RawFile that accesses the same resource. The file pointer is reset to zero.
RRawFileUnix(std::string_view url, RRawFile::ROptions options)
std::uint64_t GetSizeImpl() final
Derived classes should return the file size or kUnknownFileSize.
void ReadVImpl(RIOVec *ioVec, unsigned int nReq) final
By default implemented as a loop of ReadAt calls but can be overwritten, e.g. XRootD or DAVIX impleme...
int GetFeatures() const final
Derived classes shall inform the user about the supported functionality, which can possibly depend on...
void * MapImpl(size_t nbytes, std::uint64_t offset, std::uint64_t &mapdOffset) final
If a derived class supports mmap, the MapImpl and UnmapImpl calls are supposed to be implemented,...
void OpenImpl() final
OpenImpl() is called at most once and before any call to either DoReadAt or DoGetSize.
size_t ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset) final
Derived classes should implement low-level reading without buffering.
The RRawFile provides read-only access to local and remote files.
virtual void ReadVImpl(RIOVec *ioVec, unsigned int nReq)
By default implemented as a loop of ReadAt calls but can be overwritten, e.g. XRootD or DAVIX impleme...
Basic read event composed of IO data and a target file descriptor.
int fFileDes
The file descriptor.
std::uint64_t fOffset
The file offset.
void * fBuffer
The destination for reading.
std::size_t fSize
The number of desired bytes.
Used for vector reads from multiple offsets into multiple buffers.
std::size_t fOutBytes
The number of actually read bytes, set by ReadV()
std::size_t fSize
The number of desired bytes.
void * fBuffer
The destination for reading.
std::uint64_t fOffset
The file offset.
On construction, an ROptions parameter can customize the RRawFile behavior.