CMS 3D CMS Logo

RamdiskMonitor.cc
Go to the documentation of this file.
1 #include <filesystem>
2 #include <map>
3 #include <vector>
4 #include <sys/stat.h>
5 
6 #include <fmt/printf.h>
7 #include <boost/algorithm/string/predicate.hpp>
8 #include <boost/range.hpp>
9 #include <boost/regex.hpp>
10 
20 
21 namespace dqm {
22  namespace rdm {
23  struct Empty {};
24  } // namespace rdm
25  class RamdiskMonitor : public DQMOneEDAnalyzer<edm::LuminosityBlockCache<rdm::Empty>> {
26  public:
28  ~RamdiskMonitor() override;
29  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
30 
31  protected:
32  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
33  std::shared_ptr<rdm::Empty> globalBeginLuminosityBlock(edm::LuminosityBlock const &lumi,
34  edm::EventSetup const &eSetup) const override;
36  void analyze(edm::Event const &e, edm::EventSetup const &eSetup) override{};
37 
38  void analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const;
39  double getRunTimestamp() const;
40 
41  const unsigned int runNumber_;
43  const std::vector<std::string> streamLabels_;
45 
46  struct StreamME {
49 
52  };
53 
54  std::map<std::string, StreamME> streams_;
55  mutable std::set<std::string> filesSeen_;
56  mutable double global_start_ = 0.;
57 
58  static constexpr double LUMI = 23.310893056;
59  };
60 
62  : runNumber_{ps.getUntrackedParameter<unsigned int>("runNumber")},
63  runInputDir_{ps.getUntrackedParameter<std::string>("runInputDir")},
64  streamLabels_{ps.getUntrackedParameter<std::vector<std::string>>("streamLabels")},
65  runPath_{fmt::sprintf("%s/run%06d", runInputDir_, runNumber_)}
66 
67  {}
68 
70 
72  for (const auto &stream : streamLabels_) {
73  edm::LogInfo("RamdiskMonitor") << "Booking: " << stream;
74 
75  ib.cd();
76  ib.setCurrentFolder(std::string("Info/RamdiskMonitor/") + stream + "/");
77 
78  StreamME m;
79 
80  m.eventsAccepted = ib.book1D("EventAccepted", "# of accepted events per lumi", 4, 0., 4.);
81  m.eventsProcessed = ib.book1D("EventProcessed", "# of processed events per lumi", 4, 0., 4.);
82  m.deliveryDelayMTime =
83  ib.book1D("DeliveryDelayMTime", "Observed delivery delay for the data file (mtime).", 4, 0., 4.);
84  m.deliveryDelayCTime =
85  ib.book1D("DeliveryDelayCTime", "Observed delivery delay for the data file (ctime).", 4, 0., 4.);
86 
87  m.eventsAccepted->getTH1F()->SetCanExtend(TH1::kXaxis);
88  m.eventsProcessed->getTH1F()->SetCanExtend(TH1::kXaxis);
89  m.deliveryDelayMTime->getTH1F()->SetCanExtend(TH1::kXaxis);
90  m.deliveryDelayCTime->getTH1F()->SetCanExtend(TH1::kXaxis);
91 
92  m.eventsAccepted->setAxisTitle("Luminosity Section", 1);
93  m.eventsProcessed->setAxisTitle("Luminosity Section", 1);
94  m.deliveryDelayMTime->setAxisTitle("Luminosity Section", 1);
95  m.deliveryDelayCTime->setAxisTitle("Luminosity Section", 1);
96 
97  m.eventsAccepted->setAxisTitle("Number of events", 2);
98  m.eventsProcessed->setAxisTitle("Number of events", 2);
99  m.deliveryDelayMTime->setAxisTitle("Delay (s.)", 2);
100  m.deliveryDelayCTime->setAxisTitle("Delay (s.)", 2);
101 
102  streams_[stream] = m;
103  }
104  };
105 
107  if (global_start_ != 0)
108  return global_start_;
109 
110  std::string run_global = fmt::sprintf("%s/.run%06d.global", runInputDir_, runNumber_);
111  struct stat st;
112  if (::stat(run_global.c_str(), &st) != 0) {
113  edm::LogWarning("RamdiskMonitor") << "Stat failed: " << run_global;
114  return 0.;
115  }
116 
117  global_start_ = st.st_mtime;
118  edm::LogPrint("RamdiskMonitor") << "Run start timestamp: " << global_start_;
119  return global_start_;
120  };
121 
122  void RamdiskMonitor::analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const {
123  using LumiEntry = dqmservices::DQMFileIterator::LumiEntry;
124 
125  // we are disabled, at least for this stream
126  if (streams_.empty())
127  return;
128 
129  auto itStream = streams_.find(label);
130  if (itStream == streams_.end()) {
131  edm::LogPrint("RamdiskMonitor") << "Stream not monitored [" << label << "]: " << fn;
132  return;
133  }
134 
135  StreamME m = itStream->second;
136 
137  // decode json and fill in some histograms
138  LumiEntry lumi_jsn = LumiEntry::load_json(runPath_, fn, lumi, -1);
139  m.eventsAccepted->setBinContent(lumi, lumi_jsn.n_events_accepted);
140  m.eventsProcessed->setBinContent(lumi, lumi_jsn.n_events_processed);
141 
142  // collect stat struct and calculate mtimes
143  struct stat st;
144  if (::stat(fn.c_str(), &st) != 0) {
145  edm::LogWarning("RamdiskMonitor") << "Stat failed: " << fn;
146  return;
147  }
148 
149  // get start offset (from .global)
150  // abort the calculation if it does not exist
151  double start_offset = getRunTimestamp();
152  if (start_offset <= 0)
153  return;
154 
155  // check fff_dqmtools (separate repository)
156  // for calculation details
157  double mtime = st.st_mtime;
158  double ctime = st.st_ctime;
159 
160  // timeout from the begging of the run
161  double start_offset_mtime = mtime - start_offset - LUMI;
162  double start_offset_ctime = ctime - start_offset - LUMI;
163  double lumi_offset = (lumi - 1) * LUMI;
164 
165  // timeout from the time we think this lumi happenned
166  double delay_mtime = start_offset_mtime - lumi_offset;
167  double delay_ctime = start_offset_ctime - lumi_offset;
168 
169  m.deliveryDelayMTime->setBinContent(lumi, delay_mtime);
170  m.deliveryDelayCTime->setBinContent(lumi, delay_ctime);
171  };
172 
173  std::shared_ptr<dqm::rdm::Empty> RamdiskMonitor::globalBeginLuminosityBlock(edm::LuminosityBlock const &,
174  edm::EventSetup const &eSetup) const {
175  // search filesystem to find available lumi section files
176  using std::filesystem::directory_entry;
177  using std::filesystem::directory_iterator;
178 
179  directory_iterator dend;
180  for (directory_iterator di(runPath_); di != dend; ++di) {
181  const boost::regex fn_re("run(\\d+)_ls(\\d+)_([a-zA-Z0-9]+)(_.*)?\\.jsn");
182 
183  const std::string filename = di->path().filename().string();
184  const std::string fn = di->path().string();
185 
186  if (filesSeen_.find(filename) != filesSeen_.end()) {
187  continue;
188  }
189 
190  boost::smatch result;
191  if (boost::regex_match(filename, result, fn_re)) {
192  unsigned int run = std::stoi(result[1]);
193  unsigned int lumi = std::stoi(result[2]);
194  std::string label = result[3];
195 
196  filesSeen_.insert(filename);
197 
198  if (run != runNumber_)
199  continue;
200 
201  // check if this is EoR
202  if ((lumi == 0) && (label == "EoR")) {
203  // do not handle
204  continue;
205  }
206 
207  try {
208  this->analyzeFile(fn, run, lumi, label);
209  } catch (const std::exception &e) {
210  // it's likely we have read it too soon
211  filesSeen_.erase(filename);
212 
213  std::string msg("Found, tried to load the json, but failed (");
214  msg += e.what();
215  msg += "): ";
216  edm::LogWarning("RamdiskMonitor") << msg;
217  }
218  }
219  }
220 
221  // @TODO lookup info for the current lumi
222  return std::shared_ptr<dqm::rdm::Empty>();
223  }
224 
227 
228  desc.setComment(
229  "Analyses file timestams in the /fff/ramdisk and creates monitor "
230  "elements.");
231 
232  desc.addUntracked<std::vector<std::string>>("streamLabels")->setComment("List of streams to monitor.");
233 
234  desc.addUntracked<unsigned int>("runNumber")->setComment("Run number passed via configuration file.");
235 
236  desc.addUntracked<std::string>("runInputDir")->setComment("Directory where the DQM files will appear.");
237 
238  d.add("RamdiskMonitor", desc);
239  }
240 
241 } // namespace dqm
242 
244 
static const char runNumber_[]
void globalEndLuminosityBlock(edm::LuminosityBlock const &lumi, edm::EventSetup const &eSetup) final
void analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const
std::map< std::string, StreamME > streams_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const std::string runPath_
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
MonitorElement * eventsProcessed
T getUntrackedParameter(std::string const &, T const &) const
char const * label
MonitorElement * deliveryDelayCTime
~RamdiskMonitor() override
std::shared_ptr< rdm::Empty > globalBeginLuminosityBlock(edm::LuminosityBlock const &lumi, edm::EventSetup const &eSetup) const override
Log< level::Warning, true > LogPrint
d
Definition: ztail.py:151
Log< level::Info, false > LogInfo
const std::vector< std::string > streamLabels_
MonitorElement * deliveryDelayMTime
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
tuple msg
Definition: mps_check.py:285
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double getRunTimestamp() const
void analyze(edm::Event const &e, edm::EventSetup const &eSetup) override
RamdiskMonitor(const edm::ParameterSet &ps)
dqm::RamdiskMonitor RamdiskMonitor
const std::string runInputDir_
Log< level::Warning, false > LogWarning
Definition: DQMStore.h:18
std::set< std::string > filesSeen_
const unsigned int runNumber_
Definition: Run.h:45
ib
Definition: cuy.py:661
static constexpr double LUMI