CMS 3D CMS Logo

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

#include <LocalCacheFile.h>

Inheritance diagram for LocalCacheFile:
Storage IOInput IOOutput

Public Member Functions

void close (void) override
 
void flush (void) override
 
 LocalCacheFile (std::unique_ptr< Storage > base, const std::string &tmpdir="")
 
IOOffset position (IOOffset offset, Relative whence=SET) override
 
bool prefetch (const IOPosBuffer *what, IOSize n) override
 
IOSize read (void *into, IOSize n) override
 
IOSize read (void *into, IOSize n, IOOffset pos) override
 
IOSize readv (IOBuffer *into, IOSize n) override
 
IOSize readv (IOPosBuffer *into, IOSize n) override
 
void resize (IOOffset size) override
 
IOSize write (const void *from, IOSize n) override
 
IOSize write (const void *from, IOSize n, IOOffset pos) override
 
IOSize writev (const IOBuffer *from, IOSize n) override
 
IOSize writev (const IOPosBuffer *from, IOSize n) override
 
 ~LocalCacheFile (void) override
 
- Public Member Functions inherited from Storage
virtual bool eof (void) const
 
virtual IOOffset position (void) const
 
IOSize read (IOBuffer into, IOOffset pos)
 
virtual void rewind (void)
 
virtual IOOffset size (void) const
 
 Storage (void)
 
IOSize write (IOBuffer from, IOOffset pos)
 
 ~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)
 
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 Member Functions

void cache (IOOffset start, IOOffset end)
 

Private Attributes

unsigned int cacheCount_
 
unsigned int cacheTotal_
 
bool closedFile_
 
edm::propagate_const< std::unique_ptr< File > > file_
 
IOOffset image_
 
std::vector< char > present_
 
edm::propagate_const< std::unique_ptr< Storage > > storage_
 

Additional Inherited Members

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

Detailed Description

Proxy class to copy a file locally in large chunks.

Definition at line 12 of file LocalCacheFile.h.

Constructor & Destructor Documentation

LocalCacheFile::LocalCacheFile ( std::unique_ptr< Storage base,
const std::string &  tmpdir = "" 
)

Definition at line 21 of file LocalCacheFile.cc.

References cms::Exception::addContext(), cacheTotal_, ztee::fd, file_, edm::errors::FileOpenError, image_, AlCaHLTBitMon_ParallelJobs::p, topSingleLeptonDQM_PU_cfi::pattern, present_, AlCaHLTBitMon_QueryRunRegistry::string, and groupFilesInBlocks::temp.

22  : image_(base->size()),
23  file_(),
25  closedFile_(false),
26  cacheCount_(0),
28  present_.resize(cacheTotal_, 0);
29 
30  std::string pattern(tmpdir);
31  if (pattern.empty())
32  if (char *p = std::getenv("TMPDIR"))
33  pattern = p;
34  if (pattern.empty())
35  pattern = "/tmp";
36  pattern += "/cmssw-shadow-XXXXXX";
37 
38  std::vector<char> temp(pattern.c_str(), pattern.c_str() + pattern.size() + 1);
39  int fd = mkstemp(&temp[0]);
40  if (fd == -1) {
42  ex << "Cannot create temporary file '" << pattern << "': " << strerror(errno) << " (error " << errno << ")";
43  ex.addContext("LocalCacheFile::LocalCacheFile");
44  }
45 
46  unlink(&temp[0]);
47  file_ = std::make_unique<File>(fd);
48  file_->resize(image_);
49 }
unsigned int cacheTotal_
base
Main Program
Definition: newFWLiteAna.py:92
edm::propagate_const< std::unique_ptr< Storage > > storage_
unsigned int cacheCount_
static const IOOffset CHUNK_SIZE
std::vector< char > present_
edm::propagate_const< std::unique_ptr< File > > file_
fd
Definition: ztee.py:136
def move(src, dest)
Definition: eostools.py:511
LocalCacheFile::~LocalCacheFile ( void  )
override

Definition at line 51 of file LocalCacheFile.cc.

51 {}

Member Function Documentation

void LocalCacheFile::cache ( IOOffset  start,
IOOffset  end 
)
private

Definition at line 53 of file LocalCacheFile.cc.

References cms::Exception::addContext(), cacheCount_, cacheTotal_, CHUNK_SIZE, closedFile_, MillePedeFileConverter_cfg::e, file_, edm::errors::FileReadError, image_, min(), present_, command_line::start, storage_, and svgfig::window().

Referenced by prefetch(), read(), and readv().

53  {
55  end = std::min(end, image_);
56 
57  IOSize nread = 0;
59 
60  while (start < end) {
62  if (!present_[index]) {
63  void *window = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start);
64  if (window == MAP_FAILED) {
66  ex << "Unable to map a window of local cache file: " << strerror(errno) << " (error " << errno << ")";
67  ex.addContext("LocalCacheFile::cache()");
68  throw ex;
69  }
70 
71  try {
72  nread = storage_->read(window, len, start);
73  } catch (cms::Exception &e) {
74  munmap(window, len);
75  std::ostringstream ost;
76  ost << "Unable to cache " << len << " byte file segment at " << start << ": ";
78  ex.addContext("LocalCacheFile::cache()");
79  throw ex;
80  }
81 
82  munmap(window, len);
83 
84  if (nread != len) {
86  ex << "Unable to cache " << len << " byte file segment at " << start << ": got only " << nread << " bytes back";
87  ex.addContext("LocalCacheFile::cache()");
88  throw ex;
89  }
90 
91  present_[index] = 1;
92  ++cacheCount_;
93  if (cacheCount_ == cacheTotal_) {
94  storage_->close();
95  closedFile_ = true;
96  }
97  }
98 
99  start += len;
100  ++index;
101  }
102 }
Definition: start.py:1
unsigned int cacheTotal_
edm::propagate_const< std::unique_ptr< Storage > > storage_
unsigned int cacheCount_
def window(xmin, xmax, ymin, ymax, x=0, y=0, width=100, height=100, xlogbase=None, ylogbase=None, minusInfinity=-1000, flipx=False, flipy=True)
Definition: svgfig.py:643
#define end
Definition: vmac.h:39
T min(T a, T b)
Definition: MathUtil.h:58
static const IOOffset CHUNK_SIZE
std::vector< char > present_
void addContext(std::string const &context)
Definition: Exception.cc:165
edm::propagate_const< std::unique_ptr< File > > file_
size_t IOSize
Definition: IOTypes.h:14
void LocalCacheFile::close ( void  )
overridevirtual

Reimplemented from Storage.

Definition at line 162 of file LocalCacheFile.cc.

References closedFile_, file_, and storage_.

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

162  {
163  if (!closedFile_) {
164  storage_->close();
165  }
166  file_->close();
167 }
edm::propagate_const< std::unique_ptr< Storage > > storage_
edm::propagate_const< std::unique_ptr< File > > file_
void LocalCacheFile::flush ( void  )
overridevirtual

Reimplemented from Storage.

Definition at line 160 of file LocalCacheFile.cc.

References nowrite().

160 { nowrite("flush"); }
static void nowrite(const std::string &why)
IOOffset LocalCacheFile::position ( IOOffset  offset,
Relative  whence = SET 
)
overridevirtual

Implements Storage.

Definition at line 156 of file LocalCacheFile.cc.

References file_.

156 { return file_->position(offset, whence); }
edm::propagate_const< std::unique_ptr< File > > file_
bool LocalCacheFile::prefetch ( const IOPosBuffer what,
IOSize  n 
)
overridevirtual

Reimplemented from Storage.

Definition at line 169 of file LocalCacheFile.cc.

References cache(), end, file_, mps_fire::i, dqmiodumpmetadata::n, IOPosBuffer::offset(), and IOPosBuffer::size().

169  {
170  for (IOSize i = 0; i < n; ++i) {
171  IOOffset start = what[i].offset();
172  IOOffset end = start + what[i].size();
173  cache(start, end);
174  }
175 
176  return file_->prefetch(what, n);
177 }
Definition: start.py:1
void cache(IOOffset start, IOOffset end)
#define end
Definition: vmac.h:39
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
IOSize size(void) const
Definition: IOPosBuffer.h:45
int64_t IOOffset
Definition: IOTypes.h:19
edm::propagate_const< std::unique_ptr< File > > file_
size_t IOSize
Definition: IOTypes.h:14
IOSize LocalCacheFile::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 104 of file LocalCacheFile.cc.

References cache(), and file_.

104  {
105  IOOffset here = file_->position();
106  cache(here, here + n);
107 
108  return file_->read(into, n);
109 }
void cache(IOOffset start, IOOffset end)
int64_t IOOffset
Definition: IOTypes.h:19
edm::propagate_const< std::unique_ptr< File > > file_
IOSize LocalCacheFile::read ( void *  into,
IOSize  n,
IOOffset  pos 
)
overridevirtual

Reimplemented from Storage.

Definition at line 111 of file LocalCacheFile.cc.

References cache(), and file_.

111  {
112  cache(pos, pos + n);
113  return file_->read(into, n, pos);
114 }
void cache(IOOffset start, IOOffset end)
edm::propagate_const< std::unique_ptr< File > > file_
IOSize LocalCacheFile::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 116 of file LocalCacheFile.cc.

References cache(), end, file_, mps_fire::i, dqmiodumpmetadata::n, Storage::size(), and command_line::start.

116  {
117  IOOffset start = file_->position();
118  IOOffset end = start;
119  for (IOSize i = 0; i < n; ++i)
120  end += into[i].size();
121  cache(start, end);
122 
123  return file_->readv(into, n);
124 }
Definition: start.py:1
void cache(IOOffset start, IOOffset end)
#define end
Definition: vmac.h:39
virtual IOOffset size(void) const
Definition: Storage.cc:77
int64_t IOOffset
Definition: IOTypes.h:19
edm::propagate_const< std::unique_ptr< File > > file_
size_t IOSize
Definition: IOTypes.h:14
IOSize LocalCacheFile::readv ( IOPosBuffer into,
IOSize  n 
)
overridevirtual

Reimplemented from Storage.

Definition at line 126 of file LocalCacheFile.cc.

References cache(), end, mps_fire::i, dqmiodumpmetadata::n, IOPosBuffer::offset(), IOPosBuffer::size(), and storage_.

126  {
127  for (IOSize i = 0; i < n; ++i) {
128  IOOffset start = into[i].offset();
129  IOOffset end = start + into[i].size();
130  cache(start, end);
131  }
132 
133  return storage_->readv(into, n);
134 }
Definition: start.py:1
void cache(IOOffset start, IOOffset end)
edm::propagate_const< std::unique_ptr< Storage > > storage_
#define end
Definition: vmac.h:39
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
IOSize size(void) const
Definition: IOPosBuffer.h:45
int64_t IOOffset
Definition: IOTypes.h:19
size_t IOSize
Definition: IOTypes.h:14
void LocalCacheFile::resize ( IOOffset  size)
overridevirtual
IOSize LocalCacheFile::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 136 of file LocalCacheFile.cc.

References nowrite().

136  {
137  nowrite("write");
138  return 0;
139 }
static void nowrite(const std::string &why)
IOSize LocalCacheFile::write ( const void *  from,
IOSize  n,
IOOffset  pos 
)
overridevirtual

Reimplemented from Storage.

Definition at line 141 of file LocalCacheFile.cc.

References nowrite().

141  {
142  nowrite("write");
143  return 0;
144 }
static void nowrite(const std::string &why)
IOSize LocalCacheFile::writev ( const IOBuffer from,
IOSize  buffers 
)
overridevirtual

Write to the output stream from multiple buffers. There are buffers to fill in an array starting at from. The buffers are filled in the order given, each buffer fully before the subsequent buffers. The method uses write(const void *, IOSize), but may be implemented more efficiently in derived classes.

Note that derived classes should not normally call this method, as it simply routes the call back to derived class through the other virtual functions. Use this method only at the "outside edge" when transferring calls from one object to another, not in up/down calls in the inheritance tree.

Returns
The number of bytes actually written. This is less or equal to the size of the buffers. The value can be less than requested if the stream is unable to accept all the output for platform or implementation reasons. Note that the return value indicates the number of bytes written, not the number of buffers; it is the sum total of bytes written from all the buffers.
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.

Reimplemented from IOOutput.

Definition at line 146 of file LocalCacheFile.cc.

References nowrite().

146  {
147  nowrite("writev");
148  return 0;
149 }
static void nowrite(const std::string &why)
IOSize LocalCacheFile::writev ( const IOPosBuffer from,
IOSize  n 
)
overridevirtual

Reimplemented from Storage.

Definition at line 151 of file LocalCacheFile.cc.

References nowrite().

151  {
152  nowrite("writev");
153  return 0;
154 }
static void nowrite(const std::string &why)

Member Data Documentation

unsigned int LocalCacheFile::cacheCount_
private

Definition at line 43 of file LocalCacheFile.h.

Referenced by cache().

unsigned int LocalCacheFile::cacheTotal_
private

Definition at line 44 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

bool LocalCacheFile::closedFile_
private

Definition at line 42 of file LocalCacheFile.h.

Referenced by cache(), and close().

edm::propagate_const<std::unique_ptr<File> > LocalCacheFile::file_
private

Definition at line 40 of file LocalCacheFile.h.

Referenced by cache(), close(), LocalCacheFile(), position(), prefetch(), read(), and readv().

IOOffset LocalCacheFile::image_
private

Definition at line 38 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

std::vector<char> LocalCacheFile::present_
private

Definition at line 39 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

edm::propagate_const<std::unique_ptr<Storage> > LocalCacheFile::storage_
private

Definition at line 41 of file LocalCacheFile.h.

Referenced by cache(), close(), and readv().