CMS 3D CMS Logo

FedRawDataInputSource.cc
Go to the documentation of this file.
1 #include <fcntl.h>
2 #include <iomanip>
3 #include <iostream>
4 #include <memory>
5 #include <sstream>
6 #include <sys/types.h>
7 #include <sys/file.h>
8 #include <sys/time.h>
9 #include <unistd.h>
10 #include <vector>
11 #include <fstream>
12 #include <zlib.h>
13 #include <cstdio>
14 #include <chrono>
15 
16 #include <boost/algorithm/string.hpp>
17 
21 
23 
30 
32 
34 
38 
40 
45 
46 //JSON file reader
48 
49 using namespace evf::FastMonState;
50 
52  : edm::RawInputSource(pset, desc),
53  defPath_(pset.getUntrackedParameter<std::string>("buDefPath", "")),
54  eventChunkSize_(pset.getUntrackedParameter<unsigned int>("eventChunkSize", 32) * 1048576),
55  eventChunkBlock_(pset.getUntrackedParameter<unsigned int>("eventChunkBlock", 32) * 1048576),
56  numBuffers_(pset.getUntrackedParameter<unsigned int>("numBuffers", 2)),
57  maxBufferedFiles_(pset.getUntrackedParameter<unsigned int>("maxBufferedFiles", 2)),
58  getLSFromFilename_(pset.getUntrackedParameter<bool>("getLSFromFilename", true)),
59  alwaysStartFromFirstLS_(pset.getUntrackedParameter<bool>("alwaysStartFromFirstLS", false)),
60  verifyChecksum_(pset.getUntrackedParameter<bool>("verifyChecksum", true)),
61  useL1EventID_(pset.getUntrackedParameter<bool>("useL1EventID", false)),
62  testTCDSFEDRange_(
63  pset.getUntrackedParameter<std::vector<unsigned int>>("testTCDSFEDRange", std::vector<unsigned int>())),
64  fileNames_(pset.getUntrackedParameter<std::vector<std::string>>("fileNames", std::vector<std::string>())),
65  fileListMode_(pset.getUntrackedParameter<bool>("fileListMode", false)),
66  fileListLoopMode_(pset.getUntrackedParameter<bool>("fileListLoopMode", false)),
67  runNumber_(edm::Service<evf::EvFDaqDirector>()->getRunNumber()),
68  daqProvenanceHelper_(edm::TypeID(typeid(FEDRawDataCollection))),
69  eventID_(),
70  processHistoryID_(),
71  currentLumiSection_(0),
72  tcds_pointer_(nullptr),
73  eventsThisLumi_(0) {
74  char thishost[256];
75  gethostname(thishost, 255);
76  edm::LogInfo("FedRawDataInputSource") << "Construction. read-ahead chunk size -: " << std::endl
77  << (eventChunkSize_ / 1048576) << " MB on host " << thishost;
78 
79  if (!testTCDSFEDRange_.empty()) {
80  if (testTCDSFEDRange_.size() != 2) {
81  throw cms::Exception("FedRawDataInputSource::fillFEDRawDataCollection")
82  << "Invalid TCDS Test FED range parameter";
83  }
86  }
87 
88  long autoRunNumber = -1;
89  if (fileListMode_) {
90  autoRunNumber = initFileList();
91  if (!fileListLoopMode_) {
92  if (autoRunNumber < 0)
93  throw cms::Exception("FedRawDataInputSource::FedRawDataInputSource") << "Run number not found from filename";
94  //override run number
95  runNumber_ = (edm::RunNumber_t)autoRunNumber;
96  edm::Service<evf::EvFDaqDirector>()->overrideRunNumber((unsigned int)autoRunNumber);
97  }
98  }
99 
101  setNewRun();
102  //todo:autodetect from file name (assert if names differ)
104 
105  //make sure that chunk size is N * block size
110 
111  if (!numBuffers_)
112  throw cms::Exception("FedRawDataInputSource::FedRawDataInputSource")
113  << "no reading enabled with numBuffers parameter 0";
114 
117  readingFilesCount_ = 0;
118 
119  if (!crc32c_hw_test())
120  edm::LogError("FedRawDataInputSource::FedRawDataInputSource") << "Intel crc32c checksum computation unavailable";
121 
122  //get handles to DaqDirector and FastMonitoringService because getting them isn't possible in readSupervisor thread
123  if (fileListMode_) {
124  try {
126  } catch (cms::Exception const&) {
127  edm::LogInfo("FedRawDataInputSource") << "No FastMonitoringService found in the configuration";
128  }
129  } else {
131  if (!fms_) {
132  throw cms::Exception("FedRawDataInputSource") << "FastMonitoringService not found";
133  }
134  }
135 
137  if (!daqDirector_)
138  cms::Exception("FedRawDataInputSource") << "EvFDaqDirector not found";
139 
141  if (useFileBroker_)
142  edm::LogInfo("FedRawDataInputSource") << "EvFDaqDirector/Source configured to use file service";
143  //set DaqDirector to delete files in preGlobalEndLumi callback
145  if (fms_) {
147  fms_->setInputSource(this);
150  }
151  //should delete chunks when run stops
152  for (unsigned int i = 0; i < numBuffers_; i++) {
154  }
155 
156  quit_threads_ = false;
157 
158  for (unsigned int i = 0; i < numConcurrentReads_; i++) {
159  std::unique_lock<std::mutex> lk(startupLock_);
160  //issue a memory fence here and in threads (constructor was segfaulting without this)
161  thread_quit_signal.push_back(false);
162  workerJob_.push_back(ReaderInfo(nullptr, nullptr));
163  cvReader_.push_back(new std::condition_variable);
164  tid_active_.push_back(0);
165  threadInit_.store(false, std::memory_order_release);
166  workerThreads_.push_back(new std::thread(&FedRawDataInputSource::readWorker, this, i));
167  startupCv_.wait(lk);
168  }
169 
170  runAuxiliary()->setProcessHistoryID(processHistoryID_);
171 }
172 
174  quit_threads_ = true;
175 
176  //delete any remaining open files
177  if (!fms_ || !fms_->exceptionDetected()) {
178  for (auto it = filesToDelete_.begin(); it != filesToDelete_.end(); it++)
179  it->second.reset();
180  } else {
181  //skip deleting files with exception
182  for (auto it = filesToDelete_.begin(); it != filesToDelete_.end(); it++) {
183  //it->second->unsetDeleteFile();
184  if (fms_->isExceptionOnData(it->second->lumi_))
185  it->second->unsetDeleteFile();
186  else
187  it->second.reset();
188  }
189  //disable deleting current file with exception
190  if (currentFile_.get())
191  if (fms_->isExceptionOnData(currentFile_->lumi_))
192  currentFile_->unsetDeleteFile();
193  }
194 
196  readSupervisorThread_->join();
197  } else {
198  //join aux threads in case the supervisor thread was not started
199  for (unsigned int i = 0; i < workerThreads_.size(); i++) {
200  std::unique_lock<std::mutex> lk(mReader_);
201  thread_quit_signal[i] = true;
202  cvReader_[i]->notify_one();
203  lk.unlock();
204  workerThreads_[i]->join();
205  delete workerThreads_[i];
206  }
207  }
208  for (unsigned int i = 0; i < numConcurrentReads_; i++)
209  delete cvReader_[i];
210  /*
211  for (unsigned int i=0;i<numConcurrentReads_+1;i++) {
212  InputChunk *ch;
213  while (!freeChunks_.try_pop(ch)) {}
214  delete ch;
215  }
216  */
217 }
218 
221  desc.setComment("File-based Filter Farm input source for reading raw data from BU ramdisk");
222  desc.addUntracked<unsigned int>("eventChunkSize", 32)->setComment("Input buffer (chunk) size");
223  desc.addUntracked<unsigned int>("eventChunkBlock", 32)
224  ->setComment("Block size used in a single file read call (must be smaller or equal to buffer size)");
225  desc.addUntracked<unsigned int>("numBuffers", 2)->setComment("Number of buffers used for reading input");
226  desc.addUntracked<unsigned int>("maxBufferedFiles", 2)
227  ->setComment("Maximum number of simultaneously buffered raw files");
228  desc.addUntracked<unsigned int>("alwaysStartFromfirstLS", false)
229  ->setComment("Force source to start from LS 1 if server provides higher lumisection number");
230  desc.addUntracked<bool>("verifyChecksum", true)
231  ->setComment("Verify event CRC-32C checksum of FRDv5 and higher or Adler32 with v3 and v4");
232  desc.addUntracked<bool>("useL1EventID", false)
233  ->setComment("Use L1 event ID from FED header if true or from TCDS FED if false");
234  desc.addUntracked<std::vector<unsigned int>>("testTCDSFEDRange", std::vector<unsigned int>())
235  ->setComment("[min, max] range to search for TCDS FED ID in test setup");
236  desc.addUntracked<bool>("fileListMode", false)
237  ->setComment("Use fileNames parameter to directly specify raw files to open");
238  desc.addUntracked<std::vector<std::string>>("fileNames", std::vector<std::string>())
239  ->setComment("file list used when fileListMode is enabled");
240  desc.setAllowAnything();
241  descriptions.add("source", desc);
242 }
243 
246  //this thread opens new files and dispatches reading to worker readers
247  //threadInit_.store(false,std::memory_order_release);
248  std::unique_lock<std::mutex> lk(startupLock_);
249  readSupervisorThread_ = std::make_unique<std::thread>(&FedRawDataInputSource::readSupervisor, this);
251  startupCv_.wait(lk);
252  }
253  //signal hltd to start event accounting
254  if (!currentLumiSection_)
257  switch (nextEvent()) {
259  //maybe create EoL file in working directory before ending run
260  struct stat buf;
261  if (!useFileBroker_ && currentLumiSection_ > 0) {
262  bool eolFound = (stat(daqDirector_->getEoLSFilePathOnBU(currentLumiSection_).c_str(), &buf) == 0);
263  if (eolFound) {
265  bool found = (stat(fuEoLS.c_str(), &buf) == 0);
266  if (!found) {
268  int eol_fd =
269  open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
270  close(eol_fd);
272  }
273  }
274  }
275  //also create EoR file in FU data directory
276  bool eorFound = (stat(daqDirector_->getEoRFilePathOnFU().c_str(), &buf) == 0);
277  if (!eorFound) {
278  int eor_fd = open(daqDirector_->getEoRFilePathOnFU().c_str(),
279  O_RDWR | O_CREAT,
280  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
281  close(eor_fd);
282  }
284  eventsThisLumi_ = 0;
286  edm::LogInfo("FedRawDataInputSource") << "----------------RUN ENDED----------------";
287  return Next::kStop;
288  }
290  //this is not reachable
291  return Next::kEvent;
292  }
294  //std::cout << "--------------NEW LUMI---------------" << std::endl;
295  return Next::kEvent;
296  }
297  default: {
298  if (!getLSFromFilename_) {
299  //get new lumi from file header
300  if (event_->lumi() > currentLumiSection_) {
302  eventsThisLumi_ = 0;
304  }
305  }
308  else
309  eventRunNumber_ = event_->run();
310  L1EventID_ = event_->event();
311 
312  setEventCached();
313 
314  return Next::kEvent;
315  }
316  }
317 }
318 
319 void FedRawDataInputSource::maybeOpenNewLumiSection(const uint32_t lumiSection) {
320  if (!luminosityBlockAuxiliary() || luminosityBlockAuxiliary()->luminosityBlock() != lumiSection) {
321  if (!useFileBroker_) {
322  if (currentLumiSection_ > 0) {
324  struct stat buf;
325  bool found = (stat(fuEoLS.c_str(), &buf) == 0);
326  if (!found) {
328  int eol_fd =
329  open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
330  close(eol_fd);
331  daqDirector_->createBoLSFile(lumiSection, false);
333  }
334  } else
335  daqDirector_->createBoLSFile(lumiSection, true); //needed for initial lumisection
336  }
337 
338  currentLumiSection_ = lumiSection;
339 
341 
342  timeval tv;
343  gettimeofday(&tv, nullptr);
344  const edm::Timestamp lsopentime((unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec);
345 
347  runAuxiliary()->run(), lumiSection, lsopentime, edm::Timestamp::invalidTimestamp());
348 
349  setLuminosityBlockAuxiliary(lumiBlockAuxiliary);
350  luminosityBlockAuxiliary()->setProcessHistoryID(processHistoryID_);
351 
352  edm::LogInfo("FedRawDataInputSource") << "New lumi section was opened. LUMI -: " << lumiSection;
353  }
354 }
355 
359  if (edm::shutdown_flag.load(std::memory_order_relaxed))
360  break;
361  }
362  return status;
363 }
364 
366  if (setExceptionState_)
367  threadError();
368  if (!currentFile_.get()) {
371  if (!fileQueue_.try_pop(currentFile_)) {
372  //sleep until wakeup (only in single-buffer mode) or timeout
373  std::unique_lock<std::mutex> lkw(mWakeup_);
374  if (cvWakeup_.wait_for(lkw, std::chrono::milliseconds(100)) == std::cv_status::timeout || !currentFile_.get())
376  }
377  status = currentFile_->status_;
380  currentFile_.reset();
381  return status;
382  } else if (status == evf::EvFDaqDirector::runAbort) {
383  throw cms::Exception("FedRawDataInputSource::getNextEvent")
384  << "Run has been aborted by the input source reader thread";
385  } else if (status == evf::EvFDaqDirector::newLumi) {
387  if (getLSFromFilename_) {
388  if (currentFile_->lumi_ > currentLumiSection_) {
390  eventsThisLumi_ = 0;
392  }
393  } else { //let this be picked up from next event
395  }
396  currentFile_.reset();
397  return status;
398  } else if (status == evf::EvFDaqDirector::newFile) {
400  } else
401  assert(false);
402  }
404 
405  //file is empty
406  if (!currentFile_->fileSize_) {
408  //try to open new lumi
409  assert(currentFile_->nChunks_ == 0);
410  if (getLSFromFilename_)
411  if (currentFile_->lumi_ > currentLumiSection_) {
413  eventsThisLumi_ = 0;
415  }
416  //immediately delete empty file
417  currentFile_.reset();
419  }
420 
421  //file is finished
422  if (currentFile_->bufferPosition_ == currentFile_->fileSize_) {
424  //release last chunk (it is never released elsewhere)
425  freeChunks_.push(currentFile_->chunks_[currentFile_->currentChunk_]);
426  if (currentFile_->nEvents_ >= 0 && currentFile_->nEvents_ != int(currentFile_->nProcessed_)) {
427  throw cms::Exception("FedRawDataInputSource::getNextEvent")
428  << "Fully processed " << currentFile_->nProcessed_ << " from the file " << currentFile_->fileName_
429  << " but according to BU JSON there should be " << currentFile_->nEvents_ << " events";
430  }
431  //try to wake up supervisor thread which might be sleeping waiting for the free chunk
432  if (singleBufferMode_) {
433  std::unique_lock<std::mutex> lkw(mWakeup_);
434  cvWakeup_.notify_one();
435  }
436  bufferInputRead_ = 0;
438  //put the file in pending delete list;
439  std::unique_lock<std::mutex> lkw(fileDeleteLock_);
440  filesToDelete_.push_back(std::pair<int, std::unique_ptr<InputFile>>(currentFileIndex_, std::move(currentFile_)));
441  } else {
442  //in single-thread and stream jobs, events are already processed
443  currentFile_.reset();
444  }
446  }
447 
448  //handle RAW file header
449  if (currentFile_->bufferPosition_ == 0 && currentFile_->rawHeaderSize_ > 0) {
450  if (currentFile_->fileSize_ <= currentFile_->rawHeaderSize_) {
451  if (currentFile_->fileSize_ < currentFile_->rawHeaderSize_)
452  throw cms::Exception("FedRawDataInputSource::getNextEvent")
453  << "Premature end of input file while reading file header";
454 
455  edm::LogWarning("FedRawDataInputSource")
456  << "File with only raw header and no events received in LS " << currentFile_->lumi_;
457  if (getLSFromFilename_)
458  if (currentFile_->lumi_ > currentLumiSection_) {
460  eventsThisLumi_ = 0;
462  }
463  }
464 
465  //advance buffer position to skip file header (chunk will be acquired later)
466  currentFile_->chunkPosition_ += currentFile_->rawHeaderSize_;
467  currentFile_->bufferPosition_ += currentFile_->rawHeaderSize_;
468  }
469 
470  //file is too short
471  if (currentFile_->fileSize_ - currentFile_->bufferPosition_ < FRDHeaderVersionSize[detectedFRDversion_]) {
472  throw cms::Exception("FedRawDataInputSource::getNextEvent")
473  << "Premature end of input file while reading event header";
474  }
475  if (singleBufferMode_) {
476  //should already be there
478  while (!currentFile_->waitForChunk(currentFile_->currentChunk_)) {
479  usleep(10000);
480  if (currentFile_->parent_->exceptionState() || setExceptionState_)
481  currentFile_->parent_->threadError();
482  }
484 
485  unsigned char* dataPosition = currentFile_->chunks_[0]->buf_ + currentFile_->chunkPosition_;
486 
487  //conditions when read amount is not sufficient for the header to fit
491 
492  if (detectedFRDversion_ == 0) {
493  detectedFRDversion_ = *((uint16_t*)dataPosition);
495  throw cms::Exception("FedRawDataInputSource::getNextEvent")
496  << "Unknown FRD version -: " << detectedFRDversion_;
498  }
499 
500  //recalculate chunk position
501  dataPosition = currentFile_->chunks_[0]->buf_ + currentFile_->chunkPosition_;
503  throw cms::Exception("FedRawDataInputSource::getNextEvent")
504  << "Premature end of input file while reading event header";
505  }
506  }
507 
508  event_ = std::make_unique<FRDEventMsgView>(dataPosition);
509  if (event_->size() > eventChunkSize_) {
510  throw cms::Exception("FedRawDataInputSource::getNextEvent")
511  << " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
512  << " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << eventChunkSize_
513  << " bytes";
514  }
515 
516  const uint32_t msgSize = event_->size() - FRDHeaderVersionSize[detectedFRDversion_];
517 
518  if (currentFile_->fileSize_ - currentFile_->bufferPosition_ < msgSize) {
519  throw cms::Exception("FedRawDataInputSource::getNextEvent")
520  << "Premature end of input file while reading event data";
521  }
522  if (eventChunkSize_ - currentFile_->chunkPosition_ < msgSize) {
524  //recalculate chunk position
525  dataPosition = currentFile_->chunks_[0]->buf_ + currentFile_->chunkPosition_;
526  event_ = std::make_unique<FRDEventMsgView>(dataPosition);
527  }
528  currentFile_->bufferPosition_ += event_->size();
529  currentFile_->chunkPosition_ += event_->size();
530  //last chunk is released when this function is invoked next time
531 
532  }
533  //multibuffer mode:
534  else {
535  //wait for the current chunk to become added to the vector
537  while (!currentFile_->waitForChunk(currentFile_->currentChunk_)) {
538  usleep(10000);
539  if (setExceptionState_)
540  threadError();
541  }
543 
544  //check if header is at the boundary of two chunks
545  chunkIsFree_ = false;
546  unsigned char* dataPosition;
547 
548  //read header, copy it to a single chunk if necessary
549  bool chunkEnd = currentFile_->advance(dataPosition, FRDHeaderVersionSize[detectedFRDversion_]);
550 
551  event_ = std::make_unique<FRDEventMsgView>(dataPosition);
552  if (event_->size() > eventChunkSize_) {
553  throw cms::Exception("FedRawDataInputSource::getNextEvent")
554  << " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
555  << " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << eventChunkSize_
556  << " bytes";
557  }
558 
559  const uint32_t msgSize = event_->size() - FRDHeaderVersionSize[detectedFRDversion_];
560 
561  if (currentFile_->fileSize_ - currentFile_->bufferPosition_ < msgSize) {
562  throw cms::Exception("FedRawDataInputSource::getNextEvent")
563  << "Premature end of input file while reading event data";
564  }
565 
566  if (chunkEnd) {
567  //header was at the chunk boundary, we will have to move payload as well
568  currentFile_->moveToPreviousChunk(msgSize, FRDHeaderVersionSize[detectedFRDversion_]);
569  chunkIsFree_ = true;
570  } else {
571  //header was contiguous, but check if payload fits the chunk
572  if (eventChunkSize_ - currentFile_->chunkPosition_ < msgSize) {
573  //rewind to header start position
575  //copy event to a chunk start and move pointers
576 
578 
579  chunkEnd = currentFile_->advance(dataPosition, FRDHeaderVersionSize[detectedFRDversion_] + msgSize);
580 
582 
583  assert(chunkEnd);
584  chunkIsFree_ = true;
585  //header is moved
586  event_ = std::make_unique<FRDEventMsgView>(dataPosition);
587  } else {
588  //everything is in a single chunk, only move pointers forward
589  chunkEnd = currentFile_->advance(dataPosition, msgSize);
590  assert(!chunkEnd);
591  chunkIsFree_ = false;
592  }
593  }
594  } //end multibuffer mode
596 
597  if (verifyChecksum_ && event_->version() >= 5) {
598  uint32_t crc = 0;
599  crc = crc32c(crc, (const unsigned char*)event_->payload(), event_->eventSize());
600  if (crc != event_->crc32c()) {
601  if (fms_)
603  throw cms::Exception("FedRawDataInputSource::getNextEvent")
604  << "Found a wrong crc32c checksum: expected 0x" << std::hex << event_->crc32c() << " but calculated 0x"
605  << crc;
606  }
607  } else if (verifyChecksum_ && event_->version() >= 3) {
608  uint32_t adler = adler32(0L, Z_NULL, 0);
609  adler = adler32(adler, (Bytef*)event_->payload(), event_->eventSize());
610 
611  if (adler != event_->adler32()) {
612  if (fms_)
614  throw cms::Exception("FedRawDataInputSource::getNextEvent")
615  << "Found a wrong Adler32 checksum: expected 0x" << std::hex << event_->adler32() << " but calculated 0x"
616  << adler;
617  }
618  }
620 
621  currentFile_->nProcessed_++;
622 
624 }
625 
628  std::unique_ptr<FEDRawDataCollection> rawData(new FEDRawDataCollection);
629  bool tcdsInRange;
630  edm::Timestamp tstamp = fillFEDRawDataCollection(*rawData, tcdsInRange);
631 
632  if (useL1EventID_) {
635  aux.setProcessHistoryID(processHistoryID_);
636  makeEvent(eventPrincipal, aux);
637  } else if (tcds_pointer_ == nullptr) {
638  if (!GTPEventID_) {
639  throw cms::Exception("FedRawDataInputSource::read")
640  << "No TCDS or GTP FED in event with FEDHeader EID -: " << L1EventID_;
641  }
644  aux.setProcessHistoryID(processHistoryID_);
645  makeEvent(eventPrincipal, aux);
646  } else {
647  const FEDHeader fedHeader(tcds_pointer_);
648  tcds::Raw_v1 const* tcds = reinterpret_cast<tcds::Raw_v1 const*>(tcds_pointer_ + FEDHeader::length);
653  event_->isRealData(),
654  static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.triggerType()),
655  processGUID(),
657  !tcdsInRange);
658  aux.setProcessHistoryID(processHistoryID_);
659  makeEvent(eventPrincipal, aux);
660  }
661 
662  std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<FEDRawDataCollection>(std::move(rawData)));
663 
665 
666  eventsThisLumi_++;
668 
669  //resize vector if needed
670  while (streamFileTracker_.size() <= eventPrincipal.streamID())
671  streamFileTracker_.push_back(-1);
672 
673  streamFileTracker_[eventPrincipal.streamID()] = currentFileIndex_;
674 
675  //this old file check runs no more often than every 10 events
676  if (!((currentFile_->nProcessed_ - 1) % (checkEvery_))) {
677  //delete files that are not in processing
678  std::unique_lock<std::mutex> lkw(fileDeleteLock_);
679  auto it = filesToDelete_.begin();
680  while (it != filesToDelete_.end()) {
681  bool fileIsBeingProcessed = false;
682  for (unsigned int i = 0; i < nStreams_; i++) {
683  if (it->first == streamFileTracker_.at(i)) {
684  fileIsBeingProcessed = true;
685  break;
686  }
687  }
688  if (!fileIsBeingProcessed && (!fms_ || !fms_->isExceptionOnData(it->second->lumi_))) {
689  std::string fileToDelete = it->second->fileName_;
690  it = filesToDelete_.erase(it);
691  } else
692  it++;
693  }
694  }
695  if (chunkIsFree_)
696  freeChunks_.push(currentFile_->chunks_[currentFile_->currentChunk_ - 1]);
697  chunkIsFree_ = false;
699  return;
700 }
701 
704  timeval stv;
705  gettimeofday(&stv, nullptr);
706  time = stv.tv_sec;
707  time = (time << 32) + stv.tv_usec;
708  edm::Timestamp tstamp(time);
709 
710  uint32_t eventSize = event_->eventSize();
711  unsigned char* event = (unsigned char*)event_->payload();
712  GTPEventID_ = 0;
713  tcds_pointer_ = nullptr;
714  tcdsInRange = false;
715  uint16_t selectedTCDSFed = 0;
716  while (eventSize > 0) {
717  assert(eventSize >= FEDTrailer::length);
718  eventSize -= FEDTrailer::length;
719  const FEDTrailer fedTrailer(event + eventSize);
720  const uint32_t fedSize = fedTrailer.fragmentLength() << 3; //trailer length counts in 8 bytes
721  assert(eventSize >= fedSize - FEDHeader::length);
722  eventSize -= (fedSize - FEDHeader::length);
723  const FEDHeader fedHeader(event + eventSize);
724  const uint16_t fedId = fedHeader.sourceID();
726  throw cms::Exception("FedRawDataInputSource::fillFEDRawDataCollection") << "Out of range FED ID : " << fedId;
727  } else if (fedId >= MINTCDSuTCAFEDID_ && fedId <= MAXTCDSuTCAFEDID_) {
728  if (!selectedTCDSFed) {
729  selectedTCDSFed = fedId;
730  tcds_pointer_ = event + eventSize;
732  tcdsInRange = true;
733  }
734  } else
735  throw cms::Exception("FedRawDataInputSource::fillFEDRawDataCollection")
736  << "Second TCDS FED ID " << fedId << " found. First ID: " << selectedTCDSFed;
737  }
739  if (evf::evtn::evm_board_sense(event + eventSize, fedSize))
740  GTPEventID_ = evf::evtn::get(event + eventSize, true);
741  else
742  GTPEventID_ = evf::evtn::get(event + eventSize, false);
743  //evf::evtn::evm_board_setformat(fedSize);
744  const uint64_t gpsl = evf::evtn::getgpslow(event + eventSize);
745  const uint64_t gpsh = evf::evtn::getgpshigh(event + eventSize);
746  tstamp = edm::Timestamp(static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl));
747  }
748  //take event ID from GTPE FED
750  if (evf::evtn::gtpe_board_sense(event + eventSize)) {
751  GTPEventID_ = evf::evtn::gtpe_get(event + eventSize);
752  }
753  }
754  FEDRawData& fedData = rawData.FEDData(fedId);
755  fedData.resize(fedSize);
756  memcpy(fedData.data(), event + eventSize, fedSize);
757  }
758  assert(eventSize == 0);
759 
760  return tstamp;
761 }
762 
764 
766  bool stop = false;
767  unsigned int currentLumiSection = 0;
768  //threadInit_.exchange(true,std::memory_order_acquire);
769 
770  {
771  std::unique_lock<std::mutex> lk(startupLock_);
772  startupCv_.notify_one();
773  }
774 
775  uint32_t ls = 0;
776  uint32_t monLS = 1;
777  uint32_t lockCount = 0;
778  uint64_t sumLockWaitTimeUs = 0.;
779 
780  while (!stop) {
781  //wait for at least one free thread and chunk
782  int counter = 0;
783 
784  while ((workerPool_.empty() && !singleBufferMode_) || freeChunks_.empty() ||
786  //report state to monitoring
787  if (fms_) {
788  bool copy_active = false;
789  for (auto j : tid_active_)
790  if (j)
791  copy_active = true;
794  else if (freeChunks_.empty()) {
795  if (copy_active)
797  else
799  } else {
800  if (copy_active)
802  else
804  }
805  }
806  std::unique_lock<std::mutex> lkw(mWakeup_);
807  //sleep until woken up by condition or a timeout
808  if (cvWakeup_.wait_for(lkw, std::chrono::milliseconds(100)) == std::cv_status::timeout) {
809  counter++;
810  //if (!(counter%50)) edm::LogInfo("FedRawDataInputSource") << "No free chunks or threads...";
811  LogDebug("FedRawDataInputSource") << "No free chunks or threads...";
812  } else {
813  assert(!(workerPool_.empty() && !singleBufferMode_) || freeChunks_.empty());
814  }
815  if (quit_threads_.load(std::memory_order_relaxed) || edm::shutdown_flag.load(std::memory_order_relaxed)) {
816  stop = true;
817  break;
818  }
819  }
820  //if this is reached, there are enough buffers and threads to proceed or processing is instructed to stop
821 
822  if (stop)
823  break;
824 
825  //look for a new file
826  std::string nextFile;
827  uint32_t fileSizeIndex;
828  int64_t fileSizeFromMetadata;
829 
830  if (fms_) {
833  }
834 
836  uint16_t rawHeaderSize = 0;
837  uint32_t lsFromRaw = 0;
838  int32_t serverEventsInNewFile = -1;
839  int rawFd = -1;
840 
841  int backoff_exp = 0;
842 
843  //entering loop which tries to grab new file from ramdisk
845  //check if hltd has signalled to throttle input
846  counter = 0;
847  while (daqDirector_->inputThrottled()) {
848  if (quit_threads_.load(std::memory_order_relaxed) || edm::shutdown_flag.load(std::memory_order_relaxed))
849  break;
850 
851  unsigned int nConcurrentLumis = daqDirector_->numConcurrentLumis();
852  unsigned int nOtherLumis = nConcurrentLumis > 0 ? nConcurrentLumis - 1 : 0;
853  unsigned int checkLumiStart = currentLumiSection > nOtherLumis ? currentLumiSection - nOtherLumis : 1;
854  bool hasDiscardedLumi = false;
855  for (unsigned int i = checkLumiStart; i <= currentLumiSection; i++) {
857  edm::LogWarning("FedRawDataInputSource") << "Source detected that the lumisection is discarded -: " << i;
858  hasDiscardedLumi = true;
859  break;
860  }
861  }
862  if (hasDiscardedLumi)
863  break;
864 
866 
867  if (!(counter % 50))
868  edm::LogWarning("FedRawDataInputSource") << "Input throttled detected, reading files is paused...";
869  usleep(100000);
870  counter++;
871  }
872 
873  if (quit_threads_.load(std::memory_order_relaxed) || edm::shutdown_flag.load(std::memory_order_relaxed)) {
874  stop = true;
875  break;
876  }
877 
878  assert(rawFd == -1);
879  uint64_t thisLockWaitTimeUs = 0.;
881  if (fileListMode_) {
882  //return LS if LS not set, otherwise return file
883  status = getFile(ls, nextFile, fileSizeIndex, thisLockWaitTimeUs);
886  rawFd,
887  rawHeaderSize,
888  lsFromRaw,
889  serverEventsInNewFile,
890  fileSizeFromMetadata,
891  false,
892  false,
893  false) != 0) {
894  //error
895  setExceptionState_ = true;
896  stop = true;
897  break;
898  }
899  if (!getLSFromFilename_)
900  ls = lsFromRaw;
901  }
902  } else if (!useFileBroker_)
904  ls, nextFile, fileSizeIndex, rawHeaderSize, thisLockWaitTimeUs, setExceptionState_);
905  else {
906  status = daqDirector_->getNextFromFileBroker(currentLumiSection,
907  ls,
908  nextFile,
909  rawFd,
910  rawHeaderSize,
911  serverEventsInNewFile,
912  fileSizeFromMetadata,
913  thisLockWaitTimeUs);
914  }
915 
917 
918  //cycle through all remaining LS even if no files get assigned
919  if (currentLumiSection != ls && status == evf::EvFDaqDirector::runEnded)
921 
922  //monitoring of lock wait time
923  if (thisLockWaitTimeUs > 0.)
924  sumLockWaitTimeUs += thisLockWaitTimeUs;
925  lockCount++;
926  if (ls > monLS) {
927  monLS = ls;
928  if (lockCount)
929  if (fms_)
930  fms_->reportLockWait(monLS, sumLockWaitTimeUs, lockCount);
931  lockCount = 0;
932  sumLockWaitTimeUs = 0;
933  }
934 
935  //check again for any remaining index/EoLS files after EoR file is seen
938  usleep(100000);
939  //now all files should have appeared in ramdisk, check again if any raw files were left behind
941  ls, nextFile, fileSizeIndex, rawHeaderSize, thisLockWaitTimeUs, setExceptionState_);
942  if (currentLumiSection != ls && status == evf::EvFDaqDirector::runEnded)
944  }
945 
947  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::runEnded));
948  fileQueue_.push(std::move(inf));
949  stop = true;
950  break;
951  }
952 
953  //error from filelocking function
955  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::runAbort, 0));
956  fileQueue_.push(std::move(inf));
957  stop = true;
958  break;
959  }
960  //queue new lumisection
961  if (getLSFromFilename_) {
962  if (ls > currentLumiSection) {
963  if (!useFileBroker_) {
964  //file locking
965  //setMonStateSup(inSupNewLumi);
966  currentLumiSection = ls;
967  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, currentLumiSection));
968  fileQueue_.push(std::move(inf));
969  } else {
970  //new file service
971  if (currentLumiSection == 0 && !alwaysStartFromFirstLS_) {
973  //start transitions from LS specified by env, continue if not reached
974  if (ls < daqDirector_->getStartLumisectionFromEnv()) {
975  //skip file if from earlier LS than specified by env
976  if (rawFd != -1) {
977  close(rawFd);
978  rawFd = -1;
979  }
981  continue;
982  } else {
983  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, ls));
984  fileQueue_.push(std::move(inf));
985  }
986  } else if (ls < 100) {
987  //look at last LS file on disk to start from that lumisection (only within first 100 LS)
988  unsigned int lsToStart = daqDirector_->getLumisectionToStart();
989 
990  for (unsigned int nextLS = std::min(lsToStart, ls); nextLS <= ls; nextLS++) {
991  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, nextLS));
992  fileQueue_.push(std::move(inf));
993  }
994  } else {
995  //start from current LS
996  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, ls));
997  fileQueue_.push(std::move(inf));
998  }
999  } else {
1000  //queue all lumisections after last one seen to avoid gaps
1001  for (unsigned int nextLS = currentLumiSection + 1; nextLS <= ls; nextLS++) {
1002  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, nextLS));
1003  fileQueue_.push(std::move(inf));
1004  }
1005  }
1006  currentLumiSection = ls;
1007  }
1008  }
1009  //else
1010  if (currentLumiSection > 0 && ls < currentLumiSection) {
1011  edm::LogError("FedRawDataInputSource")
1012  << "Got old LS (" << ls << ") file from EvFDAQDirector! Expected LS:" << currentLumiSection
1013  << ". Aborting execution." << std::endl;
1014  if (rawFd != -1)
1015  close(rawFd);
1016  rawFd = -1;
1017  std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::runAbort, 0));
1018  fileQueue_.push(std::move(inf));
1019  stop = true;
1020  break;
1021  }
1022  }
1023 
1024  int dbgcount = 0;
1027  dbgcount++;
1028  if (!(dbgcount % 20))
1029  LogDebug("FedRawDataInputSource") << "No file for me... sleep and try again...";
1030  if (!useFileBroker_)
1031  usleep(100000);
1032  else {
1033  backoff_exp = std::min(4, backoff_exp); // max 1.6 seconds
1034  //backoff_exp=0; // disabled!
1035  int sleeptime = (int)(100000. * pow(2, backoff_exp));
1036  usleep(sleeptime);
1037  backoff_exp++;
1038  }
1039  } else
1040  backoff_exp = 0;
1041  }
1042  //end of file grab loop, parse result
1045  LogDebug("FedRawDataInputSource") << "The director says to grab -: " << nextFile;
1046 
1047  std::string rawFile;
1048  //file service will report raw extension
1049  if (useFileBroker_ || rawHeaderSize)
1050  rawFile = nextFile;
1051  else {
1052  std::filesystem::path rawFilePath(nextFile);
1053  rawFile = rawFilePath.replace_extension(".raw").string();
1054  }
1055 
1056  struct stat st;
1057  int stat_res = stat(rawFile.c_str(), &st);
1058  if (stat_res == -1) {
1059  edm::LogError("FedRawDataInputSource") << "Can not stat file (" << errno << "):-" << rawFile << std::endl;
1060  setExceptionState_ = true;
1061  break;
1062  }
1063  uint64_t fileSize = st.st_size;
1064 
1065  if (fms_) {
1069  }
1070  int eventsInNewFile;
1071  if (fileListMode_) {
1072  if (fileSize == 0)
1073  eventsInNewFile = 0;
1074  else
1075  eventsInNewFile = -1;
1076  } else {
1078  if (!useFileBroker_) {
1079  if (rawHeaderSize) {
1080  int rawFdEmpty = -1;
1081  uint16_t rawHeaderCheck;
1082  bool fileFound;
1083  eventsInNewFile = daqDirector_->grabNextJsonFromRaw(
1084  nextFile, rawFdEmpty, rawHeaderCheck, fileSizeFromMetadata, fileFound, 0, true);
1085  assert(fileFound && rawHeaderCheck == rawHeaderSize);
1087  } else
1088  eventsInNewFile = daqDirector_->grabNextJsonFileAndUnlock(nextFile);
1089  } else
1090  eventsInNewFile = serverEventsInNewFile;
1091  assert(eventsInNewFile >= 0);
1092  assert((eventsInNewFile > 0) ==
1093  (fileSize > rawHeaderSize)); //file without events must be empty or contain only header
1094  }
1095 
1096  if (!singleBufferMode_) {
1097  //calculate number of needed chunks
1098  unsigned int neededChunks = fileSize / eventChunkSize_;
1099  if (fileSize % eventChunkSize_)
1100  neededChunks++;
1101 
1102  std::unique_ptr<InputFile> newInputFile(new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1103  ls,
1104  rawFile,
1105  !fileListMode_,
1106  rawFd,
1107  fileSize,
1108  rawHeaderSize,
1109  neededChunks,
1110  eventsInNewFile,
1111  this));
1113  auto newInputFilePtr = newInputFile.get();
1114  fileQueue_.push(std::move(newInputFile));
1115 
1116  for (unsigned int i = 0; i < neededChunks; i++) {
1117  if (fms_) {
1118  bool copy_active = false;
1119  for (auto j : tid_active_)
1120  if (j)
1121  copy_active = true;
1122  if (copy_active)
1124  else
1126  }
1127  //get thread
1128  unsigned int newTid = 0xffffffff;
1129  while (!workerPool_.try_pop(newTid)) {
1130  usleep(100000);
1131  if (quit_threads_.load(std::memory_order_relaxed)) {
1132  stop = true;
1133  break;
1134  }
1135  }
1136 
1137  if (fms_) {
1138  bool copy_active = false;
1139  for (auto j : tid_active_)
1140  if (j)
1141  copy_active = true;
1142  if (copy_active)
1144  else
1146  }
1147  InputChunk* newChunk = nullptr;
1148  while (!freeChunks_.try_pop(newChunk)) {
1149  usleep(100000);
1150  if (quit_threads_.load(std::memory_order_relaxed)) {
1151  stop = true;
1152  break;
1153  }
1154  }
1155 
1156  if (newChunk == nullptr) {
1157  //return unused tid if we received shutdown (nullptr chunk)
1158  if (newTid != 0xffffffff)
1159  workerPool_.push(newTid);
1160  stop = true;
1161  break;
1162  }
1163  if (stop)
1164  break;
1166 
1167  std::unique_lock<std::mutex> lk(mReader_);
1168 
1169  unsigned int toRead = eventChunkSize_;
1170  if (i == neededChunks - 1 && fileSize % eventChunkSize_)
1171  toRead = fileSize % eventChunkSize_;
1172  newChunk->reset(i * eventChunkSize_, toRead, i);
1173 
1174  workerJob_[newTid].first = newInputFilePtr;
1175  workerJob_[newTid].second = newChunk;
1176 
1177  //wake up the worker thread
1178  cvReader_[newTid]->notify_one();
1179  }
1180  } else {
1181  if (!eventsInNewFile) {
1182  if (rawFd) {
1183  close(rawFd);
1184  rawFd = -1;
1185  }
1186  //still queue file for lumi update
1187  std::unique_lock<std::mutex> lkw(mWakeup_);
1188  //TODO: also file with only file header fits in this edge case. Check if read correctly in single buffer mode
1189  std::unique_ptr<InputFile> newInputFile(new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1190  ls,
1191  rawFile,
1192  !fileListMode_,
1193  rawFd,
1194  fileSize,
1195  rawHeaderSize,
1196  (rawHeaderSize > 0),
1197  0,
1198  this));
1200  fileQueue_.push(std::move(newInputFile));
1201  cvWakeup_.notify_one();
1202  break;
1203  }
1204  //in single-buffer mode put single chunk in the file and let the main thread read the file
1205  InputChunk* newChunk;
1206  //should be available immediately
1207  while (!freeChunks_.try_pop(newChunk)) {
1208  usleep(100000);
1209  if (quit_threads_.load(std::memory_order_relaxed))
1210  break;
1211  }
1212 
1213  std::unique_lock<std::mutex> lkw(mWakeup_);
1214 
1215  unsigned int toRead = eventChunkSize_;
1216  if (fileSize % eventChunkSize_)
1217  toRead = fileSize % eventChunkSize_;
1218  newChunk->reset(0, toRead, 0);
1219  newChunk->readComplete_ = true;
1220 
1221  //push file and wakeup main thread
1222  std::unique_ptr<InputFile> newInputFile(new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1223  ls,
1224  rawFile,
1225  !fileListMode_,
1226  rawFd,
1227  fileSize,
1228  rawHeaderSize,
1229  1,
1230  eventsInNewFile,
1231  this));
1232  newInputFile->chunks_[0] = newChunk;
1234  fileQueue_.push(std::move(newInputFile));
1235  cvWakeup_.notify_one();
1236  }
1237  }
1238  }
1240  //make sure threads finish reading
1241  unsigned numFinishedThreads = 0;
1242  while (numFinishedThreads < workerThreads_.size()) {
1243  unsigned tid = 0;
1244  while (!workerPool_.try_pop(tid)) {
1245  usleep(10000);
1246  }
1247  std::unique_lock<std::mutex> lk(mReader_);
1248  thread_quit_signal[tid] = true;
1249  cvReader_[tid]->notify_one();
1250  numFinishedThreads++;
1251  }
1252  for (unsigned int i = 0; i < workerThreads_.size(); i++) {
1253  workerThreads_[i]->join();
1254  delete workerThreads_[i];
1255  }
1256 }
1257 
1258 void FedRawDataInputSource::readWorker(unsigned int tid) {
1259  bool init = true;
1260  threadInit_.exchange(true, std::memory_order_acquire);
1261 
1262  while (true) {
1263  tid_active_[tid] = false;
1264  std::unique_lock<std::mutex> lk(mReader_);
1265  workerJob_[tid].first = nullptr;
1266  workerJob_[tid].first = nullptr;
1267 
1268  assert(!thread_quit_signal[tid]); //should never get it here
1269  workerPool_.push(tid);
1270 
1271  if (init) {
1272  std::unique_lock<std::mutex> lk(startupLock_);
1273  init = false;
1274  startupCv_.notify_one();
1275  }
1276  cvReader_[tid]->wait(lk);
1277 
1278  if (thread_quit_signal[tid])
1279  return;
1280  tid_active_[tid] = true;
1281 
1282  InputFile* file;
1283  InputChunk* chunk;
1284 
1285  assert(workerJob_[tid].first != nullptr && workerJob_[tid].second != nullptr);
1286 
1287  file = workerJob_[tid].first;
1288  chunk = workerJob_[tid].second;
1289 
1290  //skip reading initial header size in first chunk if inheriting file descriptor (already set at appropriate position)
1291  unsigned int bufferLeft = (chunk->offset_ == 0 && file->rawFd_ != -1) ? file->rawHeaderSize_ : 0;
1292 
1293  //if only one worker thread exists, use single fd for all operations
1294  //if more worker threads exist, use rawFd_ for only the first read operation and then close file
1295  int fileDescriptor;
1296  bool fileOpenedHere = false;
1297 
1298  if (numConcurrentReads_ == 1) {
1299  fileDescriptor = file->rawFd_;
1300  if (fileDescriptor == -1) {
1301  fileDescriptor = open(file->fileName_.c_str(), O_RDONLY);
1302  fileOpenedHere = true;
1303  file->rawFd_ = fileDescriptor;
1304  }
1305  } else {
1306  if (chunk->offset_ == 0) {
1307  fileDescriptor = file->rawFd_;
1308  file->rawFd_ = -1;
1309  if (fileDescriptor == -1) {
1310  fileDescriptor = open(file->fileName_.c_str(), O_RDONLY);
1311  fileOpenedHere = true;
1312  }
1313  } else {
1314  fileDescriptor = open(file->fileName_.c_str(), O_RDONLY);
1315  fileOpenedHere = true;
1316  }
1317  }
1318 
1319  if (fileDescriptor < 0) {
1320  edm::LogError("FedRawDataInputSource") << "readWorker failed to open file -: " << file->fileName_
1321  << " fd:" << fileDescriptor << " error: " << strerror(errno);
1322  setExceptionState_ = true;
1323  continue;
1324  }
1325  if (fileOpenedHere) { //fast forward to this chunk position
1326  off_t pos = 0;
1327  pos = lseek(fileDescriptor, chunk->offset_, SEEK_SET);
1328  if (pos == -1) {
1329  edm::LogError("FedRawDataInputSource")
1330  << "readWorker failed to seek file -: " << file->fileName_ << " fd:" << fileDescriptor << " to offset "
1331  << chunk->offset_ << " error: " << strerror(errno);
1332  setExceptionState_ = true;
1333  continue;
1334  }
1335  }
1336 
1337  LogDebug("FedRawDataInputSource") << "Reader thread opened file -: TID: " << tid << " file: " << file->fileName_
1338  << " at offset " << lseek(fileDescriptor, 0, SEEK_CUR);
1339 
1340  unsigned int skipped = bufferLeft;
1342  for (unsigned int i = 0; i < readBlocks_; i++) {
1343  ssize_t last;
1344 
1345  //protect against reading into next block
1346  last = ::read(
1347  fileDescriptor, (void*)(chunk->buf_ + bufferLeft), std::min(chunk->usedSize_ - bufferLeft, eventChunkBlock_));
1348 
1349  if (last < 0) {
1350  edm::LogError("FedRawDataInputSource") << "readWorker failed to read file -: " << file->fileName_
1351  << " fd:" << fileDescriptor << " error: " << strerror(errno);
1352  setExceptionState_ = true;
1353  break;
1354  }
1355  if (last > 0)
1356  bufferLeft += last;
1357  if (last < eventChunkBlock_) { //last read
1358  //check if this is last block, then total read size must match file size
1359  if (!(chunk->usedSize_ - skipped == i * eventChunkBlock_ + last)) {
1360  edm::LogError("FedRawDataInputSource")
1361  << "readWorker failed to read file -: " << file->fileName_ << " fd:" << fileDescriptor << " last:" << last
1362  << " expectedChunkSize:" << chunk->usedSize_
1363  << " readChunkSize:" << (skipped + i * eventChunkBlock_ + last) << " skipped:" << skipped
1364  << " block:" << (i + 1) << "/" << readBlocks_ << " error: " << strerror(errno);
1365  setExceptionState_ = true;
1366  }
1367  break;
1368  }
1369  }
1370  if (setExceptionState_)
1371  continue;
1372 
1374  auto diff = end - start;
1375  std::chrono::milliseconds msec = std::chrono::duration_cast<std::chrono::milliseconds>(diff);
1376  LogDebug("FedRawDataInputSource") << " finished reading block -: " << (bufferLeft >> 20) << " MB"
1377  << " in " << msec.count() << " ms (" << (bufferLeft >> 20) / double(msec.count())
1378  << " GB/s)";
1379 
1380  if (chunk->offset_ + bufferLeft == file->fileSize_) { //file reading finished using same fd
1381  close(fileDescriptor);
1382  fileDescriptor = -1;
1383  if (numConcurrentReads_ == 1)
1384  file->rawFd_ = -1;
1385  }
1386  if (numConcurrentReads_ > 1 && fileDescriptor != -1)
1387  close(fileDescriptor);
1388 
1389  //detect FRD event version. Skip file Header if it exists
1390  if (detectedFRDversion_ == 0 && chunk->offset_ == 0) {
1391  detectedFRDversion_ = *((uint16_t*)(chunk->buf_ + file->rawHeaderSize_));
1392  }
1394  chunk->readComplete_ =
1395  true; //this is atomic to secure the sequential buffer fill before becoming available for processing)
1396  file->chunks_[chunk->fileIndex_] = chunk; //put the completed chunk in the file chunk vector at predetermined index
1397  }
1398 }
1399 
1401  quit_threads_ = true;
1402  throw cms::Exception("FedRawDataInputSource:threadError") << " file reader thread error ";
1403 }
1404 
1406  if (fms_)
1407  fms_->setInState(state);
1408 }
1409 
1411  if (fms_)
1413 }
1414 
1415 inline bool InputFile::advance(unsigned char*& dataPosition, const size_t size) {
1416  //wait for chunk
1417 
1418  while (!waitForChunk(currentChunk_)) {
1420  usleep(100000);
1422  if (parent_->exceptionState())
1423  parent_->threadError();
1424  }
1425 
1426  dataPosition = chunks_[currentChunk_]->buf_ + chunkPosition_;
1427  size_t currentLeft = chunks_[currentChunk_]->size_ - chunkPosition_;
1428 
1429  if (currentLeft < size) {
1430  //we need next chunk
1431  while (!waitForChunk(currentChunk_ + 1)) {
1433  usleep(100000);
1435  if (parent_->exceptionState())
1436  parent_->threadError();
1437  }
1438  //copy everything to beginning of the first chunk
1439  dataPosition -= chunkPosition_;
1440  assert(dataPosition == chunks_[currentChunk_]->buf_);
1441  memmove(chunks_[currentChunk_]->buf_, chunks_[currentChunk_]->buf_ + chunkPosition_, currentLeft);
1442  memcpy(chunks_[currentChunk_]->buf_ + currentLeft, chunks_[currentChunk_ + 1]->buf_, size - currentLeft);
1443  //set pointers at the end of the old data position
1444  bufferPosition_ += size;
1445  chunkPosition_ = size - currentLeft;
1446  currentChunk_++;
1447  return true;
1448  } else {
1449  chunkPosition_ += size;
1450  bufferPosition_ += size;
1451  return false;
1452  }
1453 }
1454 
1455 inline void InputFile::moveToPreviousChunk(const size_t size, const size_t offset) {
1456  //this will fail in case of events that are too large
1458  assert(size - offset < chunks_[currentChunk_]->size_);
1459  memcpy(chunks_[currentChunk_ - 1]->buf_ + offset, chunks_[currentChunk_]->buf_ + chunkPosition_, size);
1460  chunkPosition_ += size;
1461  bufferPosition_ += size;
1462 }
1463 
1464 inline void InputFile::rewindChunk(const size_t size) {
1465  chunkPosition_ -= size;
1466  bufferPosition_ -= size;
1467 }
1468 
1470  if (rawFd_ != -1)
1471  close(rawFd_);
1472 
1473  if (deleteFile_ && !fileName_.empty()) {
1475  try {
1476  //sometimes this fails but file gets deleted
1477  LogDebug("FedRawDataInputSource:InputFile") << "Deleting input file -:" << fileName_;
1479  return;
1480  } catch (const std::filesystem::filesystem_error& ex) {
1481  edm::LogError("FedRawDataInputSource:InputFile")
1482  << " - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() << ". Trying again.";
1483  } catch (std::exception& ex) {
1484  edm::LogError("FedRawDataInputSource:InputFile")
1485  << " - deleteFile std::exception CAUGHT -: " << ex.what() << ". Trying again.";
1486  }
1488  }
1489 }
1490 
1491 //single-buffer mode file reading
1493  uint32_t existingSize = 0;
1494 
1495  if (fileDescriptor_ < 0) {
1496  bufferInputRead_ = 0;
1497  if (file->rawFd_ == -1) {
1498  fileDescriptor_ = open(file->fileName_.c_str(), O_RDONLY);
1499  if (file->rawHeaderSize_)
1500  lseek(fileDescriptor_, file->rawHeaderSize_, SEEK_SET);
1501  } else
1502  fileDescriptor_ = file->rawFd_;
1503 
1504  //skip header size in destination buffer (chunk position was already adjusted)
1505  bufferInputRead_ += file->rawHeaderSize_;
1506  existingSize += file->rawHeaderSize_;
1507 
1508  if (fileDescriptor_ >= 0)
1509  LogDebug("FedRawDataInputSource") << "opened file -: " << std::endl << file->fileName_;
1510  else {
1511  throw cms::Exception("FedRawDataInputSource:readNextChunkIntoBuffer")
1512  << "failed to open file " << std::endl
1513  << file->fileName_ << " fd:" << fileDescriptor_;
1514  }
1515  //fill chunk (skipping file header if present)
1516  for (unsigned int i = 0; i < readBlocks_; i++) {
1517  const ssize_t last = ::read(fileDescriptor_,
1518  (void*)(file->chunks_[0]->buf_ + existingSize),
1519  eventChunkBlock_ - (i == readBlocks_ - 1 ? existingSize : 0));
1521  existingSize += last;
1522  }
1523 
1524  } else {
1525  //continue reading
1526  if (file->chunkPosition_ == 0) { //in the rare case the last byte barely fit
1527  for (unsigned int i = 0; i < readBlocks_; i++) {
1528  const ssize_t last = ::read(fileDescriptor_, (void*)(file->chunks_[0]->buf_ + existingSize), eventChunkBlock_);
1530  existingSize += last;
1531  }
1532  } else {
1533  //event didn't fit in last chunk, so leftover must be moved to the beginning and completed
1534  uint32_t existingSizeLeft = eventChunkSize_ - file->chunkPosition_;
1535  memmove((void*)file->chunks_[0]->buf_, file->chunks_[0]->buf_ + file->chunkPosition_, existingSizeLeft);
1536 
1537  //calculate amount of data that can be added
1538  const uint32_t blockcount = file->chunkPosition_ / eventChunkBlock_;
1539  const uint32_t leftsize = file->chunkPosition_ % eventChunkBlock_;
1540 
1541  for (uint32_t i = 0; i < blockcount; i++) {
1542  const ssize_t last =
1543  ::read(fileDescriptor_, (void*)(file->chunks_[0]->buf_ + existingSizeLeft), eventChunkBlock_);
1545  existingSizeLeft += last;
1546  }
1547  if (leftsize) {
1548  const ssize_t last = ::read(fileDescriptor_, (void*)(file->chunks_[0]->buf_ + existingSizeLeft), leftsize);
1550  }
1551  file->chunkPosition_ = 0; //data was moved to beginning of the chunk
1552  }
1553  }
1554  if (bufferInputRead_ == file->fileSize_) { // no more data in this file
1555  if (fileDescriptor_ != -1) {
1556  LogDebug("FedRawDataInputSource") << "Closing input file -: " << std::endl << file->fileName_;
1557  close(fileDescriptor_);
1558  file->rawFd_ = fileDescriptor_ = -1;
1559  }
1560  }
1561 }
1562 
1564  std::lock_guard<std::mutex> lock(monlock_);
1565  auto itr = sourceEventsReport_.find(lumi);
1566  if (itr != sourceEventsReport_.end())
1567  itr->second += events;
1568  else
1570 }
1571 
1572 std::pair<bool, unsigned int> FedRawDataInputSource::getEventReport(unsigned int lumi, bool erase) {
1573  std::lock_guard<std::mutex> lock(monlock_);
1574  auto itr = sourceEventsReport_.find(lumi);
1575  if (itr != sourceEventsReport_.end()) {
1576  std::pair<bool, unsigned int> ret(true, itr->second);
1577  if (erase)
1578  sourceEventsReport_.erase(itr);
1579  return ret;
1580  } else
1581  return std::pair<bool, unsigned int>(false, 0);
1582 }
1583 
1585  std::sort(fileNames_.begin(), fileNames_.end(), [](std::string a, std::string b) {
1586  if (a.rfind('/') != std::string::npos)
1587  a = a.substr(a.rfind('/'));
1588  if (b.rfind('/') != std::string::npos)
1589  b = b.substr(b.rfind('/'));
1590  return b > a;
1591  });
1592 
1593  if (!fileNames_.empty()) {
1594  //get run number from first file in the vector
1596  std::string fileStem = fileName.stem().string();
1597  if (fileStem.find("file://") == 0)
1598  fileStem = fileStem.substr(7);
1599  else if (fileStem.find("file:") == 0)
1600  fileStem = fileStem.substr(5);
1601  auto end = fileStem.find('_');
1602 
1603  if (fileStem.find("run") == 0) {
1604  std::string runStr = fileStem.substr(3, end - 3);
1605  try {
1606  //get long to support test run numbers < 2^32
1607  long rval = std::stol(runStr);
1608  edm::LogInfo("FedRawDataInputSource") << "Autodetected run number in fileListMode -: " << rval;
1609  return rval;
1610  } catch (const std::exception&) {
1611  edm::LogWarning("FedRawDataInputSource")
1612  << "Unable to autodetect run number in fileListMode from file -: " << fileName;
1613  }
1614  }
1615  }
1616  return -1;
1617 }
1618 
1620  std::string& nextFile,
1621  uint32_t& fsize,
1622  uint64_t& lockWaitTime) {
1623  if (fileListIndex_ < fileNames_.size()) {
1624  nextFile = fileNames_[fileListIndex_];
1625  if (nextFile.find("file://") == 0)
1626  nextFile = nextFile.substr(7);
1627  else if (nextFile.find("file:") == 0)
1628  nextFile = nextFile.substr(5);
1629  std::filesystem::path fileName = nextFile;
1630  std::string fileStem = fileName.stem().string();
1631  if (fileStem.find("ls"))
1632  fileStem = fileStem.substr(fileStem.find("ls") + 2);
1633  if (fileStem.find('_'))
1634  fileStem = fileStem.substr(0, fileStem.find('_'));
1635 
1636  if (!fileListLoopMode_)
1637  ls = std::stoul(fileStem);
1638  else //always starting from LS 1 in loop mode
1639  ls = 1 + loopModeIterationInc_;
1640 
1641  //fsize = 0;
1642  //lockWaitTime = 0;
1643  fileListIndex_++;
1645  } else {
1646  if (!fileListLoopMode_)
1648  else {
1649  //loop through files until interrupted
1651  fileListIndex_ = 0;
1652  return getFile(ls, nextFile, fsize, lockWaitTime);
1653  }
1654  }
1655 }
size
Write out results.
static const char runNumber_[]
uint8_t triggerType() const
Event Trigger type identifier.
Definition: FEDHeader.cc:13
Definition: start.py:1
Definition: fillJson.h:27
std::vector< std::string > fileNames_
unsigned int getgpshigh(const unsigned char *)
uint32_t chunkPosition_
std::condition_variable cvWakeup_
void read(edm::EventPrincipal &eventPrincipal) override
std::shared_ptr< RunAuxiliary > runAuxiliary() const
Called by the framework to merge or insert run in principal cache.
Definition: InputSource.h:232
ProductRegistry & productRegistryUpdate()
Definition: InputSource.h:330
bool gtpe_board_sense(const unsigned char *p)
constexpr size_t FRDHeaderMaxVersion
void setExceptionDetected(unsigned int ls)
static Timestamp invalidTimestamp()
Definition: Timestamp.h:75
bool crc32c_hw_test()
Definition: crc32c.cc:354
unsigned int offset_
std::pair< bool, unsigned int > getEventReport(unsigned int lumi, bool erase)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static const uint32_t length
Definition: FEDTrailer.h:57
tbb::concurrent_queue< unsigned int > workerPool_
unsigned int get(const unsigned char *, bool)
bool useFileBroker() const
void maybeOpenNewLumiSection(const uint32_t lumiSection)
static const uint32_t length
Definition: FEDHeader.h:54
ret
prodAgent to be discontinued
uint16_t sourceID() const
Identifier of the FED.
Definition: FEDHeader.cc:19
evf::EvFDaqDirector::FileStatus getFile(unsigned int &ls, std::string &nextFile, uint32_t &fsize, uint64_t &lockWaitTime)
void rewindChunk(const size_t size)
volatile std::atomic< bool > shutdown_flag
RunNumber_t run() const
Accessor for current run number.
Definition: InputSource.cc:457
tbb::concurrent_queue< std::unique_ptr< InputFile > > fileQueue_
std::string getEoLSFilePathOnFU(const unsigned int ls) const
std::atomic< bool > quit_threads_
std::vector< ReaderInfo > workerJob_
bool lumisectionDiscarded(unsigned int ls)
FileStatus getNextFromFileBroker(const unsigned int currentLumiSection, unsigned int &ls, std::string &nextFile, int &rawFd, uint16_t &rawHeaderSize, int32_t &serverEventsInNewFile_, int64_t &fileSize, uint64_t &thisLockWaitTimeUs)
edm::Timestamp fillFEDRawDataCollection(FEDRawDataCollection &rawData, bool &tcdsInRange)
std::pair< InputFile *, InputChunk * > ReaderInfo
std::unique_ptr< std::thread > readSupervisorThread_
Log< level::Error, false > LogError
unsigned int numConcurrentLumis() const
StreamID streamID() const
int timeout
Definition: mps_check.py:53
assert(be >=bs)
bool isExceptionOnData(unsigned int ls)
std::vector< std::condition_variable * > cvReader_
ProcessHistoryID daqInit(ProductRegistry &productRegistry, ProcessHistoryRegistry &processHistoryRegistry) const
FedRawDataInputSource * parent_
std::string getEoRFilePathOnFU() const
edm::EventAuxiliary makeEventAuxiliary(const tcds::Raw_v1 *, unsigned int runNumber, unsigned int lumiSection, bool isRealData, const edm::EventAuxiliary::ExperimentType &, const std::string &processGUID, bool verifyLumiSection, bool suppressWarning)
uint32_t fragmentLength() const
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:13
FedRawDataInputSource(edm::ParameterSet const &, edm::InputSourceDescription const &)
U second(std::pair< T, U > const &p)
std::list< std::pair< int, std::unique_ptr< InputFile > > > filesToDelete_
void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex)
std::string getEoLSFilePathOnBU(const unsigned int ls) const
static Timestamp beginOfTime()
Definition: Timestamp.h:77
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
const std::vector< unsigned int > testTCDSFEDRange_
uint32_t bufferPosition_
Definition: TCDSRaw.h:16
std::string const & processGUID() const
Accessor for global process identifier.
Definition: InputSource.h:197
void resize(size_t newsize)
Definition: FEDRawData.cc:28
bool advance(unsigned char *&dataPosition, const size_t size)
void setLuminosityBlockAuxiliary(LuminosityBlockAuxiliary *lbp)
Definition: InputSource.h:337
std::vector< int > streamFileTracker_
void moveToPreviousChunk(const size_t size, const size_t offset)
unsigned char * buf_
ProductProvenance const & dummyProvenance() const
static int parseFRDFileHeader(std::string const &rawSourcePath, int &rawFd, uint16_t &rawHeaderSize, uint32_t &lsFromHeader, int32_t &eventsFromHeader, int64_t &fileSizeFromHeader, bool requireHeader, bool retry, bool closeFile)
void setMonStateSup(evf::FastMonState::InputState state)
int grabNextJsonFileAndUnlock(std::filesystem::path const &jsonSourcePath)
std::map< unsigned int, unsigned int > sourceEventsReport_
edm::ProcessHistoryID processHistoryID_
bool waitForChunk(unsigned int chunkid)
const edm::DaqProvenanceHelper daqProvenanceHelper_
unsigned int fileIndex_
unsigned long long TimeValue_t
Definition: Timestamp.h:21
std::vector< std::thread * > workerThreads_
bool evm_board_sense(const unsigned char *p, size_t size)
void setMonState(evf::FastMonState::InputState state)
void setEventCached()
Called by the framework to merge or ached() const {return eventCached_;}.
Definition: InputSource.h:362
void createProcessingNotificationMaybe() const
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
Definition: crc32c.cc:340
void setInputSource(FedRawDataInputSource *inputSource)
Log< level::Info, false > LogInfo
std::atomic< bool > readComplete_
Definition: init.py:1
FileStatus updateFuLock(unsigned int &ls, std::string &nextFile, uint32_t &fsize, uint16_t &rawHeaderSize, uint64_t &lockWaitTime, bool &setExceptionState)
def ls(path, rec=False)
Definition: eostools.py:349
BranchDescription const & branchDescription() const
unsigned int getStartLumisectionFromEnv() const
int grabNextJsonFromRaw(std::string const &rawSourcePath, int &rawFd, uint16_t &rawHeaderSize, int64_t &fileSizeFromHeader, bool &fileFound, uint32_t serverLS, bool closeFile)
def getRunNumber(filename)
void setInStateSup(FastMonState::InputState inputState)
unsigned long long uint64_t
Definition: Time.h:13
unsigned int currentChunk_
std::unique_ptr< FRDEventMsgView > event_
def load(fileName)
Definition: svgfig.py:547
std::string fileName_
double b
Definition: hdecay.h:118
void add(std::string const &label, ParameterSetDescription const &psetDescription)
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:223
std::condition_variable startupCv_
void resetLuminosityBlockAuxiliary(bool isNewLumi=true) const
Definition: InputSource.h:345
void stoppedLookingForFile(unsigned int lumi)
tbb::concurrent_vector< InputChunk * > chunks_
ProcessHistoryRegistry & processHistoryRegistryForUpdate()
Definition: InputSource.h:331
ItemType state() const
Definition: InputSource.h:332
LuminosityBlockNumber_t luminosityBlock() const
Accessor for current luminosity block number.
Definition: InputSource.cc:462
std::atomic< bool > threadInit_
HLT enums.
evf::EvFDaqDirector * daqDirector_
void setRunAuxiliary(RunAuxiliary *rp)
Definition: InputSource.h:333
void readWorker(unsigned int tid)
double a
Definition: hdecay.h:119
std::shared_ptr< LuminosityBlockAuxiliary > luminosityBlockAuxiliary() const
Called by the framework to merge or insert lumi in principal cache.
Definition: InputSource.h:235
void makeEvent(EventPrincipal &eventPrincipal, EventAuxiliary const &eventAuxiliary)
std::vector< unsigned int > thread_quit_signal
unsigned int getLumisectionToStart() const
std::atomic< unsigned int > readingFilesCount_
void reportEventsThisLumiInSource(unsigned int lumi, unsigned int events)
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
evf::FastMonitoringService * fms_
unsigned int gtpe_get(const unsigned char *)
std::vector< unsigned int > tid_active_
unsigned int RunNumber_t
void setDeleteTracking(std::mutex *fileDeleteLock, std::list< std::pair< int, std::unique_ptr< InputFile >>> *filesToDelete)
constexpr std::array< uint32, FRDHeaderMaxVersion+1 > FRDHeaderVersionSize
tbb::concurrent_queue< InputChunk * > freeChunks_
Log< level::Warning, false > LogWarning
evf::EvFDaqDirector::FileStatus getNextEvent()
evf::EvFDaqDirector::FileStatus nextEvent()
void readNextChunkIntoBuffer(InputFile *file)
void createBoLSFile(const uint32_t lumiSection, bool checkIfExists) const
void setFMS(evf::FastMonitoringService *fms)
std::unique_ptr< InputFile > currentFile_
unsigned int getgpslow(const unsigned char *)
int events
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
def move(src, dest)
Definition: eostools.py:511
void reportLockWait(unsigned int ls, double waitTime, unsigned int lockCount)
Definition: event.py:1
#define LogDebug(id)
void setInState(FastMonState::InputState inputState)