20 #include <boost/lexical_cast.hpp>
21 #include <boost/filesystem/fstream.hpp>
28 struct flock make_flock(short
type, short whence, off_t
start, off_t len, pid_t
pid)
40 testModeNoBuilderUnit_(
41 pset.getUntrackedParameter<bool> (
"testModeNoBuilderUnit",
45 pset.getUntrackedParameter<std::
string> (
"baseDir",
"/data")
48 pset.getUntrackedParameter<std::
string> (
"buBaseDir",
"/data")
51 pset.getUntrackedParameter<bool> (
"directorIsBu",
false)
53 run_(pset.getUntrackedParameter<unsigned int> (
"runNumber",0)),
54 outputAdler32Recheck_(pset.getUntrackedParameter<bool>(
"outputAdler32Recheck",
false)),
58 fu_readwritelock_fd_(-1),
59 data_readwrite_fd_(-1),
60 fulocal_rwlock_fd_(-1),
61 fulocal_rwlock_fd2_(-1),
70 dirManager_(base_dir_),
76 bu_w_flk( make_flock( F_WRLCK, SEEK_SET, 0, 0, 0 )),
77 bu_r_flk( make_flock( F_RDLCK, SEEK_SET, 0, 0, 0 )),
78 bu_w_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
79 bu_r_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
80 fu_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
81 fu_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() )),
82 data_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
83 data_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() ))
97 ss <<
"run" << std::setfill(
'0') << std::setw(6) <<
run_;
103 gethostname(hostname,32);
106 int retval = mkdir(
base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
107 if (retval != 0 && errno != EEXIST) {
108 throw cms::Exception(
"DaqDirector") <<
" Error checking for base dir -: "
109 <<
base_dir_ <<
" mkdir error:" << strerror(errno);
115 S_IRWXU | S_IRWXG | S_IROTH | S_IRWXO | S_IXOTH);
116 if (retval != 0 && errno != EEXIST) {
117 throw cms::Exception(
"DaqDirector") <<
" Error creating run dir -: "
118 <<
run_dir_ <<
" mkdir error:" << strerror(errno);
126 fulocal_rwlock_fd_ = open(fulocal_lock_.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
128 throw cms::Exception(
"DaqDirector") <<
" Error creating/opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
129 chmod(fulocal_lock_.c_str(),0777);
132 fulocal_rwlock_fd2_ = open(fulocal_lock_.c_str(), O_RDWR, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
134 throw cms::Exception(
"DaqDirector") <<
" Error opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
147 S_IRWXU | S_IRWXG | S_IRWXO);
148 if (retval != 0 && errno != EEXIST) {
151 <<
" mkdir error:" << strerror(errno) <<
"\n";
155 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
156 if (retval != 0 && errno != EEXIST) {
157 throw cms::Exception(
"DaqDirector") <<
" Error creating bu run open dir -: "
164 O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
166 edm::LogWarning(
"EvFDaqDirector") <<
"problem with creating filedesc for buwritelock -: "
169 edm::LogInfo(
"EvFDaqDirector") <<
"creating filedesc for buwritelock -: "
173 edm::LogWarning(
"EvFDaqDirector")<<
"Error creating write lock stream -: " << strerror(errno);
186 retval = mkdir(
bu_base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
187 if (retval != 0 && errno != EEXIST) {
188 throw cms::Exception(
"DaqDirector") <<
" Error checking for bu base dir -: "
189 <<
bu_base_dir_ <<
" mkdir error:" << strerror(errno) <<
"\n";
241 <<
run_dir_ <<
". This is not the highest run "
251 edm::LogWarning(
"EvFDaqDirector") <<
" Handles to check for files to delete were not set by the input source...";
258 if (it->second->lumi_ == ls) {
260 LogDebug(
"EvFDaqDirector") <<
"Deleting input file -:" << it->second->fileName_;
265 catch (
const boost::filesystem::filesystem_error& ex)
267 edm::LogError(
"EvFDaqDirector") <<
" - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() <<
". Trying again.";
276 edm::LogError(
"EvFDaqDirector") <<
" - deleteFile std::exception CAUGHT -: " << ex.what() <<
". Trying again.";
378 int retval =
remove(filename.c_str());
380 edm::LogError(
"EvFDaqDirector") <<
"Could not remove used file -: " << filename <<
". error = "
392 int lock_attempts = 0;
398 edm::LogWarning(
"EvFDaqDirector") <<
"Detected stop request from hltd. Ending run for this process after LS -: " << stopFileLS;
404 if (retval==-1) usleep(50000);
408 if (lock_attempts>100 || errno==116) {
410 edm::LogWarning(
"EvFDaqDirector") <<
"Stale lock file handle. Checking if run directory and fu.lock file are present" << std::endl;
412 edm::LogWarning(
"EvFDaqDirector") <<
"Unable to obtain a lock for 5 seconds. Checking if run directory and fu.lock file are present -: errno "<< errno <<
":"<< strerror(errno) << std::endl;
419 if(retval!=0)
return fileStatus;
423 gettimeofday(&ts_lockend,0);
428 unsigned int readLs,
readIndex, jumpLs, jumpIndex;
437 &jumpLs, &jumpIndex);
444 bool bumpedOk =
bumpFile(readLs, readIndex, nextFile, fsize, stopFileLS);
454 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for updating failed with error "
462 readIndex + 1, readLs + 2, readIndex + 1);
475 edm::LogInfo(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":"
476 << readIndex + 1 <<
" --> " << readLs + 2
477 <<
":" << readIndex + 1;
479 LogDebug(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":"
483 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for updating failed with error "
487 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for reading failed with error "
490 edm::LogError(
"EvFDaqDirector") <<
"fu read/write lock stream is invalid " << strerror(errno);
493 timeval ts_preunlock;
494 gettimeofday(&ts_preunlock,0);
495 int locked_period_int = ts_preunlock.tv_sec - ts_lockend.tv_sec;
496 double locked_period=locked_period_int+double(ts_preunlock.tv_usec - ts_lockend.tv_usec)/1000000;
505 if (retvalu==-1)
edm::LogError(
"EvFDaqDirector") <<
"Error unlocking the fu.lock " << strerror(errno);
508 edm::LogDebug(
"EvFDaqDirector") <<
"Waited during lock -: " << locked_period <<
" seconds";
511 if ( fileStatus ==
noFile ) {
516 if (stopFileLS>=0 && (
int)ls > stopFileLS) {
517 edm::LogInfo(
"EvFDaqDirector") <<
"Reached maximum lumisection set by hltd";
526 boost::filesystem::ifstream ij(BUEoLSFile);
530 if (!reader.
parse(ij, deserializeRoot)) {
531 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" << BUEoLSFile;
546 if (def.find(
'/')==0)
551 if (stat(fullpath.c_str(), &buf) == 0) {
554 DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
559 DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
561 for (
unsigned int i=0;
i<eolsDpd.
getNames().size();
i++)
568 if (def.size()<=1 || def.find(
'/')==std::string::npos) {
572 def = def.substr(def.find(
'/')+1);
579 edm::LogError(
"EvFDaqDirector") <<
" error reading number of files from BU JSON -: " << BUEoLSFile;
582 return boost::lexical_cast<
int>(
data);
592 edm::LogError(
"EvFDaqDirector") <<
" FastMonitoringService not found";
600 std::stringstream
ss;
601 unsigned int nextIndex =
index;
606 if (stat(nextFile.c_str(), &buf) == 0) {
615 bool eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
616 unsigned int startingLumi =
ls;
620 if (stat(nextFile.c_str(), &buf) == 0) {
627 if (indexFilesInLS < 0)
632 if ((
int)index<indexFilesInLS) {
634 edm::LogError(
"EvFDaqDirector") <<
"Potential miss of index file in LS -: " << ls <<
". Missing "
635 << nextFile <<
" because " << indexFilesInLS-1 <<
" is the highest index expected. Will not update fu.lock file";
644 if (maxLS>=0 && ls > (
unsigned int)maxLS)
return false;
647 if (stat(nextFile.c_str(), &buf) == 0) {
658 std::string cpCmd =
"cp " + sourceEol +
" " + destEol;
659 edm::LogInfo(
"EvFDaqDirector") <<
" testmode: Running copy cmd -: " << cpCmd;
660 int rc = system(cpCmd.c_str());
662 edm::LogError(
"EvFDaqDirector") <<
" testmode: COPY EOL FAILED!!!!! -: " << cpCmd;
669 eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
678 edm::LogError(
"EvFDaqDirector") <<
"Error creating fu read/write lock stream "
681 edm::LogInfo(
"EvFDaqDirector") <<
"Initializing FU LOCK FILE";
682 unsigned int readLs = 1,
readIndex = 0, jumpLs = 3, jumpIndex = 0;
694 S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
695 chmod(fulockfile.c_str(),0766);
700 edm::LogError(
"EvFDaqDirector") <<
"problem with creating filedesc for fuwritelock -: " << fulockfile.c_str()
701 <<
" create:" << create <<
" error:" << strerror(errno);
703 LogDebug(
"EvFDaqDirector") <<
"creating filedesc for fureadwritelock -: "
714 edm::LogError(
"EvFDaqDirector") <<
"problem with creating filedesc for datamerge "
717 LogDebug(
"EvFDaqDirector") <<
"creating filedesc for datamerge -: "
764 if (!boost::filesystem::is_directory(openPath)) {
765 LogDebug(
"EvFDaqDirector") <<
"<open> FU dir not found. Creating... -:" << openPath.string();
766 boost::filesystem::create_directories(openPath);
773 boost::filesystem::ifstream ij(file);
777 if (!reader.
parse(ij, deserializeRoot)) {
778 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" <<
file;
782 int ret = deserializeRoot.
get(
"lastLS",
"").
asInt();
unsigned int maxNumberOfThreads() const
Value get(UInt index, const Value &defaultValue) const
std::vector< std::string > & getData()
std::string protocolBufferHistogramFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
tuple start
Check for commandline option errors.
std::string streamerDataChecksumFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
std::string getMergedProtocolBufferHistogramFilePath(const unsigned int ls, std::string const &stream) const
void watchPreallocate(Preallocate::slot_type const &iSlot)
std::vector< int > streamFileTracker_
std::list< std::pair< int, InputFile * > > * filesToDeletePtr_
std::string getMergedDatChecksumFilePath(const unsigned int ls, std::string const &stream) const
void openFULockfileStream(std::string &fuLockFilePath, bool create)
void accumulateFileSize(unsigned int lumi, unsigned long fileSize)
void watchPreGlobalEndLumi(PreGlobalEndLumi::slot_type const &iSlot)
std::string getInitFilePath(std::string const &stream) const
std::string getMergedRootHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::string inputRawFileName(const unsigned int run, const unsigned int ls, const unsigned int index)
pthread_mutex_t init_lock_
LuminosityBlockID const & luminosityBlockID() const
std::string getProtocolBufferHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::string getRawFilePath(const unsigned int ls, const unsigned int index) const
std::string getOpenRawFilePath(const unsigned int ls, const unsigned int index) const
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
std::string getEoLSFilePathOnBU(const unsigned int ls) const
std::string getEoRFilePath() const
unsigned long previousFileSize_
void createRunOpendirMaybe()
FileStatus updateFuLock(unsigned int &ls, std::string &nextFile, uint32_t &fsize)
unsigned int maxNumberOfStreams() const
std::string getOpenProtocolBufferHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::mutex * fileDeleteLockPtr_
std::string streamerDataFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
tuple path
else: Piece not in the list, fine.
EvFDaqDirector(const edm::ParameterSet &pset, edm::ActivityRegistry ®)
std::string getMergedDatFilePath(const unsigned int ls, std::string const &stream) const
bool check(const std::string &)
std::string getOpenOutputJsonFilePath(const unsigned int ls, std::string const &stream) const
std::string getOpenRootHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::string getRootHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::string getOpenDatFilePath(const unsigned int ls, std::string const &stream) const
std::string inputJsonFileName(const unsigned int run, const unsigned int ls, const unsigned int index)
std::string stopFilePath_
FILE * maybeCreateAndLockFileHeadForStream(unsigned int ls, std::string &stream)
void unlockAndCloseMergeStream()
void tryInitializeFuLockFile()
void removeFile(unsigned int ls, unsigned int index)
std::string streamerJsonFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
std::string getOpenInputJsonFilePath(const unsigned int ls, const unsigned int index) const
std::string eorFileName(const unsigned int run)
int readLastLSEntry(std::string const &file)
virtual void deserialize(Json::Value &root)
unsigned int eolsNFilesIndex_
void watchPreGlobalBeginRun(PreGlobalBeginRun::slot_type const &iSlot)
std::string getOutputJsonFilePath(const unsigned int ls, std::string const &stream) const
int getNFilesFromEoLS(std::string BUEoLSFile)
void preBeginRun(edm::GlobalContext const &globalContext)
std::string rootHistogramFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
void preSourceEvent(edm::StreamID const &streamID)
std::string initFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
void preGlobalEndLumi(edm::GlobalContext const &globalContext)
bool testModeNoBuilderUnit_
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
std::string & getDefinition()
evf::FastMonitoringService * fms_
std::string rootHistogramFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
std::string findHighestRunDir()
char data[epos_bytes_allocation]
void postEndRun(edm::GlobalContext const &globalContext)
Unserialize a JSON document into a Value.
std::vector< std::string > const & getNames()
std::string getEoLSFilePathOnFU(const unsigned int ls) const
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
bool bumpFile(unsigned int &ls, unsigned int &index, std::string &nextFile, uint32_t &fsize, int maxLS)
volatile std::atomic< bool > shutdown_flag false
void preallocate(edm::service::SystemBounds const &bounds)
struct flock data_rw_fulk
std::string getInputJsonFilePath(const unsigned int ls, const unsigned int index) const
std::string getRunOpenDirPath() const
std::string protocolBufferHistogramFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
std::string eolsFileName(const unsigned int run, const unsigned int ls)
std::string streamerDataFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
std::string getEoRFilePathOnFU() const
std::string bu_run_open_dir_