CMS 3D CMS Logo

List of all members | Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
dqmservices::DQMFileIterator Class Reference

#include <DQMFileIterator.h>

Classes

struct  EorEntry
 
struct  LumiEntry
 

Public Types

enum  State { OPEN = 0, EOR_CLOSING = 1, EOR = 2 }
 

Public Member Functions

void advanceToLumi (unsigned int lumi, std::string reason)
 
void delay ()
 
 DQMFileIterator (edm::ParameterSet const &pset)
 
void initialise (int run, const std::string &, const std::string &)
 
unsigned int lastLumiFound ()
 
void logFileAction (const std::string &msg, const std::string &fileName="") const
 
void logLumiState (const LumiEntry &lumi, const std::string &msg)
 
bool lumiReady ()
 
LumiEntry open ()
 
void pop ()
 
void reset ()
 
unsigned int runNumber ()
 
State state ()
 
void update_state ()
 
 ~DQMFileIterator ()
 

Static Public Member Functions

static void fillDescription (edm::ParameterSetDescription &d)
 

Private Member Functions

void collect (bool ignoreTimers)
 
void monUpdateLumi (const LumiEntry &lumi)
 
std::time_t mtimeHash () const
 

Private Attributes

unsigned int datafnPosition_
 
unsigned long delayMillis_
 
EorEntry eor_
 
std::unordered_set< std::string > filesSeen_
 
bool flagScanOnce_
 
long forceFileCheckTimeoutMillis_
 
std::chrono::high_resolution_clock::time_point lastLumiLoad_
 
std::map< unsigned int, LumiEntrylumiSeen_
 
edm::Service< DQMMonitoringServicemon_
 
unsigned int nextLumiNumber_
 
long nextLumiTimeoutMillis_
 
std::string runInputDir_
 
unsigned int runNumber_
 
std::vector< std::string > runPath_
 
std::chrono::high_resolution_clock::time_point runPathLastCollect_
 
std::time_t runPathMTime_
 
State state_
 
std::string streamLabel_
 

Detailed Description

Definition at line 18 of file DQMFileIterator.h.

Member Enumeration Documentation

Enumerator
OPEN 
EOR_CLOSING 
EOR 

Definition at line 48 of file DQMFileIterator.h.

48  {
49  OPEN = 0,
50  EOR_CLOSING = 1, // EoR file found, but lumis are still pending
51  EOR = 2,
52  };

Constructor & Destructor Documentation

dqmservices::DQMFileIterator::DQMFileIterator ( edm::ParameterSet const &  pset)

Definition at line 80 of file DQMFileIterator.cc.

References datafnPosition_, delayMillis_, flagScanOnce_, forceFileCheckTimeoutMillis_, edm::ParameterSet::getUntrackedParameter(), nextLumiTimeoutMillis_, reset(), runInputDir_, runNumber_, streamLabel_, and AlCaHLTBitMon_QueryRunRegistry::string.

80  : state_(EOR) {
81  runNumber_ = pset.getUntrackedParameter<unsigned int>("runNumber");
82  datafnPosition_ = pset.getUntrackedParameter<unsigned int>("datafnPosition");
83  runInputDir_ = pset.getUntrackedParameter<std::string>("runInputDir");
84  streamLabel_ = pset.getUntrackedParameter<std::string>("streamLabel");
85  delayMillis_ = pset.getUntrackedParameter<uint32_t>("delayMillis");
87  pset.getUntrackedParameter<int32_t>("nextLumiTimeoutMillis");
88 
89  // scan one mode
90  flagScanOnce_ = pset.getUntrackedParameter<bool>("scanOnce");
91 
93  reset();
94 }
dqmservices::DQMFileIterator::~DQMFileIterator ( )

Definition at line 96 of file DQMFileIterator.cc.

96 {}

Member Function Documentation

void dqmservices::DQMFileIterator::advanceToLumi ( unsigned int  lumi,
std::string  reason 
)

Definition at line 154 of file DQMFileIterator.cc.

References lastLumiLoad_, csvLumiCalc::lumi, lumiSeen_, mon_, monUpdateLumi(), nextLumiNumber_, cmsPerfSuiteHarvest::now, and harvestTrackValidationPlots::str.

Referenced by open(), dqmservices::DQMStreamerReader::reset_(), and update_state().

154  {
155  using boost::property_tree::ptree;
156  using boost::str;
157 
158  unsigned int currentLumi = nextLumiNumber_;
159 
162 
163  auto iter = lumiSeen_.lower_bound(currentLumi);
164 
165  while ((iter != lumiSeen_.end()) && ((iter->first) < nextLumiNumber_)) {
166  iter->second.state = reason;
167  monUpdateLumi(iter->second);
168 
169  ++iter;
170  }
171 
172  if (mon_.isAvailable()) {
173  // report the successful lumi file open
174  ptree doc;
175  doc.put("next_lumi", nextLumiNumber_);
176  mon_->outputUpdate(doc);
177  }
178 }
edm::Service< DQMMonitoringService > mon_
std::map< unsigned int, LumiEntry > lumiSeen_
std::chrono::high_resolution_clock::time_point lastLumiLoad_
void monUpdateLumi(const LumiEntry &lumi)
void dqmservices::DQMFileIterator::collect ( bool  ignoreTimers)
private

Definition at line 202 of file DQMFileIterator.cc.

References KineDebug3::count(), datafnPosition_, MillePedeFileConverter_cfg::e, eor_, cppFunctionSkipper::exception, corrVsCorr::filename, filesSeen_, flagScanOnce_, forceFileCheckTimeoutMillis_, diffTwoXMLs::label, dqmservices::DQMFileIterator::LumiEntry::load_json(), dqmservices::DQMFileIterator::EorEntry::loaded, logFileAction(), lumiSeen_, monUpdateLumi(), mps_alisetup::msg, mtimeHash(), dqmservices::DQMFileIterator::EorEntry::n_lumi, cmsPerfSuiteHarvest::now, or, mps_fire::result, findQualityFiles::run, runNumber_, runPath_, runPathLastCollect_, runPathMTime_, streamLabel_, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by reset(), and update_state().

202  {
203  // search filesystem to find available lumi section files
204  // or the end of run files
205 
207  auto last_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
209 
210  // don't refresh if it's too soon
211  if ((!ignoreTimers) && (last_ms >= 0) && (last_ms < 100)) {
212  return;
213  }
214 
215  // check if directory changed
216  std::time_t mtime_now = mtimeHash();
217 
218  if ((!ignoreTimers) && (last_ms < forceFileCheckTimeoutMillis_) &&
219  (mtime_now == runPathMTime_)) {
220  // logFileAction("Directory hasn't changed.");
221  return;
222  } else {
223  // logFileAction("Directory changed, updating.");
224  }
225 
226  runPathMTime_ = mtime_now;
228 
229  using boost::filesystem::directory_iterator;
230  using boost::filesystem::directory_entry;
231 
232  std::string fn_eor;
233 
234  for (auto runPath : runPath_) {
235  if (!boost::filesystem::exists(runPath)) {
236  logFileAction("Directory does not exist: ", runPath);
237 
238  continue;
239  }
240 
241  directory_iterator dend;
242  for (directory_iterator di(runPath); di != dend; ++di) {
243  const boost::regex fn_re("run(\\d+)_ls(\\d+)_([a-zA-Z0-9]+)(_.*)?\\.jsn");
244 
245  const std::string filename = di->path().filename().string();
246  const std::string fn = di->path().string();
247 
248  if (filesSeen_.find(filename) != filesSeen_.end()) {
249  continue;
250  }
251 
252  boost::smatch result;
253  if (boost::regex_match(filename, result, fn_re)) {
254  unsigned int run = std::stoi(result[1]);
255  unsigned int lumi = std::stoi(result[2]);
256  std::string label = result[3];
257 
258  filesSeen_.insert(filename);
259 
260  if (run != runNumber_) continue;
261 
262  // check if this is EoR
263  // for various reasons we have to load it after all other files
264  if ((lumi == 0) && (label == "EoR") && (!eor_.loaded)) {
265  fn_eor = fn;
266  continue;
267  }
268 
269  // check if lumi is loaded
270  if (lumiSeen_.find(lumi) != lumiSeen_.end()) {
271  continue; // already loaded
272  }
273 
274  // check if this belongs to us
275  if (label != streamLabel_) {
276  std::string msg("Found and skipped json file (stream label mismatch, ");
277  msg += label + " [files] != " + streamLabel_ + " [config]";
278  msg += "): ";
279  logFileAction(msg, fn);
280  continue;
281  }
282 
283  try {
284  LumiEntry lumi_jsn = LumiEntry::load_json(runPath, fn, lumi, datafnPosition_);
285  lumiSeen_.emplace(lumi, lumi_jsn);
286  logFileAction("Found and loaded json file: ", fn);
287 
288  monUpdateLumi(lumi_jsn);
289  } catch (const std::exception& e) {
290  // don't reset the mtime, keep it waiting
291  filesSeen_.erase(filename);
292 
293  std::string msg("Found, tried to load the json, but failed (");
294  msg += e.what();
295  msg += "): ";
296  logFileAction(msg, fn);
297  }
298  }
299  }
300  }
301 
302  if ((!fn_eor.empty()) or flagScanOnce_) {
303  if (!fn_eor.empty()) {
304  logFileAction("EoR file found: ", fn_eor);
305  }
306 
307  // @TODO load EoR files correctly
308  // eor_ = EorEntry::load_json(fn_eor);
309  // logFileAction("Loaded eor file: ", fn_eor);
310 
311  // for now , set n_lumi to the highest _found_ lumi
312  eor_.loaded = true;
313 
314  if (lumiSeen_.empty()) {
315  eor_.n_lumi = 0;
316  } else {
317  eor_.n_lumi = lumiSeen_.rbegin()->first;
318  }
319  }
320 }
std::map< unsigned int, LumiEntry > lumiSeen_
static LumiEntry load_json(const std::string &run_path, const std::string &filename, int lumiNumber, int datafn_position)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
std::vector< std::string > runPath_
void logFileAction(const std::string &msg, const std::string &fileName="") const
void monUpdateLumi(const LumiEntry &lumi)
std::chrono::high_resolution_clock::time_point runPathLastCollect_
std::unordered_set< std::string > filesSeen_
std::time_t mtimeHash() const
void dqmservices::DQMFileIterator::delay ( )

Definition at line 401 of file DQMFileIterator.cc.

References delayMillis_, and mon_.

Referenced by dqmservices::DQMProtobufReader::getNextItemType(), dqmservices::DQMStreamerReader::prepareNextEvent(), and dqmservices::DQMStreamerReader::reset_().

401  {
402  if (mon_.isAvailable()) mon_->keepAlive();
403 
404  usleep(delayMillis_ * 1000);
405 }
edm::Service< DQMMonitoringService > mon_
void dqmservices::DQMFileIterator::fillDescription ( edm::ParameterSetDescription d)
static

Definition at line 407 of file DQMFileIterator.cc.

References edm::ParameterSetDescription::addUntracked(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by dqmservices::DQMProtobufReader::fillDescriptions(), and dqmservices::DQMStreamerReader::fillDescriptions().

407  {
408  desc.addUntracked<unsigned int>("runNumber")
409  ->setComment("Run number passed via configuration file.");
410 
411  desc.addUntracked<unsigned int>("datafnPosition", 3)
412  ->setComment(
413  "Data filename position in the positional arguments array 'data' in "
414  "json file.");
415 
416  desc.addUntracked<std::string>("streamLabel")
417  ->setComment("Stream label used in json discovery.");
418 
419  desc.addUntracked<uint32_t>("delayMillis")
420  ->setComment("Number of milliseconds to wait between file checks.");
421 
422  desc.addUntracked<int32_t>("nextLumiTimeoutMillis", -1)
423  ->setComment(
424  "Number of milliseconds to wait before switching to the next lumi "
425  "section if the current is missing, -1 to disable.");
426 
427  desc.addUntracked<bool>("scanOnce", false)
428  ->setComment(
429  "Don't repeat file scans: use what was found during the initial scan. "
430  "EOR file is ignored and the state is set to 'past end of run'.");
431 
432  desc.addUntracked<std::string>("runInputDir")
433  ->setComment("Directory where the DQM files will appear.");
434 }
void dqmservices::DQMFileIterator::initialise ( int  run,
const std::string &  ,
const std::string &   
)
unsigned int dqmservices::DQMFileIterator::lastLumiFound ( )

Definition at line 146 of file DQMFileIterator.cc.

References lumiSeen_.

Referenced by dqmservices::DQMStreamerReader::reset_().

146  {
147  if (!lumiSeen_.empty()) {
148  return lumiSeen_.rbegin()->first;
149  }
150 
151  return 1;
152 }
std::map< unsigned int, LumiEntry > lumiSeen_
void dqmservices::DQMFileIterator::logFileAction ( const std::string &  msg,
const std::string &  fileName = "" 
) const
void dqmservices::DQMFileIterator::logLumiState ( const LumiEntry lumi,
const std::string &  msg 
)

Definition at line 390 of file DQMFileIterator.cc.

References dqmservices::DQMFileIterator::LumiEntry::file_ls, logFileAction(), lumiSeen_, monUpdateLumi(), and mps_alisetup::msg.

Referenced by dqmservices::DQMProtobufReader::beginLuminosityBlock(), dqmservices::DQMStreamerReader::closeFile_(), and dqmservices::DQMStreamerReader::openNextFile_().

391  {
392  if (lumiSeen_.find(lumi.file_ls) != lumiSeen_.end()) {
393  lumiSeen_[lumi.file_ls].state = msg;
394 
395  monUpdateLumi(lumiSeen_[lumi.file_ls]);
396  } else {
397  logFileAction("Internal error: referenced lumi is not the map.");
398  }
399 }
std::map< unsigned int, LumiEntry > lumiSeen_
void logFileAction(const std::string &msg, const std::string &fileName="") const
void monUpdateLumi(const LumiEntry &lumi)
bool dqmservices::DQMFileIterator::lumiReady ( )

Definition at line 136 of file DQMFileIterator.cc.

References lumiSeen_, and nextLumiNumber_.

Referenced by dqmservices::DQMProtobufReader::getNextItemType(), and dqmservices::DQMStreamerReader::prepareNextFile().

136  {
137  if (lumiSeen_.find(nextLumiNumber_) != lumiSeen_.end()) {
138  return true;
139  }
140 
141  return false;
142 }
std::map< unsigned int, LumiEntry > lumiSeen_
void dqmservices::DQMFileIterator::monUpdateLumi ( const LumiEntry lumi)
private

Definition at line 180 of file DQMFileIterator.cc.

References dqmservices::DQMFileIterator::LumiEntry::file_ls, cmsPerfStripChart::format, mon_, dqmservices::DQMFileIterator::LumiEntry::state, and harvestTrackValidationPlots::str.

Referenced by advanceToLumi(), collect(), and logLumiState().

180  {
181  if (!mon_.isAvailable()) return;
182 
183  ptree doc;
184  doc.put(str(boost::format("extra.lumi_seen.lumi%06d") % lumi.file_ls),
185  lumi.state);
186  mon_->outputUpdate(doc);
187 }
edm::Service< DQMMonitoringService > mon_
format
Some error handling for the usage.
std::time_t dqmservices::DQMFileIterator::mtimeHash ( ) const
private

Definition at line 189 of file DQMFileIterator.cc.

References callgraph::path, and runPath_.

Referenced by collect().

189  {
190  std::time_t mtime_now = 0;
191 
192  for (auto path : runPath_) {
193  if (!boost::filesystem::exists(path))
194  continue;
195 
196  mtime_now = mtime_now ^ boost::filesystem::last_write_time(path);
197  }
198 
199  return mtime_now;
200 }
std::vector< std::string > runPath_
DQMFileIterator::LumiEntry dqmservices::DQMFileIterator::open ( )
void dqmservices::DQMFileIterator::pop ( )
void dqmservices::DQMFileIterator::reset ( void  )

Definition at line 98 of file DQMFileIterator.cc.

References collect(), eor_, filesSeen_, cmsPerfStripChart::format, lastLumiLoad_, dqmservices::DQMFileIterator::EorEntry::loaded, lumiSeen_, mon_, nextLumiNumber_, cmsPerfSuiteHarvest::now, runInputDir_, runNumber_, runPath_, split, state_, harvestTrackValidationPlots::str, and update_state().

Referenced by DQMFileIterator().

98  {
99  runPath_.clear();
100 
101  std::vector<std::string> tokens;
102  boost::split(tokens, runInputDir_, boost::is_any_of(":"));
103 
104  for (auto token: tokens) {
105  runPath_.push_back(boost::str(boost::format("%s/run%06d") % token % runNumber_));
106  }
107 
108  eor_.loaded = false;
109  state_ = State::OPEN;
110  nextLumiNumber_ = 1;
111  lumiSeen_.clear();
112  filesSeen_.clear();
113 
115 
116  collect(true);
117  update_state();
118 
119  if (mon_.isAvailable()) {
120  ptree doc;
121  doc.put("run", runNumber_);
122  doc.put("next_lumi", nextLumiNumber_);
123  doc.put("fi_state", std::to_string(state_));
124  mon_->outputUpdate(doc);
125  }
126 }
edm::Service< DQMMonitoringService > mon_
std::map< unsigned int, LumiEntry > lumiSeen_
std::vector< std::string > runPath_
format
Some error handling for the usage.
std::chrono::high_resolution_clock::time_point lastLumiLoad_
std::unordered_set< std::string > filesSeen_
double split
Definition: MVATrainer.cc:139
void collect(bool ignoreTimers)
unsigned int dqmservices::DQMFileIterator::runNumber ( )
DQMFileIterator::State dqmservices::DQMFileIterator::state ( )
void dqmservices::DQMFileIterator::update_state ( )

Definition at line 322 of file DQMFileIterator.cc.

References advanceToLumi(), collect(), KineDebug3::count(), eor_, flagScanOnce_, lastLumiLoad_, dqmservices::DQMFileIterator::EorEntry::loaded, logFileAction(), lumiSeen_, mon_, mps_alisetup::msg, dqmservices::DQMFileIterator::EorEntry::n_lumi, nextLumiNumber_, nextLumiTimeoutMillis_, cmsPerfSuiteHarvest::now, state_, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by dqmservices::DQMProtobufReader::getNextItemType(), dqmservices::DQMStreamerReader::prepareNextFile(), reset(), and dqmservices::DQMStreamerReader::reset_().

322  {
323  using std::chrono::high_resolution_clock;
324  using std::chrono::duration_cast;
325  using std::chrono::milliseconds;
326 
327  State old_state = state_;
328 
329  // in scanOnce mode we don't do repeated scans
330  // whatever found at reset() is be used
331  if (!flagScanOnce_) {
332  collect(false);
333  }
334 
335  if ((state_ == State::OPEN) && (eor_.loaded)) {
336  state_ = State::EOR_CLOSING;
337  }
338 
339  // special case for missing lumi files
340  // skip to the next available, but after the timeout
341  if ((state_ != State::EOR) && (nextLumiTimeoutMillis_ >= 0)) {
342  auto iter = lumiSeen_.lower_bound(nextLumiNumber_);
343  if ((iter != lumiSeen_.end()) && iter->first != nextLumiNumber_) {
344  auto elapsed = high_resolution_clock::now() - lastLumiLoad_;
345  auto elapsed_ms = duration_cast<milliseconds>(elapsed).count();
346 
347  if (elapsed_ms >= nextLumiTimeoutMillis_) {
348  std::string msg("Timeout reached, skipping lumisection(s) ");
349  msg += std::to_string(nextLumiNumber_) + " .. " +
350  std::to_string(iter->first - 1);
351  msg += ", nextLumiNumber_ is now " + std::to_string(iter->first);
353 
354  advanceToLumi(iter->first, "skipped: timeout");
355  }
356  }
357  }
358 
359  if (state_ == State::EOR_CLOSING) {
360  // check if we parsed all lumis
361  // n_lumi is both last lumi and the number of lumi
362  // since lumis are indexed from 1
363 
364  // after all lumi have been pop()'ed
365  // current lumi will become larger than the last lumi
366  if (nextLumiNumber_ > eor_.n_lumi) {
367  state_ = State::EOR;
368  }
369  }
370 
371  if (state_ != old_state) {
372  logFileAction("Streamer state changed: ",
373  std::to_string(old_state) + "->" + std::to_string(state_));
374 
375  if (mon_) {
376  ptree doc;
377  doc.put("fi_state", std::to_string(state_));
378  mon_->outputUpdate(doc);
379  }
380  }
381 }
edm::Service< DQMMonitoringService > mon_
std::map< unsigned int, LumiEntry > lumiSeen_
void logFileAction(const std::string &msg, const std::string &fileName="") const
std::chrono::high_resolution_clock::time_point lastLumiLoad_
State
Definition: hltDiff.cc:288
void advanceToLumi(unsigned int lumi, std::string reason)
void collect(bool ignoreTimers)

Member Data Documentation

unsigned int dqmservices::DQMFileIterator::datafnPosition_
private

Definition at line 101 of file DQMFileIterator.h.

Referenced by collect(), and DQMFileIterator().

unsigned long dqmservices::DQMFileIterator::delayMillis_
private

Definition at line 95 of file DQMFileIterator.h.

Referenced by delay(), and DQMFileIterator().

EorEntry dqmservices::DQMFileIterator::eor_
private

Definition at line 104 of file DQMFileIterator.h.

Referenced by collect(), reset(), and update_state().

std::unordered_set<std::string> dqmservices::DQMFileIterator::filesSeen_
private

Definition at line 109 of file DQMFileIterator.h.

Referenced by collect(), and reset().

bool dqmservices::DQMFileIterator::flagScanOnce_
private

Definition at line 98 of file DQMFileIterator.h.

Referenced by collect(), DQMFileIterator(), and update_state().

long dqmservices::DQMFileIterator::forceFileCheckTimeoutMillis_
private

Definition at line 97 of file DQMFileIterator.h.

Referenced by collect(), and DQMFileIterator().

std::chrono::high_resolution_clock::time_point dqmservices::DQMFileIterator::lastLumiLoad_
private

Definition at line 117 of file DQMFileIterator.h.

Referenced by advanceToLumi(), reset(), and update_state().

std::map<unsigned int, LumiEntry> dqmservices::DQMFileIterator::lumiSeen_
private
edm::Service<DQMMonitoringService> dqmservices::DQMFileIterator::mon_
private

Definition at line 124 of file DQMFileIterator.h.

Referenced by advanceToLumi(), delay(), monUpdateLumi(), reset(), and update_state().

unsigned int dqmservices::DQMFileIterator::nextLumiNumber_
private

Definition at line 107 of file DQMFileIterator.h.

Referenced by advanceToLumi(), lumiReady(), open(), reset(), and update_state().

long dqmservices::DQMFileIterator::nextLumiTimeoutMillis_
private

Definition at line 96 of file DQMFileIterator.h.

Referenced by DQMFileIterator(), and update_state().

std::string dqmservices::DQMFileIterator::runInputDir_
private

Definition at line 93 of file DQMFileIterator.h.

Referenced by DQMFileIterator(), and reset().

unsigned int dqmservices::DQMFileIterator::runNumber_
private

Definition at line 92 of file DQMFileIterator.h.

Referenced by collect(), DQMFileIterator(), reset(), and runNumber().

std::vector<std::string> dqmservices::DQMFileIterator::runPath_
private

Definition at line 102 of file DQMFileIterator.h.

Referenced by collect(), mtimeHash(), and reset().

std::chrono::high_resolution_clock::time_point dqmservices::DQMFileIterator::runPathLastCollect_
private

Definition at line 114 of file DQMFileIterator.h.

Referenced by collect().

std::time_t dqmservices::DQMFileIterator::runPathMTime_
private

Definition at line 113 of file DQMFileIterator.h.

Referenced by collect().

State dqmservices::DQMFileIterator::state_
private

Definition at line 105 of file DQMFileIterator.h.

Referenced by reset(), state(), and update_state().

std::string dqmservices::DQMFileIterator::streamLabel_
private

Definition at line 94 of file DQMFileIterator.h.

Referenced by collect(), and DQMFileIterator().