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 #include <boost/filesystem/fstream.hpp>
20 
21 using namespace jsoncollector;
22 
24  std::string const& defPath, std::string const defGroup, bool strictChecking, bool useSource, bool useDefinition)
25  : defPath_(defPath),
26  strictChecking_(strictChecking),
27  useSource_(useSource),
28  useDefinition_(useDefinition),
29  nStreams_(1),
30  deleteDef_(true) {
31  //get host and PID info
32  if (useSource)
34 
35  //load definition file
36  dpd_ = new DataPointDefinition();
38 }
39 
40 FastMonitor::FastMonitor(DataPointDefinition* 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;
61  fastPathStrictChecking_ = strict;
62  deleteDefFast_ = true;
63 }
64 
65 //per-process variables
67  bool NAifZeroUpdates,
68  unsigned int* nBins) {
70  dp->trackMonitorable(newMonitorable, NAifZeroUpdates);
71  dp->setNBins(nBins);
72  dataPoints_.push_back(dp);
73  dpNameMap_[newMonitorable->getName()] = dataPoints_.size() - 1;
74 
75  //checks if the same name is registered twice
76  assert(uids_.insert(newMonitorable->getName()).second);
77 }
78 
79 //fast path: no merge operation is performed
82  dp->trackMonitorable(newMonitorable, false);
83  dataPointsFastOnly_.push_back(dp);
84 }
85 
86 //per-stream variables
88  std::vector<unsigned int>* inputs,
89  bool NAifZeroUpdates,
90  unsigned int* nBins) {
92  dp->trackVectorUInt(name, inputs, NAifZeroUpdates);
93  dp->setNBins(nBins);
94  dataPoints_.push_back(dp);
95  dpNameMap_[name] = dataPoints_.size() - 1;
96  assert(uids_.insert(name).second);
97 }
98 
99 //atomic variables with guaranteed updates at the time of reading
101  std::vector<AtomicMonUInt*>* inputs,
102  bool NAifZeroUpdates,
103  unsigned int* nBins) {
104  std::string definitionToPass;
105  if (useDefinition_)
106  definitionToPass = defPath_;
107  DataPoint* dp = new DataPoint(definitionToPass, sourceInfo_);
108  dp->trackVectorUIntAtomic(name, inputs, NAifZeroUpdates);
109  dp->setNBins(nBins);
110  dataPoints_.push_back(dp);
111  dpNameMap_[name] = dataPoints_.size() - 1;
112  assert(uids_.insert(name).second);
113 }
114 
115 void FastMonitor::commit(std::vector<unsigned int>* streamLumisPtr) {
116  std::vector<std::string> const& jsonNames = dpd_->getNames();
117  regDpCount_ = dataPoints_.size();
118  if (strictChecking_)
119  assert(jsonNames.size() == regDpCount_);
120 
121  std::map<unsigned int, bool> hasJson;
122  for (unsigned int i = 0; i < jsonNames.size(); i++) {
123  bool notFoundVar = true;
124  for (unsigned int j = 0; j < regDpCount_; j++) {
125  if (dataPoints_[j]->getName() == jsonNames[i]) {
126  dataPoints_[j]->setOperation(dpd_->getOperationFor(i));
127  jsonDpIndex_.push_back(j);
128  hasJson[j] = true;
129  notFoundVar = false;
130  break;
131  }
132  }
133  if (notFoundVar) {
135  //push dummy DP if not registered by the service so that we output required JSON/CSV
136  DataPoint* dummyDp = new DataPoint(sourceInfo_, defPath_);
137  dummyDp->trackDummy(jsonNames[i], true);
138  dataPoints_.push_back(dummyDp);
139  jsonDpIndex_.push_back(dataPoints_.size() - 1);
140  }
141  }
142  for (unsigned int i = 0; i < regDpCount_; i++) {
143  dataPoints_[i]->setStreamLumiPtr(streamLumisPtr);
144  }
145 
146  //fast path:
147  if (haveFastPath_) {
148  std::vector<std::string> const& fjsonNames = dpdFast_->getNames();
150  assert(!(fastPathStrictChecking_ && fjsonNames.size() == fregDpCount_));
151  std::map<unsigned int, bool> fhasJson;
152  for (unsigned int i = 0; i < fjsonNames.size(); i++) {
153  bool notFoundVar = true;
154  for (unsigned int j = 0; j < fregDpCount_; j++) {
155  if (dataPointsFastOnly_[j]->getName() == fjsonNames[i]) {
157  fhasJson[j] = true;
158  notFoundVar = false;
159  break;
160  }
161  }
162  if (notFoundVar) {
163  //try to find variable among slow variables
164 
165  bool notFoundVarSlow = true;
166  for (unsigned int j = 0; j < regDpCount_; j++) {
167  if (dataPoints_[j]->getName() == fjsonNames[i]) {
168  jsonDpIndexFast_.push_back(dataPoints_[j]);
169  //fhasJson[j]=true;
170  notFoundVarSlow = false;
171  break;
172  }
173  }
174 
175  assert(!(fastPathStrictChecking_ && !notFoundVarSlow));
176  //push dummy DP if not registered by the service so that we output required JSON/CSV
177  if (notFoundVarSlow) {
178  DataPoint* dummyDp = new DataPoint(sourceInfo_, defPathFast_);
179  dummyDp->trackDummy(fjsonNames[i], true);
180  dataPointsFastOnly_.push_back(dummyDp);
181  jsonDpIndexFast_.push_back(dummyDp);
182  }
183  }
184  }
185  }
186 }
187 
188 //update everything
189 void FastMonitor::snap(unsigned int ls) {
190  recentSnaps_++;
192  for (unsigned int i = 0; i < regDpCount_; i++) {
193  dataPoints_[i]->snap(ls);
194  }
195 }
196 
197 //update for global variables as most of them are correct only at global EOL
198 void FastMonitor::snapGlobal(unsigned int ls) {
199  recentSnaps_++;
200  for (unsigned int i = 0; i < regDpCount_; i++) {
201  dataPoints_[i]->snapGlobal(ls);
202  }
203 }
204 
205 //update atomic per-stream vars(e.g. event counters) not updating time-based measurements (mini/microstate)
206 void FastMonitor::snapStreamAtomic(unsigned int ls, unsigned int streamID) {
207  recentSnaps_++;
208  for (unsigned int i = 0; i < regDpCount_; i++) {
209  dataPoints_[i]->snapStreamAtomic(ls, streamID);
210  }
211 }
212 
214  //output what was specified in JSON in the same order (including dummies)
215  unsigned int monSize = jsonDpIndexFast_.size();
216  std::stringstream ss;
217  if (monSize) {
218  for (unsigned int j = 0; j < monSize; j++) {
219  ss << jsonDpIndexFast_[j]->fastOutCSV(sid);
220  if (j < monSize - 1)
221  ss << ",";
222  }
223  }
224  return ss.str();
225 }
226 
227 void FastMonitor::outputCSV(std::string const& path, std::string const& csvString) {
228  std::ofstream outputFile;
230  outputFile << defPathFast_ << std::endl;
231  outputFile << csvString << std::endl;
232  outputFile.close();
233 }
234 
235 //get one variable (caller must delete it later)
237  auto it = dpNameMap_.find(name);
238  assert(it != dpNameMap_.end());
239  return dataPoints_[it->second]->mergeAndRetrieveValue(forLumi);
240 }
241 
242 bool FastMonitor::outputFullJSONs(std::string const& pathstem, std::string const& ext, unsigned int lumi) {
243  LogDebug("FastMonitor") << "SNAP updates -: " << recentSnaps_ << " (by timer: " << recentSnapsTimer_
244  << ") in lumisection ";
245 
247  for (unsigned int i = 0; i < nStreams_; i++) {
248  Json::Value serializeRoot;
249  for (unsigned int j = 0; j < jsonDpIndex_.size(); j++) {
250  dataPoints_[jsonDpIndex_[j]]->mergeAndSerialize(serializeRoot, lumi, true, i);
251  }
252  //get extension
253  std::stringstream tidext;
254  tidext << "_tid" << i;
255  std::string path = pathstem + tidext.str() + ext;
256 
258  std::string&& result = writer.write(serializeRoot);
260  }
261  return true;
262 }
263 
264 bool FastMonitor::outputFullJSON(std::string const& path, unsigned int lumi) {
265  LogDebug("FastMonitor") << "SNAP updates -: " << recentSnaps_ << " (by timer: " << recentSnapsTimer_
266  << ") in lumisection ";
267 
269  Json::Value serializeRoot;
270  for (unsigned int j = 0; j < jsonDpIndex_.size(); j++) {
271  dataPoints_[jsonDpIndex_[j]]->mergeAndSerialize(serializeRoot, lumi, j == 0, -1);
272  }
273 
275  std::string&& result = writer.write(serializeRoot);
277  return true;
278 }
279 
280 void FastMonitor::discardCollected(unsigned int forLumi) {
281  for (auto dp : dataPoints_)
282  dp->discardCollected(forLumi);
283 }
284 
286  std::stringstream hpid;
287  int pid = (int)getpid();
288  char hostname[128];
289  gethostname(hostname, sizeof hostname);
290  hpid << hostname << "_" << pid;
291  sHPid = hpid.str();
292 }
eostools.ls
def ls(path, rec=False)
Definition: eostools.py:349
ext
Definition: memstream.h:15
mps_fire.i
i
Definition: mps_fire.py:428
jsoncollector::FastMonitor::uids_
std::unordered_set< std::string > uids_
Definition: FastMonitor.h:124
MessageLogger.h
jsoncollector::FastMonitor::recentSnapsTimer_
unsigned int recentSnapsTimer_
Definition: FastMonitor.h:120
jsoncollector::DataPointDefinition::getNames
std::vector< std::string > const & getNames()
Definition: DataPointDefinition.h:44
jsoncollector::FastMonitor::dataPoints_
std::vector< DataPoint * > dataPoints_
Definition: FastMonitor.h:112
jsoncollector::FastMonitor::commit
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:115
jsoncollector::FastMonitor::deleteDefFast_
bool deleteDefFast_
Definition: FastMonitor.h:110
jsoncollector::FastMonitor::getMergedIntJForLumi
JsonMonitorable * getMergedIntJForLumi(std::string const &name, unsigned int forLumi)
Definition: FastMonitor.cc:236
cms::cuda::assert
assert(be >=bs)
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
cscNeutronWriter_cfi.writer
writer
Definition: cscNeutronWriter_cfi.py:6
jsoncollector::FastMonitor::recentSnaps_
unsigned int recentSnaps_
Definition: FastMonitor.h:119
jsoncollector::FastMonitor::sourceInfo_
std::string sourceInfo_
Definition: FastMonitor.h:106
jsoncollector::FastMonitor::getHostAndPID
void getHostAndPID(std::string &sHPid)
Definition: FastMonitor.cc:285
jsoncollector::FastMonitor::jsonDpIndexFast_
std::vector< DataPoint * > jsonDpIndexFast_
Definition: FastMonitor.h:115
FileIO.h
jsoncollector::FastMonitor::registerGlobalMonitorable
void registerGlobalMonitorable(JsonMonitorable *newMonitorable, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:66
jsoncollector::FastMonitor::dpdFast_
DataPointDefinition * dpdFast_
Definition: FastMonitor.h:108
jsoncollector::JsonMonitorable::getName
virtual std::string & getName()
Definition: JsonMonitorable.h:40
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
jsoncollector::FastMonitor::fregDpCount_
unsigned int fregDpCount_
Definition: FastMonitor.h:122
jsoncollector::FastMonitor::registerStreamMonitorableUIntVec
void registerStreamMonitorableUIntVec(std::string const &name, std::vector< unsigned int > *inputs, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:87
download_sqlite_cfg.outputFile
outputFile
Definition: download_sqlite_cfg.py:5
jsoncollector::DataPointDefinition::getDataPointDefinitionFor
static bool getDataPointDefinitionFor(std::string &defFilePath, DataPointDefinition *dpd, const std::string *defaultGroup=nullptr)
Definition: DataPointDefinition.cc:32
jsoncollector::FastMonitor::outputFullJSONs
bool outputFullJSONs(std::string const &pathstem, std::string const &ext, unsigned int lumi)
Definition: FastMonitor.cc:242
jsoncollector::FastMonitor::nStreams_
unsigned int nStreams_
Definition: FastMonitor.h:104
jsoncollector::DataPointDefinition::getOperationFor
OperationType getOperationFor(unsigned int index)
Definition: DataPointDefinition.cc:78
jsoncollector::FastMonitor::addFastPathDefinition
void addFastPathDefinition(std::string const &defPathFast, std::string const defGroupFast, bool strict)
Definition: FastMonitor.cc:56
Calorimetry_cff.dp
dp
Definition: Calorimetry_cff.py:157
jsoncollector::FastMonitor::defPath_
std::string defPath_
Definition: FastMonitor.h:96
jsoncollector::FastMonitor::outputCSV
void outputCSV(std::string const &path, std::string const &csvString)
Definition: FastMonitor.cc:227
getName
TString getName(TString structure, int layer, TString geometry)
Definition: DMRtrends.cc:235
jsoncollector::FastMonitor::deleteDef_
bool deleteDef_
Definition: FastMonitor.h:109
seedmultiplicitymonitor_newtracking_cfi.nBins
nBins
Definition: seedmultiplicitymonitor_newtracking_cfi.py:8
Json::StyledWriter
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:63
jsoncollector::FastMonitor::haveFastPath_
bool haveFastPath_
Definition: FastMonitor.h:102
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
funct::true
true
Definition: Factorize.h:173
jsoncollector::FastMonitor::snapGlobal
void snapGlobal(unsigned int ls)
Definition: FastMonitor.cc:198
JsonSerializable.h
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:223
jsoncollector::FastMonitor::fastPathStrictChecking_
bool fastPathStrictChecking_
Definition: FastMonitor.h:99
jsoncollector::FastMonitor::regDpCount_
unsigned int regDpCount_
Definition: FastMonitor.h:121
createfilelist.int
int
Definition: createfilelist.py:10
jsoncollector::FastMonitor::registerStreamMonitorableUIntVecAtomic
void registerStreamMonitorableUIntVecAtomic(std::string const &name, std::vector< AtomicMonUInt * > *inputs, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:100
jsoncollector::FileIO::writeStringToFile
static void writeStringToFile(std::string const &filename, std::string &content)
Definition: FileIO.cc:21
PixelMapPlotter.inputs
inputs
Definition: PixelMapPlotter.py:490
jsoncollector::FastMonitor::~FastMonitor
virtual ~FastMonitor()
Definition: FastMonitor.cc:47
jsoncollector::DataPoint
Definition: DataPoint.h:36
jsoncollector::FastMonitor::registerFastGlobalMonitorable
void registerFastGlobalMonitorable(JsonMonitorable *newMonitorable)
Definition: FastMonitor.cc:80
jsoncollector::FastMonitor::useDefinition_
bool useDefinition_
Definition: FastMonitor.h:101
jsoncollector::FastMonitor::dpd_
DataPointDefinition * dpd_
Definition: FastMonitor.h:107
jsoncollector::DataPoint::trackDummy
void trackDummy(std::string const &name, bool setNAifZeroUpdates)
Definition: DataPoint.h:80
jsoncollector::JsonMonitorable
Definition: JsonMonitorable.h:24
jsoncollector
Definition: DataPoint.h:26
jsoncollector::DataPointDefinition
Definition: DataPointDefinition.h:20
jsoncollector::FastMonitor::outputFullJSON
bool outputFullJSON(std::string const &path, unsigned int lumi)
Definition: FastMonitor.cc:264
jsoncollector::FastMonitor::snapStreamAtomic
void snapStreamAtomic(unsigned int ls, unsigned int streamID)
Definition: FastMonitor.cc:206
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
jsoncollector::FastMonitor::dpNameMap_
std::map< std::string, unsigned int > dpNameMap_
Definition: FastMonitor.h:117
jsoncollector::FastMonitor::snap
void snap(unsigned int ls)
Definition: FastMonitor.cc:189
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
jsoncollector::FastMonitor::strictChecking_
bool strictChecking_
Definition: FastMonitor.h:98
mps_fire.result
result
Definition: mps_fire.py:311
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
jsoncollector::FastMonitor::FastMonitor
FastMonitor(std::string const &defPath, std::string const defGroup, bool strictChecking, bool useSource=true, bool useDefinition=true)
Definition: FastMonitor.cc:23
jsoncollector::FastMonitor::getCSVString
std::string getCSVString(int sid=-1)
Definition: FastMonitor.cc:213
jsoncollector::FastMonitor::jsonDpIndex_
std::vector< unsigned int > jsonDpIndex_
Definition: FastMonitor.h:114
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
jsoncollector::FastMonitor::discardCollected
void discardCollected(unsigned int forLumi)
Definition: FastMonitor.cc:280
lumi
Definition: LumiSectionData.h:20
pileupReCalc_HLTpaths.trunc
trunc
Definition: pileupReCalc_HLTpaths.py:144
jsoncollector::FastMonitor::defPathFast_
std::string defPathFast_
Definition: FastMonitor.h:97
FastMonitor.h
Json::Value
Represents a JSON value.
Definition: value.h:99
jsoncollector::FastMonitor::dataPointsFastOnly_
std::vector< DataPoint * > dataPointsFastOnly_
Definition: FastMonitor.h:113