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.
7 
13 
14 #include <iostream>
15 #include <sstream>
16 #include <sys/time.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <sys/file.h>
20 #include <boost/lexical_cast.hpp>
21 #include <boost/filesystem/fstream.hpp>
22 
23 //#define DEBUG
24 
25 namespace evf {
26 
27  namespace {
28  struct flock make_flock(short type, short whence, off_t start, off_t len, pid_t pid)
29  {
30 #ifdef __APPLE__
31  return {start, len, pid, type, whence};
32 #else
33  return {type, whence, start, len, pid};
34 #endif
35  }
36  }
37 
39  edm::ActivityRegistry& reg) :
40  testModeNoBuilderUnit_(
41  pset.getUntrackedParameter<bool> ("testModeNoBuilderUnit",
42  false)
43  ),
44  base_dir_(
45  pset.getUntrackedParameter<std::string> ("baseDir", "/data")
46  ),
47  bu_base_dir_(
48  pset.getUntrackedParameter<std::string> ("buBaseDir", "/data")
49  ),
50  directorBu_(
51  pset.getUntrackedParameter<bool> ("directorIsBu", false)
52  ),
53  run_(pset.getUntrackedParameter<unsigned int> ("runNumber",0)),
54  outputAdler32Recheck_(pset.getUntrackedParameter<bool>("outputAdler32Recheck",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  jumpLS_(0),
74  jumpIndex_(0),
75 
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() ))
84  //fulocal_rw_flk( make_flock( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
85  //fulocal_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() )),
86  //fulocal_rw_flk2( make_flock( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
87  //fulocal_rw_fulk2( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() ))
88  {
89 
95 
96  std::stringstream ss;
97  ss << "run" << std::setfill('0') << std::setw(6) << run_;
98  run_string_ = ss.str();
100 
101  //save hostname for later
102  char hostname[33];
103  gethostname(hostname,32);
104  hostname_ = hostname;
105  // check if base dir exists or create it accordingly
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);
110  }
111 
112  //create run dir in base dir
113  umask(0);
114  retval = mkdir(run_dir_.c_str(),
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);
119  }
120 
121  //create fu-local.lock in run open dir
122  if (!directorBu_) {
123 
125  std::string fulocal_lock_ = getRunOpenDirPath() +"/fu-local.lock";
126  fulocal_rwlock_fd_ = open(fulocal_lock_.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);//O_RDWR?
127  if (fulocal_rwlock_fd_==-1)
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);
130  fsync(fulocal_rwlock_fd_);
131  //open second fd for another input source thread
132  fulocal_rwlock_fd2_ = open(fulocal_lock_.c_str(), O_RDWR, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);//O_RDWR?
133  if (fulocal_rwlock_fd2_==-1)
134  throw cms::Exception("DaqDirector") << " Error opening a local lock file -: " << fulocal_lock_.c_str() << " : " << strerror(errno);
135  }
136 
137  //bu_run_dir: for FU, for which the base dir is local and the BU is remote, it is expected to be there
138  //for BU, it is created at this point
139  if (directorBu_)
140  {
142  std::string bulockfile = bu_run_dir_ + "/bu.lock";
143  std::string fulockfile = bu_run_dir_ + "/fu.lock";
144 
145  //make or find bu run dir
146  retval = mkdir(bu_run_dir_.c_str(),
147  S_IRWXU | S_IRWXG | S_IRWXO);
148  if (retval != 0 && errno != EEXIST) {
149  throw cms::Exception("DaqDirector")
150  << " Error creating bu run dir -: " << bu_run_dir_
151  << " mkdir error:" << strerror(errno) << "\n";
152  }
153  bu_run_open_dir_ = bu_run_dir_ + "/open";
154  retval = mkdir(bu_run_open_dir_.c_str(),
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 -: "
158  << bu_run_open_dir_ << " mkdir error:" << strerror(errno)
159  << "\n";
160  }
161 
162  // the BU director does not need to know about the fu lock
163  bu_writelock_fd_ = open(bulockfile.c_str(),
164  O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
165  if (bu_writelock_fd_ == -1)
166  edm::LogWarning("EvFDaqDirector") << "problem with creating filedesc for buwritelock -: "
167  << strerror(errno);
168  else
169  edm::LogInfo("EvFDaqDirector") << "creating filedesc for buwritelock -: "
170  << bu_writelock_fd_;
171  bu_w_lock_stream = fdopen(bu_writelock_fd_, "w");
172  if (bu_w_lock_stream == 0)
173  edm::LogWarning("EvFDaqDirector")<< "Error creating write lock stream -: " << strerror(errno);
174 
175  // BU INITIALIZES LOCK FILE
176  // FU LOCK FILE OPEN
177  openFULockfileStream(fulockfile, true);
179  fflush(fu_rw_lock_stream);
180  close(fu_readwritelock_fd_);
181  }
182  else
183  {
184  // for FU, check if bu base dir exists
185 
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";
190  }
191 
193  std::string fulockfile = bu_run_dir_ + "/fu.lock";
194  openFULockfileStream(fulockfile, false);
195  }
196 
197  pthread_mutex_init(&init_lock_,NULL);
198 
199  stopFilePath_ = run_dir_+"/CMSSW_STOP";
200  }
201 
203  {
204  if (fulocal_rwlock_fd_!=-1) {
205  unlockFULocal();
206  close(fulocal_rwlock_fd_);
207  }
208 
209  if (fulocal_rwlock_fd2_!=-1) {
210  unlockFULocal2();
211  close(fulocal_rwlock_fd2_);
212  }
213 
214  }
215 
216  void EvFDaqDirector::postEndRun(edm::GlobalContext const& globalContext) {
217  close(bu_readlock_fd_);
218  close(bu_writelock_fd_);
219  if (directorBu_) {
220  std::string filename = bu_run_dir_ + "/bu.lock";
221  removeFile(filename);
222  }
223  }
224 
226 
227  for (unsigned int i=0;i<bounds.maxNumberOfStreams();i++){
228  streamFileTracker_.push_back(-1);
229  }
230  nThreads_=bounds.maxNumberOfStreams();
231  nStreams_=bounds.maxNumberOfThreads();
232  }
233 
234  void EvFDaqDirector::preBeginRun(edm::GlobalContext const& globalContext) {
235 
236 // assert(run_ == id.run());
237 
238  // check if the requested run is the latest one - issue a warning if it isn't
240  edm::LogWarning("EvFDaqDirector") << "WARNING - checking run dir -: "
241  << run_dir_ << ". This is not the highest run "
243  }
244  }
245 
247  {
248  //delete all files belonging to just closed lumi
249  unsigned int ls = globalContext.luminosityBlockID().luminosityBlock();
251  edm::LogWarning("EvFDaqDirector") << " Handles to check for files to delete were not set by the input source...";
252  return;
253  }
254 
255  std::unique_lock<std::mutex> lkw(*fileDeleteLockPtr_);
256  auto it = filesToDeletePtr_->begin();
257  while (it!=filesToDeletePtr_->end()) {
258  if (it->second->lumi_ == ls) {
259  const boost::filesystem::path filePath(it->second->fileName_);
260  LogDebug("EvFDaqDirector") << "Deleting input file -:" << it->second->fileName_;
261  try {
262  //rarely this fails but file gets deleted
263  boost::filesystem::remove(filePath);
264  }
265  catch (const boost::filesystem::filesystem_error& ex)
266  {
267  edm::LogError("EvFDaqDirector") << " - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() << ". Trying again.";
268  usleep(10000);
269  try {
270  boost::filesystem::remove(filePath);
271  }
272  catch (...) {/*file gets deleted first time but exception is still thrown*/}
273  }
274  catch (std::exception& ex)
275  {
276  edm::LogError("EvFDaqDirector") << " - deleteFile std::exception CAUGHT -: " << ex.what() << ". Trying again.";
277  usleep(10000);
278  try {
279  boost::filesystem::remove(filePath);
280  } catch (...) {/*file gets deleted first time but exception is still thrown*/}
281  }
282 
283  delete it->second;
284  it = filesToDeletePtr_->erase(it);
285  }
286  else it++;
287  }
288  }
289 
290  inline void EvFDaqDirector::preSourceEvent(edm::StreamID const& streamID) {
292  }
293 
294 
295  std::string EvFDaqDirector::getInputJsonFilePath(const unsigned int ls, const unsigned int index) const {
296  return bu_run_dir_ + "/" + fffnaming::inputJsonFileName(run_,ls,index);
297  }
298 
299 
300  std::string EvFDaqDirector::getRawFilePath(const unsigned int ls, const unsigned int index) const {
301  return bu_run_dir_ + "/" + fffnaming::inputRawFileName(run_,ls,index);
302  }
303 
304  std::string EvFDaqDirector::getOpenRawFilePath(const unsigned int ls, const unsigned int index) const {
305  return bu_run_dir_ + "/open/" + fffnaming::inputRawFileName(run_,ls,index);
306  }
307 
308  std::string EvFDaqDirector::getOpenInputJsonFilePath(const unsigned int ls, const unsigned int index) const {
309  return bu_run_dir_ + "/open/" + fffnaming::inputJsonFileName(run_,ls,index);
310  }
311 
313  return run_dir_ + "/open/" + fffnaming::streamerDataFileNameWithPid(run_,ls,stream);
314  }
315 
317  return run_dir_ + "/open/" + fffnaming::streamerJsonFileNameWithPid(run_,ls,stream);
318  }
319 
321  return run_dir_ + "/" + fffnaming::streamerJsonFileNameWithPid(run_,ls,stream);
322  }
323 
326  }
327 
330  }
331 
333  return run_dir_ + "/" + fffnaming::initFileNameWithPid(run_,0,stream);
334  }
335 
337  return run_dir_ + "/open/" + fffnaming::protocolBufferHistogramFileNameWithPid(run_,ls,stream);
338  }
339 
342  }
343 
346  }
347 
349  return run_dir_ + "/open/" + fffnaming::rootHistogramFileNameWithPid(run_,ls,stream);
350  }
351 
353  return run_dir_ + "/" + fffnaming::rootHistogramFileNameWithPid(run_,ls,stream);
354  }
355 
358  }
359 
361  return bu_run_dir_ + "/" + fffnaming::eolsFileName(run_,ls);
362  }
363 
365  return run_dir_ + "/" + fffnaming::eolsFileName(run_,ls);
366  }
367 
369  return bu_run_dir_ + "/" + fffnaming::eorFileName(run_);
370  }
371 
372 
374  return run_dir_ + "/" + fffnaming::eorFileName(run_);
375  }
376 
378  int retval = remove(filename.c_str());
379  if (retval != 0)
380  edm::LogError("EvFDaqDirector") << "Could not remove used file -: " << filename << ". error = "
381  << strerror(errno);
382  }
383 
384  void EvFDaqDirector::removeFile(unsigned int ls, unsigned int index) {
385  removeFile(getRawFilePath(ls,index));
386  }
387 
388  EvFDaqDirector::FileStatus EvFDaqDirector::updateFuLock(unsigned int& ls, std::string& nextFile, uint32_t& fsize) {
389  EvFDaqDirector::FileStatus fileStatus = noFile;
390 
391  int retval = -1;
392  int lock_attempts = 0;
393 
394  struct stat buf;
395  int stopFileLS = -1;
396  if (stat(stopFilePath_.c_str(),&buf)==0) {
397  stopFileLS = readLastLSEntry(stopFilePath_);
398  edm::LogWarning("EvFDaqDirector") << "Detected stop request from hltd. Ending run for this process after LS -: " << stopFileLS;
399  //return runEnded;
400  }
401 
402  while (retval==-1) {
403  retval = fcntl(fu_readwritelock_fd_, F_SETLK, &fu_rw_flk);
404  if (retval==-1) usleep(50000);
405  else continue;
406 
407  lock_attempts++;
408  if (lock_attempts>100 || errno==116) {
409  if (errno==116)
410  edm::LogWarning("EvFDaqDirector") << "Stale lock file handle. Checking if run directory and fu.lock file are present" << std::endl;
411  else
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;
413 
414  if (stat(bu_run_dir_.c_str(), &buf)!=0) return runEnded;
415  if (stat((bu_run_dir_+"/fu.lock").c_str(), &buf)!=0) return runEnded;
416  lock_attempts=0;
417  }
418  }
419  if(retval!=0) return fileStatus;
420 
421 #ifdef DEBUG
422  timeval ts_lockend;
423  gettimeofday(&ts_lockend,0);
424 #endif
425 
426  // if the stream is readable
427  if (fu_rw_lock_stream != 0) {
428  unsigned int readLs, readIndex, jumpLs, jumpIndex;
429  int check = 0;
430  // rewind the stream
431  check = fseek(fu_rw_lock_stream, 0, SEEK_SET);
432  // if rewinded ok
433  if (check == 0) {
434  // read its' values
436  fscanf(fu_rw_lock_stream, "%u %u %u %u", &readLs, &readIndex,
437  &jumpLs, &jumpIndex);
438  else {
439  fscanf(fu_rw_lock_stream, "%u %u", &readLs, &readIndex);
440  edm::LogInfo("EvFDaqDirector") << "Read fu.lock file file -: " << readLs << ":" << readIndex;
441  }
442 
443  // try to bump
444  bool bumpedOk = bumpFile(readLs, readIndex, nextFile, fsize, stopFileLS);
445  ls = readLs;
446  // there is a new file to grab or lumisection ended
447  if (bumpedOk) {
448  // rewind and clear
449  check = fseek(fu_rw_lock_stream, 0, SEEK_SET);
450  if (check == 0) {
451  ftruncate(fu_readwritelock_fd_, 0);
452  fflush(fu_rw_lock_stream); //this should not be needed ???
453  } else
454  edm::LogError("EvFDaqDirector") << "seek on fu read/write lock for updating failed with error "
455  << strerror(errno);
456  // write new data
457  check = fseek(fu_rw_lock_stream, 0, SEEK_SET);
458  if (check == 0) {
459  // write next index in the file, which is the file the next process should take
461  fprintf(fu_rw_lock_stream, "%u %u %u %u", readLs,
462  readIndex + 1, readLs + 2, readIndex + 1);
463  jumpLS_ = readLs + 2;
465  } else {
466  fprintf(fu_rw_lock_stream, "%u %u", readLs,
467  readIndex + 1);
468  }
469  fflush(fu_rw_lock_stream);
470  fsync(fu_readwritelock_fd_);
471 
472  fileStatus = newFile;
473 
475  edm::LogInfo("EvFDaqDirector") << "Written to file -: " << readLs << ":"
476  << readIndex + 1 << " --> " << readLs + 2
477  << ":" << readIndex + 1;
478  else
479  LogDebug("EvFDaqDirector") << "Written to file -: " << readLs << ":"
480  << readIndex + 1;
481 
482  } else
483  edm::LogError("EvFDaqDirector") << "seek on fu read/write lock for updating failed with error "
484  << strerror(errno);
485  }
486  } else
487  edm::LogError("EvFDaqDirector") << "seek on fu read/write lock for reading failed with error "
488  << strerror(errno);
489  } else
490  edm::LogError("EvFDaqDirector") << "fu read/write lock stream is invalid " << strerror(errno);
491 
492 #ifdef DEBUG
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;
497 #endif
498 
499  //if new json is present, lock file which FedRawDataInputSource will later unlock
500  if (fileStatus==newFile) lockFULocal();
501 
502  //release lock at this point
503  int retvalu=-1;
504  retvalu=fcntl(fu_readwritelock_fd_, F_SETLKW, &fu_rw_fulk);
505  if (retvalu==-1) edm::LogError("EvFDaqDirector") << "Error unlocking the fu.lock " << strerror(errno);
506 
507 #ifdef DEBUG
508  edm::LogDebug("EvFDaqDirector") << "Waited during lock -: " << locked_period << " seconds";
509 #endif
510 
511  if ( fileStatus == noFile ) {
512  struct stat buf;
513  //edm::LogInfo("EvFDaqDirector") << " looking for EoR file: " << getEoRFilePath().c_str();
514  if ( stat(getEoRFilePath().c_str(), &buf) == 0 || stat(bu_run_dir_.c_str(), &buf)!=0)
515  fileStatus = runEnded;
516  if (stopFileLS>=0 && (int)ls > stopFileLS) {
517  edm::LogInfo("EvFDaqDirector") << "Reached maximum lumisection set by hltd";
518  fileStatus = runEnded;
519  }
520  }
521  return fileStatus;
522  }
523 
525 
526  boost::filesystem::ifstream ij(BUEoLSFile);
527  Json::Value deserializeRoot;
529 
530  if (!reader.parse(ij, deserializeRoot)) {
531  edm::LogError("EvFDaqDirector") << "Cannot deserialize input JSON file -:" << BUEoLSFile;
532  return -1;
533  }
534 
536  DataPoint dp;
537  dp.deserialize(deserializeRoot);
538 
539  //read definition
540  if (readEolsDefinition_) {
541  //std::string def = boost::algorithm::trim(dp.getDefinition());
543  if (!def.size()) readEolsDefinition_=false;
544  while (def.size()) {
545  std::string fullpath;
546  if (def.find('/')==0)
547  fullpath = def;
548  else
549  fullpath = bu_run_dir_+'/'+def;
550  struct stat buf;
551  if (stat(fullpath.c_str(), &buf) == 0) {
552  DataPointDefinition eolsDpd;
553  std::string defLabel = "legend";
554  DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
555  if (eolsDpd.getNames().size()==0) {
556  //try with "data" label if "legend" format is not used
557  eolsDpd = DataPointDefinition();
558  defLabel="data";
559  DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
560  }
561  for (unsigned int i=0;i<eolsDpd.getNames().size();i++)
562  if (eolsDpd.getNames().at(i)=="NFiles")
564  readEolsDefinition_=false;
565  break;
566  }
567  //check if we can still find definition
568  if (def.size()<=1 || def.find('/')==std::string::npos) {
569  readEolsDefinition_=false;
570  break;
571  }
572  def = def.substr(def.find('/')+1);
573  }
574  }
575 
576  if (dp.getData().size()>eolsNFilesIndex_)
577  data = dp.getData()[eolsNFilesIndex_];
578  else {
579  edm::LogError("EvFDaqDirector") << " error reading number of files from BU JSON -: " << BUEoLSFile;
580  return -1;
581  }
582  return boost::lexical_cast<int>(data);
583  }
584 
585  bool EvFDaqDirector::bumpFile(unsigned int& ls, unsigned int& index, std::string& nextFile, uint32_t& fsize, int maxLS) {
586 
587  if (previousFileSize_ != 0) {
588  if (!fms_) {
589  try {
591  } catch (...) {
592  edm::LogError("EvFDaqDirector") <<" FastMonitoringService not found";
593  }
594  }
596  previousFileSize_ = 0;
597  }
598 
599  struct stat buf;
600  std::stringstream ss;
601  unsigned int nextIndex = index;
602  nextIndex++;
603 
604  // 1. Check suggested file
605  nextFile = getInputJsonFilePath(ls,index);
606  if (stat(nextFile.c_str(), &buf) == 0) {
607 
608  previousFileSize_ = buf.st_size;
609  fsize = buf.st_size;
610  return true;
611  }
612  // 2. No file -> lumi ended? (and how many?)
613  else {
614  std::string BUEoLSFile = getEoLSFilePathOnBU(ls);
615  bool eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
616  unsigned int startingLumi = ls;
617  while (eolFound) {
618 
619  // recheck that no raw file appeared in the meantime
620  if (stat(nextFile.c_str(), &buf) == 0) {
621  previousFileSize_ = buf.st_size;
622  fsize = buf.st_size;
623  return true;
624  }
625 
626  int indexFilesInLS = getNFilesFromEoLS(BUEoLSFile);
627  if (indexFilesInLS < 0)
628  //parsing failed
629  return false;
630  else {
631  //check index
632  if ((int)index<indexFilesInLS) {
633  //we have 2 files, and check for 1 failed... retry (2 will never be here)
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";
636  return false;
637  }
638  }
639  // this lumi ended, check for files
640  ++ls;
641  index = 0;
642 
643  //reached limit
644  if (maxLS>=0 && ls > (unsigned int)maxLS) return false;
645 
646  nextFile = getInputJsonFilePath(ls,0);
647  if (stat(nextFile.c_str(), &buf) == 0) {
648  // a new file was found at new lumisection, index 0
649  previousFileSize_ = buf.st_size;
650  fsize = buf.st_size;
651 
653  // rename ended lumi to + 2
654  std::string sourceEol = getEoLSFilePathOnBU(startingLumi);
655 
656  std::string destEol = getEoLSFilePathOnBU(startingLumi+2);
657 
658  std::string cpCmd = "cp " + sourceEol + " " + destEol;
659  edm::LogInfo("EvFDaqDirector") << " testmode: Running copy cmd -: " << cpCmd;
660  int rc = system(cpCmd.c_str());
661  if (rc != 0) {
662  edm::LogError("EvFDaqDirector") << " testmode: COPY EOL FAILED!!!!! -: " << cpCmd;
663  }
664  }
665 
666  return true;
667  }
668  BUEoLSFile = getEoLSFilePathOnBU(ls);
669  eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
670  }
671  }
672  // no new file found
673  return false;
674  }
675 
677  if (fu_rw_lock_stream == 0)
678  edm::LogError("EvFDaqDirector") << "Error creating fu read/write lock stream "
679  << strerror(errno);
680  else {
681  edm::LogInfo("EvFDaqDirector") << "Initializing FU LOCK FILE";
682  unsigned int readLs = 1, readIndex = 0, jumpLs = 3, jumpIndex = 0;
684  fprintf(fu_rw_lock_stream, "%u %u %u %u", readLs, readIndex,
685  jumpLs, jumpIndex);
686  else
687  fprintf(fu_rw_lock_stream, "%u %u", readLs, readIndex);
688  }
689  }
690 
692  if (create) {
693  fu_readwritelock_fd_ = open(fulockfile.c_str(), O_RDWR | O_CREAT,
694  S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
695  chmod(fulockfile.c_str(),0766);
696  } else {
697  fu_readwritelock_fd_ = open(fulockfile.c_str(), O_RDWR, S_IRWXU);
698  }
699  if (fu_readwritelock_fd_ == -1)
700  edm::LogError("EvFDaqDirector") << "problem with creating filedesc for fuwritelock -: " << fulockfile.c_str()
701  << " create:" << create << " error:" << strerror(errno);
702  else
703  LogDebug("EvFDaqDirector") << "creating filedesc for fureadwritelock -: "
705 
706  fu_rw_lock_stream = fdopen(fu_readwritelock_fd_, "r+");
707  }
708 
709  //create if does not exist then lock the merge destination file
711  data_rw_stream = fopen(getMergedDatFilePath(ls,stream).c_str(), "a"); //open stream for appending
713  if (data_readwrite_fd_ == -1)
714  edm::LogError("EvFDaqDirector") << "problem with creating filedesc for datamerge "
715  << strerror(errno);
716  else
717  LogDebug("EvFDaqDirector") << "creating filedesc for datamerge -: "
719  fcntl(data_readwrite_fd_, F_SETLKW, &data_rw_flk);
720 
721  return data_rw_stream;
722  }
723 
725  fflush(data_rw_stream);
726  fcntl(data_readwrite_fd_, F_SETLKW, &data_rw_fulk);
727  fclose(data_rw_stream);
728  }
729 
731  pthread_mutex_lock(&init_lock_);
732  }
733 
735  pthread_mutex_unlock(&init_lock_);
736  }
737 
739  //fcntl(fulocal_rwlock_fd_, F_SETLKW, &fulocal_rw_flk);
740  flock(fulocal_rwlock_fd_,LOCK_EX);
741  }
742 
744  //fcntl(fulocal_rwlock_fd_, F_SETLKW, &fulocal_rw_fulk);
745  flock(fulocal_rwlock_fd_,LOCK_UN);
746  }
747 
748 
750  //fcntl(fulocal_rwlock_fd2_, F_SETLKW, &fulocal_rw_flk2);
751  flock(fulocal_rwlock_fd2_,LOCK_EX);
752  }
753 
755  //fcntl(fulocal_rwlock_fd2_, F_SETLKW, &fulocal_rw_fulk2);
756  flock(fulocal_rwlock_fd2_,LOCK_UN);
757  }
758 
759 
761  // create open dir if not already there
762 
764  if (!boost::filesystem::is_directory(openPath)) {
765  LogDebug("EvFDaqDirector") << "<open> FU dir not found. Creating... -:" << openPath.string();
766  boost::filesystem::create_directories(openPath);
767  }
768  }
769 
770 
772 
773  boost::filesystem::ifstream ij(file);
774  Json::Value deserializeRoot;
776 
777  if (!reader.parse(ij, deserializeRoot)) {
778  edm::LogError("EvFDaqDirector") << "Cannot deserialize input JSON file -:" << file;
779  return -1;
780  }
781 
782  int ret = deserializeRoot.get("lastLS","").asInt();
783  return ret;
784 
785  }
786 
787 }
#define LogDebug(id)
unsigned int nThreads_
type
Definition: HCALResponse.h:21
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:46
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
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 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
Definition: GlobalContext.h:52
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
#define NULL
Definition: scimark2.h:8
struct flock data_rw_flk
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_
unsigned int jumpIndex_
FileStatus updateFuLock(unsigned int &ls, std::string &nextFile, uint32_t &fsize)
struct flock fu_rw_fulk
unsigned int maxNumberOfStreams() const
Definition: SystemBounds.h:43
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 &reg)
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)
std::string bu_base_dir_
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)
Definition: DataPoint.cc:56
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)
Int asInt() const
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)
tuple pid
Definition: sysUtil.py:22
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
std::string & getDefinition()
Definition: DataPoint.h:59
evf::FastMonitoringService * fms_
std::string bu_run_dir_
std::string rootHistogramFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
std::string findHighestRunDir()
Definition: DirManager.cc:20
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
std::vector< std::string > const & getNames()
unsigned int jumpLS_
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
JetCorrectorParameters::Definitions def
Definition: classes.h:6
std::string protocolBufferHistogramFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
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_