CMS 3D CMS Logo

StreamerInputFile.cc
Go to the documentation of this file.
2 
10 
13 
14 #include <iomanip>
15 #include <iostream>
16 
17 namespace edm {
18 
20 
22  std::string const& LFN,
23  std::shared_ptr<EventSkipperByID> eventSkipperByID,
24  unsigned int prefetchMBytes)
25  : startMsg_(),
26  currentEvMsg_(),
27  headerBuf_(1000 * 1000),
28  eventBuf_(1000 * 1000 * 7),
29  tempBuf_(1024 * 1024 * prefetchMBytes),
30  currentFile_(0),
31  streamerNames_(),
32  multiStreams_(false),
33  currentFileName_(),
34  currentFileOpen_(false),
35  eventSkipperByID_(eventSkipperByID),
36  currRun_(0),
37  currProto_(0),
38  newHeader_(false),
39  storage_(),
40  endOfFile_(false) {
41  openStreamerFile(name, LFN);
43  }
44 
46  std::shared_ptr<EventSkipperByID> eventSkipperByID,
47  unsigned int prefetchMBytes)
48  : StreamerInputFile(name, name, eventSkipperByID, prefetchMBytes) {}
49 
50  StreamerInputFile::StreamerInputFile(std::vector<FileCatalogItem> const& names,
51  std::shared_ptr<EventSkipperByID> eventSkipperByID,
52  unsigned int prefetchMBytes)
53  : startMsg_(),
54  currentEvMsg_(),
55  headerBuf_(1000 * 1000),
56  eventBuf_(1000 * 1000 * 7),
57  tempBuf_(1024 * 1024 * prefetchMBytes),
58  currentFile_(0),
59  streamerNames_(names),
60  multiStreams_(true),
61  currentFileName_(),
62  currentFileOpen_(false),
63  eventSkipperByID_(eventSkipperByID),
64  currRun_(0),
65  currProto_(0),
66  newHeader_(false),
67  endOfFile_(false) {
68  openStreamerFile(names.at(0).fileName(0), names.at(0).logicalFileName());
69  ++currentFile_;
71  currRun_ = startMsg_->run();
72  currProto_ = startMsg_->protocolVersion();
73  }
74 
77 
79 
80  // Check if the logical file name was found.
81  if (currentFileName_.empty()) {
82  // LFN not found in catalog.
83  throw cms::Exception("LogicalFileNameNotFound", "StreamerInputFile::openStreamerFile()\n")
84  << "Logical file name '" << LFN << "' was not found in the file catalog.\n"
85  << "If you wanted a local file, you forgot the 'file:' prefix\n"
86  << "before the file name in your configuration file.\n";
87  return;
88  }
89 
90  logFileAction(" Initiating request to open file ");
91 
92  using namespace edm::storage;
93  IOOffset size = -1;
94  if (StorageFactory::get()->check(name, &size)) {
95  try {
97  } catch (cms::Exception& e) {
99  ex.addContext("Calling StreamerInputFile::openStreamerFile()");
100  ex.clearMessage();
101  ex << "Error Opening Streamer Input File: " << name << "\n";
102  throw ex;
103  }
104  } else {
105  throw Exception(errors::FileOpenError, "StreamerInputFile::openStreamerFile")
106  << "Error Opening Streamer Input File, file does not exist: " << name << "\n";
107  }
108  currentFileOpen_ = true;
109  logFileAction(" Successfully opened file ");
110  }
111 
113  if (currentFileOpen_ && storage_) {
114  storage_->close();
115  logFileAction(" Closed file ");
116  }
117  currentFileOpen_ = false;
118  }
119 
120  std::pair<storage::IOSize, char*> StreamerInputFile::readBytes(char* buf,
121  storage::IOSize nBytes,
122  bool zeroCopy,
123  unsigned int skippedHdr) {
124  storage::IOSize n = 0;
125  //returned pointer should point to the beginning of the header
126  //even if we read event payload that comes afterwards
127  char* ptr = buf - skippedHdr;
128  try {
129  if (!tempBuf_.empty()) {
130  if (tempPos_ == tempLen_) {
131  n = storage_->read(&tempBuf_[0], tempBuf_.size());
132  tempPos_ = 0;
133  tempLen_ = n;
134  if (n == 0)
135  return std::pair<storage::IOSize, char*>(0, ptr);
136  }
137  if (nBytes <= tempLen_ - tempPos_) {
138  //zero-copy can't done when header start address is in the previous buffer
139  if (!zeroCopy || skippedHdr > tempPos_) {
140  memcpy(buf, &tempBuf_[0] + tempPos_, nBytes);
141  tempPos_ += nBytes;
142  } else {
143  //pass pointer to the prebuffer address (zero copy) at the beginning of the header
144  ptr = &tempBuf_[0] + tempPos_ - skippedHdr;
145  tempPos_ += nBytes;
146  }
147  n = nBytes;
148  } else {
149  //crossing buffer boundary
150  auto len = tempLen_ - tempPos_;
151  memcpy(buf, &tempBuf_[0] + tempPos_, len);
152  tempPos_ += len;
153  char* tmpPtr = buf + len;
154  n = len + readBytes(tmpPtr, nBytes - len, false).first;
155  }
156  } else
157  n = storage_->read(buf, nBytes);
158  } catch (cms::Exception& ce) {
159  Exception ex(errors::FileReadError, "", ce);
160  ex.addContext("Calling StreamerInputFile::readBytes()");
161  throw ex;
162  }
163  return std::pair<storage::IOSize, char*>(n, ptr);
164  }
165 
167  storage::IOOffset n = 0;
168  try {
169  // We wish to return the number of bytes skipped, not the final offset.
170  n = storage_->position(0, storage::Storage::CURRENT);
171  n = storage_->position(nBytes, storage::Storage::CURRENT) - n;
172  } catch (cms::Exception& ce) {
173  Exception ex(errors::FileReadError, "", ce);
174  ex.addContext("Calling StreamerInputFile::skipBytes()");
175  throw ex;
176  }
177  return n;
178  }
179 
181  using namespace edm::storage;
182  IOSize nWant = sizeof(InitHeader);
183  IOSize nGot = readBytes(&headerBuf_[0], nWant, false).first;
184  if (nGot != nWant) {
185  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
186  << "Failed reading streamer file, first read in readStartMessage\n";
187  }
188 
189  uint32 headerSize;
190  {
191  HeaderView head(&headerBuf_[0]);
192  uint32 code = head.code();
193  if (code != Header::INIT)
194  {
195  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
196  << "Expecting an init Message at start of file\n";
197  return;
198  }
199  headerSize = head.size();
200  }
201 
202  if (headerBuf_.size() < headerSize)
203  headerBuf_.resize(headerSize);
204 
205  if (headerSize > sizeof(InitHeader)) {
206  nWant = headerSize - sizeof(InitHeader);
207  auto res = readBytes(&headerBuf_[sizeof(InitHeader)], nWant, true, sizeof(InitHeader));
208  if (res.first != nWant) {
209  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
210  << "Failed reading streamer file, second read in readStartMessage\n";
211  }
212  startMsg_ = std::make_shared<InitMsgView>(res.second); // propagate_const<T> has no reset() function
213  } else {
214  throw Exception(errors::FileReadError, "StreamerInputFile::readStartMessage")
215  << "Failed reading streamer file, init header size from data too small\n";
216  }
217  }
218 
220  if (this->readEventMessage()) {
221  return Next::kEvent;
222  }
223  if (multiStreams_) {
224  //Try opening next file
225  if (currentFile_ <= streamerNames_.size() - 1) {
226  newHeader_ = true;
227  return Next::kFile;
228  }
229  }
230  return Next::kStop;
231  }
232 
234  if (currentFile_ <= streamerNames_.size() - 1) {
235  FDEBUG(10) << "Opening file " << streamerNames_.at(currentFile_).fileNames()[0].c_str() << std::endl;
236 
237  openStreamerFile(streamerNames_.at(currentFile_).fileNames()[0],
238  streamerNames_.at(currentFile_).logicalFileName());
239 
240  // If start message was already there, then compare the
241  // previous and new headers
242  if (startMsg_) {
243  FDEBUG(10) << "Comparing Header" << std::endl;
244  compareHeader();
245  }
246  ++currentFile_;
247  endOfFile_ = false;
248  return true;
249  }
250  return false;
251  }
252 
254  //Get the new header
256 
257  //Values from new Header should match up
258  if (currRun_ != startMsg_->run() || currProto_ != startMsg_->protocolVersion()) {
259  throw Exception(errors::MismatchedInputFiles, "StreamerInputFile::compareHeader")
260  << "File " << streamerNames_.at(currentFile_).fileNames()[0]
261  << "\nhas different run number or protocol version than previous\n";
262  return false;
263  }
264  return true;
265  }
266 
268  if (endOfFile_)
269  return 0;
270 
271  using namespace edm::storage;
272  bool eventRead = false;
273  unsigned hdrSkipped = 0;
274  while (!eventRead) {
275  IOSize nWant = sizeof(EventHeader);
276  IOSize nGot = readBytes(&eventBuf_[hdrSkipped], nWant - hdrSkipped, false).first + hdrSkipped;
277  while (nGot == nWant) {
278  //allow padding before next event or end of file.
279  //event header starts with code 0 - 17, so 0xff (Header:PADDING) uniquely represents padding
280  bool headerFetched = false;
281  for (size_t i = 0; i < nGot; i++) {
282  if ((unsigned char)eventBuf_[i] != Header::PADDING) {
283  //no padding 0xff
284  if (i != 0) {
285  memmove(&eventBuf_[0], &eventBuf_[i], nGot - i);
286  //read remainder of the header
287  nGot = nGot - i + readBytes(&eventBuf_[nGot - i], i, false).first;
288  }
289  headerFetched = true;
290  break;
291  }
292  }
293  if (headerFetched)
294  break;
295  //read another block
296  nGot = readBytes(&eventBuf_[0], nWant, false).first;
297  }
298  if (nGot == 0) {
299  // no more data available
300  endOfFile_ = true;
301  return 0;
302  }
303  if (nGot != nWant) {
304  for (size_t i = 0; i < nGot; i++) {
305  if ((unsigned char)eventBuf_[i] != Header::PADDING)
306  throw edm::Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
307  << "Failed reading streamer file, first read in readEventMessage\n"
308  << "Requested " << nWant << " bytes, read function returned " << nGot
309  << " bytes, non-padding at offset " << i;
310  }
311  //padded 0xff only
312  endOfFile_ = true;
313  return 0;
314  }
315  uint32 eventSize;
316  {
317  HeaderView head(&eventBuf_[0]);
318  uint32 code = head.code();
319 
320  // If it is not an event then something is wrong.
321  eventSize = head.size();
322  if (code != Header::EVENT) {
323  if (code == Header::INIT) {
324  edm::LogWarning("StreamerInputFile") << "Found another INIT header in the file. It will be skipped";
325  if (eventSize < sizeof(EventHeader)) {
326  //very unlikely case that EventHeader is larger than total INIT size inserted in the middle of the file
327  hdrSkipped = nGot - eventSize;
328  memmove(&eventBuf_[0], &eventBuf_[eventSize], hdrSkipped);
329  continue;
330  }
331  if (headerBuf_.size() < eventSize)
332  headerBuf_.resize(eventSize);
333  memcpy(&headerBuf_[0], &eventBuf_[0], nGot);
334  readBytes(&headerBuf_[nGot], eventSize, true, nGot);
335  //do not parse this header and proceed to the next event
336  continue;
337  }
338  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
339  << "Failed reading streamer file, unknown code in event header\n"
340  << "code = " << code << "\n";
341  }
342  }
343  if (eventSize <= sizeof(EventHeader)) {
344  throw edm::Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
345  << "Failed reading streamer file, event header size from data too small\n";
346  }
347  eventRead = true;
348  if (eventSkipperByID_) {
349  EventHeader* evh = (EventHeader*)(&eventBuf_[0]);
350  if (eventSkipperByID_->skipIt(convert32(evh->run_), convert32(evh->lumi_), convert64(evh->event_))) {
351  eventRead = false;
352  }
353  }
354  nWant = eventSize - sizeof(EventHeader);
355  if (eventRead) {
356  if (eventBuf_.size() < eventSize)
357  eventBuf_.resize(eventSize);
358 
359  auto res = readBytes(&eventBuf_[sizeof(EventHeader)], nWant, true, sizeof(EventHeader));
360  if (res.first != nWant) {
361  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
362  << "Failed reading streamer file, second read in readEventMessage\n"
363  << "Requested " << nWant << " bytes, read function returned " << res.first << " bytes\n";
364  }
365  currentEvMsg_ =
366  std::make_shared<EventMsgView>((void*)res.second); // propagate_const<T> has no reset() function
367  } else {
368  nGot = skipBytes(nWant);
369  if (nGot != nWant) {
370  throw Exception(errors::FileReadError, "StreamerInputFile::readEventMessage")
371  << "Failed reading streamer file, skip event in readEventMessage\n"
372  << "Requested " << nWant << " bytes skipped, seek function returned " << nGot << " bytes\n";
373  }
374  }
375  }
376  return 1;
377  }
378 
380  LogAbsolute("fileAction") << std::setprecision(0) << TimeOfDay() << msg << currentFileName_;
381  FlushMessageLog();
382  }
383 } // namespace edm
size
Write out results.
int64_t IOOffset
Definition: IOTypes.h:20
uint64 convert64(char_uint64 v)
Definition: MsgTools.h:20
std::vector< char > eventBuf_
void FlushMessageLog()
edm::propagate_const< std::shared_ptr< EventSkipperByID > > eventSkipperByID_
char_uint32 lumi_
Definition: EventMessage.h:66
#define FDEBUG(lev)
Definition: DebugMacros.h:19
const std::string names[nVars_]
Definition: Electron.h:6
std::pair< storage::IOSize, char * > readBytes(char *buf, storage::IOSize nBytes, bool zeroCopy, unsigned int skippedHdr=0)
uint32 size() const
Definition: MsgHeader.h:45
storage::IOOffset skipBytes(storage::IOSize nBytes)
StreamerInputFile(std::string const &name, std::string const &LFN, std::shared_ptr< EventSkipperByID > eventSkipperByID=std::shared_ptr< EventSkipperByID >(), unsigned int prefetchMBytes=0)
void clearMessage()
Definition: Exception.cc:159
void openStreamerFile(std::string const &name, std::string const &LFN)
unsigned int uint32
Definition: MsgTools.h:13
uint32 code() const
Definition: MsgHeader.h:44
char_uint32 run_
Definition: EventMessage.h:64
size_t IOSize
Definition: IOTypes.h:15
char_uint64 event_
Definition: EventMessage.h:65
void logFileAction(char const *msg)
void addContext(std::string const &context)
Definition: Exception.cc:165
tuple msg
Definition: mps_check.py:286
static void check(T const &p, std::string const &id, SelectedProducts const &iProducts, bool iVerbose)
uint32 convert32(char_uint32 v)
Definition: MsgTools.h:28
std::vector< char > headerBuf_
HLT enums.
edm::propagate_const< std::shared_ptr< EventMsgView > > currentEvMsg_
std::vector< FileCatalogItem > streamerNames_
Log< level::System, true > LogAbsolute
#define get
Log< level::Warning, false > LogWarning
std::vector< char > tempBuf_
edm::propagate_const< std::shared_ptr< InitMsgView > > startMsg_
edm::propagate_const< std::unique_ptr< edm::storage::Storage > > storage_