aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/vespa/metrics/metricset.cpp
blob: f583d2f471613538dbe6706543f00e3b0d5ffe0b (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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "metricset.h"
#include "memoryconsumption.h"
#include <vespa/vespalib/stllike/hash_map.hpp>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <list>
#include <cassert>
#include <algorithm>
#include <ostream>

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

namespace metrics {

MetricSet::MetricSet(const String& name, Tags dimensions,
                     const String& description, MetricSet* owner)
    : Metric(name, std::move(dimensions), description, owner),
      _metricOrder(),
      _registrationAltered(false)
{
}

MetricSet::MetricSet(const MetricSet& other,
                     std::vector<Metric::UP> &ownerList,
                     CopyType copyType,
                     MetricSet* owner,
                     bool includeUnused)
    : Metric(other, owner),
      _metricOrder(),
      _registrationAltered(false)
{
    for (const Metric* metric : other._metricOrder) {
        if (copyType != INACTIVE || includeUnused || metric->used()) {
            Metric* m = metric->clone(ownerList, copyType, this, includeUnused);
            ownerList.push_back(Metric::UP(m));
        }
    }
}

MetricSet::~MetricSet() = default;

MetricSet*
MetricSet::clone(std::vector<Metric::UP> &ownerList, CopyType type, MetricSet* owner, bool includeUnused) const
{
    return new MetricSet(*this, ownerList, type, owner, includeUnused);
}


const Metric*
MetricSet::getMetricInternal(stringref name) const
{
    for (const Metric* metric : _metricOrder) {
        if (metric->getMangledName() == name) {
            return metric;
        }
    }
    return 0;
}

int64_t MetricSet::getLongValue(stringref) const {
    assert(false);
    return 0;
}
double MetricSet::getDoubleValue(stringref) const {
    assert(false);
    return 0;
}

const Metric*
MetricSet::getMetric(stringref name) const
{
    vespalib::string::size_type pos = name.find('.');
    if (pos == vespalib::string::npos) {
        return getMetricInternal(name);
    } else {
        stringref child(name.substr(0, pos));
        stringref rest(name.substr(pos+1));
        const Metric* m(getMetricInternal(child));
        if (m == 0) return 0;
        if (!m->isMetricSet()) {
            throw vespalib::IllegalStateException(
                    "Metric " + child + " is not a metric set. Cannot retrieve "
                    "metric at path " + name + " within metric " + getPath(),
                    VESPA_STRLOC);
        }
        return static_cast<const MetricSet*>(m)->getMetric(rest);
    }
}

namespace {
    struct MetricSetFinder : public MetricVisitor {
        std::list<MetricSet*> _metricSets;

        bool visitMetricSet(const MetricSet& set, bool autoGenerated) override {
            if (autoGenerated) return false;
            _metricSets.push_back(const_cast<MetricSet*>(&set));
            return true;
        }
        bool visitMetric(const Metric&, bool autoGenerated) override {
            (void) autoGenerated;
            return true;
        }
    };
}

void
MetricSet::clearRegistrationAltered()
{
    MetricSetFinder finder;
    visit(finder);
    for (MetricSet* metricSet : finder._metricSets) {
        metricSet->_registrationAltered = false;
    }
}

void
MetricSet::tagRegistrationAltered()
{
    _registrationAltered = true;
    if (_owner != 0) {
        _owner->tagRegistrationAltered();
    }
}

void
MetricSet::registerMetric(Metric& metric)
{
    if (metric.isRegistered()) {
        throw vespalib::IllegalStateException(
                "Metric " + metric.getMangledName() +
                " is already registered in a metric set. "
                "Cannot register it twice.", VESPA_STRLOC);
    }
    const Metric* existing(getMetricInternal(metric.getMangledName()));
    if (existing != 0) {
        throw vespalib::IllegalStateException(
                "A metric named " + metric.getMangledName() +
                " is already registered in metric set " + getPath(),
                VESPA_STRLOC);
    }
    _metricOrder.push_back(&metric);
    metric.setRegistered(this);
    tagRegistrationAltered();
    if (metric.isMetricSet()) {
        static_cast<MetricSet &>(metric)._owner = this;
    }
    LOG(spam, "Registered metric%s %s in metric set %s.",
        metric.isMetricSet() ? "set" : "",
        metric.getMangledName().c_str(),
        getPath().c_str());
}

void
MetricSet::unregisterMetric(Metric& metric)
{
        // In case of abrubt shutdowns, don't die hard on attempts to unregister
        // non-registered metrics. Just warn and ignore.
    const Metric* existing(getMetricInternal(metric.getMangledName()));
    if (existing == 0) {
        LOG(warning, "Attempt to unregister metric %s in metric set %s, where "
                     "it wasn't registered to begin with.",
            metric.getMangledName().c_str(),
            getPath().c_str());
        return;
    }
    bool found = false;
    for (std::vector<Metric*>::iterator it = _metricOrder.begin();
         it != _metricOrder.end(); ++it)
    {
        if (*it == &metric) {
            _metricOrder.erase(it);
            found = true;
            break;
        }
    }
    assert(found); // We check above for existence.
    (void) found;
    metric.setRegistered(NULL);
    tagRegistrationAltered();
    if (metric.isMetricSet()) {
        static_cast<MetricSet &>(metric)._owner = this;
    }
    LOG(spam, "Unregistered metric%s %s from metric set %s.",
        metric.isMetricSet() ? "set" : "",
        metric.getMangledName().c_str(),
        getPath().c_str());
}

namespace {
    using TmpString = vespalib::stringref;
    class StringMetric {
    public:
        StringMetric(const TmpString & s, Metric * m) : first(s), second(m) { }
        bool operator == (const StringMetric & b) const { return first == b.first; }
        bool operator == (const TmpString & b) const { return first == b; }
        bool operator <(const StringMetric & b) const { return first < b.first; }
        bool operator <(const TmpString & b) const { return first < b; }
        TmpString   first;
        Metric    * second;
    };
    bool operator < (const TmpString & a, const StringMetric & b) { return a < b.first; }

    using SortedVector = std::vector<StringMetric>;
    
    void createMetricMap(SortedVector& metricMap,
                         const std::vector<Metric*>& orderedList)
    {
        metricMap.reserve(orderedList.size());
        for (Metric* metric : orderedList) {
            metricMap.push_back(StringMetric(metric->getMangledName(), metric));
        }
        std::sort(metricMap.begin(), metricMap.end());
    }
}

void
MetricSet::addTo(Metric& other, std::vector<Metric::UP> *ownerList) const
{
    bool mustAdd = (ownerList == 0);
    MetricSet& o(static_cast<MetricSet&>(other));
    SortedVector map1, map2;
    createMetricMap(map1, _metricOrder);
    createMetricMap(map2, o._metricOrder);
    SortedVector::iterator source(map1.begin());
    SortedVector::iterator target(map2.begin());
    using HashMap = vespalib::hash_map<TmpString, Metric*>;
    HashMap newMetrics;
    while (source != map1.end()) {
        if (target == map2.end() || source->first < target->first) {
                // Source missing in snapshot to add to. Lets create and add.
            if (!mustAdd && source->second->used()) {
                Metric::UP copy(source->second->clone(*ownerList, INACTIVE, &o, false));
                newMetrics[source->first] = copy.get();
                ownerList->push_back(std::move(copy));
            }
            ++source;
        } else if (source->first == target->first) {
            if (mustAdd) {
                source->second->addToPart(*target->second);
            } else {
                source->second->addToSnapshot(*target->second, *ownerList);
            }
            ++source;
            ++target;
        } else {
            ++target;
        }
    }
    // If we added metrics, reorder target order list to equal source
    if (!newMetrics.empty()) {
        std::vector<Metric*> newOrder;
        newOrder.reserve(o._metricOrder.size() + newMetrics.size());
        for (const Metric* metric : _metricOrder) {
            TmpString v = metric->getMangledName();
            target = std::lower_bound(map2.begin(), map2.end(), v);
            if ((target != map2.end()) && (target->first == v)) {
                newOrder.push_back(target->second);
            } else {
                HashMap::iterator starget = newMetrics.find(v);
                if (starget != newMetrics.end()) {
                    newOrder.push_back(starget->second);
                }
            }
        }
        // If target had unique metrics, add them at the end
        for (Metric* metric : o._metricOrder) {
            TmpString v = metric->getMangledName();
            if ( ! std::binary_search(map1.begin(), map1.end(), v) ) {
                LOG(debug, "Metric %s exist in one snapshot but not other."
                           "Order will be messed up. Adding target unique "
                           "metrics to end.",
                    metric->getPath().c_str());
                newOrder.push_back(metric);
            }
        }
        o._metricOrder.swap(newOrder);
    }
}

void
MetricSet::reset()
{
    for (Metric* metric : _metricOrder) {
        metric->reset();
    }
}

bool
MetricSet::visit(MetricVisitor& visitor, bool tagAsAutoGenerated) const
{
    if (!visitor.visitMetricSet(*this, tagAsAutoGenerated)) return true;
    for (const Metric* metric : _metricOrder) {
        if (!metric->visit(visitor, tagAsAutoGenerated)) {
            break;
        }
    }
    visitor.doneVisitingMetricSet(*this);
    return true;
}

void
MetricSet::print(std::ostream& out, bool verbose,
                 const std::string& indent, uint64_t secondsPassed) const
{
    out << getName() << ":";
    for (const Metric* metric : _metricOrder) {
        out << "\n" << indent << "  ";
        metric->print(out, verbose, indent + "  ", secondsPassed);
    }
}

bool
MetricSet::used() const
{
    for (const Metric* metric : _metricOrder) {
        if (metric->used()) return true;
    }
    return false;
}

void
MetricSet::addMemoryUsage(MemoryConsumption& mc) const
{
    Metric::addMemoryUsage(mc);
    ++mc._metricSetCount;
    mc._metricSetMeta += sizeof(MetricSet) - sizeof(Metric);
    mc._metricSetOrder += _metricOrder.size() * 3 * sizeof(void*);
    for (const Metric* metric : _metricOrder) {
        metric->addMemoryUsage(mc);
    }
}

void
MetricSet::printDebug(std::ostream& out, const std::string& indent) const
{
    out << "set ";
    Metric::printDebug(out, indent);
    if (_registrationAltered) out << ", regAltered";
    out << " {";
    for (const Metric* metric : _metricOrder) {
        out << "\n" << indent << "  ";
        metric->printDebug(out, indent + "  ");
    }
    out << "}";
}

} // metrics