CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EvFDaqDirector.cc
Go to the documentation of this file.
10 
16 
17 #include <iostream>
18 #include <sstream>
19 #include <sys/time.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <sys/file.h>
23 #include <boost/lexical_cast.hpp>
24 #include <boost/filesystem/fstream.hpp>
25 
26 //#define DEBUG
27 
28 using namespace jsoncollector;
29 
30 namespace evf {
31 
32  namespace {
33  struct flock make_flock(short type, short whence, off_t start, off_t len, pid_t pid)
34  {
35 #ifdef __APPLE__
36  return {start, len, pid, type, whence};
37 #else
38  return {type, whence, start, len, pid};
39 #endif
40  }
41  }
42 
43  EvFDaqDirector::EvFDaqDirector(const edm::ParameterSet &pset,
44  edm::ActivityRegistry& reg) :
45  base_dir_(pset.getUntrackedParameter<std::string> ("baseDir", ".")),
46  bu_base_dir_(pset.getUntrackedParameter<std::string> ("buBaseDir", ".")),
47  directorBu_(pset.getUntrackedParameter<bool> ("directorIsBu", false)),
48  run_(pset.getUntrackedParameter<unsigned int> ("runNumber",0)),
49  outputAdler32Recheck_(pset.getUntrackedParameter<bool>("outputAdler32Recheck",false)),
50  requireTSPSet_(pset.getUntrackedParameter<bool>("requireTransfersPSet",false)),
51  selectedTransferMode_(pset.getUntrackedParameter<std::string>("selectedTransferMode","")),
52  hltSourceDirectory_(pset.getUntrackedParameter<std::string>("hltSourceDirectory","")),
53  fuLockPollInterval_(pset.getUntrackedParameter<unsigned int>("fuLockPollInterval",2000)),
54  emptyLumisectionMode_(pset.getUntrackedParameter<bool>("emptyLumisectionMode",false)),
55  hostname_(""),
56  bu_readlock_fd_(-1),
57  bu_writelock_fd_(-1),
58  fu_readwritelock_fd_(-1),
59  data_readwrite_fd_(-1),
60  fulocal_rwlock_fd_(-1),
61  fulocal_rwlock_fd2_(-1),
62 
63  bu_w_lock_stream(0),
64  bu_r_lock_stream(0),
65  fu_rw_lock_stream(0),
66  //bu_w_monitor_stream(0),
67  //bu_t_monitor_stream(0),
68  data_rw_stream(0),
69 
70  dirManager_(base_dir_),
71 
72  previousFileSize_(0),
73 
74  bu_w_flk( make_flock( F_WRLCK, SEEK_SET, 0, 0, 0 )),
75  bu_r_flk( make_flock( F_RDLCK, SEEK_SET, 0, 0, 0 )),
76  bu_w_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
77  bu_r_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
78  fu_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
79  fu_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() )),
80  data_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
81  data_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() ))
82  //fulocal_rw_flk( make_flock( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
83  //fulocal_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() )),
84  //fulocal_rw_flk2( make_flock( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
85  //fulocal_rw_fulk2( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() ))
86  {
87 
94 
95  std::stringstream ss;
96  ss << "run" << std::setfill('0') << std::setw(6) << run_;
97  run_string_ = ss.str();
99 
100  //save hostname for later
101  char hostname[33];
102  gethostname(hostname,32);
103  hostname_ = hostname;
104 
105  char * fuLockPollIntervalPtr = getenv("FFF_LOCKPOLLINTERVAL");
106  if (fuLockPollIntervalPtr) {
107  try {
108  fuLockPollInterval_=boost::lexical_cast<unsigned int>(std::string(fuLockPollIntervalPtr));
109  edm::LogInfo("Setting fu lock poll interval by environment string: ") << fuLockPollInterval_ << " us";
110  }
111  catch( boost::bad_lexical_cast const& ) {
112  edm::LogWarning("Bad lexical cast in parsing: ") << std::string(fuLockPollIntervalPtr);
113  }
114  }
115 
116  char * emptyLumiModePtr = getenv("FFF_EMPTYLSMODE");
117  if (emptyLumiModePtr) {
118  emptyLumisectionMode_ = true;
119  edm::LogInfo("Setting empty lumisection mode");
120  }
121 
122  // check if base dir exists or create it accordingly
123  int retval = mkdir(base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
124  if (retval != 0 && errno != EEXIST) {
125  throw cms::Exception("DaqDirector") << " Error checking for base dir -: "
126  << base_dir_ << " mkdir error:" << strerror(errno);
127  }
128 
129  //create run dir in base dir
130  umask(0);
131  retval = mkdir(run_dir_.c_str(),
132  S_IRWXU | S_IRWXG | S_IROTH | S_IRWXO | S_IXOTH);
133  if (retval != 0 && errno != EEXIST) {
134  throw cms::Exception("DaqDirector") << " Error creating run dir -: "
135  << run_dir_ << " mkdir error:" << strerror(errno);
136  }
137 
138  //create fu-local.lock in run open dir
139  if (!directorBu_) {
140 
142  std::string fulocal_lock_ = getRunOpenDirPath() +"/fu-local.lock";
143  fulocal_rwlock_fd_ = open(fulocal_lock_.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);//O_RDWR?
144  if (fulocal_rwlock_fd_==-1)
145  throw cms::Exception("DaqDirector") << " Error creating/opening a local lock file -: " << fulocal_lock_.c_str() << " : " << strerror(errno);
146  chmod(fulocal_lock_.c_str(),0777);
147  fsync(fulocal_rwlock_fd_);
148  //open second fd for another input source thread
149  fulocal_rwlock_fd2_ = open(fulocal_lock_.c_str(), O_RDWR, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);//O_RDWR?
150  if (fulocal_rwlock_fd2_==-1)
151  throw cms::Exception("DaqDirector") << " Error opening a local lock file -: " << fulocal_lock_.c_str() << " : " << strerror(errno);
152  }
153 
154  //bu_run_dir: for FU, for which the base dir is local and the BU is remote, it is expected to be there
155  //for BU, it is created at this point
156  if (directorBu_)
157  {
159  std::string bulockfile = bu_run_dir_ + "/bu.lock";
160  std::string fulockfile = bu_run_dir_ + "/fu.lock";
161 
162  //make or find bu run dir
163  retval = mkdir(bu_run_dir_.c_str(),
164  S_IRWXU | S_IRWXG | S_IRWXO);
165  if (retval != 0 && errno != EEXIST) {
166  throw cms::Exception("DaqDirector")
167  << " Error creating bu run dir -: " << bu_run_dir_
168  << " mkdir error:" << strerror(errno) << "\n";
169  }
170  bu_run_open_dir_ = bu_run_dir_ + "/open";
171  retval = mkdir(bu_run_open_dir_.c_str(),
172  S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
173  if (retval != 0 && errno != EEXIST) {
174  throw cms::Exception("DaqDirector") << " Error creating bu run open dir -: "
175  << bu_run_open_dir_ << " mkdir error:" << strerror(errno)
176  << "\n";
177  }
178 
179  // the BU director does not need to know about the fu lock
180  bu_writelock_fd_ = open(bulockfile.c_str(),
181  O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
182  if (bu_writelock_fd_ == -1)
183  edm::LogWarning("EvFDaqDirector") << "problem with creating filedesc for buwritelock -: "
184  << strerror(errno);
185  else
186  edm::LogInfo("EvFDaqDirector") << "creating filedesc for buwritelock -: "
187  << bu_writelock_fd_;
188  bu_w_lock_stream = fdopen(bu_writelock_fd_, "w");
189  if (bu_w_lock_stream == 0)
190  edm::LogWarning("EvFDaqDirector")<< "Error creating write lock stream -: " << strerror(errno);
191 
192  // BU INITIALIZES LOCK FILE
193  // FU LOCK FILE OPEN
194  openFULockfileStream(fulockfile, true);
196  fflush(fu_rw_lock_stream);
197  close(fu_readwritelock_fd_);
198 
199  if (hltSourceDirectory_.size())
200  {
201  struct stat buf;
202  if (stat(hltSourceDirectory_.c_str(),&buf)==0) {
203  std::string hltdir=bu_run_dir_+"/hlt";
204  std::string tmphltdir=bu_run_open_dir_+"/hlt";
205  retval = mkdir(tmphltdir.c_str(),S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
206  if (retval != 0 && errno != EEXIST)
207  throw cms::Exception("DaqDirector")
208  << " Error creating bu run dir -: " << hltdir
209  << " mkdir error:" << strerror(errno) << "\n";
210 
211  boost::filesystem::copy_file(hltSourceDirectory_+"/HltConfig.py",tmphltdir+"/HltConfig.py");
212 
213  boost::filesystem::copy_file(hltSourceDirectory_+"/fffParameters.jsn",tmphltdir+"/fffParameters.jsn");
214 
215  boost::filesystem::rename(tmphltdir,hltdir);
216  }
217  else
218  throw cms::Exception("DaqDirector") << " Error looking for HLT configuration -: " << hltSourceDirectory_;
219  }
220  //else{}//no configuration specified
221  }
222  else
223  {
224  // for FU, check if bu base dir exists
225 
226  retval = mkdir(bu_base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
227  if (retval != 0 && errno != EEXIST) {
228  throw cms::Exception("DaqDirector") << " Error checking for bu base dir -: "
229  << bu_base_dir_ << " mkdir error:" << strerror(errno) << "\n";
230  }
231 
233  std::string fulockfile = bu_run_dir_ + "/fu.lock";
234  openFULockfileStream(fulockfile, false);
235  }
236 
237  pthread_mutex_init(&init_lock_,NULL);
238 
239  stopFilePath_ = run_dir_+"/CMSSW_STOP";
240  }
241 
243  {
244  if (fulocal_rwlock_fd_!=-1) {
245  unlockFULocal();
246  close(fulocal_rwlock_fd_);
247  }
248 
249  if (fulocal_rwlock_fd2_!=-1) {
250  unlockFULocal2();
251  close(fulocal_rwlock_fd2_);
252  }
253 
254  }
255 
257  {
259  desc.setComment("Service used for file locking arbitration and for propagating information between other EvF components");
260  desc.addUntracked<std::string> ("baseDir", ".")->setComment("Local base directory for run output");
261  desc.addUntracked<std::string> ("buBaseDir", ".")->setComment("BU base ramdisk directory ");
262  desc.addUntracked<unsigned int> ("runNumber",0)->setComment("Run Number in ramdisk to open");
263  desc.addUntracked<bool>("outputAdler32Recheck",false)->setComment("Check Adler32 of per-process output files while micro-merging");
264  desc.addUntracked<bool>("requireTransfersPSet",false)->setComment("Require complete transferSystem PSet in the process configuration");
265  desc.addUntracked<std::string>("selectedTransferMode","")->setComment("Selected transfer mode (choice in Lvl0 propagated as Python parameter");
266  desc.addUntracked<unsigned int>("fuLockPollInterval",2000)->setComment("Lock polling interval in microseconds for the input directory file lock");
267  desc.addUntracked<bool>("emptyLumisectionMode",false)->setComment("Enables writing stream output metadata even when no events are processed in a lumisection");
268  desc.setAllowAnything();
269  descriptions.add("EvFDaqDirector", desc);
270  }
271 
272  void EvFDaqDirector::postEndRun(edm::GlobalContext const& globalContext) {
273  close(bu_readlock_fd_);
274  close(bu_writelock_fd_);
275  if (directorBu_) {
276  std::string filename = bu_run_dir_ + "/bu.lock";
277  removeFile(filename);
278  }
279  }
280 
282 
283  for (unsigned int i=0;i<bounds.maxNumberOfStreams();i++){
284  streamFileTracker_.push_back(-1);
285  }
286  nThreads_=bounds.maxNumberOfStreams();
287  nStreams_=bounds.maxNumberOfThreads();
288  }
289 
291  edm::ProcessContext const& pc) {
293  }
294 
295  void EvFDaqDirector::preBeginRun(edm::GlobalContext const& globalContext) {
296 
297  //assert(run_ == id.run());
298 
299  // check if the requested run is the latest one - issue a warning if it isn't
301  edm::LogWarning("EvFDaqDirector") << "WARNING - checking run dir -: "
302  << run_dir_ << ". This is not the highest run "
304  }
305  }
306 
308  {
309  //delete all files belonging to just closed lumi
310  unsigned int ls = globalContext.luminosityBlockID().luminosityBlock();
312  edm::LogWarning("EvFDaqDirector") << " Handles to check for files to delete were not set by the input source...";
313  return;
314  }
315 
316  std::unique_lock<std::mutex> lkw(*fileDeleteLockPtr_);
317  auto it = filesToDeletePtr_->begin();
318  while (it!=filesToDeletePtr_->end()) {
319  if (it->second->lumi_ == ls) {
320  const boost::filesystem::path filePath(it->second->fileName_);
321  LogDebug("EvFDaqDirector") << "Deleting input file -:" << it->second->fileName_;
322  try {
323  //rarely this fails but file gets deleted
324  boost::filesystem::remove(filePath);
325  }
326  catch (const boost::filesystem::filesystem_error& ex)
327  {
328  edm::LogError("EvFDaqDirector") << " - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() << ". Trying again.";
329  usleep(10000);
330  try {
331  boost::filesystem::remove(filePath);
332  }
333  catch (const boost::filesystem::filesystem_error&) {/*file gets deleted first time but exception is still thrown*/}
334  }
335  catch (std::exception& ex)
336  {
337  edm::LogError("EvFDaqDirector") << " - deleteFile std::exception CAUGHT -: " << ex.what() << ". Trying again.";
338  usleep(10000);
339  try {
340  boost::filesystem::remove(filePath);
341  } catch (std::exception&) {/*file gets deleted first time but exception is still thrown*/}
342  }
343 
344  delete it->second;
345  it = filesToDeletePtr_->erase(it);
346  }
347  else it++;
348  }
349  }
350 
351  inline void EvFDaqDirector::preSourceEvent(edm::StreamID const& streamID) {
353  }
354 
355 
356  std::string EvFDaqDirector::getInputJsonFilePath(const unsigned int ls, const unsigned int index) const {
357  return bu_run_dir_ + "/" + fffnaming::inputJsonFileName(run_,ls,index);
358  }
359 
360 
361  std::string EvFDaqDirector::getRawFilePath(const unsigned int ls, const unsigned int index) const {
362  return bu_run_dir_ + "/" + fffnaming::inputRawFileName(run_,ls,index);
363  }
364 
365  std::string EvFDaqDirector::getOpenRawFilePath(const unsigned int ls, const unsigned int index) const {
366  return bu_run_dir_ + "/open/" + fffnaming::inputRawFileName(run_,ls,index);
367  }
368 
369  std::string EvFDaqDirector::getOpenInputJsonFilePath(const unsigned int ls, const unsigned int index) const {
370  return bu_run_dir_ + "/open/" + fffnaming::inputJsonFileName(run_,ls,index);
371  }
372 
373  std::string EvFDaqDirector::getOpenDatFilePath(const unsigned int ls, std::string const& stream) const {
374  return run_dir_ + "/open/" + fffnaming::streamerDataFileNameWithPid(run_,ls,stream);
375  }
376 
377  std::string EvFDaqDirector::getOpenOutputJsonFilePath(const unsigned int ls, std::string const& stream) const {
378  return run_dir_ + "/open/" + fffnaming::streamerJsonFileNameWithPid(run_,ls,stream);
379  }
380 
381  std::string EvFDaqDirector::getOutputJsonFilePath(const unsigned int ls, std::string const& stream) const {
382  return run_dir_ + "/" + fffnaming::streamerJsonFileNameWithPid(run_,ls,stream);
383  }
384 
385  std::string EvFDaqDirector::getMergedDatFilePath(const unsigned int ls, std::string const& stream) const {
387  }
388 
389  std::string EvFDaqDirector::getMergedDatChecksumFilePath(const unsigned int ls, std::string const& stream) const {
391  }
392 
394  return run_dir_ + "/open/" + fffnaming::initFileNameWithPid(run_,0,stream);
395  }
396 
398  return run_dir_ + "/" + fffnaming::initFileNameWithPid(run_,0,stream);
399  }
400 
402  return run_dir_ + "/open/" + fffnaming::protocolBufferHistogramFileNameWithPid(run_,ls,stream);
403  }
404 
407  }
408 
411  }
412 
413  std::string EvFDaqDirector::getOpenRootHistogramFilePath(const unsigned int ls, std::string const& stream) const {
414  return run_dir_ + "/open/" + fffnaming::rootHistogramFileNameWithPid(run_,ls,stream);
415  }
416 
417  std::string EvFDaqDirector::getRootHistogramFilePath(const unsigned int ls, std::string const& stream) const {
418  return run_dir_ + "/" + fffnaming::rootHistogramFileNameWithPid(run_,ls,stream);
419  }
420 
423  }
424 
426  return bu_run_dir_ + "/" + fffnaming::eolsFileName(run_,ls);
427  }
428 
430  return run_dir_ + "/" + fffnaming::eolsFileName(run_,ls);
431  }
432 
434  return run_dir_ + "/" + fffnaming::bolsFileName(run_,ls);
435  }
436 
438  return bu_run_dir_ + "/" + fffnaming::eorFileName(run_);
439  }
440 
441 
443  return run_dir_ + "/" + fffnaming::eorFileName(run_);
444  }
445 
447  int retval = remove(filename.c_str());
448  if (retval != 0)
449  edm::LogError("EvFDaqDirector") << "Could not remove used file -: " << filename << ". error = "
450  << strerror(errno);
451  }
452 
453  void EvFDaqDirector::removeFile(unsigned int ls, unsigned int index) {
454  removeFile(getRawFilePath(ls,index));
455  }
456 
457  EvFDaqDirector::FileStatus EvFDaqDirector::updateFuLock(unsigned int& ls, std::string& nextFile, uint32_t& fsize, uint64_t& lockWaitTime) {
458  EvFDaqDirector::FileStatus fileStatus = noFile;
459 
460  int retval = -1;
461  int lock_attempts = 0;
462 
463  struct stat buf;
464  int stopFileLS = -1;
465  if (stat(stopFilePath_.c_str(),&buf)==0) {
466  stopFileLS = readLastLSEntry(stopFilePath_);
467  edm::LogWarning("EvFDaqDirector") << "Detected stop request from hltd. Ending run for this process after LS -: " << stopFileLS;
468  //return runEnded;
469  }
470 
471  timeval ts_lockbegin;
472  gettimeofday(&ts_lockbegin,0);
473 
474  while (retval==-1) {
475  retval = fcntl(fu_readwritelock_fd_, F_SETLK, &fu_rw_flk);
476  if (retval==-1) usleep(fuLockPollInterval_);
477  else continue;
478 
479  lock_attempts+=fuLockPollInterval_;
480  if (lock_attempts>5000000 || errno==116) {
481  if (errno==116)
482  edm::LogWarning("EvFDaqDirector") << "Stale lock file handle. Checking if run directory and fu.lock file are present" << std::endl;
483  else
484  edm::LogWarning("EvFDaqDirector") << "Unable to obtain a lock for 5 seconds. Checking if run directory and fu.lock file are present -: errno "
485  << errno <<":"<< strerror(errno) << std::endl;
486 
487 
488  if (stat(getEoLSFilePathOnFU(ls).c_str(),&buf)==0) {
489  edm::LogWarning("EvFDaqDirector") << "Detected local EoLS for lumisection "<< ls ;
490  ls++;
491  return noFile;
492  }
493 
494  if (stat(bu_run_dir_.c_str(), &buf)!=0) return runEnded;
495  if (stat((bu_run_dir_+"/fu.lock").c_str(), &buf)!=0) return runEnded;
496  lock_attempts=0;
497  }
498  }
499 
500  timeval ts_lockend;
501  gettimeofday(&ts_lockend,0);
502  long deltat = (ts_lockend.tv_usec-ts_lockbegin.tv_usec) + (ts_lockend.tv_sec-ts_lockbegin.tv_sec)*1000000;
503  if (deltat>0.) lockWaitTime=deltat;
504 
505 
506 
507  if(retval!=0) return fileStatus;
508 
509 #ifdef DEBUG
510  timeval ts_lockend;
511  gettimeofday(&ts_lockend,0);
512 #endif
513 
514  // if the stream is readable
515  if (fu_rw_lock_stream != 0) {
516  unsigned int readLs, readIndex;
517  int check = 0;
518  // rewind the stream
519  check = fseek(fu_rw_lock_stream, 0, SEEK_SET);
520  // if rewinded ok
521  if (check == 0) {
522  // read its' values
523  fscanf(fu_rw_lock_stream, "%u %u", &readLs, &readIndex);
524  edm::LogInfo("EvFDaqDirector") << "Read fu.lock file file -: " << readLs << ":" << readIndex;
525 
526  unsigned int currentLs = readLs;
527  bool bumpedOk = false;
528  //if next lumisection in a lock file is not +1 wrt. source, cycle through the next empty one, unless initial lumi not yet set
529  //no lock file write in this case
530  if (ls && ls+1 < currentLs) ls++;
531  else {
532  // try to bump (look for new index or EoLS file)
533  bumpedOk = bumpFile(readLs, readIndex, nextFile, fsize, stopFileLS);
534  //avoid 2 lumisections jump
535  if (ls && readLs>currentLs && currentLs > ls) {
536  ls++;
537  readLs=currentLs=ls;
538  readIndex=0;
539  bumpedOk=false;
540  //no write to lock file
541  }
542  else {
543  if (ls==0 && readLs>currentLs) {
544  //make sure to intialize always with LS found in the lock file, with possibility of grabbing index file immediately
545  //in this case there is no new file in the same LS
546  readLs=currentLs;
547  readIndex=0;
548  bumpedOk=false;
549  //no write to lock file
550  }
551  //update return LS value
552  ls = readLs;
553  }
554  }
555  if (bumpedOk) {
556  // there is a new index file to grab, lock file needs to be updated
557  check = fseek(fu_rw_lock_stream, 0, SEEK_SET);
558  if (check == 0) {
559  ftruncate(fu_readwritelock_fd_, 0);
560  // write next index in the file, which is the file the next process should take
561  fprintf(fu_rw_lock_stream, "%u %u", readLs, readIndex + 1);
562  fflush(fu_rw_lock_stream);
563  fsync(fu_readwritelock_fd_);
564  fileStatus = newFile;
565  LogDebug("EvFDaqDirector") << "Written to file -: " << readLs << ":" << readIndex + 1;
566  }
567  else {
568  throw cms::Exception("EvFDaqDirector") << "seek on fu read/write lock for updating failed with error " << strerror(errno);
569  }
570  }
571  else if (currentLs < readLs) {
572  //there is no new file in next LS (yet), but lock file can be updated to the next LS
573  check = fseek(fu_rw_lock_stream, 0, SEEK_SET);
574  if (check == 0) {
575  ftruncate(fu_readwritelock_fd_, 0);
576  // in this case LS was bumped, but no new file. Thus readIndex is 0 (set by bumpFile)
577  fprintf(fu_rw_lock_stream, "%u %u", readLs, readIndex);
578  fflush(fu_rw_lock_stream);
579  fsync(fu_readwritelock_fd_);
580  LogDebug("EvFDaqDirector") << "Written to file -: " << readLs << ":" << readIndex;
581  }
582  else {
583  throw cms::Exception("EvFDaqDirector") << "seek on fu read/write lock for updating failed with error " << strerror(errno);
584  }
585  }
586  } else {
587  edm::LogError("EvFDaqDirector") << "seek on fu read/write lock for reading failed with error " << strerror(errno);
588  }
589  } else {
590  edm::LogError("EvFDaqDirector") << "fu read/write lock stream is invalid " << strerror(errno);
591  }
592 
593 #ifdef DEBUG
594  timeval ts_preunlock;
595  gettimeofday(&ts_preunlock,0);
596  int locked_period_int = ts_preunlock.tv_sec - ts_lockend.tv_sec;
597  double locked_period=locked_period_int+double(ts_preunlock.tv_usec - ts_lockend.tv_usec)/1000000;
598 #endif
599 
600  //if new json is present, lock file which FedRawDataInputSource will later unlock
601  if (fileStatus==newFile) lockFULocal();
602 
603  //release lock at this point
604  int retvalu=-1;
605  retvalu=fcntl(fu_readwritelock_fd_, F_SETLKW, &fu_rw_fulk);
606  if (retvalu==-1) edm::LogError("EvFDaqDirector") << "Error unlocking the fu.lock " << strerror(errno);
607 
608 #ifdef DEBUG
609  edm::LogDebug("EvFDaqDirector") << "Waited during lock -: " << locked_period << " seconds";
610 #endif
611 
612  if ( fileStatus == noFile ) {
613  struct stat buf;
614  //edm::LogInfo("EvFDaqDirector") << " looking for EoR file: " << getEoRFilePath().c_str();
615  if ( stat(getEoRFilePath().c_str(), &buf) == 0 || stat(bu_run_dir_.c_str(), &buf)!=0)
616  fileStatus = runEnded;
617  if (stopFileLS>=0 && (int)ls > stopFileLS) {
618  edm::LogInfo("EvFDaqDirector") << "Reached maximum lumisection set by hltd";
619  fileStatus = runEnded;
620  }
621  }
622  return fileStatus;
623  }
624 
626 
627  boost::filesystem::ifstream ij(BUEoLSFile);
628  Json::Value deserializeRoot;
630 
631  if (!reader.parse(ij, deserializeRoot)) {
632  edm::LogError("EvFDaqDirector") << "Cannot deserialize input JSON file -:" << BUEoLSFile;
633  return -1;
634  }
635 
637  DataPoint dp;
638  dp.deserialize(deserializeRoot);
639 
640  //read definition
641  if (readEolsDefinition_) {
642  //std::string def = boost::algorithm::trim(dp.getDefinition());
644  if (!def.size()) readEolsDefinition_=false;
645  while (def.size()) {
646  std::string fullpath;
647  if (def.find('/')==0)
648  fullpath = def;
649  else
650  fullpath = bu_run_dir_+'/'+def;
651  struct stat buf;
652  if (stat(fullpath.c_str(), &buf) == 0) {
653  DataPointDefinition eolsDpd;
654  std::string defLabel = "legend";
655  DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
656  if (eolsDpd.getNames().size()==0) {
657  //try with "data" label if "legend" format is not used
658  eolsDpd = DataPointDefinition();
659  defLabel="data";
660  DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
661  }
662  for (unsigned int i=0;i<eolsDpd.getNames().size();i++)
663  if (eolsDpd.getNames().at(i)=="NFiles")
665  readEolsDefinition_=false;
666  break;
667  }
668  //check if we can still find definition
669  if (def.size()<=1 || def.find('/')==std::string::npos) {
670  readEolsDefinition_=false;
671  break;
672  }
673  def = def.substr(def.find('/')+1);
674  }
675  }
676 
677  if (dp.getData().size()>eolsNFilesIndex_)
678  data = dp.getData()[eolsNFilesIndex_];
679  else {
680  edm::LogError("EvFDaqDirector") << " error reading number of files from BU JSON -: " << BUEoLSFile;
681  return -1;
682  }
683  return boost::lexical_cast<int>(data);
684  }
685 
686  bool EvFDaqDirector::bumpFile(unsigned int& ls, unsigned int& index, std::string& nextFile, uint32_t& fsize, int maxLS) {
687 
688  if (previousFileSize_ != 0) {
689  if (!fms_) {
691  }
693  previousFileSize_ = 0;
694  }
695 
696  //reached limit
697  if (maxLS>=0 && ls > (unsigned int)maxLS) return false;
698 
699  struct stat buf;
700  std::stringstream ss;
701  unsigned int nextIndex = index;
702  nextIndex++;
703 
704  // 1. Check suggested file
705  nextFile = getInputJsonFilePath(ls,index);
706  if (stat(nextFile.c_str(), &buf) == 0) {
707 
708  previousFileSize_ = buf.st_size;
709  fsize = buf.st_size;
710  return true;
711  }
712  // 2. No file -> lumi ended? (and how many?)
713  else {
714  std::string BUEoLSFile = getEoLSFilePathOnBU(ls);
715  bool eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
716  while (eolFound) {
717 
718  // recheck that no raw file appeared in the meantime
719  if (stat(nextFile.c_str(), &buf) == 0) {
720  previousFileSize_ = buf.st_size;
721  fsize = buf.st_size;
722  return true;
723  }
724 
725  int indexFilesInLS = getNFilesFromEoLS(BUEoLSFile);
726  if (indexFilesInLS < 0)
727  //parsing failed
728  return false;
729  else {
730  //check index
731  if ((int)index<indexFilesInLS) {
732  //we have 2 files, and check for 1 failed... retry (2 will never be here)
733  edm::LogError("EvFDaqDirector") << "Potential miss of index file in LS -: " << ls << ". Missing "
734  << nextFile << " because " << indexFilesInLS-1 << " is the highest index expected. Will not update fu.lock file";
735  return false;
736  }
737  }
738  // this lumi ended, check for files
739  ++ls;
740  index = 0;
741 
742  //reached limit
743  if (maxLS>=0 && ls > (unsigned int)maxLS) return false;
744 
745  nextFile = getInputJsonFilePath(ls,0);
746  if (stat(nextFile.c_str(), &buf) == 0) {
747  // a new file was found at new lumisection, index 0
748  previousFileSize_ = buf.st_size;
749  fsize = buf.st_size;
750  return true;
751  }
752  else {
753  //change of policy: we need to cycle through each LS
754  return false;
755  }
756  BUEoLSFile = getEoLSFilePathOnBU(ls);
757  eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
758  }
759  }
760  // no new file found
761  return false;
762  }
763 
765  if (fu_rw_lock_stream == 0)
766  edm::LogError("EvFDaqDirector") << "Error creating fu read/write lock stream "
767  << strerror(errno);
768  else {
769  edm::LogInfo("EvFDaqDirector") << "Initializing FU LOCK FILE";
770  unsigned int readLs = 1, readIndex = 0;
771  fprintf(fu_rw_lock_stream, "%u %u", readLs, readIndex);
772  }
773  }
774 
776  if (create) {
777  fu_readwritelock_fd_ = open(fulockfile.c_str(), O_RDWR | O_CREAT,
778  S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
779  chmod(fulockfile.c_str(),0766);
780  } else {
781  fu_readwritelock_fd_ = open(fulockfile.c_str(), O_RDWR, S_IRWXU);
782  }
783  if (fu_readwritelock_fd_ == -1)
784  edm::LogError("EvFDaqDirector") << "problem with creating filedesc for fuwritelock -: " << fulockfile.c_str()
785  << " create:" << create << " error:" << strerror(errno);
786  else
787  LogDebug("EvFDaqDirector") << "creating filedesc for fureadwritelock -: "
789 
790  fu_rw_lock_stream = fdopen(fu_readwritelock_fd_, "r+");
791  }
792 
793  //create if does not exist then lock the merge destination file
795  data_rw_stream = fopen(getMergedDatFilePath(ls,stream).c_str(), "a"); //open stream for appending
797  if (data_readwrite_fd_ == -1)
798  edm::LogError("EvFDaqDirector") << "problem with creating filedesc for datamerge "
799  << strerror(errno);
800  else
801  LogDebug("EvFDaqDirector") << "creating filedesc for datamerge -: "
803  fcntl(data_readwrite_fd_, F_SETLKW, &data_rw_flk);
804 
805  return data_rw_stream;
806  }
807 
809  fflush(data_rw_stream);
810  fcntl(data_readwrite_fd_, F_SETLKW, &data_rw_fulk);
811  fclose(data_rw_stream);
812  }
813 
815  pthread_mutex_lock(&init_lock_);
816  }
817 
819  pthread_mutex_unlock(&init_lock_);
820  }
821 
823  //fcntl(fulocal_rwlock_fd_, F_SETLKW, &fulocal_rw_flk);
824  flock(fulocal_rwlock_fd_,LOCK_SH);
825  }
826 
828  //fcntl(fulocal_rwlock_fd_, F_SETLKW, &fulocal_rw_fulk);
829  flock(fulocal_rwlock_fd_,LOCK_UN);
830  }
831 
832 
834  //fcntl(fulocal_rwlock_fd2_, F_SETLKW, &fulocal_rw_flk2);
835  flock(fulocal_rwlock_fd2_,LOCK_EX);
836  }
837 
839  //fcntl(fulocal_rwlock_fd2_, F_SETLKW, &fulocal_rw_fulk2);
840  flock(fulocal_rwlock_fd2_,LOCK_UN);
841  }
842 
843 
845  // create open dir if not already there
846 
848  if (!boost::filesystem::is_directory(openPath)) {
849  LogDebug("EvFDaqDirector") << "<open> FU dir not found. Creating... -:" << openPath.string();
850  boost::filesystem::create_directories(openPath);
851  }
852  }
853 
854 
856 
857  boost::filesystem::ifstream ij(file);
858  Json::Value deserializeRoot;
860 
861  if (!reader.parse(ij, deserializeRoot)) {
862  edm::LogError("EvFDaqDirector") << "Cannot deserialize input JSON file -:" << file;
863  return -1;
864  }
865 
866  int ret = deserializeRoot.get("lastLS","").asInt();
867  return ret;
868 
869  }
870 
871  //if transferSystem PSet is present in the menu, we require it to be complete and consistent for all specified streams
873  {
874  if(transferSystemJson_) return;
875 
876  transferSystemJson_.reset(new Json::Value);
878  if (topPset.existsAs<edm::ParameterSet>("transferSystem",true))
879  {
880  const edm::ParameterSet& tsPset(topPset.getParameterSet("transferSystem"));
881 
882  Json::Value destinationsVal(Json::arrayValue);
883  std::vector<std::string> destinations = tsPset.getParameter<std::vector<std::string>>("destinations");
884  for (auto & dest: destinations) destinationsVal.append(dest);
885  (*transferSystemJson_)["destinations"]=destinationsVal;
886 
887  Json::Value modesVal(Json::arrayValue);
888  std::vector<std::string> modes = tsPset.getParameter< std::vector<std::string> >("transferModes");
889  for (auto & mode: modes) modesVal.append(mode);
890  (*transferSystemJson_)["transferModes"]=modesVal;
891 
892  for (auto psKeyItr =tsPset.psetTable().begin();psKeyItr!=tsPset.psetTable().end(); ++ psKeyItr) {
893  if (psKeyItr->first!="destinations" && psKeyItr->first!="transferModes") {
894  const edm::ParameterSet & streamDef = tsPset.getParameterSet(psKeyItr->first);
895  Json::Value streamVal;
896  for (auto & mode : modes) {
897  //validation
898  if (!streamDef.existsAs<std::vector<std::string>>(mode,true))
899  throw cms::Exception("EvFDaqDirector") << " Missing transfer system specification for -:" << psKeyItr->first << " (transferMode " << mode << ")";
900  std::vector<std::string> streamDestinations = streamDef.getParameter<std::vector<std::string>>(mode);
901 
902  Json::Value sDestsValue(Json::arrayValue);
903 
904  if (!streamDestinations.size())
905  throw cms::Exception("EvFDaqDirector") << " Missing transter system destination(s) for -: "<< psKeyItr->first << ", mode:" << mode;
906 
907  for (auto & sdest:streamDestinations) {
908  bool sDestValid=false;
909  sDestsValue.append(sdest);
910  for (auto & dest: destinations) {
911  if (dest==sdest) sDestValid=true;
912  }
913  if (!sDestValid)
914  throw cms::Exception("EvFDaqDirector") << " Invalid transter system destination specified for -: "<< psKeyItr->first << ", mode:" << mode << ", dest:"<<sdest;
915  }
916  streamVal[mode]=sDestsValue;
917  }
918  (*transferSystemJson_)[psKeyItr->first] = streamVal;
919  }
920  }
921  }
922  else {
923  if (requireTSPSet_)
924  throw cms::Exception("EvFDaqDirector") << "transferSystem PSet not found";
925  }
926  }
927 
929  {
930  std::string streamRequestName;
931  if (transferSystemJson_->isMember(stream.c_str()))
932  streamRequestName = stream;
933  else {
934  std::stringstream msg;
935  msg << "Transfer system mode definitions missing for -: " << stream;
936  if (requireTSPSet_)
937  throw cms::Exception("EvFDaqDirector") << msg.str();
938  else {
939  edm::LogWarning("EvFDaqDirector") << msg.str() << " (permissive mode)";
940  return std::string("Failsafe");
941  }
942  }
943  //return empty if strict check parameter is not on
945  edm::LogWarning("EvFDaqDirector") << "Selected mode string is not provided as DaqDirector parameter."
946  << "Switch on requireTSPSet parameter to enforce this requirement. Setting mode to empty string.";
947  return std::string("Failsafe");
948  }
950  throw cms::Exception("EvFDaqDirector") << "Selected mode string is not provided as DaqDirector parameter.";
951  }
952  //check if stream has properly listed transfer stream
953  if (!transferSystemJson_->get(streamRequestName, "").isMember(selectedTransferMode_.c_str()))
954  {
955  std::stringstream msg;
956  msg << "Selected transfer mode " << selectedTransferMode_ << " is not specified for stream " << streamRequestName;
957  if (requireTSPSet_)
958  throw cms::Exception("EvFDaqDirector") << msg.str();
959  else
960  edm::LogWarning("EvFDaqDirector") << msg.str() << " (permissive mode)";
961  return std::string("Failsafe");
962  }
963  Json::Value destsVec = transferSystemJson_->get(streamRequestName, "").get(selectedTransferMode_,"");
964 
965  //flatten string json::Array into CSV std::string
967  for (Json::Value::iterator it = destsVec.begin(); it!=destsVec.end(); it++)
968  {
969  if (ret!="") ret +=",";
970  ret+=(*it).asString();
971  }
972  return ret;
973  }
974 
976  std::string proc_flag = run_dir_ + "/processing";
977  int proc_flag_fd = open(proc_flag.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
978  close(proc_flag_fd);
979  }
980 
981 }
#define LogDebug(id)
unsigned int nThreads_
type
Definition: HCALResponse.h:21
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:46
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()
Definition: DataPoint.h:58
int i
Definition: DBlmapReader.cc:9
struct flock fu_rw_flk
tuple ret
prodAgent to be discontinued
std::string run_string_
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.
Definition: dqm_diff.py:58
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 &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
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
Definition: GlobalContext.h:52
def ls
Definition: eostools.py:348
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.
#define NULL
Definition: scimark2.h:8
struct flock data_rw_flk
void createProcessingNotificationMaybe() const
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
Represents a JSON value.
Definition: value.h:111
std::string getEoLSFilePathOnBU(const unsigned int ls) const
std::string getEoRFilePath() const
unsigned long previousFileSize_
ParameterSetID const & parameterSetID() const
def chmod
Definition: eostools.py:293
struct flock fu_rw_fulk
unsigned int maxNumberOfStreams() const
Definition: SystemBounds.h:43
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)
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
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)
std::string bu_base_dir_
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)
Definition: DataPoint.cc:56
unsigned int eolsNFilesIndex_
void preBeginJob(edm::PathsAndConsumesOfModulesBase const &, edm::ProcessContext const &)
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)
Int asInt() 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
Definition: Time.h:15
ParameterSet const & getParameterSet(std::string const &) const
void preGlobalEndLumi(edm::GlobalContext const &globalContext)
auto dp
Definition: deltaR.h:22
tuple pid
Definition: sysUtil.py:22
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::string & getDefinition()
Definition: DataPoint.h:59
evf::FastMonitoringService * fms_
void watchPreBeginJob(PreBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
std::string bu_run_dir_
std::string rootHistogramFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
def mkdir
Definition: eostools.py:250
std::string findHighestRunDir()
Definition: DirManager.cc:20
std::string getBoLSFilePathOnFU(const unsigned int ls) const
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void postEndRun(edm::GlobalContext const &globalContext)
tuple filename
Definition: lut2db_cfg.py:20
Unserialize a JSON document into a Value.
Definition: reader.h:16
tuple destinations
Definition: gather_cfg.py:144
const_iterator end() const
void checkTransferSystemPSet(edm::ProcessContext const &pc)
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.
Definition: value.h:1007
std::string getInputJsonFilePath(const unsigned int ls, const unsigned int index) const
std::string getRunOpenDirPath() const
JetCorrectorParameters::Definitions def
Definition: classes.h:6
std::string protocolBufferHistogramFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
unsigned int fuLockPollInterval_
SurfaceDeformation * create(int type, const std::vector< double > &params)
std::string eolsFileName(const unsigned int run, const unsigned int ls)
unsigned int nStreams_
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)
Definition: value.h:31