CMS 3D CMS Logo

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 
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 
edm::GlobalContext::luminosityBlockID
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:60
edm::StreamID
Definition: StreamID.h:30
dqmservices::DQMMonitoringService::outputUpdate
void outputUpdate(ptree &doc)
Definition: DQMMonitoringService.cc:105
common_cff.doc
doc
Definition: common_cff.py:54
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
dqmservices
Definition: DQMFileIterator.cc:18
dqmservices::DQMMonitoringService::keepAlive
void keepAlive()
Definition: DQMMonitoringService.cc:123
dqmservices::DQMMonitoringService::last_lumi_
unsigned long last_lumi_
Definition: DQMMonitoringService.h:68
submitPVValidationJobs.now
now
Definition: submitPVValidationJobs.py:639
DEFINE_FWK_SERVICE
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:96
protons_cff.time
time
Definition: protons_cff.py:39
dqmservices::DQMMonitoringService::lumi_
unsigned long lumi_
Definition: DQMMonitoringService.h:75
RPCpg::rate
double rate(double x)
Definition: Constants.cc:3
dqmservices::DQMMonitoringService::last_lumi_nevents_
long last_lumi_nevents_
Definition: DQMMonitoringService.h:72
dqmservices::DQMMonitoringService::run_
unsigned long run_
Definition: DQMMonitoringService.h:74
dqmservices::DQMMonitoringService::outputLumiUpdate
void outputLumiUpdate()
Definition: DQMMonitoringService.cc:43
edm::ActivityRegistry
Definition: ActivityRegistry.h:134
ServiceMaker.h
dqmservices::DQMMonitoringService::evEvent
void evEvent(StreamID const &)
Definition: DQMMonitoringService.cc:100
edm::GlobalContext
Definition: GlobalContext.h:29
dqmservices::DQMMonitoringService::~DQMMonitoringService
~DQMMonitoringService()
Definition: DQMMonitoringService.cc:41
edm::ParameterSet
Definition: ParameterSet.h:47
visualization-live-secondInstance_cfg.DQMMonitoringService
DQMMonitoringService
Definition: visualization-live-secondInstance_cfg.py:112
edm::LuminosityBlockID::luminosityBlock
LuminosityBlockNumber_t luminosityBlock() const
Definition: LuminosityBlockID.h:42
dqmservices::DQMMonitoringService::mstream_
boost::asio::local::stream_protocol::iostream mstream_
Definition: DQMMonitoringService.h:61
dqmservices::DQMMonitoringService::DQMMonitoringService
DQMMonitoringService(const edm::ParameterSet &, edm::ActivityRegistry &)
Definition: DQMMonitoringService.cc:18
edm::ActivityRegistry::watchPreGlobalBeginLumi
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
Definition: ActivityRegistry.h:391
dqmservices::DQMMonitoringService::last_lumi_time_
std::chrono::high_resolution_clock::time_point last_lumi_time_
Definition: DQMMonitoringService.h:70
edm::ActivityRegistry::watchPreSourceEvent
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
Definition: ActivityRegistry.h:184
edm::LuminosityBlockID::run
RunNumber_t run() const
Definition: LuminosityBlockID.h:41
dqmservices::DQMMonitoringService::nevents_
long nevents_
Definition: DQMMonitoringService.h:64
dqmservices::DQMMonitoringService::evLumi
void evLumi(GlobalContext const &)
Definition: DQMMonitoringService.cc:84
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
dqmservices::DQMMonitoringService::last_update_time_
std::chrono::high_resolution_clock::time_point last_update_time_
Definition: DQMMonitoringService.h:71
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
dqmservices::DQMMonitoringService
Definition: DQMMonitoringService.h:44
DQMMonitoringService.h
dqmservices::DQMMonitoringService::tryUpdate
void tryUpdate()
Definition: DQMMonitoringService.cc:133