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 | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 3 | #include "metrics/collection_data.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 | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 13 | Metric::Metric(Sensors sensorsIn, OperationType operationTypeIn, |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 14 | CollectionTimeScope timeScopeIn, |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 15 | CollectionDuration collectionDurationIn, |
| 16 | std::unique_ptr<interfaces::Clock> clockIn) : |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 17 | sensors(std::move(sensorsIn)), |
| 18 | operationType(operationTypeIn), collectionTimeScope(timeScopeIn), |
| 19 | collectionDuration(collectionDurationIn), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 20 | collectionAlgorithms( |
| 21 | metrics::makeCollectionData(sensors.size(), operationType, |
| 22 | collectionTimeScope, collectionDuration)), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 23 | clock(std::move(clockIn)) |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 24 | {} |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 25 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 26 | void Metric::registerForUpdates(interfaces::MetricListener& listener) |
| 27 | { |
| 28 | listeners.emplace_back(listener); |
| 29 | } |
| 30 | |
| 31 | void Metric::unregisterFromUpdates(interfaces::MetricListener& listener) |
| 32 | { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 33 | listeners.erase( |
| 34 | std::remove_if(listeners.begin(), listeners.end(), |
| 35 | [&listener](const interfaces::MetricListener& item) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 36 | return &item == &listener; |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 37 | }), |
| 38 | listeners.end()); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 39 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 40 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 41 | void Metric::initialize() |
| 42 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 43 | for (const auto& sensor : sensors) |
| 44 | { |
| 45 | sensor->registerForUpdates(weak_from_this()); |
| 46 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 49 | void Metric::deinitialize() |
| 50 | { |
| 51 | for (const auto& sensor : sensors) |
| 52 | { |
| 53 | sensor->unregisterFromUpdates(weak_from_this()); |
| 54 | } |
| 55 | } |
| 56 | |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 57 | const std::vector<MetricValue>& Metric::getUpdatedReadings() |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 58 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 59 | const auto steadyTimestamp = clock->steadyTimestamp(); |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 60 | const auto systemTimestamp = |
| 61 | std::chrono::duration_cast<Milliseconds>(clock->systemTimestamp()) |
| 62 | .count(); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 63 | |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 64 | for (size_t i = 0; i < collectionAlgorithms.size(); ++i) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 65 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 66 | if (const auto value = collectionAlgorithms[i]->update(steadyTimestamp)) |
| 67 | { |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 68 | if (i < readings.size()) |
| 69 | { |
| 70 | readings[i].timestamp = systemTimestamp; |
| 71 | readings[i].value = *value; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | if (i > readings.size()) |
| 76 | { |
| 77 | const auto idx = readings.size(); |
| 78 | std::swap(collectionAlgorithms[i], |
| 79 | collectionAlgorithms[idx]); |
| 80 | std::swap(sensors[i], sensors[idx]); |
| 81 | i = idx; |
| 82 | } |
| 83 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 84 | readings.emplace_back(sensors[i]->metadata(), *value, |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 85 | systemTimestamp); |
| 86 | } |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 87 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 90 | return readings; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 93 | void Metric::sensorUpdated(interfaces::Sensor& notifier, Milliseconds timestamp, |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 94 | double value) |
| 95 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 96 | auto& data = findAssociatedData(notifier); |
| 97 | double newValue = data.update(timestamp, value); |
| 98 | |
| 99 | if (data.updateLastValue(newValue)) |
| 100 | { |
| 101 | for (interfaces::MetricListener& listener : listeners) |
| 102 | { |
| 103 | listener.metricUpdated(); |
| 104 | } |
| 105 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 108 | metrics::CollectionData& |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 109 | Metric::findAssociatedData(const interfaces::Sensor& notifier) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 110 | { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 111 | auto it = std::find_if( |
| 112 | sensors.begin(), sensors.end(), |
| 113 | [¬ifier](const auto& sensor) { return sensor.get() == ¬ifier; }); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 114 | auto index = std::distance(sensors.begin(), it); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 115 | return *collectionAlgorithms.at(index); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 118 | LabeledMetricParameters Metric::dumpConfiguration() const |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 119 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 120 | auto sensorPath = utils::transform(sensors, [this](const auto& sensor) { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 121 | return LabeledSensorInfo(sensor->id().service, sensor->id().path, |
| 122 | sensor->metadata()); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 123 | }); |
| 124 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 125 | return LabeledMetricParameters(std::move(sensorPath), operationType, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 126 | collectionTimeScope, collectionDuration); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Krzysztof Grobelny | 18e7101 | 2022-11-02 13:17:01 +0000 | [diff] [blame] | 129 | uint64_t Metric::metricCount() const |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 130 | { |
| 131 | return sensors.size(); |
| 132 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 133 | |
| 134 | void Metric::updateReadings(Milliseconds timestamp) |
| 135 | { |
| 136 | for (auto& data : collectionAlgorithms) |
| 137 | { |
| 138 | if (std::optional<double> newValue = data->update(timestamp)) |
| 139 | { |
| 140 | if (data->updateLastValue(*newValue)) |
| 141 | { |
| 142 | for (interfaces::MetricListener& listener : listeners) |
| 143 | { |
| 144 | listener.metricUpdated(); |
| 145 | } |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | bool Metric::isTimerRequired() const |
| 153 | { |
| 154 | if (collectionTimeScope == CollectionTimeScope::point) |
| 155 | { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (collectionTimeScope == CollectionTimeScope::startup && |
| 160 | (operationType == OperationType::min || |
| 161 | operationType == OperationType::max)) |
| 162 | { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | return true; |
| 167 | } |