23 #include <boost/lexical_cast.hpp>
24 #include <boost/filesystem/fstream.hpp>
31 struct flock make_flock(short
type, short whence, off_t
start, off_t len, pid_t
pid)
43 base_dir_(pset.getUntrackedParameter<std::
string> (
"baseDir",
".")),
44 bu_base_dir_(pset.getUntrackedParameter<std::
string> (
"buBaseDir",
".")),
45 directorBu_(pset.getUntrackedParameter<bool> (
"directorIsBu",
false)),
46 run_(pset.getUntrackedParameter<unsigned int> (
"runNumber",0)),
47 outputAdler32Recheck_(pset.getUntrackedParameter<bool>(
"outputAdler32Recheck",
false)),
48 requireTSPSet_(pset.getUntrackedParameter<bool>(
"requireTransfersPSet",
false)),
49 selectedTransferMode_(pset.getUntrackedParameter<std::
string>(
"selectedTransferMode",
"")),
50 hltSourceDirectory_(pset.getUntrackedParameter<std::
string>(
"hltSourceDirectory",
"")),
51 fuLockPollInterval_(pset.getUntrackedParameter<unsigned int>(
"fuLockPollInterval",2000)),
52 emptyLumisectionMode_(pset.getUntrackedParameter<bool>(
"emptyLumisectionMode",
false)),
56 fu_readwritelock_fd_(-1),
57 data_readwrite_fd_(-1),
58 fulocal_rwlock_fd_(-1),
59 fulocal_rwlock_fd2_(-1),
68 dirManager_(base_dir_),
72 bu_w_flk( make_flock( F_WRLCK, SEEK_SET, 0, 0, 0 )),
73 bu_r_flk( make_flock( F_RDLCK, SEEK_SET, 0, 0, 0 )),
74 bu_w_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
75 bu_r_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
76 fu_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
77 fu_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() )),
78 data_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
79 data_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() ))
93 ss <<
"run" << std::setfill(
'0') << std::setw(6) <<
run_;
99 gethostname(hostname,32);
102 char * fuLockPollIntervalPtr = getenv(
"FFF_LOCKPOLLINTERVAL");
103 if (fuLockPollIntervalPtr) {
111 char * emptyLumiModePtr = getenv(
"FFF_EMPTYLSMODE");
112 if (emptyLumiModePtr) {
118 int retval = mkdir(
base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
119 if (retval != 0 && errno != EEXIST) {
120 throw cms::Exception(
"DaqDirector") <<
" Error checking for base dir -: "
121 <<
base_dir_ <<
" mkdir error:" << strerror(errno);
127 S_IRWXU | S_IRWXG | S_IROTH | S_IRWXO | S_IXOTH);
128 if (retval != 0 && errno != EEXIST) {
129 throw cms::Exception(
"DaqDirector") <<
" Error creating run dir -: "
130 <<
run_dir_ <<
" mkdir error:" << strerror(errno);
138 fulocal_rwlock_fd_ = open(fulocal_lock_.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
140 throw cms::Exception(
"DaqDirector") <<
" Error creating/opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
141 chmod(fulocal_lock_.c_str(),0777);
144 fulocal_rwlock_fd2_ = open(fulocal_lock_.c_str(), O_RDWR, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
146 throw cms::Exception(
"DaqDirector") <<
" Error opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
159 S_IRWXU | S_IRWXG | S_IRWXO);
160 if (retval != 0 && errno != EEXIST) {
163 <<
" mkdir error:" << strerror(errno) <<
"\n";
167 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
168 if (retval != 0 && errno != EEXIST) {
169 throw cms::Exception(
"DaqDirector") <<
" Error creating bu run open dir -: "
176 O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
178 edm::LogWarning(
"EvFDaqDirector") <<
"problem with creating filedesc for buwritelock -: "
181 edm::LogInfo(
"EvFDaqDirector") <<
"creating filedesc for buwritelock -: "
185 edm::LogWarning(
"EvFDaqDirector")<<
"Error creating write lock stream -: " << strerror(errno);
200 retval = mkdir(tmphltdir.c_str(),S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
202 boost::filesystem::copy_file(
hltSourceDirectory_+
"/HltConfig.py",tmphltdir+
"/HltConfig.py");
204 boost::filesystem::copy_file(
hltSourceDirectory_+
"/CMSSW_VERSION",tmphltdir+
"/CMSSW_VERSION");
208 boost::filesystem::copy_file(
hltSourceDirectory_+
"/fffParameters.jsn",tmphltdir+
"/fffParameters.jsn");
210 boost::filesystem::rename(tmphltdir,hltdir);
221 retval = mkdir(
bu_base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
222 if (retval != 0 && errno != EEXIST) {
223 throw cms::Exception(
"DaqDirector") <<
" Error checking for bu base dir -: "
224 <<
bu_base_dir_ <<
" mkdir error:" << strerror(errno) <<
"\n";
254 desc.
setComment(
"Service used for file locking arbitration and for propagating information between other EvF components");
257 desc.
addUntracked<
unsigned int> (
"runNumber",0)->setComment(
"Run Number in ramdisk to open");
258 desc.
addUntracked<
bool>(
"outputAdler32Recheck",
false)->setComment(
"Check Adler32 of per-process output files while micro-merging");
259 desc.
addUntracked<
bool>(
"requireTransfersPSet",
false)->setComment(
"Require complete transferSystem PSet in the process configuration");
260 desc.
addUntracked<
std::string>(
"selectedTransferMode",
"")->setComment(
"Selected transfer mode (choice in Lvl0 propagated as Python parameter");
261 desc.
addUntracked<
unsigned int>(
"fuLockPollInterval",2000)->setComment(
"Lock polling interval in microseconds for the input directory file lock");
262 desc.
addUntracked<
bool>(
"emptyLumisectionMode",
false)->setComment(
"Enables writing stream output metadata even when no events are processed in a lumisection");
264 descriptions.
add(
"EvFDaqDirector", desc);
294 <<
run_dir_ <<
". This is not the highest run "
304 edm::LogWarning(
"EvFDaqDirector") <<
" Handles to check for files to delete were not set by the input source...";
311 if (it->second->lumi_ == ls) {
313 LogDebug(
"EvFDaqDirector") <<
"Deleting input file -:" << it->second->fileName_;
318 catch (
const boost::filesystem::filesystem_error& ex)
320 edm::LogError(
"EvFDaqDirector") <<
" - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() <<
". Trying again.";
329 edm::LogError(
"EvFDaqDirector") <<
" - deleteFile std::exception CAUGHT -: " << ex.what() <<
". Trying again.";
439 int retval =
remove(filename.c_str());
441 edm::LogError(
"EvFDaqDirector") <<
"Could not remove used file -: " << filename <<
". error = "
453 int lock_attempts = 0;
459 edm::LogWarning(
"EvFDaqDirector") <<
"Detected stop request from hltd. Ending run for this process after LS -: " << stopFileLS;
463 timeval ts_lockbegin;
464 gettimeofday(&ts_lockbegin,0);
472 if (lock_attempts>5000000 || errno==116) {
474 edm::LogWarning(
"EvFDaqDirector") <<
"Stale lock file handle. Checking if run directory and fu.lock file are present" << std::endl;
476 edm::LogWarning(
"EvFDaqDirector") <<
"Unable to obtain a lock for 5 seconds. Checking if run directory and fu.lock file are present -: errno "
477 << errno <<
":"<< strerror(errno) << std::endl;
481 edm::LogWarning(
"EvFDaqDirector") <<
"Detected local EoLS for lumisection "<<
ls ;
493 gettimeofday(&ts_lockend,0);
494 long deltat = (ts_lockend.tv_usec-ts_lockbegin.tv_usec) + (ts_lockend.tv_sec-ts_lockbegin.tv_sec)*1000000;
495 if (deltat>0.) lockWaitTime=deltat;
499 if(retval!=0)
return fileStatus;
503 gettimeofday(&ts_lockend,0);
518 unsigned int currentLs = readLs;
519 bool bumpedOk =
false;
522 if (ls && ls+1 < currentLs) ls++;
525 bumpedOk =
bumpFile(readLs, readIndex, nextFile, fsize, stopFileLS);
527 if (ls && readLs>currentLs && currentLs > ls) {
535 if (ls==0 && readLs>currentLs) {
557 LogDebug(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":" << readIndex + 1;
560 throw cms::Exception(
"EvFDaqDirector") <<
"seek on fu read/write lock for updating failed with error " << strerror(errno);
563 else if (currentLs < readLs) {
572 LogDebug(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":" <<
readIndex;
575 throw cms::Exception(
"EvFDaqDirector") <<
"seek on fu read/write lock for updating failed with error " << strerror(errno);
579 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for reading failed with error " << strerror(errno);
582 edm::LogError(
"EvFDaqDirector") <<
"fu read/write lock stream is invalid " << strerror(errno);
586 timeval ts_preunlock;
587 gettimeofday(&ts_preunlock,0);
588 int locked_period_int = ts_preunlock.tv_sec - ts_lockend.tv_sec;
589 double locked_period=locked_period_int+double(ts_preunlock.tv_usec - ts_lockend.tv_usec)/1000000;
598 if (retvalu==-1)
edm::LogError(
"EvFDaqDirector") <<
"Error unlocking the fu.lock " << strerror(errno);
601 edm::LogDebug(
"EvFDaqDirector") <<
"Waited during lock -: " << locked_period <<
" seconds";
604 if ( fileStatus ==
noFile ) {
609 if (stopFileLS>=0 && (
int)ls > stopFileLS) {
610 edm::LogInfo(
"EvFDaqDirector") <<
"Reached maximum lumisection set by hltd";
619 boost::filesystem::ifstream ij(BUEoLSFile);
623 if (!reader.
parse(ij, deserializeRoot)) {
624 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" << BUEoLSFile;
639 if (def.find(
'/')==0)
644 if (stat(fullpath.c_str(), &buf) == 0) {
647 DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
652 DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
654 for (
unsigned int i=0;
i<eolsDpd.
getNames().size();
i++)
661 if (def.size()<=1 || def.find(
'/')==std::string::npos) {
665 def = def.substr(def.find(
'/')+1);
672 edm::LogError(
"EvFDaqDirector") <<
" error reading number of files from BU JSON -: " << BUEoLSFile;
675 return boost::lexical_cast<
int>(
data);
685 edm::LogError(
"EvFDaqDirector") <<
" FastMonitoringService not found";
693 if (maxLS>=0 && ls > (
unsigned int)maxLS)
return false;
696 std::stringstream
ss;
697 unsigned int nextIndex =
index;
702 if (stat(nextFile.c_str(), &buf) == 0) {
711 bool eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
715 if (stat(nextFile.c_str(), &buf) == 0) {
722 if (indexFilesInLS < 0)
727 if ((
int)index<indexFilesInLS) {
729 edm::LogError(
"EvFDaqDirector") <<
"Potential miss of index file in LS -: " << ls <<
". Missing "
730 << nextFile <<
" because " << indexFilesInLS-1 <<
" is the highest index expected. Will not update fu.lock file";
739 if (maxLS>=0 && ls > (
unsigned int)maxLS)
return false;
742 if (stat(nextFile.c_str(), &buf) == 0) {
753 eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
762 edm::LogError(
"EvFDaqDirector") <<
"Error creating fu read/write lock stream "
765 edm::LogInfo(
"EvFDaqDirector") <<
"Initializing FU LOCK FILE";
774 S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
775 chmod(fulockfile.c_str(),0766);
780 edm::LogError(
"EvFDaqDirector") <<
"problem with creating filedesc for fuwritelock -: " << fulockfile.c_str()
781 <<
" create:" << create <<
" error:" << strerror(errno);
783 LogDebug(
"EvFDaqDirector") <<
"creating filedesc for fureadwritelock -: "
794 edm::LogError(
"EvFDaqDirector") <<
"problem with creating filedesc for datamerge "
797 LogDebug(
"EvFDaqDirector") <<
"creating filedesc for datamerge -: "
844 if (!boost::filesystem::is_directory(openPath)) {
845 LogDebug(
"EvFDaqDirector") <<
"<open> FU dir not found. Creating... -:" << openPath.string();
846 boost::filesystem::create_directories(openPath);
853 boost::filesystem::ifstream ij(file);
857 if (!reader.
parse(ij, deserializeRoot)) {
858 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" <<
file;
862 int ret = deserializeRoot.
get(
"lastLS",
"").
asInt();
878 (*transferSystemJson_)[
"destinations"]=destinationsVal;
881 std::vector<std::string> modes = tsPset.
getParameter< std::vector<std::string> >(
"transferModes");
883 (*transferSystemJson_)[
"transferModes"]=modesVal;
885 for (
auto psKeyItr =tsPset.
psetTable().begin();psKeyItr!=tsPset.
psetTable().end(); ++ psKeyItr) {
886 if (psKeyItr->first!=
"destinations" && psKeyItr->first!=
"transferModes") {
889 for (
auto &
mode : modes) {
891 if (!streamDef.
existsAs<std::vector<std::string>>(
mode,
true))
892 throw cms::Exception(
"EvFDaqDirector") <<
" Missing transfer system specification for -:" << psKeyItr->first <<
" (transferMode " <<
mode <<
")";
893 std::vector<std::string> streamDestinations = streamDef.
getParameter<std::vector<std::string>>(
mode);
897 if (!streamDestinations.size())
898 throw cms::Exception(
"EvFDaqDirector") <<
" Missing transter system destination(s) for -: "<< psKeyItr->first <<
", mode:" <<
mode;
900 for (
auto & sdest:streamDestinations) {
901 bool sDestValid=
false;
902 sDestsValue.append(sdest);
903 for (
auto &
dest: destinations) {
904 if (
dest==sdest) sDestValid=
true;
907 throw cms::Exception(
"EvFDaqDirector") <<
" Invalid transter system destination specified for -: "<< psKeyItr->first <<
", mode:" <<
mode <<
", dest:"<<sdest;
909 streamVal[
mode]=sDestsValue;
911 (*transferSystemJson_)[psKeyItr->first] = streamVal;
917 throw cms::Exception(
"EvFDaqDirector") <<
"transferSystem PSet not found";
925 streamRequestName = stream;
927 std::stringstream
msg;
928 msg <<
"Transfer system mode definitions missing for -: " << stream;
932 edm::LogWarning(
"EvFDaqDirector") << msg.str() <<
" (permissive mode)";
938 edm::LogWarning(
"EvFDaqDirector") <<
"Selected mode string is not provided as DaqDirector parameter."
939 <<
"Switch on requireTSPSet parameter to enforce this requirement. Setting mode to empty string.";
943 throw cms::Exception(
"EvFDaqDirector") <<
"Selected mode string is not provided as DaqDirector parameter.";
948 std::stringstream
msg;
949 msg <<
"Selected transfer mode " <<
selectedTransferMode_ <<
" is not specified for stream " << streamRequestName;
953 edm::LogWarning(
"EvFDaqDirector") << msg.str() <<
" (permissive mode)";
962 if (ret!=
"") ret +=
",";
963 ret+=(*it).asString();
970 int proc_flag_fd = open(proc_flag.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
unsigned int maxNumberOfThreads() const
T getParameter(std::string const &) const
std::string getStreamDestinations(std::string const &stream) 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 bolsFileName(const unsigned int run, const unsigned int ls)
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_
const_iterator begin() const
bool existsAs(std::string const ¶meterName, bool trackiness=true) const
checks if a parameter exists as a given type
std::list< std::pair< int, InputFile * > > * filesToDeletePtr_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::string getMergedDatChecksumFilePath(const unsigned int ls, std::string const &stream) const
std::shared_ptr< Json::Value > transferSystemJson_
void setAllowAnything()
allow any parameter label/value pairs
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)
ParameterSet const & getParameterSet(ParameterSetID const &id)
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 getOpenInitFilePath(std::string const &stream) const
std::string getOpenRawFilePath(const unsigned int ls, const unsigned int index) const
Value & append(const Value &value)
Append value to array at the end.
void createProcessingNotificationMaybe() 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()
unsigned int maxNumberOfStreams() const
std::string hltSourceDirectory_
std::string getOpenProtocolBufferHistogramFilePath(const unsigned int ls, std::string const &stream) const
void setComment(std::string const &value)
std::mutex * fileDeleteLockPtr_
std::string streamerDataFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
EvFDaqDirector(const edm::ParameterSet &pset, edm::ActivityRegistry ®)
std::string getMergedDatFilePath(const unsigned int ls, std::string const &stream) const
bool check(const std::string &)
FileStatus updateFuLock(unsigned int &ls, std::string &nextFile, uint32_t &fsize, uint64_t &lockWaitTime)
std::string getOpenOutputJsonFilePath(const unsigned int ls, std::string const &stream) const
std::string getOpenRootHistogramFilePath(const unsigned int ls, std::string const &stream) const
void checkTransferSystemPSet()
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 selectedTransferMode_
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_
bool emptyLumisectionMode_
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)
psettable const & psetTable() const
void preSourceEvent(edm::StreamID const &streamID)
std::string initFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
unsigned long long uint64_t
ParameterSet const & getProcessParameterSet()
ParameterSet const & getParameterSet(std::string const &) const
void preGlobalEndLumi(edm::GlobalContext const &globalContext)
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
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()
std::string getBoLSFilePathOnFU(const unsigned int ls) const
char data[epos_bytes_allocation]
void postEndRun(edm::GlobalContext const &globalContext)
Unserialize a JSON document into a Value.
const_iterator end() const
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
Iterator for object and array value.
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)
unsigned int fuLockPollInterval_
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_
array value (ordered list)