CMS 3D CMS Logo

DQMMonitoringService.cc
Go to the documentation of this file.
1 #include "DQMMonitoringService.h"
2 
9 
10 #include <cstdlib>
11 #include <ctime>
12 #include <exception>
13 #include <iostream>
14 
15 #include <boost/property_tree/json_parser.hpp>
16 
17 #include <fmt/printf.h>
18 
19 /*
20  * This service is very similar to the FastMonitoringService in the HLT,
21  * except that it is used for monitoring online DQM applications
22  */
23 
24 namespace dqmservices {
25 
27  const char* x = std::getenv("DQM2_SOCKET");
28  if (x) {
29  std::cerr << "Monitoring pipe: " << x << std::endl;
30  mstream_.connect(boost::asio::local::stream_protocol::endpoint(x));
31  } else {
32  std::cerr << "Monitoring file not found, disabling." << std::endl;
33  }
34 
35  // init counters
36  nevents_ = 0;
37 
38  last_lumi_ = 0;
41 
42  run_ = 0;
43  lumi_ = 0;
44 
47  }
48 
51 
52  boost::property_tree::ptree doc;
53 
54  // these might be different than the numbers we want to report
55  // rate/stats per lumi are calculated from last_*_ fields
56  doc.put("cmssw_run", run_);
57  doc.put("cmssw_lumi", lumi_);
58  doc.put("events_total", nevents_);
59 
60  // do statistics for the last (elapsed) ls
61  auto lumi_millis = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_lumi_time_).count();
62  auto lumi_events = nevents_ - last_lumi_nevents_;
63  auto lumi_last = last_lumi_;
64 
65  if ((lumi_last > 0) && (lumi_millis > 0)) {
66  doc.put("lumi_number", lumi_last);
67  doc.put("lumi_events", lumi_events);
68  doc.put("lumi_duration_ms", lumi_millis);
69 
70  float rate = ((float)lumi_events * 1000) / lumi_millis;
71  doc.put("events_rate", rate);
72 
73  // also save the history entry
74  boost::property_tree::ptree plumi;
75  plumi.put("n", lumi_last);
76  plumi.put("nevents", lumi_events);
77  plumi.put("nmillis", lumi_millis);
78  plumi.put("rate", rate);
79 
80  std::time_t hkey = std::time(nullptr);
81  doc.add_child(fmt::sprintf("extra.lumi_stats.%d", hkey), plumi);
82  }
83 
85  }
86 
88  // these might be different than the numbers we want to report
89  // rate/stats per lumi are calculated from last_*_ fields
90  run_ = iContext.luminosityBlockID().run();
92 
94 
97  last_lumi_ = lumi_;
98  }
99 
101  nevents_ += 1;
102  tryUpdate();
103  }
104 
105  void DQMMonitoringService::outputUpdate(boost::property_tree::ptree& doc) {
106  if (!mstream_)
107  return;
108 
109  try {
111  doc.put("update_timestamp", std::time(nullptr));
112 
113  write_json(mstream_, doc, false);
114  mstream_.flush();
115  } catch (std::exception const& exc) {
116  LogDebug("DQMMonitoringService") << "Exception thrown in outputUpdate method: " << exc.what();
117  }
118  }
119 
121  if (!mstream_)
122  return;
123 
124  mstream_ << "\n";
125  mstream_.flush();
126 
127  tryUpdate();
128  }
129 
131  if (!mstream_)
132  return;
133 
134  // sometimes we don't see any transition for a very long time
135  // but we still want updates
136  // luckily, keepAlive is called rather often by the input source
138  auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update_time_).count();
139  if (millis >= (25 * 1000)) {
141  }
142  }
143 
144 } // namespace dqmservices
145 
147 
void evEvent(edm::StreamID const &)
LuminosityBlockNumber_t luminosityBlock() const
void evLumi(edm::GlobalContext const &)
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:66
RunNumber_t run() const
boost::asio::local::stream_protocol::iostream mstream_
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
std::chrono::high_resolution_clock::time_point last_lumi_time_
double rate(double x)
Definition: Constants.cc:3
void outputUpdate(boost::property_tree::ptree &doc)
DQMMonitoringService(const edm::ParameterSet &, edm::ActivityRegistry &)
float x
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
std::chrono::high_resolution_clock::time_point last_update_time_
#define LogDebug(id)