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
15#ifdef R__HAS_URING
16 #include "ROOT/RIoUring.hxx"
17#endif
18
19#include "TError.h"
20
21#include <cerrno>
22#include <cstring>
23#include <memory>
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 thread_local bool uring_failed = false;
119 if (!uring_failed) {
120 try {
121 RIoUring ring; // throws std::runtime_error
122 std::vector<RIoUring::RReadEvent> reads;
123 reads.reserve(nReq);
124 for (std::size_t i = 0; i < nReq; ++i) {
126 ev.fBuffer = ioVec[i].fBuffer;
127 ev.fOffset = ioVec[i].fOffset;
128 ev.fSize = ioVec[i].fSize;
129 ev.fFileDes = fFileDes;
130 reads.push_back(ev);
131 }
132 ring.SubmitReadsAndWait(reads.data(), nReq);
133 for (std::size_t i = 0; i < nReq; ++i) {
134 ioVec[i].fOutBytes = reads.at(i).fOutBytes;
135 }
136 return;
137 }
138 catch(const std::runtime_error &e) {
139 Warning("RIoUring", "io_uring is unexpectedly not available because:\n%s", e.what());
140 Warning("RRawFileUnix",
141 "io_uring setup failed, falling back to blocking I/O in ReadV");
142 uring_failed = true;
143 }
144 }
145#endif
146 RRawFile::ReadVImpl(ioVec, nReq);
147}
148
149size_t ROOT::Internal::RRawFileUnix::ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset)
150{
151 size_t total_bytes = 0;
152 while (nbytes) {
153#ifdef R__SEEK64
154 ssize_t res = pread64(fFileDes, buffer, nbytes, offset);
155#else
156 ssize_t res = pread(fFileDes, buffer, nbytes, offset);
157#endif
158 if (res < 0) {
159 if (errno == EINTR)
160 continue;
161 throw std::runtime_error("Cannot read from '" + fUrl + "', error: " + std::string(strerror(errno)));
162 } else if (res == 0) {
163 return total_bytes;
164 }
165 R__ASSERT(static_cast<size_t>(res) <= nbytes);
166 buffer = reinterpret_cast<unsigned char *>(buffer) + res;
167 nbytes -= res;
168 total_bytes += res;
169 offset += res;
170 }
171 return total_bytes;
172}
173
174void ROOT::Internal::RRawFileUnix::UnmapImpl(void *region, size_t nbytes)
175{
176 int rv = munmap(region, nbytes);
177 if (rv != 0)
178 throw std::runtime_error(std::string("Cannot remove memory mapping: ") + strerror(errno));
179}
#define e(i)
Definition RSha256.hxx:103
#define R__ASSERT(e)
Definition TError.h:118
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
void SubmitReadsAndWait(RReadEvent *readEvents, unsigned int nReads)
Submit a number of read events and wait for completion.
Definition RIoUring.hxx:99
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:114
Basic read event composed of IO data and a target file descriptor.
Definition RIoUring.hxx:84
int fFileDes
The file descriptor.
Definition RIoUring.hxx:94
std::uint64_t fOffset
The file offset.
Definition RIoUring.hxx:88
void * fBuffer
The destination for reading.
Definition RIoUring.hxx:86
std::size_t fSize
The number of desired bytes.
Definition RIoUring.hxx:90
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