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
96{
97#ifndef __APPLE__
99#else
100 (void)value;
101#endif
102}
103
105{
106#ifdef R__HAS_URING
107 // ROOT may have been built with liburing support while the running kernel does not
108 // support io_uring (GH #12701); probe once per process and warn at the first attempt.
109 static const bool sIoUringAvailable = [] {
110 std::string errMsg;
112 return true;
113 Warning("RIoUring", "io_uring is not available because:\n%s", errMsg.c_str());
114 Warning("RRawFileUnix", "io_uring setup failed, falling back to blocking I/O in ReadV");
115 return false;
116 }();
117 thread_local bool uring_failed = false;
119 try {
120 RIoUring ring; // throws std::runtime_error
121 std::vector<RIoUring::RReadEvent> reads;
122 reads.reserve(nReq);
123 for (std::size_t i = 0; i < nReq; ++i) {
125 ev.fBuffer = ioVec[i].fBuffer;
126 ev.fOffset = ioVec[i].fOffset;
127 ev.fSize = ioVec[i].fSize;
128 ev.fFileDes = fFileDes;
129 reads.push_back(ev);
130 }
131 ring.SubmitReadsAndWait(reads.data(), nReq);
132 for (std::size_t i = 0; i < nReq; ++i) {
133 ioVec[i].fOutBytes = reads.at(i).fOutBytes;
134 }
135 return;
136 }
137 catch(const std::runtime_error &e) {
138 Warning("RIoUring", "io_uring is not available because:\n%s", e.what());
139 Warning("RRawFileUnix",
140 "io_uring setup failed, falling back to blocking I/O in ReadV");
141 uring_failed = true;
142 }
143 }
144#endif
146}
147
148size_t ROOT::Internal::RRawFileUnix::ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset)
149{
150 size_t total_bytes = 0;
151 while (nbytes) {
152#ifdef R__SEEK64
153 ssize_t res = pread64(fFileDes, buffer, nbytes, offset);
154#else
155 ssize_t res = pread(fFileDes, buffer, nbytes, offset);
156#endif
157 if (res < 0) {
158 if (errno == EINTR)
159 continue;
160 throw std::runtime_error("Cannot read from '" + fUrl + "', error: " + std::string(strerror(errno)));
161 } else if (res == 0) {
162 return total_bytes;
163 }
164 R__ASSERT(static_cast<size_t>(res) <= nbytes);
165 buffer = reinterpret_cast<unsigned char *>(buffer) + res;
166 nbytes -= res;
167 total_bytes += res;
168 offset += res;
169 }
170 return total_bytes;
171}
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:130
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
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 value
static bool IsAvailable(std::string *errMsg=nullptr)
Probes whether io_uring is actually usable at runtime.
Definition RIoUring.hxx:90
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 SetDiscourageReadAheadImpl(bool value) final
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:102
Basic read event composed of IO data and a target file descriptor.
Definition RIoUring.hxx:113
Used for vector reads from multiple offsets into multiple buffers.
Definition RRawFile.hxx:61
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition RRawFile.hxx:49