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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
* \class metrics::MetricSnapshot
* \ingroup metrics
*
* \brief Represents a snapshot of a metric set.
*/
#pragma once
#include <map>
#include <vespa/metrics/metric.h>
#include <vespa/metrics/metricset.h>
#include <vespa/vespalib/util/time.h>
namespace metrics {
using system_time = vespalib::system_time;
class MetricManager;
class MetricSnapshot
{
Metric::String _name;
// Period length of this snapshot
system_time::duration _period;
// Time this snapshot was last updated.
system_time _fromTime;
// If set to 0, use _fromTime + _period.
system_time _toTime;
// Keeps the metrics set view of the snapshot
std::unique_ptr<MetricSet> _snapshot;
// Snapshots must own their own metrics
mutable std::vector<Metric::UP> _metrics;
public:
/** Create a fresh empty top level snapshot. */
MetricSnapshot(const Metric::String& name);
/** Create a snapshot of another metric source. */
MetricSnapshot(const Metric::String& name, system_time::duration period,
const MetricSet& source, bool copyUnset);
~MetricSnapshot();
void addToSnapshot(MetricSnapshot& other, bool reset_, system_time currentTime) {
_snapshot->addToSnapshot(other.getMetrics(), other._metrics);
if (reset_) reset(currentTime);
other._toTime = currentTime;
}
void addToSnapshot(MetricSnapshot& other, system_time currentTime) const {
_snapshot->addToSnapshot(other.getMetrics(), other._metrics);
other._toTime = currentTime;
}
void setFromTime(system_time fromTime) { _fromTime = fromTime; }
void setToTime(system_time toTime) { _toTime = toTime; }
const Metric::String& getName() const { return _name; }
system_time::duration getPeriod() const { return _period; }
system_time getFromTime() const { return _fromTime; }
system_time getToTime() const { return _toTime; }
const MetricSet& getMetrics() const { return *_snapshot; }
MetricSet& getMetrics() { return *_snapshot; }
void reset(system_time currentTime);
void reset();
/**
* Recreate snapshot by cloning given metric set and then add the data
* from the old one. New metrics have been added.
*/
void recreateSnapshot(const MetricSet& metrics, bool copyUnset);
void addMemoryUsage(MemoryConsumption&) const;
};
class MetricSnapshotSet {
const uint32_t _count; // Number of times we need to add to building period
// before we have a full time window.
uint32_t _builderCount; // Number of times we've currently added to the
// building instance.
std::unique_ptr<MetricSnapshot> _current; // The last full period
std::unique_ptr<MetricSnapshot> _building; // The building period
bool _current_is_assigned;
public:
MetricSnapshotSet(const Metric::String& name, system_time::duration period, uint32_t count,
const MetricSet& source, bool snapshotUnsetMetrics);
~MetricSnapshotSet();
const Metric::String& getName() const { return _current->getName(); }
system_time::duration getPeriod() const { return _current->getPeriod(); }
system_time getFromTime() const { return _current->getFromTime(); }
system_time getToTime() const { return _current->getToTime(); }
system_time getNextWorkTime() const { return getToTime() + getPeriod(); }
uint32_t getCount() const { return _count; }
uint32_t getBuilderCount() const { return _builderCount; }
MetricSnapshot& getSnapshot() {
return getSnapshot(false);
}
MetricSnapshot& getSnapshot(bool temporary) {
return *((temporary && _count > 1) ? _building : _current);
}
const MetricSnapshot& getSnapshot(bool temporary) const {
return *((temporary && _count > 1) ? _building : _current);
}
MetricSnapshot& getNextTarget();
bool timeForAnotherSnapshot(system_time currentTime);
bool haveCompletedNewPeriod(system_time newFromTime);
void reset(system_time currentTime);
/**
* Recreate snapshot by cloning given metric set and then add the data
* from the old one. New metrics have been added.
*/
void recreateSnapshot(const MetricSet& metrics, bool copyUnset);
void addMemoryUsage(MemoryConsumption&) const;
void setFromTime(system_time fromTime);
[[nodiscard]] bool current_is_assigned() const noexcept {
return _current_is_assigned;
}
void tag_current_as_assigned() noexcept {
_current_is_assigned = true;
}
};
} // metrics
|