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
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <exceptions/exceptions.h>
#include <monitored_duration_store.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <util/multi_threading_mgr.h>

using namespace isc;
using namespace isc::dhcp;
using namespace isc::util;
using namespace boost::posix_time;

namespace isc {
namespace perfmon {

MonitoredDurationStore::MonitoredDurationStore(uint16_t family,
                                               const Duration& interval_duration)
    : family_(family),
      interval_duration_(interval_duration),
      durations_(),
      mutex_(new std::mutex) {
    if (family != AF_INET && family_ != AF_INET6) {
        isc_throw(BadValue, "MonitoredDurationStore - invalid family "
                            << family_ << ", must be AF_INET or AF_INET6");
    }

    if (interval_duration_ <= DurationDataInterval::ZERO_DURATION()) {
        isc_throw(BadValue, "MonitoredDurationStore - invalid interval_duration "
                            << interval_duration_ << ", must be greater than zero");
    }
}

void
MonitoredDurationStore::validateKey(const std::string& label, DurationKeyPtr key) const {
    if (!key) {
        isc_throw(BadValue, "MonitoredDurationStore::" << label << " - key is empty");
    }

    if (key->getFamily() != family_) {
        isc_throw(BadValue, "MonitoredDurationStore::" << label
                            << " - family mismatch, key is " << (family_ == AF_INET ?
                            "v6, store is v4" : "v4, store is v6"));
    }
}

MonitoredDurationPtr
MonitoredDurationStore::addDurationSample(DurationKeyPtr key, const Duration& sample) {
    validateKey("addDurationSample", key);


    MultiThreadingLock lock(*mutex_);
    auto& index = durations_.get<DurationKeyTag>();
    auto duration_iter = index.find(boost::make_tuple(key->getQueryType(),
                                                      key->getResponseType(),
                                                      key->getStartEventLabel(),
                                                      key->getStopEventLabel(),
                                                      key->getSubnetId()));

    if (duration_iter != index.end()) {
        bool should_report = false;

        // Modify updates in place and only re-indexes if keys change.
        bool modified = index.modify(duration_iter,
                                     [sample, &should_report](MonitoredDurationPtr mond) {
            should_report = mond->addSample(sample);
        });

        if (!modified) {
            // Possible but unlikely.
            isc_throw(Unexpected,
                      "MonitoredDurationStore::addDurationSample - modify failed for: "
                      << key->getLabel());
        }

        // If it's time to report return a copy otherwise an empty pointer.
        return (should_report ? MonitoredDurationPtr(new MonitoredDuration(**duration_iter))
                              : MonitoredDurationPtr());
    }

    // It doesn't exist, add it.
    MonitoredDurationPtr mond(new MonitoredDuration(*key, interval_duration_));
    static_cast<void>(mond->addSample(sample));
    auto ret = durations_.insert(mond);
    if (ret.second == false) {
        // Shouldn't be possible.
        isc_throw(DuplicateDurationKey,
                  "MonitoredDurationStore::addDurationSample: duration already exists for: "
                  << mond->getLabel());
    }

    // Nothing to report.
    return (MonitoredDurationPtr());
}

MonitoredDurationPtr
MonitoredDurationStore::addDuration(DurationKeyPtr key) {
    validateKey("addDuration", key);

    // Create the duration instance.
    MonitoredDurationPtr mond;
    try {
        mond.reset(new MonitoredDuration(*key, interval_duration_));
    } catch (const std::exception& ex) {
        isc_throw(BadValue, "MonitoredDurationStore::addDuration failed: " << ex.what());
    }

    // Now lock and insert the new duration.
    {
        MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
        auto ret = durations_.insert(mond);
        if (ret.second == false) {
            isc_throw(DuplicateDurationKey,
                      "MonitoredDurationStore::addDuration: duration already exists for: "
                      << mond->getLabel());
        }
    }

    // Return a copy of what we inserted.
    return (MonitoredDurationPtr(new MonitoredDuration(*mond)));
}


MonitoredDurationPtr
MonitoredDurationStore::getDuration(DurationKeyPtr key) {
    validateKey("getDuration", key);

    MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
    const auto& index = durations_.get<DurationKeyTag>();
    auto duration_iter = index.find(boost::make_tuple(key->getQueryType(),
                                                      key->getResponseType(),
                                                      key->getStartEventLabel(),
                                                      key->getStopEventLabel(),
                                                      key->getSubnetId()));

    return (duration_iter == index.end() ? MonitoredDurationPtr()
                                         : MonitoredDurationPtr(new MonitoredDuration(**duration_iter)));
}

void
MonitoredDurationStore::updateDuration(MonitoredDurationPtr& duration) {
    validateKey("updateDuration", duration);

    MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
    auto& index = durations_.get<DurationKeyTag>();
    auto duration_iter = index.find(boost::make_tuple(duration->getQueryType(),
                                                      duration->getResponseType(),
                                                      duration->getStartEventLabel(),
                                                      duration->getStopEventLabel(),
                                                      duration->getSubnetId()));

    if (duration_iter == index.end()) {
        isc_throw(InvalidOperation, "MonitoredDurationStore::updateDuration duration not found: "
                  << duration->getLabel());
    }

    // Use replace() which only re-indexes if keys change.
    index.replace(duration_iter, MonitoredDurationPtr(new MonitoredDuration(*duration)));
}

void
MonitoredDurationStore::deleteDuration(DurationKeyPtr key) {
    validateKey("deleteDuration", key);

    MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
    auto& index = durations_.get<DurationKeyTag>();
    auto duration_iter = index.find(boost::make_tuple(key->getQueryType(),
                                                      key->getResponseType(),
                                                      key->getStartEventLabel(),
                                                      key->getStopEventLabel(),
                                                      key->getSubnetId()));

    if (duration_iter == index.end()) {
        // Not there, just return.
        return;
    }

    // Remove the duration from the store.
    durations_.erase(duration_iter);
}

MonitoredDurationCollectionPtr
MonitoredDurationStore::getAll() {
    MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
    const auto& index = durations_.get<DurationKeyTag>();
    MonitoredDurationCollectionPtr collection(new MonitoredDurationCollection());
    for (auto const& mond : index) {
        collection->push_back(MonitoredDurationPtr(new MonitoredDuration(*mond)));
    }

    return (collection);
}

void
MonitoredDurationStore::clear() {
    MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
    durations_.clear();
}

MonitoredDurationPtr
MonitoredDurationStore::getReportsNext() {
    MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
    const auto& index = durations_.get<IntervalStartTag>();
    // We want to find the oldest interval that is less than interval_duration in the past.
    auto duration_iter = index.lower_bound(dhcp::PktEvent::now() - interval_duration_);
    return (duration_iter == index.end() ? MonitoredDurationPtr()
                                         : MonitoredDurationPtr(new MonitoredDuration(**duration_iter)));
}

MonitoredDurationCollectionPtr
MonitoredDurationStore::getOverdueReports(const Timestamp& since /* = PktEvent::now() */) {
    MultiThreadingLock lock(*mutex_);<--- Variable 'lock' is assigned a value that is never used.
    // We use a lower bound of MIN + 1us to avoid dormant durations
    static Timestamp lower_limit_time(PktEvent::MIN_TIME() + microseconds(1));

    // We want to find anything since the start of time who's start time
    // is more than interval_duration_ in the past.
    const auto& index = durations_.get<IntervalStartTag>();
    auto lower_limit = index.lower_bound(lower_limit_time);
    auto upper_limit = index.upper_bound(since - interval_duration_);

    MonitoredDurationCollectionPtr collection(new MonitoredDurationCollection());
    for (auto duration_iter = lower_limit; duration_iter != upper_limit; ++duration_iter) {
        collection->push_back(MonitoredDurationPtr(new MonitoredDuration(**duration_iter)));
    }

    return (collection);
}

} // end of namespace perfmon
} // end of namespace isc