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

References cacheTotal_, edm::hlt::Exception, reco_application_tbsim_DetSim-Digi_cfg::File, file_, image_, AlCaHLTBitMon_ParallelJobs::p, listBenchmarks::pattern, present_, File::resize(), AlCaHLTBitMon_QueryRunRegistry::string, and groupFilesInBlocks::temp.

23  : image_(base->size()),
24  file_(0),
25  storage_(base),
26  closedFile_(false),
27  cacheCount_(0),
29 {
30  present_.resize(cacheTotal_, 0);
31 
32  std::string pattern(tmpdir);
33  if (pattern.empty())
34  if (char *p = getenv("TMPDIR"))
35  pattern = p;
36  if (pattern.empty())
37  pattern = "/tmp";
38  pattern += "/cmssw-shadow-XXXXXX";
39 
40  std::vector<char> temp(pattern.c_str(), pattern.c_str()+pattern.size()+1);
41  int fd = mkstemp(&temp[0]);
42  if (fd == -1)
43  throw cms::Exception("LocalCacheFile")
44  << "Cannot create temporary file '" << pattern << "': "
45  << strerror(errno) << " (error " << errno << ")";
46 
47  unlink(&temp[0]);
48  file_ = new File(fd);
50 }
unsigned int cacheTotal_
unsigned int cacheCount_
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 52 of file LocalCacheFile.cc.

References file_, and storage_.

53 {
54  delete file_;
55  delete storage_;
56 }
Storage * storage_

Member Function Documentation

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

Definition at line 59 of file LocalCacheFile.cc.

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

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

60 {
62  end = std::min(end, image_);
63 
64  IOSize nread = 0;
66 
67  while (start < end)
68  {
70  if (! present_[index])
71  {
72  void *window = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start);
73  if (window == MAP_FAILED)
74  throw cms::Exception("LocalCacheFile")
75  << "Unable to map a window of local cache file: "
76  << strerror(errno) << " (error " << errno << ")";
77 
78  try
79  {
80  nread = storage_->read(window, len, start);
81  }
82  catch (cms::Exception &e)
83  {
84  munmap(window, len);
85  std::ostringstream ost;
86  ost << "Unable to cache " << len << " byte file segment at " << start << ": ";
87  throw cms::Exception("LocalCacheFile", ost.str(), e);
88  }
89 
90  munmap(window, len);
91 
92  if (nread != len)
93  throw cms::Exception("LocalCacheFile")
94  << "Unable to cache " << len << " byte file segment at " << start
95  << ": got only " << nread << " bytes back";
96 
97  present_[index] = 1;
98  ++cacheCount_;
99  if (cacheCount_ == cacheTotal_)
100  {
101  storage_->close();
102  closedFile_ = true;
103  }
104  }
105 
106  start += len;
107  ++index;
108  }
109 }
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_
size_t IOSize
Definition: IOTypes.h:14
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
Storage * storage_
void LocalCacheFile::close ( void  )
virtual

Reimplemented from Storage.

Definition at line 181 of file LocalCacheFile.cc.

References Storage::close(), File::close(), closedFile_, file_, and storage_.

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

182 {
183  if (!closedFile_)
184  {
185  storage_->close();
186  }
187  file_->close();
188 }
virtual void close(void)
Definition: Storage.cc:128
virtual void close(void)
Definition: File.cc:277
Storage * storage_
void LocalCacheFile::flush ( void  )
virtual

Reimplemented from Storage.

Definition at line 177 of file LocalCacheFile.cc.

References nowrite().

178 { nowrite("flush"); }
static void nowrite(const char *why)
IOOffset LocalCacheFile::position ( IOOffset  offset,
Relative  whence = SET 
)
virtual

Implements Storage.

Definition at line 169 of file LocalCacheFile.cc.

References file_, and File::position().

170 { return file_->position(offset, whence); }
unsigned int offset(bool)
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 191 of file LocalCacheFile.cc.

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

192 {
193  for (IOSize i = 0; i < n; ++i)
194  {
195  IOOffset start = what[i].offset();
196  IOOffset end = start + what[i].size();
197  cache(start, end);
198  }
199 
200  return file_->prefetch(what, n);
201 }
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 112 of file LocalCacheFile.cc.

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

113 {
114  IOOffset here = file_->position();
115  cache(here, here + n);
116 
117  return file_->read(into, n);
118 }
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 121 of file LocalCacheFile.cc.

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

122 {
123  cache(pos, pos + n);
124  return file_->read(into, n, pos);
125 }
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 128 of file LocalCacheFile.cc.

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

129 {
131  IOOffset end = start;
132  for (IOSize i = 0; i < n; ++i)
133  end += into[i].size();
134  cache(start, end);
135 
136  return file_->readv(into, n);
137 }
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 140 of file LocalCacheFile.cc.

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

141 {
142  for (IOSize i = 0; i < n; ++i)
143  {
144  IOOffset start = into[i].offset();
145  IOOffset end = start + into[i].size();
146  cache(start, end);
147  }
148 
149  return storage_->readv(into, n);
150 }
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 153 of file LocalCacheFile.cc.

References nowrite().

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

Reimplemented from Storage.

Definition at line 157 of file LocalCacheFile.cc.

References nowrite().

158 { nowrite("write"); return 0; }
static void nowrite(const char *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 161 of file LocalCacheFile.cc.

References nowrite().

162 { nowrite("writev"); return 0; }
static void nowrite(const char *why)
IOSize LocalCacheFile::writev ( const IOPosBuffer from,
IOSize  n 
)
virtual

Reimplemented from Storage.

Definition at line 165 of file LocalCacheFile.cc.

References nowrite().

166 { nowrite("writev"); return 0; }
static void nowrite(const char *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().