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 (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_
 
Filefile_
 
IOOffset image_
 
std::vector< char > present_
 
Storagestorage_
 

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 10 of file LocalCacheFile.h.

Constructor & Destructor Documentation

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

Definition at line 24 of file LocalCacheFile.cc.

References cms::Exception::addContext(), cacheTotal_, ztee::fd, reco_application_tbsim_DetSim-Digi_cfg::File, file_, edm::errors::FileOpenError, image_, AlCaHLTBitMon_ParallelJobs::p, chain::pattern, present_, File::resize(), AlCaHLTBitMon_QueryRunRegistry::string, and groupFilesInBlocks::temp.

25  : image_(base->size()),
26  file_(0),
27  storage_(base),
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_ = new File(fd);
55 }
unsigned int cacheTotal_
list pattern
Definition: chain.py:104
unsigned int cacheCount_
tuple fd
Definition: ztee.py:136
static const IOOffset CHUNK_SIZE
std::vector< char > present_
virtual IOOffset size(void) const
Definition: Storage.cc:102
virtual void resize(IOOffset size)
Definition: UnixFile.cc:130
Storage * storage_
LocalCacheFile::~LocalCacheFile ( void  )

Definition at line 57 of file LocalCacheFile.cc.

References file_, and storage_.

58 {
59  delete file_;
60  delete storage_;
61 }
Storage * storage_

Member Function Documentation

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

Definition at line 64 of file LocalCacheFile.cc.

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

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

65 {
67  end = std::min(end, image_);
68 
69  IOSize nread = 0;
71 
72  while (start < end)
73  {
75  if (! present_[index])
76  {
77  void *window = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start);
78  if (window == MAP_FAILED)
79  {
81  ex << "Unable to map a window of local cache file: "
82  << strerror(errno) << " (error " << errno << ")";
83  ex.addContext("LocalCacheFile::cache()");
84  throw ex;
85  }
86 
87  try
88  {
89  nread = storage_->read(window, len, start);
90  }
91  catch (cms::Exception &e)
92  {
93  munmap(window, len);
94  std::ostringstream ost;
95  ost << "Unable to cache " << len << " byte file segment at " << start << ": ";
97  ex.addContext("LocalCacheFile::cache()");
98  throw ex;
99  }
100 
101  munmap(window, len);
102 
103  if (nread != len)
104  {
106  ex << "Unable to cache " << len << " byte file segment at " << start
107  << ": got only " << nread << " bytes back";
108  ex.addContext("LocalCacheFile::cache()");
109  throw ex;
110  }
111 
112  present_[index] = 1;
113  ++cacheCount_;
114  if (cacheCount_ == cacheTotal_)
115  {
116  storage_->close();
117  closedFile_ = true;
118  }
119  }
120 
121  start += len;
122  ++index;
123  }
124 }
def window
Definition: svgfig.py:642
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
virtual IOSize read(void *into, IOSize n, IOOffset pos)
Definition: Storage.cc:17
unsigned int cacheTotal_
unsigned int cacheCount_
virtual void close(void)
Definition: Storage.cc:128
#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
size_t IOSize
Definition: IOTypes.h:14
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
Storage * storage_
void LocalCacheFile::close ( void  )
virtual
void LocalCacheFile::flush ( void  )
virtual

Reimplemented from Storage.

Definition at line 192 of file LocalCacheFile.cc.

References nowrite().

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

Implements Storage.

Definition at line 184 of file LocalCacheFile.cc.

References file_, and File::position().

185 { return file_->position(offset, whence); }
virtual IOOffset position(IOOffset offset, Relative whence=SET)
Definition: UnixFile.cc:113
bool LocalCacheFile::prefetch ( const IOPosBuffer what,
IOSize  n 
)
virtual

Reimplemented from Storage.

Definition at line 206 of file LocalCacheFile.cc.

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

207 {
208  for (IOSize i = 0; i < n; ++i)
209  {
210  IOOffset start = what[i].offset();
211  IOOffset end = start + what[i].size();
212  cache(start, end);
213  }
214 
215  return file_->prefetch(what, n);
216 }
int i
Definition: DBlmapReader.cc:9
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
void cache(IOOffset start, IOOffset end)
virtual bool prefetch(const IOPosBuffer *what, IOSize n)
Definition: File.cc:210
#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
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 127 of file LocalCacheFile.cc.

References cache(), file_, File::position(), and File::read().

128 {
129  IOOffset here = file_->position();
130  cache(here, here + n);
131 
132  return file_->read(into, n);
133 }
void cache(IOOffset start, IOOffset end)
virtual IOSize read(void *into, IOSize n)
Definition: File.cc:231
virtual IOOffset position(IOOffset offset, Relative whence=SET)
Definition: UnixFile.cc:113
int64_t IOOffset
Definition: IOTypes.h:19
IOSize LocalCacheFile::read ( void *  into,
IOSize  n,
IOOffset  pos 
)
virtual

Reimplemented from Storage.

Definition at line 136 of file LocalCacheFile.cc.

References cache(), file_, and File::read().

137 {
138  cache(pos, pos + n);
139  return file_->read(into, n, pos);
140 }
void cache(IOOffset start, IOOffset end)
virtual IOSize read(void *into, IOSize n)
Definition: File.cc:231
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 143 of file LocalCacheFile.cc.

References cache(), end, file_, i, gen::n, File::position(), File::readv(), Storage::size(), and dqm_diff::start.

144 {
146  IOOffset end = start;
147  for (IOSize i = 0; i < n; ++i)
148  end += into[i].size();
149  cache(start, end);
150 
151  return file_->readv(into, n);
152 }
virtual IOSize readv(IOBuffer *into, IOSize length)
Definition: File.cc:236
int i
Definition: DBlmapReader.cc:9
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
void cache(IOOffset start, IOOffset end)
#define end
Definition: vmac.h:37
virtual IOOffset position(IOOffset offset, Relative whence=SET)
Definition: UnixFile.cc:113
virtual IOOffset size(void) const
Definition: Storage.cc:102
int64_t IOOffset
Definition: IOTypes.h:19
size_t IOSize
Definition: IOTypes.h:14
IOSize LocalCacheFile::readv ( IOPosBuffer into,
IOSize  n 
)
virtual

Reimplemented from Storage.

Definition at line 155 of file LocalCacheFile.cc.

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

156 {
157  for (IOSize i = 0; i < n; ++i)
158  {
159  IOOffset start = into[i].offset();
160  IOOffset end = start + into[i].size();
161  cache(start, end);
162  }
163 
164  return storage_->readv(into, n);
165 }
int i
Definition: DBlmapReader.cc:9
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
void cache(IOOffset start, IOOffset end)
virtual IOSize readv(IOPosBuffer *into, IOSize buffers)
Definition: Storage.cc:31
#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
Storage * storage_
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 168 of file LocalCacheFile.cc.

References nowrite().

169 { 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 172 of file LocalCacheFile.cc.

References nowrite().

173 { 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 176 of file LocalCacheFile.cc.

References nowrite().

177 { 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 180 of file LocalCacheFile.cc.

References nowrite().

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

Member Data Documentation

unsigned int LocalCacheFile::cacheCount_
private

Definition at line 42 of file LocalCacheFile.h.

Referenced by cache().

unsigned int LocalCacheFile::cacheTotal_
private

Definition at line 43 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

bool LocalCacheFile::closedFile_
private

Definition at line 41 of file LocalCacheFile.h.

Referenced by cache(), and close().

File* LocalCacheFile::file_
private
IOOffset LocalCacheFile::image_
private

Definition at line 37 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

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

Definition at line 38 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

Storage* LocalCacheFile::storage_
private

Definition at line 40 of file LocalCacheFile.h.

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