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 
28 // -------------------------------------------------------------------
30  : store_(&*edm::Service<DQMStore>()),
31  net_(0),
32  filter_(0),
33  lastFlush_(0),
34  publishFrequency_(5.0)
35 {
37 
38  std::string host = pset.getUntrackedParameter<std::string>("collectorHost", "");
39  int port = pset.getUntrackedParameter<int>("collectorPort", 9090);
40  bool verbose = pset.getUntrackedParameter<bool>("verbose", false);
41  publishFrequency_ = pset.getUntrackedParameter<double>("publishFrequency", publishFrequency_);
43 
44  if (host != "" && port > 0)
45  {
46  net_ = new DQMBasicNet;
47  net_->debug(verbose);
48  net_->updateToCollector(host, port);
49  net_->start();
50  }
51 
52  if (! filter.empty())
53  {
54  try
55  {
56  filter_ = new lat::Regexp(filter);
57  if (! filter_->valid())
58  throw cms::Exception("DQMService")
59  << "Invalid 'filter' parameter value '" << filter << "':"
60  << " bad regular expression syntax at character "
61  << filter_->errorOffset() << ": " << filter_->errorMessage();
62  filter_->study();
63  }
64  catch (lat::Error &e)
65  {
66  throw cms::Exception("DQMService")
67  << "Invalid regular expression 'filter' parameter value '"
68  << filter << "': " << e.explain();
69  }
70  }
71 }
72 
74 {
75  shutdown();
76 }
77 
78 // Flush updates to the network layer at the end of each event. This
79 // is the only point at which the main application and the network
80 // layer interact outside initialisation and exit.
82 {
83  // Avoid sending updates excessively often.
85  double vtime = version * 1e-9;
86  if (vtime - lastFlush_ < publishFrequency_)
87  return;
88 
89  // OK, send an update.
90  if (net_)
91  {
93  std::set<std::string> seen;
94  std::string fullpath;
95 
96  // Lock the network layer so we can modify the data.
97  net_->lock();
98  bool updated = false;
99 
100  // Find updated contents and update the network cache.
101  DQMStore::MEMap::iterator i, e;
102  net_->reserveLocalSpace(store_->data_.size());
103  for (i = store_->data_.begin(), e = store_->data_.end(); i != e; ++i)
104  {
105  const MonitorElement &me = *i;
106  fullpath.clear();
107  fullpath += *me.data_.dirname;
108  if (! me.data_.dirname->empty())
109  fullpath += '/';
110  fullpath += me.data_.objname;
111 
112  if (filter_ && filter_->search(fullpath) < 0)
113  continue;
114 
115  seen.insert(fullpath);
116  if (! me.wasUpdated())
117  continue;
118 
119  o.lastreq = 0;
120  o.hash = DQMNet::dqmhash(fullpath.c_str(), fullpath.size());
121  o.flags = me.data_.flags;
122  o.tag = me.data_.tag;
123  o.version = version;
124  o.dirname = me.data_.dirname;
125  o.objname = me.data_.objname;
126  assert(o.rawdata.empty());
127  assert(o.scalar.empty());
128  assert(o.qdata.empty());
129 
130  // Pack object and reference, scalar and quality data.
131  switch (me.kind())
132  {
136  me.packScalarData(o.scalar, "");
137  break;
138 
139  default:
140  {
141  TBufferFile buffer(TBufferFile::kWrite);
142  buffer.WriteObject(me.object_);
143  if (me.reference_)
144  buffer.WriteObject(me.reference_);
145  else
146  buffer.WriteObjectAny(0, 0);
147  o.rawdata.resize(buffer.Length());
148  memcpy(&o.rawdata[0], buffer.Buffer(), buffer.Length());
150  break;
151  }
152  }
153 
154  // Update.
155  net_->updateLocalObject(o);
156  DQMNet::DataBlob().swap(o.rawdata);
157  std::string().swap(o.scalar);
158  std::string().swap(o.qdata);
159  updated = true;
160  }
161 
162  // Find removed contents and clear the network cache.
163  if (net_->removeLocalExcept(seen))
164  updated = true;
165 
166  // Unlock the network layer.
167  net_->unlock();
168 
169  // Tell network to flush if we updated something.
170  if (updated)
171  net_->sendLocalChanges();
172  }
173 
174  store_->reset();
175  lastFlush_ = lat::Time::current().ns() * 1e-9;
176 
177 
178 }
179 void
181 {
182  // Call a function independent to the framework
183  flushStandalone();
184 }
185 
186 // Disengage the network service.
187 void
189 {
190  // If we have a network, let it go.
191  if (net_)
192  net_->shutdown();
193 }
194 
195 #endif // !WITHOUT_CMS_FRAMEWORK
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
QReports qreports
Definition: DQMNet.h:106
static std::recursive_mutex s_mutex
Definition: DQMService.cc:17
void shutdown(void)
Definition: DQMService.cc:188
uint64_t version
Definition: DQMNet.h:99
double publishFrequency_
Definition: DQMService.h:28
DQMScope(void)
Definition: DQMService.cc:21
void watchPostEvent(PostEvent::slot_type const &iSlot)
uint32_t flags
Definition: DQMNet.h:97
double lastFlush_
Definition: DQMService.h:27
lat::Regexp * filter_
Definition: DQMService.h:26
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 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
uint64_t lastreq
Definition: DQMNet.h:112
DQMStore * store_
Definition: DQMService.h:24
void flush(edm::StreamContext const &sc)
Definition: DQMService.cc:180
std::vector< unsigned char > DataBlob
Definition: DQMNet.h:79
static size_t dqmhash(const void *key, size_t keylen)
Definition: DQMNet.h:215
Kind kind(void) const
Get the type of the monitor element.
Definition: mlp_gen.h:16
void flushStandalone()
Definition: DQMService.cc:81
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:686
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:29
~DQMService(void)
Definition: DQMService.cc:73
void reset(void)
Definition: DQMStore.cc:1966
DataBlob rawdata
Definition: DQMNet.h:113