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