CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
DCacheFile Class Reference

#include <DCacheFile.h>

Inheritance diagram for DCacheFile:
Storage IOInput IOOutput

Public Member Functions

virtual void abort (void)
 
void close (void) override
 
virtual void create (const char *name, bool exclusive=false, int perms=0666)
 
virtual void create (const std::string &name, bool exclusive=false, int perms=0666)
 
 DCacheFile (void)
 
 DCacheFile (IOFD fd)
 
 DCacheFile (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 
 DCacheFile (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 
virtual void open (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 
virtual void open (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 
IOOffset position (IOOffset offset, Relative whence=SET) override
 
IOSize read (void *into, IOSize n) override
 
IOSize readv (IOBuffer *into, IOSize buffers) override
 
IOSize readv (IOPosBuffer *into, IOSize buffers) override
 
void resize (IOOffset size) override
 
IOSize write (const void *from, IOSize n) override
 
 ~DCacheFile (void) override
 
- Public Member Functions inherited from Storage
virtual bool eof (void) const
 
virtual void flush (void)
 
virtual IOOffset position (void) const
 
virtual bool prefetch (const IOPosBuffer *what, IOSize n)
 
virtual IOSize read (void *into, IOSize n, IOOffset pos)
 
IOSize read (IOBuffer into, IOOffset pos)
 
virtual void rewind (void)
 
virtual IOOffset size (void) const
 
 Storage (void)
 
virtual IOSize write (const void *from, IOSize n, IOOffset pos)
 
IOSize write (IOBuffer from, IOOffset pos)
 
virtual IOSize writev (const IOPosBuffer *from, IOSize buffers)
 
 ~Storage (void) override
 
- Public Member Functions inherited from IOInput
int read (void)
 
IOSize read (IOBuffer into)
 
IOSize xread (IOBuffer into)
 
IOSize xread (void *into, IOSize n)
 
IOSize xreadv (IOBuffer *into, IOSize buffers)
 
virtual ~IOInput (void)
 Destruct the stream. A no-op. More...
 
- Public Member Functions inherited from IOOutput
IOSize write (unsigned char byte)
 
IOSize write (IOBuffer from)
 
virtual IOSize writev (const IOBuffer *from, IOSize buffers)
 
IOSize xwrite (const void *from, IOSize n)
 
IOSize xwrite (IOBuffer from)
 
IOSize xwritev (const IOBuffer *from, IOSize buffers)
 
virtual ~IOOutput (void)
 Destruct the stream. A no-op. More...
 

Private Attributes

bool m_close
 
IOFD m_fd
 
std::string m_name
 

Additional Inherited Members

- Public Types inherited from Storage
enum  Relative { SET, CURRENT, END }
 

Detailed Description

Definition at line 8 of file DCacheFile.h.

Constructor & Destructor Documentation

DCacheFile::DCacheFile ( void  )

Definition at line 11 of file DCacheFile.cc.

11 : m_fd(EDM_IOFD_INVALID), m_close(false) {}
IOFD m_fd
Definition: DCacheFile.h:37
bool m_close
Definition: DCacheFile.h:38
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
DCacheFile::DCacheFile ( IOFD  fd)

Definition at line 13 of file DCacheFile.cc.

13 : m_fd(fd), m_close(true) {}
IOFD m_fd
Definition: DCacheFile.h:37
bool m_close
Definition: DCacheFile.h:38
fd
Definition: ztee.py:136
DCacheFile::DCacheFile ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Definition at line 15 of file DCacheFile.cc.

References open().

16  : m_fd(EDM_IOFD_INVALID), m_close(false) {
17  open(name, flags, perms);
18 }
IOFD m_fd
Definition: DCacheFile.h:37
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:48
bool m_close
Definition: DCacheFile.h:38
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
DCacheFile::DCacheFile ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Definition at line 20 of file DCacheFile.cc.

References HLT_2018_cff::flags, and open().

21  : m_fd(EDM_IOFD_INVALID), m_close(false) {
22  open(name.c_str(), flags, perms);
23 }
IOFD m_fd
Definition: DCacheFile.h:37
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:48
bool m_close
Definition: DCacheFile.h:38
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
DCacheFile::~DCacheFile ( void  )
override

Definition at line 25 of file DCacheFile.cc.

References m_close, and m_name.

25  {
26  if (m_close)
27  edm::LogError("DCacheFileError") << "Destructor called on dCache file '" << m_name
28  << "' but the file is still open";
29 }
bool m_close
Definition: DCacheFile.h:38
std::string m_name
Definition: DCacheFile.h:39

Member Function Documentation

void DCacheFile::abort ( void  )
virtual

Definition at line 141 of file DCacheFile.cc.

References EDM_IOFD_INVALID, m_close, and m_fd.

141  {
142  if (m_fd != EDM_IOFD_INVALID)
143  dc_close(m_fd);
144 
145  m_close = false;
147 }
IOFD m_fd
Definition: DCacheFile.h:37
bool m_close
Definition: DCacheFile.h:38
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
void DCacheFile::close ( void  )
overridevirtual

Reimplemented from Storage.

Definition at line 122 of file DCacheFile.cc.

References EDM_IOFD_INVALID, m_close, m_fd, and m_name.

Referenced by esMonitoring.AsyncLineReaderMixin::handle_close(), esMonitoring.FDJsonServer::handle_close(), Vispa.Gui.BoxContentDialog.BoxContentDialog::keyPressEvent(), Vispa.Gui.FindDialog.FindDialog::keyPressEvent(), and open().

122  {
123  if (m_fd == EDM_IOFD_INVALID) {
124  edm::LogError("DCacheFileError") << "DCacheFile::close(name='" << m_name << "') called but the file is not open";
125  m_close = false;
126  return;
127  }
128 
129  dc_errno = 0;
130  if (dc_close(m_fd) == -1)
131  edm::LogWarning("DCacheFileWarning") << "dc_close(name='" << m_name << "') failed with error '"
132  << dc_strerror(dc_errno) << "' (dc_errno=" << dc_errno << ")";
133 
134  m_close = false;
136 
137  // Caused hang. Will be added back after problem is fixed.
138  // edm::LogInfo("DCacheFileInfo") << "Closed " << m_name;
139 }
IOFD m_fd
Definition: DCacheFile.h:37
bool m_close
Definition: DCacheFile.h:38
std::string m_name
Definition: DCacheFile.h:39
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
void DCacheFile::create ( const char *  name,
bool  exclusive = false,
int  perms = 0666 
)
virtual

Definition at line 32 of file DCacheFile.cc.

References open(), IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

32  {
33  open(name,
35  perms);
36 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:48
void DCacheFile::create ( const std::string &  name,
bool  exclusive = false,
int  perms = 0666 
)
virtual

Definition at line 38 of file DCacheFile.cc.

References open(), IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

38  {
39  open(name.c_str(),
41  perms);
42 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:48
void DCacheFile::open ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)
virtual

Definition at line 48 of file DCacheFile.cc.

References cms::Exception::addContext(), close(), TauDecayModes::dec, EDM_IOFD_INVALID, edm::errors::FileOpenError, m_close, m_fd, m_name, Skims_PA_cff::name, O_NONBLOCK, IOFlags::OpenAppend, IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenNonBlock, IOFlags::OpenRead, IOFlags::OpenTruncate, IOFlags::OpenUnbuffered, and IOFlags::OpenWrite.

Referenced by create(), DCacheFile(), and open().

48  {
49  // Actual open
50  if ((name == nullptr) || (*name == 0)) {
52  ex << "Cannot open a file without a name";
53  ex.addContext("Calling DCacheFile::open()");
54  throw ex;
55  }
56 
57  if ((flags & (IOFlags::OpenRead | IOFlags::OpenWrite)) == 0) {
59  ex << "Must open file '" << name << "' at least for read or write";
60  ex.addContext("Calling DCacheFile::open()");
61  throw ex;
62  }
63 
64  // If I am already open, close old file first
65  if (m_fd != EDM_IOFD_INVALID && m_close)
66  close();
67 
68  // Translate our flags to system flags
69  int openflags = 0;
70 
72  openflags |= O_RDWR;
73  else if (flags & IOFlags::OpenRead)
74  openflags |= O_RDONLY;
75  else if (flags & IOFlags::OpenWrite)
76  openflags |= O_WRONLY;
77 
79  openflags |= O_NONBLOCK;
80 
82  openflags |= O_APPEND;
83 
85  openflags |= O_CREAT;
86 
88  openflags |= O_EXCL;
89 
91  openflags |= O_TRUNC;
92 
93  IOFD newfd = EDM_IOFD_INVALID;
94  dc_errno = 0;
95  if ((newfd = dc_open(name, openflags, perms)) == -1) {
97  ex << "dc_open(name='" << name << "', flags=0x" << std::hex << openflags << ", permissions=0" << std::oct << perms
98  << std::dec << ") => error '" << dc_strerror(dc_errno) << "' (dc_errno=" << dc_errno << ")";
99  ex.addContext("Calling DCacheFile::open()");
100  throw ex;
101  }
102 
103  m_name = name;
104  m_fd = newfd;
105 
106  // Turn off read-ahead, or adjust read-ahead size depending on
107  // whether buffering has been requested. This is a very tricky
108  // balance here. Without read-ahead data processing appears to
109  // become exceedingly slow, and with default (1MB) read-ahead
110  // it appears to saturate disk servers and network. Try tread
111  // reasonable middle ground here.
113  dc_noBuffering(m_fd);
114  else
115  dc_setBufferSize(m_fd, 64000);
116 
117  m_close = true;
118 
119  edm::LogInfo("DCacheFileInfo") << "Opened " << m_name;
120 }
void close(void) override
Definition: DCacheFile.cc:122
IOFD m_fd
Definition: DCacheFile.h:37
bool m_close
Definition: DCacheFile.h:38
int IOFD
Definition: IOTypes.h:22
std::string m_name
Definition: DCacheFile.h:39
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
#define O_NONBLOCK
Definition: SysFile.h:21
void DCacheFile::open ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)
virtual

Definition at line 44 of file DCacheFile.cc.

References HLT_2018_cff::flags, and open().

44  {
45  open(name.c_str(), flags, perms);
46 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:48
IOOffset DCacheFile::position ( IOOffset  offset,
Relative  whence = SET 
)
overridevirtual

Implements Storage.

Definition at line 279 of file DCacheFile.cc.

References cms::Exception::addContext(), Storage::CURRENT, EDM_IOFD_INVALID, Storage::END, m_fd, m_name, mps_fire::result, and Storage::SET.

279  {
280  if (m_fd == EDM_IOFD_INVALID) {
281  cms::Exception ex("FilePositionError");
282  ex << "DCacheFile::position() called on a closed file";
283  throw ex;
284  }
285  if (whence != CURRENT && whence != SET && whence != END) {
286  cms::Exception ex("FilePositionError");
287  ex << "DCacheFile::position() called with incorrect 'whence' parameter";
288  throw ex;
289  }
291  int mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
292 
293  dc_errno = 0;
294  if ((result = dc_lseek64(m_fd, offset, mywhence)) == -1) {
295  cms::Exception ex("FilePositionError");
296  ex << "dc_lseek64(name='" << m_name << "', offset=" << offset << ", whence=" << mywhence << ") failed with error '"
297  << dc_strerror(dc_errno) << "' (dc_errno=" << dc_errno << ")";
298  ex.addContext("Calling DCacheFile::position()");
299  throw ex;
300  }
301  // FIXME: dCache returns incorrect value on SEEK_END.
302  // Remove this hack when dcap has been fixed.
303  if (whence == SEEK_END && (result = dc_lseek64(m_fd, result, SEEK_SET)) == -1) {
304  cms::Exception ex("FilePositionError");
305  ex << "dc_lseek64(name='" << m_name << "', offset=" << offset << ", whence=" << SEEK_SET << ") failed with error '"
306  << dc_strerror(dc_errno) << "' (dc_errno=" << dc_errno << ")";
307  ex.addContext("Calling DCacheFile::position()");
308  throw ex;
309  }
310  return result;
311 }
IOFD m_fd
Definition: DCacheFile.h:37
int64_t IOOffset
Definition: IOTypes.h:19
std::string m_name
Definition: DCacheFile.h:39
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
IOSize DCacheFile::read ( void *  into,
IOSize  n 
)
overridevirtual

Read into into at most n number of bytes.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

Returns
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons.
Exceptions
Incase of error, a #IOError exception is thrown. This includes the situation where the input stream is in non-blocking mode and no input is currently available (FIXME: make this simpler; clarify which exception).

Implements IOInput.

Definition at line 165 of file DCacheFile.cc.

References cms::Exception::addContext(), BUGLINE, fileCollector::done, edm::errors::FileReadError, m_fd, m_name, and alignCSCRings::s.

165  {
166  IOSize done = 0;
167  while (done < n) {
168  dc_errno = 0;
169  ssize_t s = dc_read(m_fd, (char *)into + done, n - done);
170  if (s == -1) {
172  ex << "dc_read(name='" << m_name << "', n=" << (n - done) << ") failed with error '" << dc_strerror(dc_errno)
173  << "' (dc_errno=" << dc_errno << ")";
174  ex.addContext("Calling DCacheFile::read()");
175  throw ex;
176  } else if (s == 0)
177  // end of file
178  break;
179  else if (s < ssize_t(n - done))
180  edm::LogInfo("DCacheFileWarning") << "dc_read(name='" << m_name << "', n=" << (n - done)
181  << ") returned a short read of " << s << " bytes; "
182  << "please report a bug in dCache referencing the "
183  << "comment on line " << BUGLINE << " of " << __FILE__;
184  done += s;
185  }
186 
187  return done;
188 }
IOFD m_fd
Definition: DCacheFile.h:37
static const int BUGLINE
Definition: DCacheFile.cc:150
std::string m_name
Definition: DCacheFile.h:39
size_t IOSize
Definition: IOTypes.h:14
IOSize DCacheFile::readv ( IOBuffer into,
IOSize  buffers 
)
overridevirtual

Read from the input stream into multiple scattered buffers. There are buffers to fill in an array starting at into; the memory those buffers occupy does not need to be contiguous. The buffers are filled in the order given, eac buffer is filled fully before the subsequent buffers.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

The base class implementation uses read(void *, IOSize) method, but derived classes may implement a more efficient alternative.

Returns
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons. Note that the return value indicates the number of bytes read, not the number of buffers; it is the sum total of bytes filled into all the buffers.
Exceptions
Incase of error, a #IOError exception is thrown. However if some data has already been read, the error is swallowed and the method returns the data read so far. It is assumed that persistent errors will occur anyway on the next read and sporadic errors like stream becoming unvailable can be ignored. Use xread() if a different policy is desirable.

Reimplemented from IOInput.

Definition at line 212 of file DCacheFile.cc.

References cms::Exception::addContext(), data, edm::errors::FileReadError, mps_fire::i, m_fd, m_name, dqmiodumpmetadata::n, and IOBuffer::size().

212  {
213  assert(!buffers || into);
214 
215  // readv may not support zero buffers.
216  if (!buffers)
217  return 0;
218 
219  // Convert the buffers to system format.
220  std::vector<iovec> bufs(buffers);
221  for (IOSize i = 0; i < buffers; ++i) {
222  bufs[i].iov_len = into[i].size();
223  bufs[i].iov_base = (caddr_t)into[i].data();
224  }
225 
226  // Read as long as signals cancel the read before doing anything.
227  dc_errno = 0;
228  ssize_t n = dc_readv(m_fd, &bufs[0], buffers);
229 
230  // If it was serious error, throw it.
231  if (n == -1) {
233  ex << "dc_readv(name='" << m_name << "', iov[" << buffers << "]) failed with error '" << dc_strerror(dc_errno)
234  << "' (dc_errno=" << dc_errno << ")";
235  ex.addContext("Calling DCacheFile::readv()");
236  throw ex;
237  }
238 
239  // Return the number of bytes actually read.
240  return n;
241 }
IOFD m_fd
Definition: DCacheFile.h:37
IOSize size(void) const
Definition: IOBuffer.h:34
std::string m_name
Definition: DCacheFile.h:39
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
size_t IOSize
Definition: IOTypes.h:14
IOSize DCacheFile::readv ( IOPosBuffer into,
IOSize  buffers 
)
overridevirtual

Reimplemented from Storage.

Definition at line 243 of file DCacheFile.cc.

References cms::Exception::addContext(), data, edm::errors::FileReadError, mps_fire::i, m_fd, m_name, dqmiodumpmetadata::n, IOPosBuffer::offset(), IOPosBuffer::size(), and dqmMemoryStats::total.

243  {
244  assert(!buffers || into);
245 
246  // readv may not support zero buffers.
247  if (!buffers)
248  return 0;
249 
250  // Convert the buffers to system format.
251  std::vector<iovec2> bufs(buffers);
252  IOSize total = 0;
253  for (IOSize i = 0; i < buffers; ++i) {
254  bufs[i].offset = into[i].offset();
255  bufs[i].len = into[i].size();
256  bufs[i].buf = (char *)into[i].data();
257  total += into[i].size();
258  }
259 
260  // Read as long as signals cancel the read before doing anything.
261  dc_errno = 0;
262  ssize_t n = dc_readv2(m_fd, &bufs[0], buffers);
263 
264  // If it was serious error, throw it.
265  if (n == -1) {
267  ex << "dc_readv2(name='" << m_name << "', iov2[" << buffers << "]) failed with error '" << dc_strerror(dc_errno)
268  << "' (dc_errno=" << dc_errno << ")";
269  ex.addContext("Calling DCacheFile::readv()");
270  throw ex;
271  }
272  // dc_readv2 returns 0 on success.
273  return (n == 0) ? total : 0;
274 }
IOFD m_fd
Definition: DCacheFile.h:37
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
IOSize size(void) const
Definition: IOPosBuffer.h:45
std::string m_name
Definition: DCacheFile.h:39
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
size_t IOSize
Definition: IOTypes.h:14
void DCacheFile::resize ( IOOffset  size)
overridevirtual

Implements Storage.

Definition at line 313 of file DCacheFile.cc.

References m_name.

Referenced by Vispa.Gui.TextDialog.TextDialog::__init__(), Vispa.Plugins.ConfigEditor.ToolDialog.ToolDialog::__init__(), Vispa.Main.MainWindow.MainWindow::_loadIni(), and Vispa.Gui.PortConnection.PointToPointConnection::updateConnection().

313  {
314  cms::Exception ex("FileResizeError");
315  ex << "DCacheFile::resize(name='" << m_name << "') not implemented";
316  throw ex;
317 }
std::string m_name
Definition: DCacheFile.h:39
IOSize DCacheFile::write ( const void *  from,
IOSize  n 
)
overridevirtual

Write n bytes of data starting at address from.

Returns
The number of bytes written. Normally this will be n, but can be less, even zero, for example if the stream is non-blocking mode and cannot accept input at this time.
Exceptions
Incase of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception – the return value will be less than requested.

Implements IOOutput.

Definition at line 190 of file DCacheFile.cc.

References cms::Exception::addContext(), BUGLINE, fileCollector::done, edm::errors::FileWriteError, m_fd, m_name, and alignCSCRings::s.

190  {
191  IOSize done = 0;
192  while (done < n) {
193  dc_errno = 0;
194  ssize_t s = dc_write(m_fd, (const char *)from + done, n - done);
195  if (s == -1) {
197  ex << "dc_write(name='" << m_name << "', n=" << (n - done) << ") failed with error '" << dc_strerror(dc_errno)
198  << "' (dc_errno=" << dc_errno << ")";
199  ex.addContext("Calling DCacheFile::write()");
200  throw ex;
201  } else if (s < ssize_t(n - done))
202  edm::LogInfo("DCacheFileWarning") << "dc_write(name='" << m_name << "', n=" << (n - done)
203  << ") returned a short write of " << s << " bytes; "
204  << "please report a bug in dCache referencing the "
205  << "comment on line " << BUGLINE << " of " << __FILE__;
206  done += s;
207  }
208 
209  return done;
210 }
IOFD m_fd
Definition: DCacheFile.h:37
static const int BUGLINE
Definition: DCacheFile.cc:150
std::string m_name
Definition: DCacheFile.h:39
size_t IOSize
Definition: IOTypes.h:14

Member Data Documentation

bool DCacheFile::m_close
private

Definition at line 38 of file DCacheFile.h.

Referenced by abort(), close(), open(), and ~DCacheFile().

IOFD DCacheFile::m_fd
private

Definition at line 37 of file DCacheFile.h.

Referenced by abort(), close(), open(), position(), read(), readv(), and write().

std::string DCacheFile::m_name
private

Definition at line 39 of file DCacheFile.h.

Referenced by close(), open(), position(), read(), readv(), resize(), write(), and ~DCacheFile().