CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMService.cc
Go to the documentation of this file.
1 #if !WITHOUT_CMS_FRAMEWORK
8 # include "classlib/utils/Regexp.h"
9 # include "classlib/utils/Error.h"
10 # include <mutex>
11 # include <iostream>
12 # include <string>
13 # include <memory>
14 #include "TBufferFile.h"
15 
16 // -------------------------------------------------------------------
17 static std::recursive_mutex s_mutex;
18 
22 { s_mutex.lock(); }
23 
26 { s_mutex.unlock(); }
27 
29 static void
31 { s_mutex.lock(); }
32 
33 static void
35 { restrictDQMAccess(); }
36 
37 static void
39 { restrictDQMAccess(); }
40 
42 static void
44 { s_mutex.unlock(); }
45 
46 static void
48 { releaseDQMAccess(); }
49 
50 static void
52 { releaseDQMAccess(); }
53 
54 // -------------------------------------------------------------------
56  : store_(&*edm::Service<DQMStore>()),
57  net_(0),
58  filter_(0),
59  lastFlush_(0),
60  publishFrequency_(5.0)
61 {
70 
71  std::string host = pset.getUntrackedParameter<std::string>("collectorHost", "");
72  int port = pset.getUntrackedParameter<int>("collectorPort", 9090);
73  bool verbose = pset.getUntrackedParameter<bool>("verbose", false);
74  publishFrequency_ = pset.getUntrackedParameter<double>("publishFrequency", publishFrequency_);
76 
77  if (host != "" && port > 0)
78  {
79  net_ = new DQMBasicNet;
80  net_->debug(verbose);
81  net_->updateToCollector(host, port);
82  net_->start();
83  }
84 
85  if (! filter.empty())
86  {
87  try
88  {
89  filter_ = new lat::Regexp(filter);
90  if (! filter_->valid())
91  throw cms::Exception("DQMService")
92  << "Invalid 'filter' parameter value '" << filter << "':"
93  << " bad regular expression syntax at character "
94  << filter_->errorOffset() << ": " << filter_->errorMessage();
95  filter_->study();
96  }
97  catch (lat::Error &e)
98  {
99  throw cms::Exception("DQMService")
100  << "Invalid regular expression 'filter' parameter value '"
101  << filter << "': " << e.explain();
102  }
103  }
104 }
105 
107 {
108  shutdown();
109 }
110 
111 // Flush updates to the network layer at the end of each event. This
112 // is the only point at which the main application and the network
113 // layer interact outside initialisation and exit.
115 {
116  // Avoid sending updates excessively often.
118  double vtime = version * 1e-9;
119  if (vtime - lastFlush_ < publishFrequency_)
120  return;
121 
122  // OK, send an update.
123  if (net_)
124  {
126  std::set<std::string> seen;
127  std::string fullpath;
128 
129  // Lock the network layer so we can modify the data.
130  net_->lock();
131  bool updated = false;
132 
133  // Find updated contents and update the network cache.
134  DQMStore::MEMap::iterator i, e;
135  net_->reserveLocalSpace(store_->data_.size());
136  for (i = store_->data_.begin(), e = store_->data_.end(); i != e; ++i)
137  {
138  const MonitorElement &me = *i;
139  fullpath.clear();
140  fullpath += *me.data_.dirname;
141  if (! me.data_.dirname->empty())
142  fullpath += '/';
143  fullpath += me.data_.objname;
144 
145  if (filter_ && filter_->search(fullpath) < 0)
146  continue;
147 
148  seen.insert(fullpath);
149  if (! me.wasUpdated())
150  continue;
151 
152  o.lastreq = 0;
153  o.hash = DQMNet::dqmhash(fullpath.c_str(), fullpath.size());
154  o.flags = me.data_.flags;
155  o.tag = me.data_.tag;
156  o.version = version;
157  o.dirname = me.data_.dirname;
158  o.objname = me.data_.objname;
159  assert(o.rawdata.empty());
160  assert(o.scalar.empty());
161  assert(o.qdata.empty());
162 
163  // Pack object and reference, scalar and quality data.
164  switch (me.kind())
165  {
169  me.packScalarData(o.scalar, "");
170  break;
171 
172  default:
173  {
174  TBufferFile buffer(TBufferFile::kWrite);
175  buffer.WriteObject(me.object_);
176  if (me.reference_)
177  buffer.WriteObject(me.reference_);
178  else
179  buffer.WriteObjectAny(0, 0);
180  o.rawdata.resize(buffer.Length());
181  memcpy(&o.rawdata[0], buffer.Buffer(), buffer.Length());
183  break;
184  }
185  }
186 
187  // Update.
188  net_->updateLocalObject(o);
189  DQMNet::DataBlob().swap(o.rawdata);
190  std::string().swap(o.scalar);
191  std::string().swap(o.qdata);
192  updated = true;
193  }
194 
195  // Find removed contents and clear the network cache.
196  if (net_->removeLocalExcept(seen))
197  updated = true;
198 
199  // Unlock the network layer.
200  net_->unlock();
201 
202  // Tell network to flush if we updated something.
203  if (updated)
204  net_->sendLocalChanges();
205  }
206 
207  store_->reset();
208  lastFlush_ = lat::Time::current().ns() * 1e-9;
209 
210 
211 }
212 void
214 {
215  // Call a function independent to the framework
216  flushStandalone();
217 }
218 
219 // Disengage the network service.
220 void
222 {
223  // If we have a network, let it go.
224  if (net_)
225  net_->shutdown();
226 }
227 
228 #endif // !WITHOUT_CMS_FRAMEWORK
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
static void releaseDQMAccessM(const edm::ModuleDescription &)
Definition: DQMService.cc:47
QReports qreports
Definition: DQMNet.h:106
static std::recursive_mutex s_mutex
Definition: DQMService.cc:17
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
void shutdown(void)
Definition: DQMService.cc:221
uint64_t version
Definition: DQMNet.h:99
double publishFrequency_
Definition: DQMService.h:28
DQMScope(void)
Definition: DQMService.cc:21
void watchPostModule(PostModule::slot_type const &iSlot)
uint32_t flags
Definition: DQMNet.h:97
void watchPreSourceConstruction(PreSourceConstruction::slot_type const &iSlot)
double lastFlush_
Definition: DQMService.h:27
void watchPostSourceConstruction(PostSourceConstruction::slot_type const &iSlot)
lat::Regexp * filter_
Definition: DQMService.h:26
void watchPostSourceEvent(PostSourceEvent::slot_type const &iSlot)
uint32_t tag
Definition: DQMNet.h:98
const std::string * dirname
Definition: DQMNet.h:104
std::string qdata
Definition: DQMNet.h:115
int port
Definition: query.py:115
void watchPreModule(PreModule::slot_type const &iSlot)
void packScalarData(std::string &into, const char *prefix) const
convert scalar data into a string.
bool wasUpdated(void) const
true if ME was updated in last monitoring cycle
void watchPostProcessEvent(PostProcessEvent::slot_type const &iSlot)
static void releaseDQMAccess(void)
Release access to the DQM core.
Definition: DQMService.cc:43
static void restrictDQMAccessM(const edm::ModuleDescription &)
Definition: DQMService.cc:34
uint64_t lastreq
Definition: DQMNet.h:112
static void restrictDQMAccessS(edm::StreamID)
Definition: DQMService.cc:38
DQMStore * store_
Definition: DQMService.h:24
std::vector< unsigned char > DataBlob
Definition: DQMNet.h:79
static size_t dqmhash(const void *key, size_t keylen)
Definition: DQMNet.h:215
static void restrictDQMAccess(void)
Restrict access to the DQM core.
Definition: DQMService.cc:30
Kind kind(void) const
Get the type of the monitor element.
Definition: mlp_gen.h:16
void flushStandalone()
Definition: DQMService.cc:114
std::string objname
Definition: DQMNet.h:105
std::string scalar
Definition: DQMNet.h:114
DQMNet::CoreObject data_
string host
Definition: query.py:114
~DQMScope(void)
Release access lock to the DQM core.
Definition: DQMService.cc:25
unsigned long long uint64_t
Definition: Time.h:15
MEMap data_
Definition: DQMStore.h:606
static void releaseDQMAccessS(edm::StreamID)
Definition: DQMService.cc:51
uint64_t hash
Definition: DQMNet.h:111
static void packQualityData(std::string &into, const QReports &qr)
Definition: DQMNet.cc:177
DQMService(const edm::ParameterSet &pset, edm::ActivityRegistry &ar)
Definition: DQMService.cc:55
void flush(const edm::Event &, const edm::EventSetup &)
Definition: DQMService.cc:213
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
~DQMService(void)
Definition: DQMService.cc:106
void reset(void)
Definition: DQMStore.cc:1911
DataBlob rawdata
Definition: DQMNet.h:113