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 thread_local bool uring_failed = false;
108 if (!uring_failed) {
109 try {
110 RIoUring ring; // throws std::runtime_error
111 std::vector<RIoUring::RReadEvent> reads;
112 reads.reserve(nReq);
113 for (std::size_t i = 0; i < nReq; ++i) {
115 ev.fBuffer = ioVec[i].fBuffer;
116 ev.fOffset = ioVec[i].fOffset;
117 ev.fSize = ioVec[i].fSize;
118 ev.fFileDes = fFileDes;
119 reads.push_back(ev);
120 }
121 ring.SubmitReadsAndWait(reads.data(), nReq);
122 for (std::size_t i = 0; i < nReq; ++i) {
123 ioVec[i].fOutBytes = reads.at(i).fOutBytes;
124 }
125 return;
126 }
127 catch(const std::runtime_error &e) {
128 Warning("RIoUring", "io_uring is unexpectedly not available because:\n%s", e.what());
129 Warning("RRawFileUnix",
130 "io_uring setup failed, falling back to blocking I/O in ReadV");
131 uring_failed = true;
132 }
133 }
134#endif
136}
137
138size_t ROOT::Internal::RRawFileUnix::ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset)
139{
140 size_t total_bytes = 0;
141 while (nbytes) {
142#ifdef R__SEEK64
143 ssize_t res = pread64(fFileDes, buffer, nbytes, offset);
144#else
145 ssize_t res = pread(fFileDes, buffer, nbytes, offset);
146#endif
147 if (res < 0) {
148 if (errno == EINTR)
149 continue;
150 throw std::runtime_error("Cannot read from '" + fUrl + "', error: " + std::string(strerror(errno)));
151 } else if (res == 0) {
152 return total_bytes;
153 }
154 R__ASSERT(static_cast<size_t>(res) <= nbytes);
155 buffer = reinterpret_cast<unsigned char *>(buffer) + res;
156 nbytes -= res;
157 total_bytes += res;
158 offset += res;
159 }
160 return total_bytes;
161}
#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:125
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
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:98
Basic read event composed of IO data and a target file descriptor.
Definition RIoUring.hxx:84
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