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