CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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

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

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

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

Definition at line 57 of file LocalCacheFile.cc.

58 {
59 }

Member Function Documentation

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

Definition at line 62 of file LocalCacheFile.cc.

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

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

63 {
65  end = std::min(end, image_);
66 
67  IOSize nread = 0;
69 
70  while (start < end)
71  {
73  if (! present_[index])
74  {
75  void *window = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start);
76  if (window == MAP_FAILED)
77  {
79  ex << "Unable to map a window of local cache file: "
80  << strerror(errno) << " (error " << errno << ")";
81  ex.addContext("LocalCacheFile::cache()");
82  throw ex;
83  }
84 
85  try
86  {
87  nread = storage_->read(window, len, start);
88  }
89  catch (cms::Exception &e)
90  {
91  munmap(window, len);
92  std::ostringstream ost;
93  ost << "Unable to cache " << len << " byte file segment at " << start << ": ";
95  ex.addContext("LocalCacheFile::cache()");
96  throw ex;
97  }
98 
99  munmap(window, len);
100 
101  if (nread != len)
102  {
104  ex << "Unable to cache " << len << " byte file segment at " << start
105  << ": got only " << nread << " bytes back";
106  ex.addContext("LocalCacheFile::cache()");
107  throw ex;
108  }
109 
110  present_[index] = 1;
111  ++cacheCount_;
112  if (cacheCount_ == cacheTotal_)
113  {
114  storage_->close();
115  closedFile_ = true;
116  }
117  }
118 
119  start += len;
120  ++index;
121  }
122 }
def window
Definition: svgfig.py:642
unsigned int cacheTotal_
edm::propagate_const< std::unique_ptr< Storage > > storage_
unsigned int cacheCount_
#define end
Definition: vmac.h:37
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:227
edm::propagate_const< std::unique_ptr< File > > file_
size_t IOSize
Definition: IOTypes.h:14
void LocalCacheFile::close ( void  )
virtual

Reimplemented from Storage.

Definition at line 194 of file LocalCacheFile.cc.

References closedFile_, file_, and storage_.

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

195 {
196  if (!closedFile_)
197  {
198  storage_->close();
199  }
200  file_->close();
201 }
edm::propagate_const< std::unique_ptr< Storage > > storage_
edm::propagate_const< std::unique_ptr< File > > file_
void LocalCacheFile::flush ( void  )
virtual

Reimplemented from Storage.

Definition at line 190 of file LocalCacheFile.cc.

References nowrite().

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

Implements Storage.

Definition at line 182 of file LocalCacheFile.cc.

References file_.

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

Reimplemented from Storage.

Definition at line 204 of file LocalCacheFile.cc.

References cache(), end, file_, i, gen::n, IOPosBuffer::offset(), IOPosBuffer::size(), and command_line::start.

205 {
206  for (IOSize i = 0; i < n; ++i)
207  {
208  IOOffset start = what[i].offset();
209  IOOffset end = start + what[i].size();
210  cache(start, end);
211  }
212 
213  return file_->prefetch(what, n);
214 }
int i
Definition: DBlmapReader.cc:9
void cache(IOOffset start, IOOffset end)
#define end
Definition: vmac.h:37
IOOffset offset(void) const
Definition: IOPosBuffer.h:54
IOSize size(void) const
Definition: IOPosBuffer.h:64
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 
)
virtual

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 125 of file LocalCacheFile.cc.

References cache(), and file_.

126 {
127  IOOffset here = file_->position();
128  cache(here, here + n);
129 
130  return file_->read(into, n);
131 }
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 
)
virtual

Reimplemented from Storage.

Definition at line 134 of file LocalCacheFile.cc.

References cache(), and file_.

135 {
136  cache(pos, pos + n);
137  return file_->read(into, n, pos);
138 }
void cache(IOOffset start, IOOffset end)
edm::propagate_const< std::unique_ptr< File > > file_
IOSize LocalCacheFile::readv ( IOBuffer into,
IOSize  buffers 
)
virtual

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 141 of file LocalCacheFile.cc.

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

142 {
143  IOOffset start = file_->position();
144  IOOffset end = start;
145  for (IOSize i = 0; i < n; ++i)
146  end += into[i].size();
147  cache(start, end);
148 
149  return file_->readv(into, n);
150 }
int i
Definition: DBlmapReader.cc:9
void cache(IOOffset start, IOOffset end)
#define end
Definition: vmac.h:37
virtual IOOffset size(void) const
Definition: Storage.cc:102
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 
)
virtual

Reimplemented from Storage.

Definition at line 153 of file LocalCacheFile.cc.

References cache(), end, i, gen::n, IOPosBuffer::offset(), IOPosBuffer::size(), command_line::start, and storage_.

154 {
155  for (IOSize i = 0; i < n; ++i)
156  {
157  IOOffset start = into[i].offset();
158  IOOffset end = start + into[i].size();
159  cache(start, end);
160  }
161 
162  return storage_->readv(into, n);
163 }
int i
Definition: DBlmapReader.cc:9
void cache(IOOffset start, IOOffset end)
edm::propagate_const< std::unique_ptr< Storage > > storage_
#define end
Definition: vmac.h:37
IOOffset offset(void) const
Definition: IOPosBuffer.h:54
IOSize size(void) const
Definition: IOPosBuffer.h:64
int64_t IOOffset
Definition: IOTypes.h:19
size_t IOSize
Definition: IOTypes.h:14
void LocalCacheFile::resize ( IOOffset  size)
virtual
IOSize LocalCacheFile::write ( const void *  from,
IOSize  n 
)
virtual

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 166 of file LocalCacheFile.cc.

References nowrite().

167 { nowrite("write"); return 0; }
static void nowrite(const std::string &why)
IOSize LocalCacheFile::write ( const void *  from,
IOSize  n,
IOOffset  pos 
)
virtual

Reimplemented from Storage.

Definition at line 170 of file LocalCacheFile.cc.

References nowrite().

171 { nowrite("write"); return 0; }
static void nowrite(const std::string &why)
IOSize LocalCacheFile::writev ( const IOBuffer from,
IOSize  buffers 
)
virtual

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 174 of file LocalCacheFile.cc.

References nowrite().

175 { nowrite("writev"); return 0; }
static void nowrite(const std::string &why)
IOSize LocalCacheFile::writev ( const IOPosBuffer from,
IOSize  n 
)
virtual

Reimplemented from Storage.

Definition at line 178 of file LocalCacheFile.cc.

References nowrite().

179 { nowrite("writev"); return 0; }
static void nowrite(const std::string &why)

Member Data Documentation

unsigned int LocalCacheFile::cacheCount_
private

Definition at line 44 of file LocalCacheFile.h.

Referenced by cache().

unsigned int LocalCacheFile::cacheTotal_
private

Definition at line 45 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

bool LocalCacheFile::closedFile_
private

Definition at line 43 of file LocalCacheFile.h.

Referenced by cache(), and close().

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

Definition at line 41 of file LocalCacheFile.h.

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

IOOffset LocalCacheFile::image_
private

Definition at line 39 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

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

Definition at line 40 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

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

Definition at line 42 of file LocalCacheFile.h.

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