16 #include <boost/algorithm/string.hpp>
17 #include <boost/filesystem/fstream.hpp>
50 #include <boost/lexical_cast.hpp>
54 defPath_(
pset.getUntrackedParameter<
std::
string>(
"buDefPath",
"")),
55 eventChunkSize_(
pset.getUntrackedParameter<unsigned
int>(
"eventChunkSize", 32) * 1048576),
56 eventChunkBlock_(
pset.getUntrackedParameter<unsigned
int>(
"eventChunkBlock", 32) * 1048576),
57 numBuffers_(
pset.getUntrackedParameter<unsigned
int>(
"numBuffers", 2)),
58 maxBufferedFiles_(
pset.getUntrackedParameter<unsigned
int>(
"maxBufferedFiles", 2)),
59 getLSFromFilename_(
pset.getUntrackedParameter<
bool>(
"getLSFromFilename",
true)),
60 alwaysStartFromFirstLS_(
pset.getUntrackedParameter<
bool>(
"alwaysStartFromFirstLS",
false)),
61 verifyChecksum_(
pset.getUntrackedParameter<
bool>(
"verifyChecksum",
true)),
62 useL1EventID_(
pset.getUntrackedParameter<
bool>(
"useL1EventID",
false)),
66 fileListMode_(
pset.getUntrackedParameter<
bool>(
"fileListMode",
false)),
67 fileListLoopMode_(
pset.getUntrackedParameter<
bool>(
"fileListLoopMode",
false)),
72 currentLumiSection_(0),
73 tcds_pointer_(nullptr),
76 gethostname(thishost, 255);
77 edm::LogInfo(
"FedRawDataInputSource") <<
"Construction. read-ahead chunk size -: " << std::endl
82 throw cms::Exception(
"FedRawDataInputSource::fillFEDRawDataCollection")
83 <<
"Invalid TCDS Test FED range parameter";
89 long autoRunNumber = -1;
93 if (autoRunNumber < 0)
94 throw cms::Exception(
"FedRawDataInputSource::FedRawDataInputSource") <<
"Run number not found from filename";
113 throw cms::Exception(
"FedRawDataInputSource::FedRawDataInputSource")
114 <<
"no reading enabled with numBuffers parameter 0";
121 edm::LogError(
"FedRawDataInputSource::FedRawDataInputSource") <<
"Intel crc32c checksum computation unavailable";
128 edm::LogInfo(
"FedRawDataInputSource") <<
"No FastMonitoringService found in the configuration";
133 throw cms::Exception(
"FedRawDataInputSource") <<
"FastMonitoringService not found";
139 cms::Exception(
"FedRawDataInputSource") <<
"EvFDaqDirector not found";
143 edm::LogInfo(
"FedRawDataInputSource") <<
"EvFDaqDirector/Source configured to use file service";
164 cvReader_.push_back(
new std::condition_variable);
166 threadInit_.store(
false, std::memory_order_release);
186 std::unique_lock<std::mutex> lk(
mReader_);
207 desc.setComment(
"File-based Filter Farm input source for reading raw data from BU ramdisk");
208 desc.addUntracked<
unsigned int>(
"eventChunkSize", 32)->setComment(
"Input buffer (chunk) size");
209 desc.addUntracked<
unsigned int>(
"eventChunkBlock", 32)
210 ->setComment(
"Block size used in a single file read call (must be smaller or equal to buffer size)");
211 desc.addUntracked<
unsigned int>(
"numBuffers", 2)->setComment(
"Number of buffers used for reading input");
212 desc.addUntracked<
unsigned int>(
"maxBufferedFiles", 2)
213 ->setComment(
"Maximum number of simultaneously buffered raw files");
214 desc.addUntracked<
unsigned int>(
"alwaysStartFromfirstLS",
false)
215 ->setComment(
"Force source to start from LS 1 if server provides higher lumisection number");
216 desc.addUntracked<
bool>(
"verifyChecksum",
true)
217 ->setComment(
"Verify event CRC-32C checksum of FRDv5 and higher or Adler32 with v3 and v4");
218 desc.addUntracked<
bool>(
"useL1EventID",
false)
219 ->setComment(
"Use L1 event ID from FED header if true or from TCDS FED if false");
220 desc.addUntracked<std::vector<unsigned int>>(
"testTCDSFEDRange", std::vector<unsigned int>())
221 ->setComment(
"[min, max] range to search for TCDS FED ID in test setup");
222 desc.addUntracked<
bool>(
"fileListMode",
false)
223 ->setComment(
"Use fileNames parameter to directly specify raw files to open");
224 desc.addUntracked<std::vector<std::string>>(
"fileNames", std::vector<std::string>())
225 ->setComment(
"file list used when fileListMode is enabled");
226 desc.setAllowAnything();
227 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;
361 std::unique_lock<std::mutex> lkw(
mWakeup_);
373 <<
"Run has been aborted by the input source reader thread";
420 <<
" but according to BU JSON there should be " <<
currentFile_->nEvents_ <<
" events";
424 std::unique_lock<std::mutex> lkw(
mWakeup_);
444 <<
"Premature end of input file while reading file header";
447 <<
"File with only raw header and no events received in LS " <<
currentFile_->lumi_;
464 <<
"Premature end of input file while reading event header";
497 <<
"Premature end of input file while reading event header";
501 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
504 <<
" event id:" <<
event_->event() <<
" lumi:" <<
event_->lumi() <<
" run:" <<
event_->run()
513 <<
"Premature end of input file while reading event data";
519 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
541 unsigned char* dataPosition;
546 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
549 <<
" event id:" <<
event_->event() <<
" lumi:" <<
event_->lumi() <<
" run:" <<
event_->run()
558 <<
"Premature end of input file while reading event data";
575 event_ = std::make_unique<FRDEventMsgView>(dataPosition);
578 chunkEnd =
currentFile_->advance(dataPosition, msgSize);
590 if (crc !=
event_->crc32c()) {
594 <<
"Found a wrong crc32c checksum: expected 0x" << std::hex <<
event_->crc32c() <<
" but calculated 0x"
598 uint32_t adler = adler32(0
L, Z_NULL, 0);
599 adler = adler32(adler, (Bytef*)
event_->payload(),
event_->eventSize());
601 if (adler !=
event_->adler32()) {
605 <<
"Found a wrong Adler32 checksum: expected 0x" << std::hex <<
event_->adler32() <<
" but calculated 0x"
631 <<
"No TCDS or GTP FED in event with FEDHeader EID -: " <<
L1EventID_;
645 static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.
triggerType()),
672 bool fileIsBeingProcessed =
false;
675 fileIsBeingProcessed =
true;
679 if (!fileIsBeingProcessed) {
697 gettimeofday(&stv,
nullptr);
702 uint32_t eventSize =
event_->eventSize();
703 unsigned char*
event = (
unsigned char*)
event_->payload();
706 uint16_t selectedTCDSFed = 0;
707 while (eventSize > 0) {
717 throw cms::Exception(
"FedRawDataInputSource::fillFEDRawDataCollection") <<
"Out of range FED ID : " <<
fedId;
719 if (!selectedTCDSFed) {
720 selectedTCDSFed =
fedId;
723 throw cms::Exception(
"FedRawDataInputSource::fillFEDRawDataCollection")
724 <<
"Second TCDS FED ID " <<
fedId <<
" found. First ID: " << selectedTCDSFed;
734 tstamp =
edm::Timestamp(static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl));
744 memcpy(fedData.
data(),
event + eventSize, fedSize);
755 unsigned int currentLumiSection = 0;
765 uint32_t lockCount = 0;
775 bool copy_active =
false;
793 std::unique_lock<std::mutex> lkw(
mWakeup_);
798 LogDebug(
"FedRawDataInputSource") <<
"No free chunks or threads...";
814 uint32_t fileSizeIndex;
815 int64_t fileSizeFromMetadata;
824 uint16_t rawHeaderSize = 0;
825 uint32_t lsFromRaw = 0;
826 int32_t serverEventsInNewFile = -1;
848 serverEventsInNewFile,
849 fileSizeFromMetadata,
870 serverEventsInNewFile,
871 fileSizeFromMetadata,
883 if (thisLockWaitTimeUs > 0.)
884 sumLockWaitTimeUs += thisLockWaitTimeUs;
892 sumLockWaitTimeUs = 0;
923 if (
ls > currentLumiSection) {
927 currentLumiSection =
ls;
937 for (
unsigned int nextLS = lsToStart; nextLS <=
ls; nextLS++) {
948 for (
unsigned int nextLS = currentLumiSection + 1; nextLS <=
ls; nextLS++) {
953 currentLumiSection =
ls;
957 if (currentLumiSection > 0 &&
ls < currentLumiSection) {
959 <<
"Got old LS (" <<
ls <<
") file from EvFDAQDirector! Expected LS:" << currentLumiSection
960 <<
". Aborting execution." << std::endl;
976 if (!(dbgcount % 20))
977 LogDebug(
"FedRawDataInputSource") <<
"No file for me... sleep and try again...";
981 backoff_exp =
std::min(4, backoff_exp);
983 int sleeptime = (
int)(100000. *
pow(2, backoff_exp));
994 LogDebug(
"FedRawDataInputSource") <<
"The director says to grab -: " << nextFile;
1002 rawFile = rawFilePath.replace_extension(
".raw").string();
1006 int stat_res =
stat(rawFile.c_str(), &st);
1007 if (stat_res == -1) {
1008 edm::LogError(
"FedRawDataInputSource") <<
"Can not stat file (" << errno <<
"):-" << rawFile << std::endl;
1019 int eventsInNewFile;
1022 eventsInNewFile = 0;
1024 eventsInNewFile = -1;
1028 if (rawHeaderSize) {
1029 int rawFdEmpty = -1;
1030 uint16_t rawHeaderCheck;
1033 nextFile, rawFdEmpty, rawHeaderCheck, fileSizeFromMetadata, fileFound, 0,
true);
1034 assert(fileFound && rawHeaderCheck == rawHeaderSize);
1039 eventsInNewFile = serverEventsInNewFile;
1040 assert(eventsInNewFile >= 0);
1041 assert((eventsInNewFile > 0) ==
1042 (fileSize > rawHeaderSize));
1051 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1062 auto newInputFilePtr = newInputFile.get();
1065 for (
unsigned int i = 0;
i < neededChunks;
i++) {
1067 bool copy_active =
false;
1077 unsigned int newTid = 0xffffffff;
1087 bool copy_active =
false;
1105 if (newChunk ==
nullptr) {
1107 if (newTid != 0xffffffff)
1117 std::unique_lock<std::mutex> lk(
mReader_);
1131 if (!eventsInNewFile) {
1137 std::unique_lock<std::mutex> lkw(
mWakeup_);
1139 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1146 (rawHeaderSize > 0),
1163 std::unique_lock<std::mutex> lkw(
mWakeup_);
1168 newChunk->
reset(0, toRead, 0);
1172 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1182 newInputFile->chunks_[0] = newChunk;
1192 unsigned numFinishedThreads = 0;
1198 std::unique_lock<std::mutex> lk(
mReader_);
1201 numFinishedThreads++;
1211 threadInit_.exchange(
true, std::memory_order_acquire);
1215 std::unique_lock<std::mutex> lk(
mReader_);
1242 unsigned int bufferLeft = (chunk->
offset_ == 0 &&
file->rawFd_ != -1) ?
file->rawHeaderSize_ : 0;
1247 bool fileOpenedHere =
false;
1250 fileDescriptor =
file->rawFd_;
1251 if (fileDescriptor == -1) {
1252 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1253 fileOpenedHere =
true;
1254 file->rawFd_ = fileDescriptor;
1258 fileDescriptor =
file->rawFd_;
1260 if (fileDescriptor == -1) {
1261 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1262 fileOpenedHere =
true;
1265 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1266 fileOpenedHere =
true;
1270 if (fileDescriptor < 0) {
1271 edm::LogError(
"FedRawDataInputSource") <<
"readWorker failed to open file -: " <<
file->fileName_
1272 <<
" fd:" << fileDescriptor <<
" error: " << strerror(errno);
1276 if (fileOpenedHere) {
1278 pos = lseek(fileDescriptor, chunk->
offset_, SEEK_SET);
1281 <<
"readWorker failed to seek file -: " <<
file->fileName_ <<
" fd:" << fileDescriptor <<
" to offset "
1282 << chunk->
offset_ <<
" error: " << strerror(errno);
1288 LogDebug(
"FedRawDataInputSource") <<
"Reader thread opened file -: TID: " << tid <<
" file: " <<
file->fileName_
1289 <<
" at offset " << lseek(fileDescriptor, 0, SEEK_CUR);
1291 unsigned int skipped = bufferLeft;
1301 edm::LogError(
"FedRawDataInputSource") <<
"readWorker failed to read file -: " <<
file->fileName_
1302 <<
" fd:" << fileDescriptor <<
" error: " << strerror(errno);
1312 <<
"readWorker failed to read file -: " <<
file->fileName_ <<
" fd:" << fileDescriptor <<
" last:" <<
last
1313 <<
" expectedChunkSize:" << chunk->
usedSize_
1315 <<
" block:" << (
i + 1) <<
"/" <<
readBlocks_ <<
" error: " << strerror(errno);
1326 std::chrono::milliseconds msec = std::chrono::duration_cast<std::chrono::milliseconds>(
diff);
1327 LogDebug(
"FedRawDataInputSource") <<
" finished reading block -: " << (bufferLeft >> 20) <<
" MB"
1328 <<
" in " << msec.count() <<
" ms (" << (bufferLeft >> 20) /
double(msec.count())
1331 if (chunk->
offset_ + bufferLeft ==
file->fileSize_) {
1332 close(fileDescriptor);
1333 fileDescriptor = -1;
1338 close(fileDescriptor);
1353 throw cms::Exception(
"FedRawDataInputSource:threadError") <<
" file reader thread error ";
1367 if (currentLeft <
size) {
1413 LogDebug(
"FedRawDataInputSource:InputFile") <<
"Deleting input file -:" <<
fileName_;
1416 }
catch (
const std::filesystem::filesystem_error& ex) {
1418 <<
" - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() <<
". Trying again.";
1421 <<
" - deleteFile std::exception CAUGHT -: " << ex.what() <<
". Trying again.";
1429 uint32_t existingSize = 0;
1433 if (
file->rawFd_ == -1) {
1435 if (
file->rawHeaderSize_)
1442 existingSize +=
file->rawHeaderSize_;
1445 LogDebug(
"FedRawDataInputSource") <<
"opened file -: " << std::endl <<
file->fileName_;
1447 throw cms::Exception(
"FedRawDataInputSource:readNextChunkIntoBuffer")
1448 <<
"failed to open file " << std::endl
1454 (
void*)(
file->chunks_[0]->buf_ + existingSize),
1457 existingSize +=
last;
1462 if (
file->chunkPosition_ == 0) {
1466 existingSize +=
last;
1471 memmove((
void*)
file->chunks_[0]->buf_,
file->chunks_[0]->buf_ +
file->chunkPosition_, existingSizeLeft);
1477 for (uint32_t
i = 0;
i < blockcount;
i++) {
1478 const ssize_t
last =
1481 existingSizeLeft +=
last;
1487 file->chunkPosition_ = 0;
1492 LogDebug(
"FedRawDataInputSource") <<
"Closing input file -: " << std::endl <<
file->fileName_;
1512 std::pair<bool, unsigned int>
ret(
true,
itr->second);
1517 return std::pair<bool, unsigned int>(
false, 0);
1522 if (
a.rfind(
'/') != std::string::npos)
1523 a =
a.substr(
a.rfind(
'/'));
1524 if (
b.rfind(
'/') != std::string::npos)
1525 b =
b.substr(
b.rfind(
'/'));
1533 if (fileStem.find(
"file://") == 0)
1534 fileStem = fileStem.substr(7);
1535 else if (fileStem.find(
"file:") == 0)
1536 fileStem = fileStem.substr(5);
1537 auto end = fileStem.find(
'_');
1539 if (fileStem.find(
"run") == 0) {
1543 long rval = boost::lexical_cast<long>(runStr);
1544 edm::LogInfo(
"FedRawDataInputSource") <<
"Autodetected run number in fileListMode -: " << rval;
1546 }
catch (boost::bad_lexical_cast
const&) {
1548 <<
"Unable to autodetect run number in fileListMode from file -: " <<
fileName;
1561 if (nextFile.find(
"file://") == 0)
1562 nextFile = nextFile.substr(7);
1563 else if (nextFile.find(
"file:") == 0)
1564 nextFile = nextFile.substr(5);
1567 if (fileStem.find(
"ls"))
1568 fileStem = fileStem.substr(fileStem.find(
"ls") + 2);
1569 if (fileStem.find(
'_'))
1570 fileStem = fileStem.substr(0, fileStem.find(
'_'));
1573 ls = boost::lexical_cast<unsigned int>(fileStem);
1588 return getFile(
ls, nextFile, fsize, lockWaitTime);