Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 1 | #include "metric.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 2 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 3 | #include "details/collection_function.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 4 | #include "types/report_types.hpp" |
Szymon Dompke | 3a61702 | 2021-07-19 18:23:02 +0200 | [diff] [blame] | 5 | #include "utils/labeled_tuple.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 6 | #include "utils/transform.hpp" |
| 7 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 8 | #include <sdbusplus/exception.hpp> |
| 9 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 12 | class Metric::CollectionData |
| 13 | { |
| 14 | public: |
| 15 | using ReadingItem = details::ReadingItem; |
| 16 | |
| 17 | virtual ~CollectionData() = default; |
| 18 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 19 | virtual std::optional<double> update(Milliseconds timestamp) = 0; |
| 20 | virtual double update(Milliseconds timestamp, double value) = 0; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | class Metric::DataPoint : public Metric::CollectionData |
| 24 | { |
| 25 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 26 | std::optional<double> update(Milliseconds) override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 27 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 28 | return lastReading; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 31 | double update(Milliseconds, double reading) override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 32 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 33 | lastReading = reading; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 34 | return reading; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | private: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 38 | std::optional<double> lastReading; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class Metric::DataInterval : public Metric::CollectionData |
| 42 | { |
| 43 | public: |
| 44 | DataInterval(std::shared_ptr<details::CollectionFunction> function, |
| 45 | CollectionDuration duration) : |
| 46 | function(std::move(function)), |
| 47 | duration(duration) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 48 | { |
| 49 | if (duration.t.count() == 0) |
| 50 | { |
| 51 | throw sdbusplus::exception::SdBusError( |
| 52 | static_cast<int>(std::errc::invalid_argument), |
| 53 | "Invalid CollectionDuration"); |
| 54 | } |
| 55 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 56 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 57 | std::optional<double> update(Milliseconds timestamp) override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 58 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 59 | if (readings.empty()) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 60 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 61 | return std::nullopt; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 64 | cleanup(timestamp); |
| 65 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 66 | return function->calculate(readings, timestamp); |
| 67 | } |
| 68 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 69 | double update(Milliseconds timestamp, double reading) override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 70 | { |
| 71 | readings.emplace_back(timestamp, reading); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 72 | |
| 73 | cleanup(timestamp); |
| 74 | |
| 75 | return function->calculate(readings, timestamp); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 79 | void cleanup(Milliseconds timestamp) |
| 80 | { |
| 81 | auto it = readings.begin(); |
| 82 | for (auto kt = std::next(readings.rbegin()); kt != readings.rend(); |
| 83 | ++kt) |
| 84 | { |
| 85 | const auto& [nextItemTimestamp, nextItemReading] = *std::prev(kt); |
| 86 | if (timestamp >= nextItemTimestamp && |
| 87 | timestamp - nextItemTimestamp > duration.t) |
| 88 | { |
| 89 | it = kt.base(); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | readings.erase(readings.begin(), it); |
| 94 | |
| 95 | if (timestamp > duration.t) |
| 96 | { |
| 97 | readings.front().first = |
| 98 | std::max(readings.front().first, timestamp - duration.t); |
| 99 | } |
| 100 | } |
| 101 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 102 | std::shared_ptr<details::CollectionFunction> function; |
| 103 | std::vector<ReadingItem> readings; |
| 104 | CollectionDuration duration; |
| 105 | }; |
| 106 | |
| 107 | class Metric::DataStartup : public Metric::CollectionData |
| 108 | { |
| 109 | public: |
Krzysztof Grobelny | fbeb5bf | 2022-01-03 09:41:29 +0100 | [diff] [blame] | 110 | explicit DataStartup( |
| 111 | std::shared_ptr<details::CollectionFunction> function) : |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 112 | function(std::move(function)) |
| 113 | {} |
| 114 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 115 | std::optional<double> update(Milliseconds timestamp) override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 116 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 117 | if (readings.empty()) |
| 118 | { |
| 119 | return std::nullopt; |
| 120 | } |
| 121 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 122 | return function->calculateForStartupInterval(readings, timestamp); |
| 123 | } |
| 124 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 125 | double update(Milliseconds timestamp, double reading) override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 126 | { |
| 127 | readings.emplace_back(timestamp, reading); |
| 128 | return function->calculateForStartupInterval(readings, timestamp); |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | std::shared_ptr<details::CollectionFunction> function; |
| 133 | std::vector<ReadingItem> readings; |
| 134 | }; |
| 135 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 136 | Metric::Metric(Sensors sensorsIn, OperationType operationTypeIn, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 137 | std::string idIn, CollectionTimeScope timeScopeIn, |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 138 | CollectionDuration collectionDurationIn, |
| 139 | std::unique_ptr<interfaces::Clock> clockIn) : |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 140 | id(std::move(idIn)), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 141 | sensors(std::move(sensorsIn)), operationType(operationTypeIn), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 142 | collectionTimeScope(timeScopeIn), collectionDuration(collectionDurationIn), |
| 143 | collectionAlgorithms(makeCollectionData(sensors.size(), operationType, |
| 144 | collectionTimeScope, |
| 145 | collectionDuration)), |
| 146 | clock(std::move(clockIn)) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 147 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 148 | readings = utils::transform(sensors, [this](const auto& sensor) { |
| 149 | return MetricValue{id, sensor->metadata(), 0.0, 0u}; |
| 150 | }); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 151 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 152 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 153 | Metric::~Metric() = default; |
| 154 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 155 | void Metric::initialize() |
| 156 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 157 | for (const auto& sensor : sensors) |
| 158 | { |
| 159 | sensor->registerForUpdates(weak_from_this()); |
| 160 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 163 | void Metric::deinitialize() |
| 164 | { |
| 165 | for (const auto& sensor : sensors) |
| 166 | { |
| 167 | sensor->unregisterFromUpdates(weak_from_this()); |
| 168 | } |
| 169 | } |
| 170 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 171 | std::vector<MetricValue> Metric::getReadings() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 172 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 173 | const auto steadyTimestamp = clock->steadyTimestamp(); |
| 174 | const auto systemTimestamp = clock->systemTimestamp(); |
| 175 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 176 | auto resultReadings = readings; |
| 177 | |
| 178 | for (size_t i = 0; i < resultReadings.size(); ++i) |
| 179 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 180 | if (const auto value = collectionAlgorithms[i]->update(steadyTimestamp)) |
| 181 | { |
| 182 | resultReadings[i].timestamp = |
| 183 | std::chrono::duration_cast<Milliseconds>(systemTimestamp) |
| 184 | .count(); |
| 185 | resultReadings[i].value = *value; |
| 186 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return resultReadings; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 192 | void Metric::sensorUpdated(interfaces::Sensor& notifier, Milliseconds timestamp) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 193 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 194 | findAssociatedData(notifier).update(timestamp); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 197 | void Metric::sensorUpdated(interfaces::Sensor& notifier, Milliseconds timestamp, |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 198 | double value) |
| 199 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 200 | findAssociatedData(notifier).update(timestamp, value); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 203 | Metric::CollectionData& |
| 204 | Metric::findAssociatedData(const interfaces::Sensor& notifier) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 205 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 206 | auto it = std::find_if( |
| 207 | sensors.begin(), sensors.end(), |
| 208 | [¬ifier](const auto& sensor) { return sensor.get() == ¬ifier; }); |
| 209 | auto index = std::distance(sensors.begin(), it); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 210 | return *collectionAlgorithms.at(index); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 213 | LabeledMetricParameters Metric::dumpConfiguration() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 214 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 215 | auto sensorPath = utils::transform(sensors, [this](const auto& sensor) { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 216 | return LabeledSensorParameters(sensor->id().service, sensor->id().path, |
| 217 | sensor->metadata()); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 218 | }); |
| 219 | |
| 220 | return LabeledMetricParameters(std::move(sensorPath), operationType, id, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 221 | collectionTimeScope, collectionDuration); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 224 | std::vector<std::unique_ptr<Metric::CollectionData>> |
| 225 | Metric::makeCollectionData(size_t size, OperationType op, |
| 226 | CollectionTimeScope timeScope, |
| 227 | CollectionDuration duration) |
| 228 | { |
| 229 | using namespace std::string_literals; |
| 230 | |
| 231 | std::vector<std::unique_ptr<Metric::CollectionData>> result; |
| 232 | |
| 233 | result.reserve(size); |
| 234 | |
| 235 | switch (timeScope) |
| 236 | { |
| 237 | case CollectionTimeScope::interval: |
| 238 | std::generate_n( |
| 239 | std::back_inserter(result), size, |
| 240 | [cf = details::makeCollectionFunction(op), duration] { |
| 241 | return std::make_unique<DataInterval>(cf, duration); |
| 242 | }); |
| 243 | break; |
| 244 | case CollectionTimeScope::point: |
| 245 | std::generate_n(std::back_inserter(result), size, |
| 246 | [] { return std::make_unique<DataPoint>(); }); |
| 247 | break; |
| 248 | case CollectionTimeScope::startup: |
| 249 | std::generate_n(std::back_inserter(result), size, |
| 250 | [cf = details::makeCollectionFunction(op)] { |
| 251 | return std::make_unique<DataStartup>(cf); |
| 252 | }); |
| 253 | break; |
| 254 | default: |
| 255 | throw std::runtime_error("timeScope: "s + |
| 256 | utils::enumToString(timeScope) + |
| 257 | " is not supported"s); |
| 258 | } |
| 259 | |
| 260 | return result; |
| 261 | } |
| 262 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 263 | uint64_t Metric::sensorCount() const |
| 264 | { |
| 265 | return sensors.size(); |
| 266 | } |