16 #include <boost/algorithm/string.hpp>
49 #include <boost/lexical_cast.hpp>
55 defPath_(
pset.getUntrackedParameter<
std::
string>(
"buDefPath",
"")),
56 eventChunkSize_(
pset.getUntrackedParameter<unsigned
int>(
"eventChunkSize", 32) * 1048576),
57 eventChunkBlock_(
pset.getUntrackedParameter<unsigned
int>(
"eventChunkBlock", 32) * 1048576),
58 numBuffers_(
pset.getUntrackedParameter<unsigned
int>(
"numBuffers", 2)),
59 maxBufferedFiles_(
pset.getUntrackedParameter<unsigned
int>(
"maxBufferedFiles", 2)),
60 getLSFromFilename_(
pset.getUntrackedParameter<
bool>(
"getLSFromFilename",
true)),
61 alwaysStartFromFirstLS_(
pset.getUntrackedParameter<
bool>(
"alwaysStartFromFirstLS",
false)),
62 verifyChecksum_(
pset.getUntrackedParameter<
bool>(
"verifyChecksum",
true)),
63 useL1EventID_(
pset.getUntrackedParameter<
bool>(
"useL1EventID",
false)),
67 fileListMode_(
pset.getUntrackedParameter<
bool>(
"fileListMode",
false)),
68 fileListLoopMode_(
pset.getUntrackedParameter<
bool>(
"fileListLoopMode",
false)),
73 currentLumiSection_(0),
74 tcds_pointer_(nullptr),
77 gethostname(thishost, 255);
78 edm::LogInfo(
"FedRawDataInputSource") <<
"Construction. read-ahead chunk size -: " << std::endl
83 throw cms::Exception(
"FedRawDataInputSource::fillFEDRawDataCollection")
84 <<
"Invalid TCDS Test FED range parameter";
90 long autoRunNumber = -1;
94 if (autoRunNumber < 0)
95 throw cms::Exception(
"FedRawDataInputSource::FedRawDataInputSource") <<
"Run number not found from filename";
114 throw cms::Exception(
"FedRawDataInputSource::FedRawDataInputSource")
115 <<
"no reading enabled with numBuffers parameter 0";
122 edm::LogError(
"FedRawDataInputSource::FedRawDataInputSource") <<
"Intel crc32c checksum computation unavailable";
129 edm::LogInfo(
"FedRawDataInputSource") <<
"No FastMonitoringService found in the configuration";
134 throw cms::Exception(
"FedRawDataInputSource") <<
"FastMonitoringService not found";
140 cms::Exception(
"FedRawDataInputSource") <<
"EvFDaqDirector not found";
144 edm::LogInfo(
"FedRawDataInputSource") <<
"EvFDaqDirector/Source configured to use file service";
165 cvReader_.push_back(
new std::condition_variable);
167 threadInit_.store(
false, std::memory_order_release);
187 std::unique_lock<std::mutex> lk(
mReader_);
208 desc.setComment(
"File-based Filter Farm input source for reading raw data from BU ramdisk");
209 desc.addUntracked<
unsigned int>(
"eventChunkSize", 32)->setComment(
"Input buffer (chunk) size");
210 desc.addUntracked<
unsigned int>(
"eventChunkBlock", 32)
211 ->setComment(
"Block size used in a single file read call (must be smaller or equal to buffer size)");
212 desc.addUntracked<
unsigned int>(
"numBuffers", 2)->setComment(
"Number of buffers used for reading input");
213 desc.addUntracked<
unsigned int>(
"maxBufferedFiles", 2)
214 ->setComment(
"Maximum number of simultaneously buffered raw files");
215 desc.addUntracked<
unsigned int>(
"alwaysStartFromfirstLS",
false)
216 ->setComment(
"Force source to start from LS 1 if server provides higher lumisection number");
217 desc.addUntracked<
bool>(
"verifyChecksum",
true)
218 ->setComment(
"Verify event CRC-32C checksum of FRDv5 and higher or Adler32 with v3 and v4");
219 desc.addUntracked<
bool>(
"useL1EventID",
false)
220 ->setComment(
"Use L1 event ID from FED header if true or from TCDS FED if false");
221 desc.addUntracked<std::vector<unsigned int>>(
"testTCDSFEDRange", std::vector<unsigned int>())
222 ->setComment(
"[min, max] range to search for TCDS FED ID in test setup");
223 desc.addUntracked<
bool>(
"fileListMode",
false)
224 ->setComment(
"Use fileNames parameter to directly specify raw files to open");
225 desc.addUntracked<std::vector<std::string>>(
"fileNames", std::vector<std::string>())
226 ->setComment(
"file list used when fileListMode is enabled");
227 desc.setAllowAnything();
228 descriptions.
add(
"source",
desc);
256 open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
267 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
273 edm::LogInfo(
"FedRawDataInputSource") <<
"----------------RUN ENDED----------------";
316 open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
330 gettimeofday(&tv,
nullptr);
331 const edm::Timestamp lsopentime((
unsigned long long)tv.tv_sec * 1000000 + (
unsigned long long)tv.tv_usec);
339 edm::LogInfo(
"FedRawDataInputSource") <<
"New lumi section was opened. LUMI -: " << lumiSection;
360 std::unique_lock<std::mutex> lkw(
mWakeup_);
371 <<
"Run has been aborted by the input source reader thread";
416 <<
" but according to BU JSON there should be " <<
currentFile_->nEvents_ <<
" events";
420 std::unique_lock<std::mutex> lkw(
mWakeup_);
440 <<
"Premature end of input file while reading file header";
443 <<
"File with only raw header and no events received in LS " <<
currentFile_->lumi_;
460 <<
"Premature end of input file while reading event header";
491 <<
"Premature end of input file while reading event header";
495 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
498 <<
" event id:" <<
event_->event() <<
" lumi:" <<
event_->lumi() <<
" run:" <<
event_->run()
507 <<
"Premature end of input file while reading event data";
513 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
533 unsigned char* dataPosition;
538 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
541 <<
" event id:" <<
event_->event() <<
" lumi:" <<
event_->lumi() <<
" run:" <<
event_->run()
550 <<
"Premature end of input file while reading event data";
573 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
576 chunkEnd =
currentFile_->advance(dataPosition, msgSize);
587 if (crc !=
event_->crc32c()) {
591 <<
"Found a wrong crc32c checksum: expected 0x" << std::hex <<
event_->crc32c() <<
" but calculated 0x"
595 uint32_t adler = adler32(0
L, Z_NULL, 0);
596 adler = adler32(adler, (Bytef*)
event_->payload(),
event_->eventSize());
598 if (adler !=
event_->adler32()) {
602 <<
"Found a wrong Adler32 checksum: expected 0x" << std::hex <<
event_->adler32() <<
" but calculated 0x"
626 <<
"No TCDS or GTP FED in event with FEDHeader EID -: " <<
L1EventID_;
640 static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.
triggerType()),
666 bool fileIsBeingProcessed =
false;
669 fileIsBeingProcessed =
true;
673 if (!fileIsBeingProcessed) {
690 gettimeofday(&stv,
nullptr);
695 uint32_t eventSize =
event_->eventSize();
696 unsigned char*
event = (
unsigned char*)
event_->payload();
699 uint16_t selectedTCDSFed = 0;
700 while (eventSize > 0) {
710 throw cms::Exception(
"FedRawDataInputSource::fillFEDRawDataCollection") <<
"Out of range FED ID : " <<
fedId;
712 if (!selectedTCDSFed) {
713 selectedTCDSFed =
fedId;
716 throw cms::Exception(
"FedRawDataInputSource::fillFEDRawDataCollection")
717 <<
"Second TCDS FED ID " <<
fedId <<
" found. First ID: " << selectedTCDSFed;
727 tstamp =
edm::Timestamp(static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl));
737 memcpy(fedData.
data(),
event + eventSize, fedSize);
748 unsigned int currentLumiSection = 0;
758 uint32_t lockCount = 0;
768 bool copy_active =
false;
786 std::unique_lock<std::mutex> lkw(
mWakeup_);
791 LogDebug(
"FedRawDataInputSource") <<
"No free chunks or threads...";
807 uint32_t fileSizeIndex;
808 int64_t fileSizeFromMetadata;
817 uint16_t rawHeaderSize = 0;
818 uint32_t lsFromRaw = 0;
819 int32_t serverEventsInNewFile = -1;
841 serverEventsInNewFile,
842 fileSizeFromMetadata,
863 serverEventsInNewFile,
864 fileSizeFromMetadata,
875 if (thisLockWaitTimeUs > 0.)
876 sumLockWaitTimeUs += thisLockWaitTimeUs;
884 sumLockWaitTimeUs = 0;
914 if (
ls > currentLumiSection) {
918 currentLumiSection =
ls;
928 for (
unsigned int nextLS = lsToStart; nextLS <=
ls; nextLS++) {
939 for (
unsigned int nextLS = currentLumiSection + 1; nextLS <=
ls; nextLS++) {
944 currentLumiSection =
ls;
948 if (currentLumiSection > 0 &&
ls < currentLumiSection) {
950 <<
"Got old LS (" <<
ls <<
") file from EvFDAQDirector! Expected LS:" << currentLumiSection
951 <<
". Aborting execution." << std::endl;
966 if (!(dbgcount % 20))
967 LogDebug(
"FedRawDataInputSource") <<
"No file for me... sleep and try again...";
971 backoff_exp =
std::min(4, backoff_exp);
973 int sleeptime = (
int)(100000. *
pow(2, backoff_exp));
983 LogDebug(
"FedRawDataInputSource") <<
"The director says to grab -: " << nextFile;
991 rawFile = rawFilePath.replace_extension(
".raw").string();
995 int stat_res =
stat(rawFile.c_str(), &st);
996 if (stat_res == -1) {
997 edm::LogError(
"FedRawDataInputSource") <<
"Can not stat file (" << errno <<
"):-" << rawFile << std::endl;
1008 int eventsInNewFile;
1011 eventsInNewFile = 0;
1013 eventsInNewFile = -1;
1017 if (rawHeaderSize) {
1018 int rawFdEmpty = -1;
1019 uint16_t rawHeaderCheck;
1022 nextFile, rawFdEmpty, rawHeaderCheck, fileSizeFromMetadata, fileFound, 0,
true);
1023 assert(fileFound && rawHeaderCheck == rawHeaderSize);
1028 eventsInNewFile = serverEventsInNewFile;
1029 assert(eventsInNewFile >= 0);
1030 assert((eventsInNewFile > 0) ==
1031 (fileSize > rawHeaderSize));
1040 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1051 auto newInputFilePtr = newInputFile.get();
1054 for (
unsigned int i = 0;
i < neededChunks;
i++) {
1056 bool copy_active =
false;
1066 unsigned int newTid = 0xffffffff;
1076 bool copy_active =
false;
1094 if (newChunk ==
nullptr) {
1096 if (newTid != 0xffffffff)
1105 std::unique_lock<std::mutex> lk(
mReader_);
1119 if (!eventsInNewFile) {
1125 std::unique_lock<std::mutex> lkw(
mWakeup_);
1127 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1134 (rawHeaderSize > 0),
1151 std::unique_lock<std::mutex> lkw(
mWakeup_);
1156 newChunk->
reset(0, toRead, 0);
1160 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1170 newInputFile->chunks_[0] = newChunk;
1179 unsigned numFinishedThreads = 0;
1185 std::unique_lock<std::mutex> lk(
mReader_);
1188 numFinishedThreads++;
1198 threadInit_.exchange(
true, std::memory_order_acquire);
1202 std::unique_lock<std::mutex> lk(
mReader_);
1229 unsigned int bufferLeft = (chunk->
offset_ == 0 &&
file->rawFd_ != -1) ?
file->rawHeaderSize_ : 0;
1234 bool fileOpenedHere =
false;
1237 fileDescriptor =
file->rawFd_;
1238 if (fileDescriptor == -1) {
1239 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1240 fileOpenedHere =
true;
1241 file->rawFd_ = fileDescriptor;
1245 fileDescriptor =
file->rawFd_;
1247 if (fileDescriptor == -1) {
1248 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1249 fileOpenedHere =
true;
1252 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1253 fileOpenedHere =
true;
1257 if (fileDescriptor < 0) {
1258 edm::LogError(
"FedRawDataInputSource") <<
"readWorker failed to open file -: " <<
file->fileName_
1259 <<
" fd:" << fileDescriptor <<
" error: " << strerror(errno);
1263 if (fileOpenedHere) {
1265 pos = lseek(fileDescriptor, chunk->
offset_, SEEK_SET);
1268 <<
"readWorker failed to seek file -: " <<
file->fileName_ <<
" fd:" << fileDescriptor <<
" to offset "
1269 << chunk->
offset_ <<
" error: " << strerror(errno);
1275 LogDebug(
"FedRawDataInputSource") <<
"Reader thread opened file -: TID: " << tid <<
" file: " <<
file->fileName_
1276 <<
" at offset " << lseek(fileDescriptor, 0, SEEK_CUR);
1278 unsigned int skipped = bufferLeft;
1288 edm::LogError(
"FedRawDataInputSource") <<
"readWorker failed to read file -: " <<
file->fileName_
1289 <<
" fd:" << fileDescriptor <<
" error: " << strerror(errno);
1299 <<
"readWorker failed to read file -: " <<
file->fileName_ <<
" fd:" << fileDescriptor <<
" last:" <<
last
1300 <<
" expectedChunkSize:" << chunk->
usedSize_
1302 <<
" block:" << (
i + 1) <<
"/" <<
readBlocks_ <<
" error: " << strerror(errno);
1313 std::chrono::milliseconds msec = std::chrono::duration_cast<std::chrono::milliseconds>(
diff);
1314 LogDebug(
"FedRawDataInputSource") <<
" finished reading block -: " << (bufferLeft >> 20) <<
" MB"
1315 <<
" in " << msec.count() <<
" ms (" << (bufferLeft >> 20) /
double(msec.count())
1318 if (chunk->
offset_ + bufferLeft ==
file->fileSize_) {
1319 close(fileDescriptor);
1320 fileDescriptor = -1;
1325 close(fileDescriptor);
1340 throw cms::Exception(
"FedRawDataInputSource:threadError") <<
" file reader thread error ";
1367 if (currentLeft <
size) {
1415 LogDebug(
"FedRawDataInputSource:InputFile") <<
"Deleting input file -:" <<
fileName_;
1418 }
catch (
const std::filesystem::filesystem_error& ex) {
1420 <<
" - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() <<
". Trying again.";
1423 <<
" - deleteFile std::exception CAUGHT -: " << ex.what() <<
". Trying again.";
1431 uint32_t existingSize = 0;
1435 if (
file->rawFd_ == -1) {
1437 if (
file->rawHeaderSize_)
1444 existingSize +=
file->rawHeaderSize_;
1447 LogDebug(
"FedRawDataInputSource") <<
"opened file -: " << std::endl <<
file->fileName_;
1449 throw cms::Exception(
"FedRawDataInputSource:readNextChunkIntoBuffer")
1450 <<
"failed to open file " << std::endl
1456 (
void*)(
file->chunks_[0]->buf_ + existingSize),
1459 existingSize +=
last;
1464 if (
file->chunkPosition_ == 0) {
1468 existingSize +=
last;
1473 memmove((
void*)
file->chunks_[0]->buf_,
file->chunks_[0]->buf_ +
file->chunkPosition_, existingSizeLeft);
1479 for (uint32_t
i = 0;
i < blockcount;
i++) {
1480 const ssize_t
last =
1483 existingSizeLeft +=
last;
1489 file->chunkPosition_ = 0;
1494 LogDebug(
"FedRawDataInputSource") <<
"Closing input file -: " << std::endl <<
file->fileName_;
1514 std::pair<bool, unsigned int>
ret(
true, itr->second);
1519 return std::pair<bool, unsigned int>(
false, 0);
1524 if (
a.rfind(
'/') != std::string::npos)
1525 a =
a.substr(
a.rfind(
'/'));
1526 if (
b.rfind(
'/') != std::string::npos)
1527 b =
b.substr(
b.rfind(
'/'));
1535 if (fileStem.find(
"file://") == 0)
1536 fileStem = fileStem.substr(7);
1537 else if (fileStem.find(
"file:") == 0)
1538 fileStem = fileStem.substr(5);
1539 auto end = fileStem.find(
'_');
1541 if (fileStem.find(
"run") == 0) {
1545 long rval = boost::lexical_cast<long>(runStr);
1546 edm::LogInfo(
"FedRawDataInputSource") <<
"Autodetected run number in fileListMode -: " << rval;
1548 }
catch (boost::bad_lexical_cast
const&) {
1550 <<
"Unable to autodetect run number in fileListMode from file -: " <<
fileName;
1563 if (nextFile.find(
"file://") == 0)
1564 nextFile = nextFile.substr(7);
1565 else if (nextFile.find(
"file:") == 0)
1566 nextFile = nextFile.substr(5);
1569 if (fileStem.find(
"ls"))
1570 fileStem = fileStem.substr(fileStem.find(
"ls") + 2);
1571 if (fileStem.find(
'_'))
1572 fileStem = fileStem.substr(0, fileStem.find(
'_'));
1575 ls = boost::lexical_cast<unsigned int>(fileStem);
1590 return getFile(
ls, nextFile, fsize, lockWaitTime);