#include <Utilities/StorageFactory/interface/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, IOOffset pos) |
virtual IOSize | read (void *into, IOSize n) |
Read into into at most n number of bytes. | |
virtual IOSize | readv (IOPosBuffer *into, IOSize n) |
virtual IOSize | readv (IOBuffer *into, IOSize n) |
Read from the input stream into multiple scattered buffers. | |
virtual void | resize (IOOffset size) |
virtual IOSize | write (const void *from, IOSize n, IOOffset pos) |
virtual IOSize | write (const void *from, IOSize n) |
Write n bytes of data starting at address from. | |
virtual IOSize | writev (const IOPosBuffer *from, IOSize n) |
virtual IOSize | writev (const IOBuffer *from, IOSize n) |
Write to the output stream from multiple buffers. | |
~LocalCacheFile (void) | |
Private Member Functions | |
void | cache (IOOffset start, IOOffset end) |
Private Attributes | |
File * | file_ |
IOOffset | image_ |
std::vector< char > | present_ |
Storage * | storage_ |
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 CHUNK_SIZE, Exception, fd, file_, image_, mkstemp(), p, present_, File::resize(), and pyDBSRunClass::temp.
00023 : image_(base->size()), 00024 file_(0), 00025 storage_(base) 00026 { 00027 present_.resize((image_ + CHUNK_SIZE - 1) / CHUNK_SIZE, 0); 00028 00029 std::string pattern(tmpdir); 00030 if (pattern.empty()) 00031 if (char *p = getenv("TMPDIR")) 00032 pattern = p; 00033 if (pattern.empty()) 00034 pattern = "/tmp"; 00035 pattern += "/cmssw-shadow-XXXXXX"; 00036 00037 std::vector<char> temp(pattern.c_str(), pattern.c_str()+pattern.size()+1); 00038 int fd = mkstemp(&temp[0]); 00039 if (fd == -1) 00040 throw cms::Exception("LocalCacheFile") 00041 << "Cannot create temporary file '" << pattern << "': " 00042 << strerror(errno) << " (error " << errno << ")"; 00043 00044 unlink(&temp[0]); 00045 file_ = new File(fd); 00046 file_->resize(image_); 00047 }
LocalCacheFile::~LocalCacheFile | ( | void | ) |
Definition at line 56 of file LocalCacheFile.cc.
References CHUNK_SIZE, e, Exception, IOChannel::fd(), file_, image_, index, len, min, mmap, present_, Storage::read(), storage_, and cms::Exception::what().
Referenced by prefetch(), read(), and readv().
00057 { 00058 start = (start / CHUNK_SIZE) * CHUNK_SIZE; 00059 end = std::min(end, image_); 00060 00061 IOSize nread = 0; 00062 IOSize index = start / CHUNK_SIZE; 00063 00064 while (start < end) 00065 { 00066 IOSize len = std::min(image_ - start, CHUNK_SIZE); 00067 if (! present_[index]) 00068 { 00069 void *window = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start); 00070 if (window == MAP_FAILED) 00071 throw cms::Exception("LocalCacheFile") 00072 << "Unable to map a window of local cache file: " 00073 << strerror(errno) << " (error " << errno << ")"; 00074 00075 try 00076 { 00077 nread = storage_->read(window, len, start); 00078 } 00079 catch (cms::Exception &e) 00080 { 00081 munmap(window, len); 00082 throw cms::Exception("LocalCacheFile") 00083 << "Unable to cache " << len << " byte file segment at " << start 00084 << ": " << e.what(); 00085 } 00086 00087 munmap(window, len); 00088 00089 if (nread != len) 00090 throw cms::Exception("LocalCacheFile") 00091 << "Unable to cache " << len << " byte file segment at " << start 00092 << ": got only " << nread << " bytes back"; 00093 00094 present_[index] = 1; 00095 } 00096 00097 start += len; 00098 ++index; 00099 } 00100 }
Reimplemented from Storage.
Definition at line 172 of file LocalCacheFile.cc.
References Storage::close(), File::close(), file_, and storage_.
Reimplemented from Storage.
Definition at line 168 of file LocalCacheFile.cc.
References nowrite().
00169 { nowrite("flush"); }
Implements Storage.
Definition at line 160 of file LocalCacheFile.cc.
References file_, and File::position().
bool LocalCacheFile::prefetch | ( | const IOPosBuffer * | what, | |
IOSize | n | |||
) | [virtual] |
Reimplemented from Storage.
Definition at line 179 of file LocalCacheFile.cc.
References cache(), end, file_, i, IOPosBuffer::offset(), File::prefetch(), and IOPosBuffer::size().
00180 { 00181 for (IOSize i = 0; i < n; ++i) 00182 { 00183 IOOffset start = what[i].offset(); 00184 IOOffset end = start + what[i].size(); 00185 cache(start, end); 00186 } 00187 00188 return file_->prefetch(what, n); 00189 }
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 103 of file LocalCacheFile.cc.
References cache(), file_, File::position(), and File::read().
00104 { 00105 IOOffset here = file_->position(); 00106 cache(here, here + n); 00107 00108 return file_->read(into, n); 00109 }
IOSize LocalCacheFile::readv | ( | IOPosBuffer * | into, | |
IOSize | n | |||
) | [virtual] |
Reimplemented from Storage.
Definition at line 131 of file LocalCacheFile.cc.
References cache(), end, i, IOPosBuffer::offset(), Storage::readv(), IOPosBuffer::size(), and storage_.
00132 { 00133 for (IOSize i = 0; i < n; ++i) 00134 { 00135 IOOffset start = into[i].offset(); 00136 IOOffset end = start + into[i].size(); 00137 cache(start, end); 00138 } 00139 00140 return storage_->readv(into, n); 00141 }
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 119 of file LocalCacheFile.cc.
References cache(), end, file_, i, File::position(), File::readv(), and Storage::size().
00120 { 00121 IOOffset start = file_->position(); 00122 IOOffset end = start; 00123 for (IOSize i = 0; i < n; ++i) 00124 end += into[i].size(); 00125 cache(start, end); 00126 00127 return file_->readv(into, n); 00128 }
Implements Storage.
Definition at line 164 of file LocalCacheFile.cc.
References nowrite().
00165 { nowrite("resize"); }
Reimplemented from Storage.
Definition at line 148 of file LocalCacheFile.cc.
References nowrite().
00149 { 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 144 of file LocalCacheFile.cc.
References nowrite().
00145 { nowrite("write"); return 0; }
IOSize LocalCacheFile::writev | ( | const IOPosBuffer * | from, | |
IOSize | n | |||
) | [virtual] |
Reimplemented from Storage.
Definition at line 156 of file LocalCacheFile.cc.
References nowrite().
00157 { 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 152 of file LocalCacheFile.cc.
References nowrite().
00153 { nowrite("writev"); return 0; }
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] |
std::vector<char> LocalCacheFile::present_ [private] |
Storage* LocalCacheFile::storage_ [private] |
Definition at line 40 of file LocalCacheFile.h.
Referenced by cache(), close(), readv(), and ~LocalCacheFile().