CMS 3D CMS Logo

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