Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RRawFileUnix.cxx
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Jakob Blomer
3
4/*************************************************************************
5 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "ROOT/RConfig.hxx"
13#include "ROOT/RRawFileUnix.hxx"
14#include "ROOT/RMakeUnique.hxx"
15
16#ifdef R__HAS_URING
17 #include "ROOT/RIoUring.hxx"
18#endif
19
20#include "TError.h"
21
22#include <cerrno>
23#include <cstring>
24#include <stdexcept>
25#include <string>
26#include <utility>
27#include <vector>
28
29#include <fcntl.h>
30#include <sys/mman.h>
31#include <sys/stat.h>
32#include <unistd.h>
33
34namespace {
35constexpr int kDefaultBlockSize = 4096; // If fstat() does not provide a block size hint, use this value instead
36} // anonymous namespace
37
39 : RRawFile(url, options), fFileDes(-1)
40{
41}
42
44{
45 if (fFileDes >= 0)
46 close(fFileDes);
47}
48
49std::unique_ptr<ROOT::Internal::RRawFile> ROOT::Internal::RRawFileUnix::Clone() const
50{
51 return std::make_unique<RRawFileUnix>(fUrl, fOptions);
52}
53
55 return kFeatureHasSize | kFeatureHasMmap;
56}
57
59{
60#ifdef R__SEEK64
61 struct stat64 info;
62 int res = fstat64(fFileDes, &info);
63#else
64 struct stat info;
65 int res = fstat(fFileDes, &info);
66#endif
67 if (res != 0)
68 throw std::runtime_error("Cannot call fstat on '" + fUrl + "', error: " + std::string(strerror(errno)));
69 return info.st_size;
70}
71
72void *ROOT::Internal::RRawFileUnix::MapImpl(size_t nbytes, std::uint64_t offset, std::uint64_t &mapdOffset)
73{
74 static std::uint64_t szPageBitmap = sysconf(_SC_PAGESIZE) - 1;
75 mapdOffset = offset & ~szPageBitmap;
76 nbytes += offset & szPageBitmap;
77
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));
81 return result;
82}
83
85{
86#ifdef R__SEEK64
87 fFileDes = open64(GetLocation(fUrl).c_str(), O_RDONLY);
88#else
89 fFileDes = open(GetLocation(fUrl).c_str(), O_RDONLY);
90#endif
91 if (fFileDes < 0) {
92 throw std::runtime_error("Cannot open '" + fUrl + "', error: " + std::string(strerror(errno)));
93 }
94
95 if (fOptions.fBlockSize >= 0)
96 return;
97
98#ifdef R__SEEK64
99 struct stat64 info;
100 int res = fstat64(fFileDes, &info);
101#else
102 struct stat info;
103 int res = fstat(fFileDes, &info);
104#endif
105 if (res != 0) {
106 throw std::runtime_error("Cannot call fstat on '" + fUrl + "', error: " + std::string(strerror(errno)));
107 }
108 if (info.st_blksize > 0) {
109 fOptions.fBlockSize = info.st_blksize;
110 } else {
111 fOptions.fBlockSize = kDefaultBlockSize;
112 }
113}
114
115void ROOT::Internal::RRawFileUnix::ReadVImpl(RIOVec *ioVec, unsigned int nReq)
116{
117#ifdef R__HAS_URING
118 if (RIoUring::IsAvailable()) {
119 RIoUring ring;
120 std::vector<RIoUring::RReadEvent> reads;
121 reads.reserve(nReq);
122 for (std::size_t i = 0; i < nReq; ++i) {
124 ev.fBuffer = ioVec[i].fBuffer;
125 ev.fOffset = ioVec[i].fOffset;
126 ev.fSize = ioVec[i].fSize;
127 ev.fFileDes = fFileDes;
128 reads.push_back(ev);
129 }
130 ring.SubmitReadsAndWait(reads.data(), nReq);
131 for (std::size_t i = 0; i < nReq; ++i) {
132 ioVec[i].fOutBytes = reads.at(i).fOutBytes;
133 }
134 return;
135 }
136 Warning("RRawFileUnix",
137 "io_uring setup failed, falling back to default ReadV implementation");
138#endif
139 RRawFile::ReadVImpl(ioVec, nReq);
140}
141
142size_t ROOT::Internal::RRawFileUnix::ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset)
143{
144 size_t total_bytes = 0;
145 while (nbytes) {
146#ifdef R__SEEK64
147 ssize_t res = pread64(fFileDes, buffer, nbytes, offset);
148#else
149 ssize_t res = pread(fFileDes, buffer, nbytes, offset);
150#endif
151 if (res < 0) {
152 if (errno == EINTR)
153 continue;
154 throw std::runtime_error("Cannot read from '" + fUrl + "', error: " + std::string(strerror(errno)));
155 } else if (res == 0) {
156 return total_bytes;
157 }
158 R__ASSERT(static_cast<size_t>(res) <= nbytes);
159 buffer = reinterpret_cast<unsigned char *>(buffer) + res;
160 nbytes -= res;
161 total_bytes += res;
162 offset += res;
163 }
164 return total_bytes;
165}
166
167void ROOT::Internal::RRawFileUnix::UnmapImpl(void *region, size_t nbytes)
168{
169 int rv = munmap(region, nbytes);
170 if (rv != 0)
171 throw std::runtime_error(std::string("Cannot remove memory mapping: ") + strerror(errno));
172}
#define R__ASSERT(e)
Definition TError.h:120
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:231
static bool IsAvailable()
Check if io_uring is available on this system.
Definition RIoUring.hxx:85
void SubmitReadsAndWait(RReadEvent *readEvents, unsigned int nReads)
Submit a number of read events and wait for completion.
Definition RIoUring.hxx:115
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.
Definition RRawFile.hxx:43
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...
Definition RRawFile.cxx:101
Basic read event composed of IO data and a target file descriptor.
Definition RIoUring.hxx:100
int fFileDes
The file descriptor.
Definition RIoUring.hxx:110
std::uint64_t fOffset
The file offset.
Definition RIoUring.hxx:104
void * fBuffer
The destination for reading.
Definition RIoUring.hxx:102
std::size_t fSize
The number of desired bytes.
Definition RIoUring.hxx:106
Used for vector reads from multiple offsets into multiple buffers.
Definition RRawFile.hxx:71
std::size_t fOutBytes
The number of actually read bytes, set by ReadV()
Definition RRawFile.hxx:79
std::size_t fSize
The number of desired bytes.
Definition RRawFile.hxx:77
void * fBuffer
The destination for reading.
Definition RRawFile.hxx:73
std::uint64_t fOffset
The file offset.
Definition RRawFile.hxx:75
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition RRawFile.hxx:59