CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
9 /*
10  * This service is very similar to the FastMonitoringService in the HLT,
11  * except that it is used for monitoring online DQM applications
12  */
13 
14 namespace dqmservices {
15 
18  const char* x = getenv("DQM2_SOCKET");
19  if (x) {
20  std::cerr << "Monitoring pipe: " << x << std::endl;
21  mstream_.connect(boost::asio::local::stream_protocol::endpoint(x));
22  } else {
23  std::cerr << "Monitoring file not found, disabling." << std::endl;
24  }
25 
26  // init counters
27  nevents_ = 0;
28 
29  last_lumi_ = 0;
32 
33  run_ = 0;
34  lumi_ = 0;
35 
38 }
39 
41 
43  using std::chrono::duration_cast;
44  using std::chrono::milliseconds;
45 
47 
48  ptree doc;
49 
50  // these might be different than the numbers we want to report
51  // rate/stats per lumi are calculated from last_*_ fields
52  doc.put("cmssw_run", run_);
53  doc.put("cmssw_lumi", lumi_);
54  doc.put("events_total", nevents_);
55 
56  // do statistics for the last (elapsed) ls
57  auto lumi_millis = duration_cast<milliseconds>(now - last_lumi_time_).count();
58  auto lumi_events = nevents_ - last_lumi_nevents_;
59  auto lumi_last = last_lumi_;
60 
61  if ((lumi_last > 0) && (lumi_millis > 0)) {
62  doc.put("lumi_number", lumi_last);
63  doc.put("lumi_events", lumi_events);
64  doc.put("lumi_duration_ms", lumi_millis);
65 
66  float rate = ((float)lumi_events * 1000) / lumi_millis;
67  doc.put("events_rate", rate);
68 
69  // also save the history entry
70  ptree plumi;
71  plumi.put("n", lumi_last);
72  plumi.put("nevents", lumi_events);
73  plumi.put("nmillis", lumi_millis);
74  plumi.put("rate", rate);
75 
76  std::time_t hkey = std::time(NULL);
77  doc.add_child(str(boost::format("extra.lumi_stats.%d") % hkey), plumi);
78  }
79 
80  outputUpdate(doc);
81 }
82 
84  using std::chrono::duration_cast;
85  using std::chrono::milliseconds;
86 
87  // these might be different than the numbers we want to report
88  // rate/stats per lumi are calculated from last_*_ fields
89  run_ = iContext.luminosityBlockID().run();
91 
93 
96  last_lumi_ = lumi_;
97 }
98 
99 void DQMMonitoringService::evEvent(StreamID const& iContext) {
100  nevents_ += 1;
101  tryUpdate();
102 }
103 
105  using std::chrono::duration_cast;
106  using std::chrono::milliseconds;
107 
108  if (!mstream_) return;
109 
110  try {
112  doc.put("update_timestamp", std::time(NULL));
113 
114  write_json(mstream_, doc, false);
115  mstream_.flush();
116  } catch (...) {
117  // pass
118  }
119 }
120 
122  if (!mstream_) return;
123 
124  mstream_ << "\n";
125  mstream_.flush();
126 
127  tryUpdate();
128 }
129 
131  using std::chrono::duration_cast;
132  using std::chrono::milliseconds;
133 
134  if (!mstream_) return;
135 
136  // sometimes we don't see any transition for a very long time
137  // but we still want updates
138  // luckily, keepAlive is called rather often by the input source
140  auto millis = duration_cast<milliseconds>(now - last_update_time_).count();
141  if (millis >= (25 * 1000)) {
143  }
144 }
145 
146 } // end-of-namespace
147 
149 
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:52
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
#define NULL
Definition: scimark2.h:8
string format
Some error handling for the usage.
T x() const
Cartesian x coordinate.
RunNumber_t run() const
boost::asio::local::stream_protocol::iostream mstream_
tuple DQMMonitoringService
#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_
void evLumi(GlobalContext const &)