aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/vespa/metrics/metricsnapshot.cpp
blob: 104ad858e436465418b86e6219ab75a288e78c06 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "metricsnapshot.h"
#include "metricmanager.h"
#include <cassert>

#include <vespa/log/log.h>
LOG_SETUP(".metrics.snapshot");

using vespalib::to_string;
using vespalib::to_s;


namespace metrics {

static constexpr system_time system_time_epoch = system_time();

MetricSnapshot::MetricSnapshot(const Metric::String& name)
    : _name(name),
      _period(0),
      _fromTime(system_time_epoch),
      _toTime(system_time_epoch),
      _snapshot(new MetricSet("top", {}, "", nullptr)),
      _metrics()
{
}

MetricSnapshot::MetricSnapshot(const Metric::String& name, system_time::duration period, const MetricSet& source, bool copyUnset)
    : _name(name),
      _period(period),
      _fromTime(system_time_epoch),
      _toTime(system_time_epoch),
      _snapshot(),
      _metrics()
{
    _snapshot.reset(source.clone(_metrics, Metric::INACTIVE, nullptr, copyUnset));
    _metrics.shrink_to_fit();
}

MetricSnapshot::~MetricSnapshot() = default;

void
MetricSnapshot::reset() {
    reset(system_time_epoch);
}
void
MetricSnapshot::reset(system_time currentTime)
{
    _fromTime = currentTime;
    _toTime = system_time_epoch;
    _snapshot->reset();
}

void
MetricSnapshot::recreateSnapshot(const MetricSet& metrics, bool copyUnset)
{
    std::vector<Metric::UP> newMetrics;
    Metric* m = metrics.clone(newMetrics, Metric::INACTIVE, nullptr, copyUnset);
    assert(m->isMetricSet());
    std::unique_ptr<MetricSet> newSnapshot(static_cast<MetricSet*>(m));
    newSnapshot->reset();
    _snapshot->addToSnapshot(*newSnapshot, newMetrics);
    _snapshot = std::move(newSnapshot);
    _metrics.swap(newMetrics);
    _metrics.shrink_to_fit();
}

void
MetricSnapshot::addMemoryUsage(MemoryConsumption& mc) const
{
    ++mc._snapshotCount;
    mc._snapshotName += mc.getStringMemoryUsage(_name, mc._snapshotNameUnique);
    mc._snapshotMeta += sizeof(MetricSnapshot) + _metrics.capacity() * sizeof(Metric::SP);
    _snapshot->addMemoryUsage(mc);
}

MetricSnapshotSet::MetricSnapshotSet(const Metric::String& name, system_time::duration period, uint32_t count,
                                     const MetricSet& source, bool snapshotUnsetMetrics)
    : _count(count),
      _builderCount(0),
      _current(std::make_unique<MetricSnapshot>(name, period, source, snapshotUnsetMetrics)),
      _building(count == 1 ? nullptr : new MetricSnapshot(name, period, source, snapshotUnsetMetrics)),
      _current_is_assigned(false)
{
    _current->reset();
    if (_building) _building->reset();
}

MetricSnapshotSet::~MetricSnapshotSet() = default;

MetricSnapshot&
MetricSnapshotSet::getNextTarget()
{
    if (_count == 1) return *_current;
    return *_building;
}

bool
MetricSnapshotSet::haveCompletedNewPeriod(system_time newFromTime)
{
    if (_count == 1) {
        _current->setToTime(newFromTime);
        return true;
    }
    _building->setToTime(newFromTime);
    // If not time to roll yet, just return
    if (++_builderCount < _count) return false;
    // Building buffer done. Use that as current and reset current.
    std::swap(_current, _building);
    _building->reset(newFromTime);
    _builderCount = 0;
    return true;
}

bool
MetricSnapshotSet::timeForAnotherSnapshot(system_time currentTime) {
    system_time lastTime = getToTime();
    vespalib::duration period = getPeriod();
    if (currentTime >= lastTime + period) {
        if (currentTime >= lastTime + 2 * period) {
            LOG(warning, "Metric snapshot set %s was asked if it was time for another snapshot, a whole period beyond "
                         "when it should have been done (Last update was at time %s, current time is %s and period "
                         "is %f seconds). Clearing data and updating time to current time.",
                getName().c_str(), to_string(lastTime).c_str(), to_string(currentTime).c_str(), to_s(getPeriod()));
            reset(currentTime);
        }
        return true;
    }
    return false;
}

void
MetricSnapshotSet::reset(system_time currentTime) {
    if (_count != 1) _building->reset(currentTime);
    _current->reset(currentTime);
    _builderCount = 0;
}

void
MetricSnapshotSet::recreateSnapshot(const MetricSet& metrics, bool copyUnset)
{
    if (_count != 1) _building->recreateSnapshot(metrics, copyUnset);
    _current->recreateSnapshot(metrics, copyUnset);
}

void
MetricSnapshotSet::addMemoryUsage(MemoryConsumption& mc) const
{
    ++mc._snapshotSetCount;
    mc._snapshotSetMeta += sizeof(MetricSnapshotSet);
    if (_count != 1) _building->addMemoryUsage(mc);
    _current->addMemoryUsage(mc);
}

void
MetricSnapshotSet::setFromTime(system_time fromTime)
{
    if (_count != 1) _building->setFromTime(fromTime);
    _current->setFromTime(fromTime);
}

} // metrics