/
derived_stats.h
384 lines (336 loc) · 12.2 KB
/
derived_stats.h
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/
//
// derived_stats.h
//
// This file has the interface for derived stats
// towards the sandesh generated code, and
// the derived stats base class
//
#ifndef __DERIVED_STATS_H__
#define __DERIVED_STATS_H__
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/function.hpp>
#include <base/time_util.h>
template <typename T>
class SandeshStructDeleteTrait;
namespace contrail {
namespace sandesh {
enum DSReturnType {
DSR_INVALID=0,
DSR_SKIP,
DSR_OK
};
template<typename ElemT>
std::map<std::string, ElemT> DerivedStatsAgg(const std::map<std::string, ElemT> & raw,
std::map<std::string, ElemT> & agg,
const std::map<std::string, bool> &del) {
std::map<std::string, ElemT> diff;
typename std::map<std::string, ElemT>::const_iterator rit;
typename std::map<std::string, ElemT>::iterator ait;
// Go through all raw elements to generate diff and update agg
for (rit=raw.begin(); rit!= raw.end(); rit++) {
ait=agg.find(rit->first);
// The aggregate already has this element
if (ait!=agg.end()) {
diff[rit->first] = rit->second - ait->second;
// Remove from agg if this element is marked for deletion
// (It will still be in the diff)
if (!del.at(rit->first)) ait->second = ait->second + diff[rit->first];
else agg.erase(ait);
} else {
diff[rit->first] = rit->second;
if (!del.at(rit->first)) agg[rit->first] = diff[rit->first];
}
}
return diff;
}
template<template<class,class> class DSTT, typename ElemT, typename ResultT>
void DerivedStatsMerge(const std::map<std::string, ElemT> & raw,
std::map<std::string, boost::shared_ptr<DSTT<ElemT,ResultT> > > & dsm,
std::string annotation, const std::map<std::string, bool> &del,
uint64_t mono_usec) {
std::map<std::string, bool> srcmap;
typename std::map<std::string, ElemT>::const_iterator rit;
std::map<std::string, bool>::const_iterator dlt;
// If the new map is empty, clear all DS objects
if (raw.empty()) {
dsm.clear();
return;
}
for(rit=raw.begin(); rit!= raw.end(); rit++) {
srcmap[rit->first] = false;
}
// Go through all existing DS object
typename std::map<std::string,
boost::shared_ptr<DSTT<
ElemT,ResultT> > >::iterator dt = dsm.begin();
while (dt != dsm.end()) {
rit = raw.find(dt->first);
dlt = del.find(dt->first);
if (rit != raw.end()) {
assert(dlt!=del.end());
// we have information about this element
srcmap[rit->first] = true;
if (dlt->second) {
// raw element is requesting deletion
typename std::map<std::string,
boost::shared_ptr<DSTT<
ElemT,ResultT> > >::iterator et = dt;
dt++;
dsm.erase(et);
} else {
dt->second->Update(rit->second, mono_usec);
++dt;
}
} else {
// We have no information about this element
++dt;
}
}
// Now add all new entries that are not requesting deletion
std::map<std::string, bool>::iterator mt;
for (mt = srcmap.begin(); mt != srcmap.end(); mt++) {
rit = raw.find(mt->first);
dlt = del.find(mt->first);
if ((mt->second == false) && (!dlt->second)) {
assert(dsm.find(mt->first) == dsm.end());
dsm[mt->first] = boost::make_shared<DSTT<ElemT,ResultT> >(annotation);
dsm[mt->first]->Update((raw.find(mt->first))->second, mono_usec);
}
}
}
template <template<class,class> class DSTT, typename ElemT, typename ResultT>
class DerivedStatsIf {
private:
typedef std::map<std::string, boost::shared_ptr<DSTT<ElemT,ResultT> > > result_map;
boost::shared_ptr<result_map> dsm_;
boost::shared_ptr<DSTT<ElemT,ResultT> > ds_;
std::string annotation_;
bool is_agg_;
ElemT agg_;
std::map<std::string, ElemT> aggm_;
ElemT diff_;
std::map<std::string, ElemT> diffm_;
public:
DerivedStatsIf(std::string annotation, bool is_agg = false):
annotation_(annotation), is_agg_(is_agg) {}
bool IsResult(void) const {
if ((!ds_) && (!dsm_)) return false;
else return true;
}
// This is the interface to retrieve the current value
// of the DerivedStat object.
void FillResult(ResultT &res, bool& isset, bool force=false) const {
isset = false;
if (ds_) {
DSReturnType rt = ds_->FillResult(res);
if ((force && rt) || (!force && rt == DSR_OK)) {
isset = true;
}
}
}
void FillResult(std::map<std::string, ResultT> &mres, bool& isset, bool force=false) const {
mres.clear();
if (dsm_) {
for (typename result_map::const_iterator dit = dsm_->begin();
dit != dsm_->end(); dit++) {
ResultT res;
DSReturnType rt = dit->second->FillResult(res);
if ((force && rt) || (!force && rt == DSR_OK)) {
mres.insert(std::make_pair(dit->first, res));
}
}
}
isset = !mres.empty();
return;
}
// This is the interface to feed in all updates to the raw stat
// on which the derived stat will be based.
void Update(ElemT raw, uint64_t mono_usec) {
if (!ds_) {
ds_ = boost::make_shared<DSTT<ElemT,ResultT> >(annotation_);
if (is_agg_) {
diff_ = raw;
agg_ = raw;
}
} else {
if (is_agg_) {
diff_ = raw - agg_;
agg_ = agg_ + diff_;
}
}
if (is_agg_) ds_->Update(diff_, mono_usec);
else ds_->Update(raw, mono_usec);
}
void Update(const std::map<std::string, ElemT> & raw,
const std::map<std::string, bool> &del, uint64_t mono_usec) {
if (!dsm_) {
dsm_ = boost::make_shared<result_map>();
}
if (is_agg_) {
diffm_ = DerivedStatsAgg<ElemT>(raw, aggm_, del);
DerivedStatsMerge<DSTT,ElemT,ResultT>(diffm_, *dsm_, annotation_, del, mono_usec);
} else {
DerivedStatsMerge<DSTT,ElemT,ResultT>(raw, *dsm_, annotation_, del, mono_usec);
}
}
};
template <template<class,class> class DSTT, typename ElemT,
typename SubResultT, typename ResultT>
class DerivedStatsPeriodicIf {
private:
typedef std::map<std::string, boost::shared_ptr<DSTT<ElemT,SubResultT> > > result_map;
boost::shared_ptr<result_map> dsm_;
boost::shared_ptr< std::map<std::string,ResultT> > dsm_cache_;
boost::shared_ptr<DSTT<ElemT,SubResultT> > ds_;
boost::shared_ptr<ResultT> ds_cache_;
std::string annotation_;
bool is_agg_;
bool init_;
ElemT agg_;
std::map<std::string, ElemT> aggm_;
ElemT diff_;
std::map<std::string, ElemT> diffm_;
public:
DerivedStatsPeriodicIf(std::string annotation, bool is_agg=false):
annotation_(annotation), is_agg_(is_agg), init_(false) {}
bool IsResult(void) const {
if (ds_ || ds_cache_) return true;
if (dsm_ || dsm_cache_) return true;
return false;
}
// This is the interface to feed in all updates to the raw stat
// on which the derived stat will be based.
void Update(ElemT raw, uint64_t mono_usec) {
if (!ds_) {
ds_ = boost::make_shared<DSTT<ElemT,SubResultT> >(annotation_);
}
if (is_agg_) {
if (!init_) {
diff_ = raw;
agg_ = raw;
} else {
diff_ = raw - agg_;
agg_ = agg_ + diff_;
}
ds_->Update(diff_, mono_usec);
} else ds_->Update(raw, mono_usec);
init_ = true;
}
void Update(const std::map<std::string, ElemT> & raw,
const std::map<std::string, bool> &del, uint64_t mono_usec) {
if (!dsm_) {
dsm_ = boost::make_shared<result_map>();
}
if (is_agg_) {
diffm_ = DerivedStatsAgg<ElemT>(raw, aggm_, del);
DerivedStatsMerge<DSTT,ElemT,SubResultT>(
diffm_, *dsm_, annotation_, del, mono_usec);
} else DerivedStatsMerge<DSTT,ElemT,SubResultT>(
raw, *dsm_, annotation_, del, mono_usec);
init_ = true;
}
bool Flush(const ResultT &res) {
(void)res;
if (!ds_) {
// There were no updates to the DerivedStat
// since the last flush
ds_cache_.reset();
return ds_cache_;
}
ds_cache_ = boost::make_shared<ResultT>();
if (ds_->FillResult(ds_cache_->value)) {
ds_cache_->__isset.value = true;
} else ds_cache_.reset();
// Clear the DerivedStat
ds_.reset();
return ds_cache_;
}
// This is the interface to retrieve the current value
// of the DerivedStat object.
void FillResult(ResultT &res, bool& isset, bool force=false) const {
isset = false;
if (ds_cache_) {
// Fill in previous information
res.value = ds_cache_->value;
res.__isset.value = true;
isset = true;
}
if (ds_) {
// Fill in current information
DSReturnType rt = ds_->FillResult(res.staging);
if ((force && rt) || (!force && rt == DSR_OK)) {
res.__isset.staging = true;
isset = true;
}
}
}
bool Flush(const std::map<std::string, ResultT> &mres) {
(void)mres;
if (!dsm_) {
// There were no updates to the DerivedStats
// since the last flush
dsm_cache_.reset();
return dsm_cache_;
}
dsm_cache_ = boost::make_shared<std::map<std::string,ResultT> >();
for (typename result_map::const_iterator dit = dsm_->begin();
dit != dsm_->end(); dit++) {
ResultT res;
if (dit->second->FillResult(res.value)) {
res.__isset.value = true;
dsm_cache_->insert(std::make_pair(dit->first, res));
}
}
if (dsm_cache_->empty()) dsm_cache_.reset();
// Clear the DerivedStats
dsm_.reset();
return dsm_cache_;
}
// This is the interface to retrieve the current value
// of the DerivedStat object.
void FillResult(std::map<std::string, ResultT> &mres, bool& isset, bool force=false) const {
mres.clear();
if (dsm_cache_) {
// Fill in previous information
for (typename std::map<std::string,ResultT>::const_iterator dit = dsm_cache_->begin();
dit != dsm_cache_->end(); dit++) {
ResultT res;
res.value = dit->second.value;
res.__isset.value = true;
mres.insert(std::make_pair(dit->first, res));
}
}
if (dsm_) {
for (typename result_map::const_iterator dit = (*dsm_).begin();
dit != (*dsm_).end(); dit++) {
// If this derived stat had a previous value, merge the current into it.
typename std::map<std::string, ResultT>::iterator wit =
mres.find(dit->first);
if (wit != mres.end()) {
DSReturnType rt =
dit->second->FillResult(wit->second.staging);
if ((force && rt) || (!force && rt == DSR_OK)) {
wit->second.__isset.staging = true;
}
} else {
ResultT res;
DSReturnType rt =
dit->second->FillResult(res.staging);
if ((force && rt) || (!force && rt == DSR_OK)) {
res.__isset.staging = true;
mres.insert(std::make_pair(dit->first, res));
}
}
}
}
isset = !mres.empty();
}
};
} // namespace sandesh
} // namespace contrail
#endif