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 (IOBuffer into, IOOffset pos)
 
IOSize read (void *into, IOSize n) override
 
virtual IOSize read (void *into, IOSize n, IOOffset pos)
 
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
 
virtual IOSize write (const void *from, IOSize n, IOOffset pos)
 
IOSize write (const void *from, IOSize n, IOOffset pos) override
 
IOSize write (IOBuffer from, IOOffset pos)
 
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)
 
IOSize read (IOBuffer into, IOOffset pos)
 
virtual IOSize read (void *into, IOSize n)=0
 
int read (void)
 
virtual IOSize readv (IOBuffer *into, IOSize buffers)
 
virtual void rewind (void)
 
virtual IOOffset size (void) const
 
 Storage (void)
 
virtual IOSize write (const void *from, IOSize n)=0
 
IOSize write (IOBuffer from)
 
IOSize write (IOBuffer from, IOOffset pos)
 
IOSize write (unsigned char byte)
 
virtual IOSize writev (const IOBuffer *from, IOSize buffers)
 
 ~Storage (void) override
 
- Public Member Functions inherited from IOInput
IOSize read (IOBuffer into)
 
int read (void)
 
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 (IOBuffer from)
 
IOSize write (unsigned char byte)
 
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::LocalCacheFile ( std::unique_ptr< Storage base,
const std::string &  tmpdir = "" 
)

Definition at line 21 of file LocalCacheFile.cc.

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 }

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.

◆ ~LocalCacheFile()

LocalCacheFile::~LocalCacheFile ( void  )
override

Definition at line 51 of file LocalCacheFile.cc.

51 {}

Member Function Documentation

◆ cache()

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

Definition at line 53 of file LocalCacheFile.cc.

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 }

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

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

◆ close()

void LocalCacheFile::close ( void  )
overridevirtual

Reimplemented from Storage.

Definition at line 162 of file LocalCacheFile.cc.

162  {
163  if (!closedFile_) {
164  storage_->close();
165  }
166  file_->close();
167 }

References closedFile_, file_, and storage_.

Referenced by esMonitoring.AsyncLineReaderMixin::handle_close(), and esMonitoring.FDJsonServer::handle_close().

◆ flush()

void LocalCacheFile::flush ( void  )
overridevirtual

Reimplemented from Storage.

Definition at line 160 of file LocalCacheFile.cc.

160 { nowrite("flush"); }

References nowrite().

◆ position()

IOOffset LocalCacheFile::position ( IOOffset  offset,
Relative  whence = SET 
)
overridevirtual

Implements Storage.

Definition at line 156 of file LocalCacheFile.cc.

156 { return file_->position(offset, whence); }

References file_, and hltrates_dqm_sourceclient-live_cfg::offset.

◆ prefetch()

bool LocalCacheFile::prefetch ( const IOPosBuffer what,
IOSize  n 
)
overridevirtual

Reimplemented from Storage.

Definition at line 169 of file LocalCacheFile.cc.

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 }

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

◆ read() [1/4]

IOSize Storage::read

Definition at line 10 of file Storage.cc.

10 { return read(into.data(), into.size(), pos); }

◆ read() [2/4]

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.

104  {
105  IOOffset here = file_->position();
106  cache(here, here + n);
107 
108  return file_->read(into, n);
109 }

References cache(), file_, and dqmiodumpmetadata::n.

◆ read() [3/4]

IOSize Storage::read

Definition at line 12 of file Storage.cc.

12  {
13  // FIXME: this is not thread safe! split into separate interface
14  // that a particular storage can choose to support or not? make
15  // sure that throw semantics are correct here!
16  // FIXME: use saveposition object in case exceptions are thrown?
17  IOOffset here = position();
18  position(pos);
19  n = read(into, n);
20  position(here);
21  return n;
22 }

◆ read() [4/4]

IOSize LocalCacheFile::read ( void *  into,
IOSize  n,
IOOffset  pos 
)
overridevirtual

Reimplemented from Storage.

Definition at line 111 of file LocalCacheFile.cc.

111  {
112  cache(pos, pos + n);
113  return file_->read(into, n, pos);
114 }

References cache(), file_, and dqmiodumpmetadata::n.

◆ readv() [1/2]

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.

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 }

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

◆ readv() [2/2]

IOSize LocalCacheFile::readv ( IOPosBuffer into,
IOSize  n 
)
overridevirtual

Reimplemented from Storage.

Definition at line 126 of file LocalCacheFile.cc.

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 }

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

◆ resize()

void LocalCacheFile::resize ( IOOffset  size)
overridevirtual

Implements Storage.

Definition at line 158 of file LocalCacheFile.cc.

158 { nowrite("resize"); }

References nowrite().

◆ write() [1/4]

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.

136  {
137  nowrite("write");
138  return 0;
139 }

References nowrite().

◆ write() [2/4]

IOSize Storage::write

Definition at line 44 of file Storage.cc.

44  {
45  // FIXME: this is not thread safe! split into separate interface
46  // that a particular storage can choose to support or not? make
47  // sure that throw semantics are correct here!
48 
49  // FIXME: use saveposition object in case exceptions are thrown?
50  IOOffset here = position();
51  position(pos);
52  n = write(from, n);
53  position(here);
54  return n;
55 }

◆ write() [3/4]

IOSize LocalCacheFile::write ( const void *  from,
IOSize  n,
IOOffset  pos 
)
overridevirtual

Reimplemented from Storage.

Definition at line 141 of file LocalCacheFile.cc.

141  {
142  nowrite("write");
143  return 0;
144 }

References nowrite().

◆ write() [4/4]

IOSize Storage::write

Definition at line 42 of file Storage.cc.

42 { return write(from.data(), from.size(), pos); }

◆ writev() [1/2]

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.

146  {
147  nowrite("writev");
148  return 0;
149 }

References nowrite().

◆ writev() [2/2]

IOSize LocalCacheFile::writev ( const IOPosBuffer from,
IOSize  n 
)
overridevirtual

Reimplemented from Storage.

Definition at line 151 of file LocalCacheFile.cc.

151  {
152  nowrite("writev");
153  return 0;
154 }

References nowrite().

Member Data Documentation

◆ cacheCount_

unsigned int LocalCacheFile::cacheCount_
private

Definition at line 43 of file LocalCacheFile.h.

Referenced by cache().

◆ cacheTotal_

unsigned int LocalCacheFile::cacheTotal_
private

Definition at line 44 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

◆ closedFile_

bool LocalCacheFile::closedFile_
private

Definition at line 42 of file LocalCacheFile.h.

Referenced by cache(), and close().

◆ file_

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().

◆ image_

IOOffset LocalCacheFile::image_
private

Definition at line 38 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

◆ present_

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

Definition at line 39 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

◆ storage_

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

Definition at line 41 of file LocalCacheFile.h.

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

Storage::size
virtual IOOffset size(void) const
Definition: Storage.cc:77
mps_fire.i
i
Definition: mps_fire.py:355
start
Definition: start.py:1
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
cms::Exception::addContext
void addContext(std::string const &context)
Definition: Exception.cc:165
min
T min(T a, T b)
Definition: MathUtil.h:58
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
pos
Definition: PixelAliasList.h:18
LocalCacheFile::image_
IOOffset image_
Definition: LocalCacheFile.h:38
svgfig.window
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
LocalCacheFile::closedFile_
bool closedFile_
Definition: LocalCacheFile.h:42
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
end
#define end
Definition: vmac.h:39
edm::errors::FileOpenError
Definition: EDMException.h:49
edm::Exception
Definition: EDMException.h:77
LocalCacheFile::present_
std::vector< char > present_
Definition: LocalCacheFile.h:39
ztee.fd
fd
Definition: ztee.py:136
CHUNK_SIZE
static const IOOffset CHUNK_SIZE
Definition: LocalCacheFile.cc:12
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
LocalCacheFile::cacheTotal_
unsigned int cacheTotal_
Definition: LocalCacheFile.h:44
LocalCacheFile::file_
edm::propagate_const< std::unique_ptr< File > > file_
Definition: LocalCacheFile.h:40
LocalCacheFile::storage_
edm::propagate_const< std::unique_ptr< Storage > > storage_
Definition: LocalCacheFile.h:41
IOOffset
int64_t IOOffset
Definition: IOTypes.h:19
LocalCacheFile::cacheCount_
unsigned int cacheCount_
Definition: LocalCacheFile.h:43
topSingleLeptonDQM_PU_cfi.pattern
pattern
Definition: topSingleLeptonDQM_PU_cfi.py:39
LocalCacheFile::write
IOSize write(const void *from, IOSize n) override
Definition: LocalCacheFile.cc:136
eostools.move
def move(src, dest)
Definition: eostools.py:511
IOPosBuffer::size
IOSize size(void) const
Definition: IOPosBuffer.h:45
IOPosBuffer::offset
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
Storage::position
virtual IOOffset position(void) const
Definition: Storage.cc:72
LocalCacheFile::cache
void cache(IOOffset start, IOOffset end)
Definition: LocalCacheFile.cc:53
IOInput::read
int read(void)
Definition: IOInput.cc:52
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
cms::Exception
Definition: Exception.h:70
command_line.start
start
Definition: command_line.py:167
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
IOSize
size_t IOSize
Definition: IOTypes.h:14
newFWLiteAna.base
base
Definition: newFWLiteAna.py:92
edm::errors::FileReadError
Definition: EDMException.h:50
nowrite
static void nowrite(const std::string &why)
Definition: LocalCacheFile.cc:14
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37