summaryrefslogtreecommitdiffstats
path: root/metrics/src/vespa/metrics/metricsnapshot.cpp
blob: 1625de3b10638e77b80a41b393e3e676d9f28b53 (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
162
163
164
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "metricsnapshot.h"
#include "metricmanager.h"
#include <vespa/log/log.h>

LOG_SETUP(".metrics.snapshot");

namespace metrics {

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

MetricSnapshot::MetricSnapshot(
        const Metric::String& name, uint32_t period, const MetricSet& source,
        bool copyUnset)
    : _name(name),
      _period(period),
      _fromTime(0),
      _toTime(0),
      _snapshot(),
      _metrics()
{
    Metric* m = source.clone(_metrics, Metric::INACTIVE, 0, copyUnset);
    assert(m->isMetricSet());
    _snapshot.reset(static_cast<MetricSet*>(m));
    _metrics.shrink_to_fit();
}

MetricSnapshot::~MetricSnapshot() { }

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

void
MetricSnapshot::recreateSnapshot(const MetricSet& metrics, bool copyUnset)
{
    std::vector<Metric::UP> newMetrics;
    Metric* m = metrics.clone(newMetrics, Metric::INACTIVE, 0, 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, uint32_t period,
        uint32_t count, const MetricSet& source, bool snapshotUnsetMetrics)
    : _count(count),
      _builderCount(0),
      _current(new MetricSnapshot(name, period, source, snapshotUnsetMetrics)),
      _building(count == 1 ? 0 : new MetricSnapshot(
                  name, period, source, snapshotUnsetMetrics))
{
    _current->reset(0);
    if (_building.get()) _building->reset(0);
}

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

bool
MetricSnapshotSet::haveCompletedNewPeriod(time_t 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.
    MetricSnapshot::UP tmp(std::move(_current));
    _current = std::move(_building);
    _building = std::move(tmp);
    _building->reset(newFromTime);
    _builderCount = 0;
    return true;
}

bool
MetricSnapshotSet::timeForAnotherSnapshot(time_t currentTime) {
    time_t lastTime = getToTime();
    if (currentTime >= lastTime + getPeriod()) {
        if (currentTime >= lastTime + 2 * getPeriod()) {
            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 %"
                         PRIu64 ", current time is %" PRIu64 " and period is %u). "
                         "Clearing data and updating time to current time.",
                getName().c_str(), lastTime, currentTime, getPeriod());
            reset(currentTime);
        }
        return true;
    }
    return false;
}

void
MetricSnapshotSet::reset(time_t 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::updateNames(NameHash& hash) const
{
    if (_count != 1) _building->updateNames(hash);
    _current->updateNames(hash);
}

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

} // metrics