CMS 3D CMS Logo

LocalCacheFile.cc
Go to the documentation of this file.
3 #include <utility>
4 #include <iostream>
5 #include <cstdlib>
6 #include <cstring>
7 #include <unistd.h>
8 #include <sys/mman.h>
9 #include <cerrno>
10 #include <sstream>
11 
12 static const IOOffset CHUNK_SIZE = 128 * 1024 * 1024;
13 
14 static void nowrite(const std::string &why) {
15  cms::Exception ex("LocalCacheFile");
16  ex << "Cannot change file but operation '" << why << "' was called";
17  ex.addContext("LocalCacheFile::" + why + "()");
18  throw ex;
19 }
20 
21 LocalCacheFile::LocalCacheFile(std::unique_ptr<Storage> base, const std::string &tmpdir /* = "" */)
22  : image_(base->size()),
23  file_(),
24  storage_(std::move(base)),
25  closedFile_(false),
26  cacheCount_(0),
27  cacheTotal_((image_ + CHUNK_SIZE - 1) / CHUNK_SIZE) {
28  present_.resize(cacheTotal_, 0);
29 
30  std::string pattern(tmpdir);
31  if (pattern.empty())
32  if (char *p = std::getenv("TMPDIR"))
33  pattern = p;
34  if (pattern.empty())
35  pattern = "/tmp";
36  pattern += "/cmssw-shadow-XXXXXX";
37 
38  std::vector<char> temp(pattern.c_str(), pattern.c_str() + pattern.size() + 1);
39  int fd = mkstemp(&temp[0]);
40  if (fd == -1) {
42  ex << "Cannot create temporary file '" << pattern << "': " << strerror(errno) << " (error " << errno << ")";
43  ex.addContext("LocalCacheFile::LocalCacheFile");
44  }
45 
46  unlink(&temp[0]);
47  file_ = std::make_unique<File>(fd);
48  file_->resize(image_);
49 }
50 
52 
55  end = std::min(end, image_);
56 
57  IOSize nread = 0;
59 
60  while (start < end) {
62  if (!present_[index]) {
63  void *window = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start);
64  if (window == MAP_FAILED) {
66  ex << "Unable to map a window of local cache file: " << strerror(errno) << " (error " << errno << ")";
67  ex.addContext("LocalCacheFile::cache()");
68  throw ex;
69  }
70 
71  try {
72  nread = storage_->read(window, len, start);
73  } catch (cms::Exception &e) {
74  munmap(window, len);
75  std::ostringstream ost;
76  ost << "Unable to cache " << len << " byte file segment at " << start << ": ";
78  ex.addContext("LocalCacheFile::cache()");
79  throw ex;
80  }
81 
82  munmap(window, len);
83 
84  if (nread != len) {
86  ex << "Unable to cache " << len << " byte file segment at " << start << ": got only " << nread << " bytes back";
87  ex.addContext("LocalCacheFile::cache()");
88  throw ex;
89  }
90 
91  present_[index] = 1;
92  ++cacheCount_;
93  if (cacheCount_ == cacheTotal_) {
94  storage_->close();
95  closedFile_ = true;
96  }
97  }
98 
99  start += len;
100  ++index;
101  }
102 }
103 
105  IOOffset here = file_->position();
106  cache(here, here + n);
107 
108  return file_->read(into, n);
109 }
110 
112  cache(pos, pos + n);
113  return file_->read(into, n, pos);
114 }
115 
117  IOOffset start = file_->position();
118  IOOffset end = start;
119  for (IOSize i = 0; i < n; ++i)
120  end += into[i].size();
121  cache(start, end);
122 
123  return file_->readv(into, n);
124 }
125 
127  for (IOSize i = 0; i < n; ++i) {
128  IOOffset start = into[i].offset();
129  IOOffset end = start + into[i].size();
130  cache(start, end);
131  }
132 
133  return storage_->readv(into, n);
134 }
135 
136 IOSize LocalCacheFile::write(const void * /*from*/, IOSize) {
137  nowrite("write");
138  return 0;
139 }
140 
141 IOSize LocalCacheFile::write(const void * /*from*/, IOSize, IOOffset /*pos*/) {
142  nowrite("write");
143  return 0;
144 }
145 
147  nowrite("writev");
148  return 0;
149 }
150 
152  nowrite("writev");
153  return 0;
154 }
155 
156 IOOffset LocalCacheFile::position(IOOffset offset, Relative whence) { return file_->position(offset, whence); }
157 
158 void LocalCacheFile::resize(IOOffset /*size*/) { nowrite("resize"); }
159 
160 void LocalCacheFile::flush(void) { nowrite("flush"); }
161 
163  if (!closedFile_) {
164  storage_->close();
165  }
166  file_->close();
167 }
168 
170  for (IOSize i = 0; i < n; ++i) {
171  IOOffset start = what[i].offset();
172  IOOffset end = start + what[i].size();
173  cache(start, end);
174  }
175 
176  return file_->prefetch(what, n);
177 }
Storage::size
virtual IOOffset size(void) const
Definition: Storage.cc:77
mps_fire.i
i
Definition: mps_fire.py:355
start
Definition: start.py:1
funct::false
false
Definition: Factorize.h:34
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
cms::Exception::addContext
void addContext(std::string const &context)
Definition: Exception.cc:165
min
T min(T a, T b)
Definition: MathUtil.h:58
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
LocalCacheFile::~LocalCacheFile
~LocalCacheFile(void) override
Definition: LocalCacheFile.cc:51
pos
Definition: PixelAliasList.h:18
LocalCacheFile::image_
IOOffset image_
Definition: LocalCacheFile.h:38
svgfig.window
def window(xmin, xmax, ymin, ymax, x=0, y=0, width=100, height=100, xlogbase=None, ylogbase=None, minusInfinity=-1000, flipx=False, flipy=True)
Definition: svgfig.py:643
Storage::Relative
Relative
Definition: Storage.h:22
LocalCacheFile::closedFile_
bool closedFile_
Definition: LocalCacheFile.h:42
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
end
#define end
Definition: vmac.h:39
edm::errors::FileOpenError
Definition: EDMException.h:49
edm::Exception
Definition: EDMException.h:77
EDMException.h
LocalCacheFile::present_
std::vector< char > present_
Definition: LocalCacheFile.h:39
ztee.fd
fd
Definition: ztee.py:136
CHUNK_SIZE
static const IOOffset CHUNK_SIZE
Definition: LocalCacheFile.cc:12
LocalCacheFile::close
void close(void) override
Definition: LocalCacheFile.cc:162
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
LocalCacheFile::readv
IOSize readv(IOBuffer *into, IOSize n) override
Definition: LocalCacheFile.cc:116
LocalCacheFile::cacheTotal_
unsigned int cacheTotal_
Definition: LocalCacheFile.h:44
LocalCacheFile::resize
void resize(IOOffset size) override
Definition: LocalCacheFile.cc:158
LocalCacheFile::file_
edm::propagate_const< std::unique_ptr< File > > file_
Definition: LocalCacheFile.h:40
LocalCacheFile::storage_
edm::propagate_const< std::unique_ptr< Storage > > storage_
Definition: LocalCacheFile.h:41
IOBuffer
Definition: IOBuffer.h:7
LocalCacheFile::flush
void flush(void) override
Definition: LocalCacheFile.cc:160
IOOffset
int64_t IOOffset
Definition: IOTypes.h:19
LocalCacheFile::cacheCount_
unsigned int cacheCount_
Definition: LocalCacheFile.h:43
topSingleLeptonDQM_PU_cfi.pattern
pattern
Definition: topSingleLeptonDQM_PU_cfi.py:39
LocalCacheFile::write
IOSize write(const void *from, IOSize n) override
Definition: LocalCacheFile.cc:136
LocalCacheFile::writev
IOSize writev(const IOBuffer *from, IOSize n) override
Definition: LocalCacheFile.cc:146
LocalCacheFile.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
IOPosBuffer::size
IOSize size(void) const
Definition: IOPosBuffer.h:45
IOPosBuffer::offset
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
Storage::position
virtual IOOffset position(void) const
Definition: Storage.cc:72
LocalCacheFile::prefetch
bool prefetch(const IOPosBuffer *what, IOSize n) override
Definition: LocalCacheFile.cc:169
LocalCacheFile::cache
void cache(IOOffset start, IOOffset end)
Definition: LocalCacheFile.cc:53
IOInput::read
int read(void)
Definition: IOInput.cc:52
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
IOPosBuffer
Definition: IOPosBuffer.h:7
cms::Exception
Definition: Exception.h:70
LocalCacheFile::LocalCacheFile
LocalCacheFile(std::unique_ptr< Storage > base, const std::string &tmpdir="")
Definition: LocalCacheFile.cc:21
command_line.start
start
Definition: command_line.py:167
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:78
IOSize
size_t IOSize
Definition: IOTypes.h:14
newFWLiteAna.base
base
Definition: newFWLiteAna.py:92
edm::errors::FileReadError
Definition: EDMException.h:50
nowrite
static void nowrite(const std::string &why)
Definition: LocalCacheFile.cc:14
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37