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/stat.h>
31#include <unistd.h>
32
33namespace {
34constexpr int kDefaultBlockSize = 4096; // If fstat() does not provide a block size hint, use this value instead
35} // anonymous namespace
36
37ROOT::Internal::RRawFileUnix::RRawFileUnix(std::string_view url, ROptions options) : RRawFile(url, options) {}
38
40{
41 if (fFileDes >= 0)
42 close(fFileDes);
43}
44
45std::unique_ptr<ROOT::Internal::RRawFile> ROOT::Internal::RRawFileUnix::Clone() const
46{
47 return std::make_unique<RRawFileUnix>(fUrl, fOptions);
48}
49
51{
52#ifdef R__SEEK64
53 struct stat64 info;
54 int res = fstat64(fFileDes, &info);
55#else
56 struct stat info;
57 int res = fstat(fFileDes, &info);
58#endif
59 if (res != 0)
60 throw std::runtime_error("Cannot call fstat on '" + fUrl + "', error: " + std::string(strerror(errno)));
61 return info.st_size;
62}
63
65{
66#ifdef R__SEEK64
67 fFileDes = open64(GetLocation(fUrl).c_str(), O_RDONLY);
68#else
69 fFileDes = open(GetLocation(fUrl).c_str(), O_RDONLY);
70#endif
71 if (fFileDes < 0) {
72 throw std::runtime_error("Cannot open '" + fUrl + "', error: " + std::string(strerror(errno)));
73 }
74
75 if (fOptions.fBlockSize != ROptions::kUseDefaultBlockSize)
76 return;
77
78#ifdef R__SEEK64
79 struct stat64 info;
80 int res = fstat64(fFileDes, &info);
81#else
82 struct stat info;
83 int res = fstat(fFileDes, &info);
84#endif
85 if (res != 0) {
86 throw std::runtime_error("Cannot call fstat on '" + fUrl + "', error: " + std::string(strerror(errno)));
87 }
88 if (info.st_blksize > 0) {
89 fOptions.fBlockSize = info.st_blksize;
90 } else {
91 fOptions.fBlockSize = kDefaultBlockSize;
92 }
93}
94
95void ROOT::Internal::RRawFileUnix::ReadVImpl(RIOVec *ioVec, unsigned int nReq)
96{
97#ifdef R__HAS_URING
98 thread_local bool uring_failed = false;
99 if (!uring_failed) {
100 try {
101 RIoUring ring; // throws std::runtime_error
102 std::vector<RIoUring::RReadEvent> reads;
103 reads.reserve(nReq);
104 for (std::size_t i = 0; i < nReq; ++i) {
106 ev.fBuffer = ioVec[i].fBuffer;
107 ev.fOffset = ioVec[i].fOffset;
108 ev.fSize = ioVec[i].fSize;
109 ev.fFileDes = fFileDes;
110 reads.push_back(ev);
111 }
112 ring.SubmitReadsAndWait(reads.data(), nReq);
113 for (std::size_t i = 0; i < nReq; ++i) {
114 ioVec[i].fOutBytes = reads.at(i).fOutBytes;
115 }
116 return;
117 }
118 catch(const std::runtime_error &e) {
119 Warning("RIoUring", "io_uring is unexpectedly not available because:\n%s", e.what());
120 Warning("RRawFileUnix",
121 "io_uring setup failed, falling back to blocking I/O in ReadV");
122 uring_failed = true;
123 }
124 }
125#endif
126 RRawFile::ReadVImpl(ioVec, nReq);
127}
128
129size_t ROOT::Internal::RRawFileUnix::ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset)
130{
131 size_t total_bytes = 0;
132 while (nbytes) {
133#ifdef R__SEEK64
134 ssize_t res = pread64(fFileDes, buffer, nbytes, offset);
135#else
136 ssize_t res = pread(fFileDes, buffer, nbytes, offset);
137#endif
138 if (res < 0) {
139 if (errno == EINTR)
140 continue;
141 throw std::runtime_error("Cannot read from '" + fUrl + "', error: " + std::string(strerror(errno)));
142 } else if (res == 0) {
143 return total_bytes;
144 }
145 R__ASSERT(static_cast<size_t>(res) <= nbytes);
146 buffer = reinterpret_cast<unsigned char *>(buffer) + res;
147 nbytes -= res;
148 total_bytes += res;
149 offset += res;
150 }
151 return total_bytes;
152}
#define e(i)
Definition RSha256.hxx:103
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
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
void SubmitReadsAndWait(RReadEvent *readEvents, unsigned int nReads)
Submit a number of read events and wait for completion.
Definition RIoUring.hxx:99
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.
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...
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:99
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:61
std::size_t fOutBytes
The number of actually read bytes, set by ReadV()
Definition RRawFile.hxx:69
std::size_t fSize
The number of desired bytes.
Definition RRawFile.hxx:67
void * fBuffer
The destination for reading.
Definition RRawFile.hxx:63
std::uint64_t fOffset
The file offset.
Definition RRawFile.hxx:65
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition RRawFile.hxx:49