-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitoring.h
109 lines (92 loc) · 2.89 KB
/
monitoring.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
#ifndef MONITORING_H
#define MONITORING_H
#include <algorithm>
#include <array>
#include <ostream>
#include <boost/serialization/vector.hpp>
namespace Monitoring {
using TCK = unsigned int;
using HistId = unsigned int;
using RunNumber = unsigned long;
using BinContent = unsigned long long;
using Seconds = std::size_t;
static const Seconds RUN_PERIOD = 70 * 60;
/** A serialize-able chunk of data that spans DELTA_T seconds.
*/
struct Chunk {
Chunk() = default;
Chunk(RunNumber rn, TCK t, HistId id, Seconds s, size_t n = 10)
: runNumber{rn}, tck{t}, histId{id}, start{s}, data(n) {}
Chunk& operator+=(const Chunk& c) {
auto size = data.size();
auto new_size = c.start + c.data.size();
if (new_size > data.size()) {
data.resize(new_size);
for (unsigned int i = size; i < new_size; ++i) data[i] = 0;
}
for (unsigned int i = 0; i < c.data.size(); ++i)
data[c.start + i] += c.data[i];
return *this;
}
RunNumber runNumber;
TCK tck;
HistId histId;
Seconds start;
std::vector<BinContent> data;
};
/**
*/
struct Histogram {
Histogram() = default;
Histogram(RunNumber runNumber, TCK tck, HistId histId)
: runNumber{runNumber}, tck{tck}, histId{histId}, data{} {}
auto addChunk(const Chunk& c) noexcept -> void {
assert(runNumber == c.runNumber && tck == c.tck && histId == c.histId);
assert(c.start + c.data.size() < RUN_PERIOD);
std::transform(std::begin(c.data), std::end(c.data),
std::begin(data) + c.start, std::begin(data) + c.start,
std::plus<>());
}
RunNumber runNumber;
TCK tck;
HistId histId;
std::array<BinContent, RUN_PERIOD> data;
};
/** Write a chunk of data to a std::ostream.
*/
auto operator<<(std::ostream& os, const Chunk& chunk) -> std::ostream & {
os << "Monitoring::Chunk [Run: " << chunk.runNumber << ", TCK: " << chunk.tck
<< ", HistId: " << chunk.histId << ", Start: " << chunk.start
<< "]\n [";
std::copy(std::begin(chunk.data), std::end(chunk.data),
std::ostream_iterator<BinContent>(os, " "));
os << ']';
return os;
}
/** Write histogram to a std::ostream.
*/
auto operator<<(std::ostream& os, const Histogram& h) -> std::ostream& {
os << "Monitoring::Histogram [Run: " << h.runNumber << ", TCK: " << h.tck
<< ", HistId: " << h.histId << "]";
// For testing print something that depends on the data.
os << "\n Integral = "
<< std::accumulate(std::begin(h.data), std::end(h.data), 0);
return os;
}
}
/** Serialize a chunk of data using boost::serialization.
*/
namespace boost {
namespace serialization {
template <typename Archive>
auto serialize(Archive& archive, Monitoring::Chunk& chunk,
const unsigned int version) -> void {
archive& chunk.runNumber;
archive& chunk.histId;
archive& chunk.tck;
archive& chunk.start;
archive& chunk.data;
}
}
}
#endif // MONITORING_H