CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DQMMonitoringService.cc
Go to the documentation of this file.
1 #include <ctime>
2 #include <iostream>
3 
4 #include <fmt/printf.h>
5 
6 #include <boost/algorithm/string.hpp>
7 #include <boost/algorithm/string/predicate.hpp>
8 
9 #include "DQMMonitoringService.h"
10 
11 /*
12  * This service is very similar to the FastMonitoringService in the HLT,
13  * except that it is used for monitoring online DQM applications
14  */
15 
16 namespace dqmservices {
17 
19  const char* x = std::getenv("DQM2_SOCKET");
20  if (x) {
21  std::cerr << "Monitoring pipe: " << x << std::endl;
22  mstream_.connect(boost::asio::local::stream_protocol::endpoint(x));
23  } else {
24  std::cerr << "Monitoring file not found, disabling." << std::endl;
25  }
26 
27  // init counters
28  nevents_ = 0;
29 
30  last_lumi_ = 0;
33 
34  run_ = 0;
35  lumi_ = 0;
36 
39  }
40 
42 
44  using std::chrono::duration_cast;
45  using std::chrono::milliseconds;
46 
48 
49  ptree doc;
50 
51  // these might be different than the numbers we want to report
52  // rate/stats per lumi are calculated from last_*_ fields
53  doc.put("cmssw_run", run_);
54  doc.put("cmssw_lumi", lumi_);
55  doc.put("events_total", nevents_);
56 
57  // do statistics for the last (elapsed) ls
58  auto lumi_millis = duration_cast<milliseconds>(now - last_lumi_time_).count();
59  auto lumi_events = nevents_ - last_lumi_nevents_;
60  auto lumi_last = last_lumi_;
61 
62  if ((lumi_last > 0) && (lumi_millis > 0)) {
63  doc.put("lumi_number", lumi_last);
64  doc.put("lumi_events", lumi_events);
65  doc.put("lumi_duration_ms", lumi_millis);
66 
67  float rate = ((float)lumi_events * 1000) / lumi_millis;
68  doc.put("events_rate", rate);
69 
70  // also save the history entry
71  ptree plumi;
72  plumi.put("n", lumi_last);
73  plumi.put("nevents", lumi_events);
74  plumi.put("nmillis", lumi_millis);
75  plumi.put("rate", rate);
76 
77  std::time_t hkey = std::time(nullptr);
78  doc.add_child(fmt::sprintf("extra.lumi_stats.%d", hkey), plumi);
79  }
80 
81  outputUpdate(doc);
82  }
83 
85  using std::chrono::duration_cast;
86  using std::chrono::milliseconds;
87 
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 
100  void DQMMonitoringService::evEvent(StreamID const& iContext) {
101  nevents_ += 1;
102  tryUpdate();
103  }
104 
106  using std::chrono::duration_cast;
107  using std::chrono::milliseconds;
108 
109  if (!mstream_)
110  return;
111 
112  try {
114  doc.put("update_timestamp", std::time(nullptr));
115 
116  write_json(mstream_, doc, false);
117  mstream_.flush();
118  } catch (...) {
119  // pass
120  }
121  }
122 
124  if (!mstream_)
125  return;
126 
127  mstream_ << "\n";
128  mstream_.flush();
129 
130  tryUpdate();
131  }
132 
134  using std::chrono::duration_cast;
135  using std::chrono::milliseconds;
136 
137  if (!mstream_)
138  return;
139 
140  // sometimes we don't see any transition for a very long time
141  // but we still want updates
142  // luckily, keepAlive is called rather often by the input source
144  auto millis = duration_cast<milliseconds>(now - last_update_time_).count();
145  if (millis >= (25 * 1000)) {
147  }
148  }
149 
150 } // namespace dqmservices
151 
153 
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:60
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
RunNumber_t run() const
boost::asio::local::stream_protocol::iostream mstream_
tuple DQMMonitoringService
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:96
LuminosityBlockNumber_t luminosityBlock() const
std::chrono::high_resolution_clock::time_point last_lumi_time_
double rate(double x)
Definition: Constants.cc:3
DQMMonitoringService(const edm::ParameterSet &, edm::ActivityRegistry &)
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
std::chrono::high_resolution_clock::time_point last_update_time_
void evLumi(GlobalContext const &)