16 #include <boost/algorithm/string.hpp>
17 #include <boost/filesystem/fstream.hpp>
51 #include <boost/lexical_cast.hpp>
54 :
edm::RawInputSource(
pset, desc),
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)),
65 fileListMode_(
pset.getUntrackedParameter<
bool>(
"fileListMode",
false)),
66 fileListLoopMode_(
pset.getUntrackedParameter<
bool>(
"fileListLoopMode",
false)),
71 currentLumiSection_(0),
72 tcds_pointer_(nullptr),
75 gethostname(thishost, 255);
76 edm::LogInfo(
"FedRawDataInputSource") <<
"Construction. read-ahead chunk size -: " << std::endl
79 long autoRunNumber = -1;
83 if (autoRunNumber < 0)
84 throw cms::Exception(
"FedRawDataInputSource::FedRawDataInputSource") <<
"Run number not found from filename";
103 throw cms::Exception(
"FedRawDataInputSource::FedRawDataInputSource")
104 <<
"no reading enabled with numBuffers parameter 0";
111 edm::LogError(
"FedRawDataInputSource::FedRawDataInputSource") <<
"Intel crc32c checksum computation unavailable";
118 edm::LogInfo(
"FedRawDataInputSource") <<
"No FastMonitoringService found in the configuration";
123 throw cms::Exception(
"FedRawDataInputSource") <<
"FastMonitoringService not found";
129 cms::Exception(
"FedRawDataInputSource") <<
"EvFDaqDirector not found";
133 edm::LogInfo(
"FedRawDataInputSource") <<
"EvFDaqDirector/Source configured to use file service";
154 cvReader_.push_back(
new std::condition_variable);
156 threadInit_.store(
false, std::memory_order_release);
170 it->second.release();
177 std::unique_lock<std::mutex> lk(
mReader_);
198 desc.
setComment(
"File-based Filter Farm input source for reading raw data from BU ramdisk");
199 desc.
addUntracked<
unsigned int>(
"eventChunkSize", 32)->setComment(
"Input buffer (chunk) size");
201 ->setComment(
"Block size used in a single file read call (must be smaller or equal to buffer size)");
202 desc.
addUntracked<
unsigned int>(
"numBuffers", 2)->setComment(
"Number of buffers used for reading input");
204 ->setComment(
"Maximum number of simultaneously buffered raw files");
205 desc.
addUntracked<
unsigned int>(
"alwaysStartFromfirstLS",
false)
206 ->setComment(
"Force source to start from LS 1 if server provides higher lumisection number");
208 ->setComment(
"Verify event CRC-32C checksum of FRDv5 and higher or Adler32 with v3 and v4");
210 ->setComment(
"Use L1 event ID from FED header if true or from TCDS FED if false");
212 ->setComment(
"Use fileNames parameter to directly specify raw files to open");
213 desc.
addUntracked<std::vector<std::string>>(
"fileNames", std::vector<std::string>())
214 ->setComment(
"file list used when fileListMode is enabled");
216 descriptions.
add(
"source", desc);
245 open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
256 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
262 edm::LogInfo(
"FedRawDataInputSource") <<
"----------------RUN ENDED----------------";
305 open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
319 gettimeofday(&tv,
nullptr);
320 const edm::Timestamp lsopentime((
unsigned long long)tv.tv_sec * 1000000 + (
unsigned long long)tv.tv_usec);
328 edm::LogInfo(
"FedRawDataInputSource") <<
"New lumi section was opened. LUMI -: " << lumiSection;
350 std::unique_lock<std::mutex> lkw(
mWakeup_);
362 <<
"Run has been aborted by the input source reader thread";
411 <<
" but according to BU JSON there should be " <<
currentFile_->nEvents_ <<
" events";
415 std::unique_lock<std::mutex> lkw(
mWakeup_);
436 <<
"Premature end of input file while reading file header";
439 <<
"File with only raw header and no events received in LS " <<
currentFile_->lumi_;
456 <<
"Premature end of input file while reading event header";
489 <<
"Premature end of input file while reading event header";
496 <<
" event id:" <<
event_->event() <<
" lumi:" <<
event_->lumi() <<
" run:" <<
event_->run()
505 <<
"Premature end of input file while reading event data";
533 unsigned char* dataPosition;
541 <<
" event id:" <<
event_->event() <<
" lumi:" <<
event_->lumi() <<
" run:" <<
event_->run()
550 <<
"Premature end of input file while reading event data";
570 chunkEnd =
currentFile_->advance(dataPosition, msgSize);
582 if (crc !=
event_->crc32c()) {
586 <<
"Found a wrong crc32c checksum: expected 0x" << std::hex <<
event_->crc32c() <<
" but calculated 0x"
590 uint32_t adler = adler32(0
L, Z_NULL, 0);
591 adler = adler32(adler, (Bytef*)
event_->payload(),
event_->eventSize());
593 if (adler !=
event_->adler32()) {
597 <<
"Found a wrong Adler32 checksum: expected 0x" << std::hex <<
event_->adler32() <<
" but calculated 0x"
633 static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.
triggerType()),
660 bool fileIsBeingProcessed =
false;
663 fileIsBeingProcessed =
true;
667 if (!fileIsBeingProcessed) {
686 gettimeofday(&stv,
nullptr);
691 uint32_t eventSize =
event_->eventSize();
692 unsigned char*
event = (
unsigned char*)
event_->payload();
695 while (eventSize > 0) {
705 throw cms::Exception(
"FedRawDataInputSource::fillFEDRawDataCollection") <<
"Out of range FED ID : " <<
fedId;
718 tstamp =
edm::Timestamp(static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl));
728 memcpy(fedData.
data(),
event + eventSize, fedSize);
739 unsigned int currentLumiSection = 0;
749 uint32_t lockCount = 0;
759 bool copy_active =
false;
777 std::unique_lock<std::mutex> lkw(
mWakeup_);
782 LogDebug(
"FedRawDataInputSource") <<
"No free chunks or threads...";
798 uint32_t fileSizeIndex;
799 int64_t fileSizeFromMetadata;
808 uint16_t rawHeaderSize = 0;
809 uint32_t lsFromRaw = 0;
810 int32_t serverEventsInNewFile = -1;
832 serverEventsInNewFile,
833 fileSizeFromMetadata,
853 serverEventsInNewFile,
854 fileSizeFromMetadata,
866 if (thisLockWaitTimeUs > 0.)
867 sumLockWaitTimeUs += thisLockWaitTimeUs;
875 sumLockWaitTimeUs = 0;
905 if (
ls > currentLumiSection) {
909 currentLumiSection =
ls;
919 for (
unsigned int nextLS = lsToStart; nextLS <=
ls; nextLS++) {
930 for (
unsigned int nextLS = currentLumiSection + 1; nextLS <=
ls; nextLS++) {
935 currentLumiSection =
ls;
939 if (currentLumiSection > 0 &&
ls < currentLumiSection) {
941 <<
"Got old LS (" <<
ls <<
") file from EvFDAQDirector! Expected LS:" << currentLumiSection
942 <<
". Aborting execution." << std::endl;
958 if (!(dbgcount % 20))
959 LogDebug(
"FedRawDataInputSource") <<
"No file for me... sleep and try again...";
963 backoff_exp =
std::min(4, backoff_exp);
965 int sleeptime = (
int)(100000. *
pow(2, backoff_exp));
976 LogDebug(
"FedRawDataInputSource") <<
"The director says to grab -: " << nextFile;
984 rawFile = rawFilePath.replace_extension(
".raw").string();
988 int stat_res =
stat(rawFile.c_str(), &st);
989 if (stat_res == -1) {
990 edm::LogError(
"FedRawDataInputSource") <<
"Can not stat file (" << errno <<
"):-" << rawFile << std::endl;
1001 int eventsInNewFile;
1004 eventsInNewFile = 0;
1006 eventsInNewFile = -1;
1012 eventsInNewFile = serverEventsInNewFile;
1013 assert(eventsInNewFile >= 0);
1014 assert((eventsInNewFile > 0) ==
1015 (fileSize > rawHeaderSize));
1024 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1035 auto newInputFilePtr = newInputFile.get();
1038 for (
unsigned int i = 0;
i < neededChunks;
i++) {
1040 bool copy_active =
false;
1050 unsigned int newTid = 0xffffffff;
1060 bool copy_active =
false;
1078 if (newChunk ==
nullptr) {
1080 if (newTid != 0xffffffff)
1090 std::unique_lock<std::mutex> lk(
mReader_);
1104 if (!eventsInNewFile) {
1110 std::unique_lock<std::mutex> lkw(
mWakeup_);
1112 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1119 (rawHeaderSize > 0),
1136 std::unique_lock<std::mutex> lkw(
mWakeup_);
1141 newChunk->
reset(0, toRead, 0);
1145 std::unique_ptr<InputFile> newInputFile(
new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
1155 newInputFile->chunks_[0] = newChunk;
1165 unsigned numFinishedThreads = 0;
1171 std::unique_lock<std::mutex> lk(
mReader_);
1174 numFinishedThreads++;
1184 threadInit_.exchange(
true, std::memory_order_acquire);
1188 std::unique_lock<std::mutex> lk(
mReader_);
1215 unsigned int bufferLeft = (chunk->
offset_ == 0 &&
file->rawFd_ != 0) ?
file->rawHeaderSize_ : 0;
1220 bool fileOpenedHere =
false;
1223 fileDescriptor =
file->rawFd_;
1224 if (fileDescriptor == -1) {
1225 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1226 fileOpenedHere =
true;
1227 file->rawFd_ = fileDescriptor;
1231 fileDescriptor =
file->rawFd_;
1233 if (fileDescriptor == -1) {
1234 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1235 fileOpenedHere =
true;
1238 fileDescriptor = open(
file->fileName_.c_str(), O_RDONLY);
1239 fileOpenedHere =
true;
1243 if (fileDescriptor < 0) {
1244 edm::LogError(
"FedRawDataInputSource") <<
"readWorker failed to open file -: " <<
file->fileName_
1245 <<
" fd:" << fileDescriptor <<
" error: " << strerror(errno);
1249 if (fileOpenedHere) {
1251 pos = lseek(fileDescriptor, chunk->
offset_, SEEK_SET);
1254 <<
"readWorker failed to seek file -: " <<
file->fileName_ <<
" fd:" << fileDescriptor <<
" to offset "
1255 << chunk->
offset_ <<
" error: " << strerror(errno);
1261 LogDebug(
"FedRawDataInputSource") <<
"Reader thread opened file -: TID: " << tid <<
" file: " <<
file->fileName_
1262 <<
" at offset " << lseek(fileDescriptor, 0, SEEK_CUR);
1264 unsigned int skipped = bufferLeft;
1274 edm::LogError(
"FedRawDataInputSource") <<
"readWorker failed to read file -: " <<
file->fileName_
1275 <<
" fd:" << fileDescriptor <<
" error: " << strerror(errno);
1285 <<
"readWorker failed to read file -: " <<
file->fileName_ <<
" fd:" << fileDescriptor <<
" last:" <<
last
1286 <<
" expectedChunkSize:" << chunk->
usedSize_
1288 <<
" block:" << (
i + 1) <<
"/" <<
readBlocks_ <<
" error: " << strerror(errno);
1299 std::chrono::milliseconds msec = std::chrono::duration_cast<std::chrono::milliseconds>(
diff);
1300 LogDebug(
"FedRawDataInputSource") <<
" finished reading block -: " << (bufferLeft >> 20) <<
" MB"
1301 <<
" in " << msec.count() <<
" ms (" << (bufferLeft >> 20) /
double(msec.count())
1304 if (chunk->
offset_ + bufferLeft ==
file->fileSize_) {
1305 close(fileDescriptor);
1306 fileDescriptor = -1;
1311 close(fileDescriptor);
1326 throw cms::Exception(
"FedRawDataInputSource:threadError") <<
" file reader thread error ";
1340 if (currentLeft <
size) {
1386 LogDebug(
"FedRawDataInputSource:InputFile") <<
"Deleting input file -:" <<
fileName_;
1389 }
catch (
const boost::filesystem::filesystem_error& ex) {
1391 <<
" - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() <<
". Trying again.";
1394 <<
" - deleteFile std::exception CAUGHT -: " << ex.what() <<
". Trying again.";
1402 uint32_t existingSize = 0;
1406 if (
file->rawFd_ == -1)
1412 existingSize +=
file->rawHeaderSize_;
1416 LogDebug(
"FedRawDataInputSource") <<
"opened file -: " << std::endl <<
file->fileName_;
1418 throw cms::Exception(
"FedRawDataInputSource:readNextChunkIntoBuffer")
1419 <<
"failed to open file " << std::endl
1424 if (
file->chunkPosition_ == 0) {
1428 existingSize +=
last;
1431 const uint32_t chunksize =
file->chunkPosition_;
1435 memmove((
void*)
file->chunks_[0]->buf_,
file->chunks_[0]->buf_ +
file->chunkPosition_, existingSizeLeft);
1437 for (uint32_t
i = 0;
i < blockcount;
i++) {
1438 const ssize_t
last =
1441 existingSizeLeft +=
last;
1447 file->chunkPosition_ = 0;
1451 LogDebug(
"FedRawDataInputSource") <<
"Closing input file -: " << std::endl <<
file->fileName_;
1471 std::pair<bool, unsigned int>
ret(
true,
itr->second);
1476 return std::pair<bool, unsigned int>(
false, 0);
1481 if (
a.rfind(
"/") != std::string::npos)
1482 a =
a.substr(
a.rfind(
"/"));
1483 if (
b.rfind(
"/") != std::string::npos)
1484 b =
b.substr(
b.rfind(
"/"));
1492 auto end = fileStem.find(
"_");
1493 if (fileStem.find(
"run") == 0) {
1497 long rval = boost::lexical_cast<long>(runStr);
1498 edm::LogInfo(
"FedRawDataInputSource") <<
"Autodetected run number in fileListMode -: " <<
rval;
1500 }
catch (boost::bad_lexical_cast
const&) {
1502 <<
"Unable to autodetect run number in fileListMode from file -: " <<
fileName;
1515 if (nextFile.find(
"file://") == 0)
1516 nextFile = nextFile.substr(7);
1517 else if (nextFile.find(
"file:") == 0)
1518 nextFile = nextFile.substr(5);
1521 if (fileStem.find(
"ls"))
1522 fileStem = fileStem.substr(fileStem.find(
"ls") + 2);
1523 if (fileStem.find(
"_"))
1524 fileStem = fileStem.substr(0, fileStem.find(
"_"));
1527 ls = boost::lexical_cast<unsigned int>(fileStem);
1542 return getFile(
ls, nextFile, fsize, lockWaitTime);