CMS 3D CMS Logo

DQMMonitoringService.cc
Go to the documentation of this file.
1 #include "DQMMonitoringService.h"
2 
3 #include <boost/algorithm/string.hpp>
4 #include <boost/format.hpp>
5 #include <boost/algorithm/string/predicate.hpp>
6 
7 #include <ctime>
8 #include <iostream>
9 
10 /*
11  * This service is very similar to the FastMonitoringService in the HLT,
12  * except that it is used for monitoring online DQM applications
13  */
14 
15 namespace dqmservices {
16 
19  const char* x = 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(str(boost::format("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 
101  nevents_ += 1;
102  tryUpdate();
103 }
104 
106  using std::chrono::duration_cast;
107  using std::chrono::milliseconds;
108 
109  if (!mstream_) return;
110 
111  try {
113  doc.put("update_timestamp", std::time(nullptr));
114 
115  write_json(mstream_, doc, false);
116  mstream_.flush();
117  } catch (...) {
118  // pass
119  }
120 }
121 
123  if (!mstream_) return;
124 
125  mstream_ << "\n";
126  mstream_.flush();
127 
128  tryUpdate();
129 }
130 
132  using std::chrono::duration_cast;
133  using std::chrono::milliseconds;
134 
135  if (!mstream_) return;
136 
137  // sometimes we don't see any transition for a very long time
138  // but we still want updates
139  // luckily, keepAlive is called rather often by the input source
141  auto millis = duration_cast<milliseconds>(now - last_update_time_).count();
142  if (millis >= (25 * 1000)) {
144  }
145 }
146 
147 } // end-of-namespace
148 
150 
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:57
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
RunNumber_t run() const
format
Some error handling for the usage.
boost::asio::local::stream_protocol::iostream mstream_
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
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_
#define str(s)
void evLumi(GlobalContext const &)