#include <LocalCacheFile.h>
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 IOPosBuffer *from, IOSize n) |
virtual IOSize | writev (const IOBuffer *from, IOSize n) |
~LocalCacheFile (void) | |
Private Member Functions | |
void | cache (IOOffset start, IOOffset end) |
Private Attributes | |
unsigned int | cacheCount_ |
unsigned int | cacheTotal_ |
bool | closedFile_ |
File * | file_ |
IOOffset | image_ |
std::vector< char > | present_ |
Storage * | storage_ |
Proxy class to copy a file locally in large chunks.
Definition at line 10 of file LocalCacheFile.h.
LocalCacheFile::LocalCacheFile | ( | Storage * | base, |
const std::string & | tmpdir = "" |
||
) |
Definition at line 22 of file LocalCacheFile.cc.
References cacheTotal_, Exception, reco_application_tbsim_DetSim-Digi_cfg::File, file_, image_, AlCaHLTBitMon_ParallelJobs::p, listBenchmarks::pattern, present_, File::resize(), and groupFilesInBlocks::temp.
: image_(base->size()), file_(0), storage_(base), closedFile_(false), cacheCount_(0), cacheTotal_((image_ + CHUNK_SIZE - 1) / CHUNK_SIZE) { present_.resize(cacheTotal_, 0); std::string pattern(tmpdir); if (pattern.empty()) if (char *p = getenv("TMPDIR")) pattern = p; if (pattern.empty()) pattern = "/tmp"; pattern += "/cmssw-shadow-XXXXXX"; std::vector<char> temp(pattern.c_str(), pattern.c_str()+pattern.size()+1); int fd = mkstemp(&temp[0]); if (fd == -1) throw cms::Exception("LocalCacheFile") << "Cannot create temporary file '" << pattern << "': " << strerror(errno) << " (error " << errno << ")"; unlink(&temp[0]); file_ = new File(fd); file_->resize(image_); }
LocalCacheFile::~LocalCacheFile | ( | void | ) |
Definition at line 59 of file LocalCacheFile.cc.
References cacheCount_, cacheTotal_, CHUNK_SIZE, Storage::close(), closedFile_, alignCSCRings::e, Exception, IOChannel::fd(), file_, image_, getHLTprescales::index, min, present_, Storage::read(), errorMatrix2Lands_multiChannel::start, storage_, and svgfig::window().
Referenced by prefetch(), read(), and readv().
{ start = (start / CHUNK_SIZE) * CHUNK_SIZE; end = std::min(end, image_); IOSize nread = 0; IOSize index = start / CHUNK_SIZE; while (start < end) { IOSize len = std::min(image_ - start, CHUNK_SIZE); if (! present_[index]) { void *window = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start); if (window == MAP_FAILED) throw cms::Exception("LocalCacheFile") << "Unable to map a window of local cache file: " << strerror(errno) << " (error " << errno << ")"; try { nread = storage_->read(window, len, start); } catch (cms::Exception &e) { munmap(window, len); std::ostringstream ost; ost << "Unable to cache " << len << " byte file segment at " << start << ": "; throw cms::Exception("LocalCacheFile", ost.str(), e); } munmap(window, len); if (nread != len) throw cms::Exception("LocalCacheFile") << "Unable to cache " << len << " byte file segment at " << start << ": got only " << nread << " bytes back"; present_[index] = 1; ++cacheCount_; if (cacheCount_ == cacheTotal_) { storage_->close(); closedFile_ = true; } } start += len; ++index; } }
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_.
{ if (!closedFile_) { storage_->close(); } file_->close(); }
void LocalCacheFile::flush | ( | void | ) | [virtual] |
Reimplemented from Storage.
Definition at line 177 of file LocalCacheFile.cc.
References nowrite().
{ nowrite("flush"); }
Implements Storage.
Definition at line 169 of file LocalCacheFile.cc.
References file_, and File::position().
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 errorMatrix2Lands_multiChannel::start.
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.
In | case 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().
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.
In | case 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 errorMatrix2Lands_multiChannel::start.
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(), errorMatrix2Lands_multiChannel::start, and storage_.
void LocalCacheFile::resize | ( | IOOffset | size | ) | [virtual] |
Implements Storage.
Definition at line 173 of file LocalCacheFile.cc.
References nowrite().
{ nowrite("resize"); }
Reimplemented from Storage.
Definition at line 157 of file LocalCacheFile.cc.
References nowrite().
{ nowrite("write"); return 0; }
Write n bytes of data starting at address from.
In | case 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().
{ nowrite("write"); return 0; }
IOSize LocalCacheFile::writev | ( | const IOPosBuffer * | from, |
IOSize | n | ||
) | [virtual] |
Reimplemented from Storage.
Definition at line 165 of file LocalCacheFile.cc.
References nowrite().
{ nowrite("writev"); return 0; }
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.
In | case 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().
{ nowrite("writev"); return 0; }
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.
File* LocalCacheFile::file_ [private] |
Definition at line 39 of file LocalCacheFile.h.
Referenced by cache(), close(), LocalCacheFile(), position(), prefetch(), read(), readv(), and ~LocalCacheFile().
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().