CMS 3D CMS Logo

FastMonitor.cc
Go to the documentation of this file.
1 /*
2  * FastMonitor.cc
3  *
4  * Created on: Nov 27, 2012
5  * Author: aspataru
6  */
7 
12 
13 #include <fstream>
14 #include <iostream>
15 #include <sstream>
16 #include <cassert>
17 #include <sys/types.h>
18 #include <unistd.h>
19 
20 using namespace jsoncollector;
21 
23  std::string const& defPath, std::string const defGroup, bool strictChecking, bool useSource, bool useDefinition)
24  : defPath_(defPath),
25  strictChecking_(strictChecking),
26  useSource_(useSource),
27  useDefinition_(useDefinition),
28  nStreams_(1),
29  deleteDef_(true) {
30  //get host and PID info
31  if (useSource)
33 
34  //load definition file
35  auto temp = new DataPointDefinition();
37  dpd_ = temp;
38 }
39 
40 FastMonitor::FastMonitor(DataPointDefinition const* dpd, bool strictChecking, bool useSource, bool useDefinition)
41  : strictChecking_(strictChecking), useSource_(useSource), useDefinition_(useDefinition), nStreams_(1), dpd_(dpd) {
42  //get host and PID info
43  if (useSource)
45 }
46 
48  for (auto dp : dataPoints_)
49  delete dp;
50  if (deleteDef_)
51  delete dpd_;
52  if (deleteDefFast_)
53  delete dpdFast_;
54 }
55 
56 void FastMonitor::addFastPathDefinition(std::string const& defPathFast, std::string const defGroupFast, bool strict) {
57  haveFastPath_ = true;
58  defPathFast_ = defPathFast;
59  auto temp = new DataPointDefinition();
61  dpdFast_ = temp;
62  fastPathStrictChecking_ = strict;
63  deleteDefFast_ = true;
64 }
65 
66 //per-process variables
68  bool NAifZeroUpdates,
69  unsigned int* nBins) {
71  dp->trackMonitorable(newMonitorable, NAifZeroUpdates);
72  dp->setNBins(nBins);
73  dataPoints_.push_back(dp);
74  dpNameMap_[newMonitorable->getName()] = dataPoints_.size() - 1;
75 
76  //checks if the same name is registered twice
77  assert(uids_.insert(newMonitorable->getName()).second);
78 }
79 
80 //fast path: no merge operation is performed
83  dp->trackMonitorable(newMonitorable, false);
84  dataPointsFastOnly_.push_back(dp);
85 }
86 
87 //per-stream variables
89  std::vector<unsigned int>* inputs,
90  bool NAifZeroUpdates,
91  unsigned int* nBins) {
93  dp->trackVectorUInt(name, inputs, NAifZeroUpdates);
94  dp->setNBins(nBins);
95  dataPoints_.push_back(dp);
96  dpNameMap_[name] = dataPoints_.size() - 1;
97  assert(uids_.insert(name).second);
98 }
99 
100 //atomic variables with guaranteed updates at the time of reading
102  std::vector<AtomicMonUInt*>* inputs,
103  bool NAifZeroUpdates,
104  unsigned int* nBins) {
105  std::string definitionToPass;
106  if (useDefinition_)
107  definitionToPass = defPath_;
108  DataPoint* dp = new DataPoint(definitionToPass, sourceInfo_);
109  dp->trackVectorUIntAtomic(name, inputs, NAifZeroUpdates);
110  dp->setNBins(nBins);
111  dataPoints_.push_back(dp);
112  dpNameMap_[name] = dataPoints_.size() - 1;
113  assert(uids_.insert(name).second);
114 }
115 
116 void FastMonitor::commit(std::vector<unsigned int>* streamLumisPtr) {
117  std::vector<std::string> const& jsonNames = dpd_->getNames();
118  regDpCount_ = dataPoints_.size();
119  if (strictChecking_)
120  assert(jsonNames.size() == regDpCount_);
121 
122  std::map<unsigned int, bool> hasJson;
123  for (unsigned int i = 0; i < jsonNames.size(); i++) {
124  bool notFoundVar = true;
125  for (unsigned int j = 0; j < regDpCount_; j++) {
126  if (dataPoints_[j]->getName() == jsonNames[i]) {
127  dataPoints_[j]->setOperation(dpd_->getOperationFor(i));
128  jsonDpIndex_.push_back(j);
129  hasJson[j] = true;
130  notFoundVar = false;
131  break;
132  }
133  }
134  if (notFoundVar) {
136  //push dummy DP if not registered by the service so that we output required JSON/CSV
137  DataPoint* dummyDp = new DataPoint(sourceInfo_, defPath_);
138  dummyDp->trackDummy(jsonNames[i], true);
139  dataPoints_.push_back(dummyDp);
140  jsonDpIndex_.push_back(dataPoints_.size() - 1);
141  }
142  }
143  for (unsigned int i = 0; i < regDpCount_; i++) {
144  dataPoints_[i]->setStreamLumiPtr(streamLumisPtr);
145  }
146 
147  //fast path:
148  if (haveFastPath_) {
149  std::vector<std::string> const& fjsonNames = dpdFast_->getNames();
151  assert(!(fastPathStrictChecking_ && fjsonNames.size() == fregDpCount_));
152  std::map<unsigned int, bool> fhasJson;
153  for (unsigned int i = 0; i < fjsonNames.size(); i++) {
154  bool notFoundVar = true;
155  for (unsigned int j = 0; j < fregDpCount_; j++) {
156  if (dataPointsFastOnly_[j]->getName() == fjsonNames[i]) {
158  fhasJson[j] = true;
159  notFoundVar = false;
160  break;
161  }
162  }
163  if (notFoundVar) {
164  //try to find variable among slow variables
165 
166  bool notFoundVarSlow = true;
167  for (unsigned int j = 0; j < regDpCount_; j++) {
168  if (dataPoints_[j]->getName() == fjsonNames[i]) {
169  jsonDpIndexFast_.push_back(dataPoints_[j]);
170  //fhasJson[j]=true;
171  notFoundVarSlow = false;
172  break;
173  }
174  }
175 
176  assert(!(fastPathStrictChecking_ && !notFoundVarSlow));
177  //push dummy DP if not registered by the service so that we output required JSON/CSV
178  if (notFoundVarSlow) {
179  DataPoint* dummyDp = new DataPoint(sourceInfo_, defPathFast_);
180  dummyDp->trackDummy(fjsonNames[i], true);
181  dataPointsFastOnly_.push_back(dummyDp);
182  jsonDpIndexFast_.push_back(dummyDp);
183  }
184  }
185  }
186  }
187 }
188 
189 //update everything
190 void FastMonitor::snap(unsigned int ls) {
191  recentSnaps_++;
193  for (unsigned int i = 0; i < regDpCount_; i++) {
194  dataPoints_[i]->snap(ls);
195  }
196 }
197 
198 //update for global variables as most of them are correct only at global EOL
199 void FastMonitor::snapGlobal(unsigned int ls) {
200  recentSnaps_++;
201  for (unsigned int i = 0; i < regDpCount_; i++) {
202  dataPoints_[i]->snapGlobal(ls);
203  }
204 }
205 
206 //update atomic per-stream vars(e.g. event counters) not updating time-based measurements (mini/microstate)
207 void FastMonitor::snapStreamAtomic(unsigned int ls, unsigned int streamID) {
208  recentSnaps_++;
209  for (unsigned int i = 0; i < regDpCount_; i++) {
210  dataPoints_[i]->snapStreamAtomic(ls, streamID);
211  }
212 }
213 
215  //output what was specified in JSON in the same order (including dummies)
216  unsigned int monSize = jsonDpIndexFast_.size();
217  std::stringstream ss;
218  if (monSize) {
219  for (unsigned int j = 0; j < monSize; j++) {
220  ss << jsonDpIndexFast_[j]->fastOutCSV(sid);
221  if (j < monSize - 1)
222  ss << ",";
223  }
224  }
225  return ss.str();
226 }
227 
228 void FastMonitor::outputCSV(std::string const& path, std::vector<std::string> const& csvs) {
229  std::ofstream outputFile;
231  outputFile << defPathFast_ << std::endl;
232  for (const auto& csvString : csvs)
233  outputFile << csvString << std::endl;
234  outputFile.close();
235 }
236 
237 //get one variable (caller must delete it later)
239  auto it = dpNameMap_.find(name);
240  assert(it != dpNameMap_.end());
241  return dataPoints_[it->second]->mergeAndRetrieveValue(forLumi);
242 }
243 
244 bool FastMonitor::outputFullJSON(std::string const& path, unsigned int lumi, bool output) {
245  LogDebug("FastMonitor") << "SNAP updates -: " << recentSnaps_ << " (by timer: " << recentSnapsTimer_
246  << ") in lumisection ";
247 
249  Json::Value serializeRoot;
250  for (unsigned int j = 0; j < jsonDpIndex_.size(); j++) {
251  dataPoints_[jsonDpIndex_[j]]->mergeAndSerialize(serializeRoot, lumi, j == 0, -1);
252  }
253  if (!output)
254  return false;
255 
257  std::string&& result = writer.write(serializeRoot);
259  return true;
260 }
261 
262 void FastMonitor::discardCollected(unsigned int forLumi) {
263  for (auto dp : dataPoints_)
264  dp->discardCollected(forLumi);
265 }
266 
268  std::stringstream hpid;
269  int pid = (int)getpid();
270  char hostname[128];
271  gethostname(hostname, sizeof hostname);
272  hpid << hostname << "_" << pid;
273  sHPid = hpid.str();
274 }
void registerFastGlobalMonitorable(JsonMonitorable *newMonitorable)
Definition: FastMonitor.cc:81
void addFastPathDefinition(std::string const &defPathFast, std::string const defGroupFast, bool strict)
Definition: FastMonitor.cc:56
void registerStreamMonitorableUIntVecAtomic(std::string const &name, std::vector< AtomicMonUInt *> *inputs, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:101
DataPointDefinition const * dpd_
Definition: FastMonitor.h:106
void trackDummy(std::string const &name, bool setNAifZeroUpdates)
Definition: DataPoint.h:82
void snapStreamAtomic(unsigned int ls, unsigned int streamID)
Definition: FastMonitor.cc:207
std::vector< DataPoint * > dataPointsFastOnly_
Definition: FastMonitor.h:112
assert(be >=bs)
FastMonitor(std::string const &defPath, std::string const defGroup, bool strictChecking, bool useSource=true, bool useDefinition=true)
Definition: FastMonitor.cc:22
void registerGlobalMonitorable(JsonMonitorable *newMonitorable, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:67
std::vector< unsigned int > jsonDpIndex_
Definition: FastMonitor.h:113
U second(std::pair< T, U > const &p)
static bool getDataPointDefinitionFor(std::string &defFilePath, DataPointDefinition *dpd, const std::string *defaultGroup=nullptr)
std::vector< DataPoint * > dataPoints_
Definition: FastMonitor.h:111
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:64
void registerStreamMonitorableUIntVec(std::string const &name, std::vector< unsigned int > *inputs, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:88
std::map< std::string, unsigned int > dpNameMap_
Definition: FastMonitor.h:116
std::unordered_set< std::string > uids_
Definition: FastMonitor.h:123
void outputCSV(std::string const &path, std::vector< std::string > const &csvString)
Definition: FastMonitor.cc:228
static void writeStringToFile(std::string const &filename, std::string &content)
Definition: FileIO.cc:21
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:116
virtual std::string const & getName() const
std::string getCSVString(int sid=-1)
Definition: FastMonitor.cc:214
std::vector< std::string > const & getNames() const
bool outputFullJSON(std::string const &path, unsigned int lumi, bool output=true)
Definition: FastMonitor.cc:244
std::vector< DataPoint * > jsonDpIndexFast_
Definition: FastMonitor.h:114
DataPointDefinition const * dpdFast_
Definition: FastMonitor.h:107
void snap(unsigned int ls)
Definition: FastMonitor.cc:190
OperationType getOperationFor(unsigned int index) const
JsonMonitorable * getMergedIntJForLumi(std::string const &name, unsigned int forLumi)
Definition: FastMonitor.cc:238
std::string getName(const G4String &)
Definition: ForwardName.cc:3
unsigned int recentSnapsTimer_
Definition: FastMonitor.h:119
Definition: output.py:1
void getHostAndPID(std::string &sHPid)
Definition: FastMonitor.cc:267
Represents a JSON value.
Definition: value.h:101
JSON (JavaScript Object Notation).
Definition: DataPoint.h:26
void discardCollected(unsigned int forLumi)
Definition: FastMonitor.cc:262
void snapGlobal(unsigned int ls)
Definition: FastMonitor.cc:199
#define LogDebug(id)