blob: bb164d92ee02ccd2e7c561814e09faba171181e4 [file] [log] [blame]
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02001#pragma once
2
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01003#include "interfaces/clock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01004#include "interfaces/json_storage.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02005#include "interfaces/metric.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +01006#include "interfaces/metric_listener.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007#include "interfaces/report.hpp"
Szymon Dompkefdb06a12022-02-11 11:04:44 +01008#include "interfaces/report_factory.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009#include "interfaces/report_manager.hpp"
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +020010#include "state.hpp"
11#include "types/error_message.hpp"
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010012#include "types/readings.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010013#include "types/report_action.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000014#include "types/report_types.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010015#include "types/report_updates.hpp"
16#include "types/reporting_type.hpp"
Szymon Dompke3eb56862021-09-20 15:32:04 +020017#include "utils/circular_vector.hpp"
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020018#include "utils/dbus_path_utils.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010019#include "utils/ensure.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010020#include "utils/messanger.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020021
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020022#include <boost/asio/io_context.hpp>
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020023#include <boost/asio/steady_timer.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020024#include <sdbusplus/asio/object_server.hpp>
25
26#include <chrono>
27#include <memory>
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010028#include <unordered_set>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020029
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010030class Report : public interfaces::Report, public interfaces::MetricListener
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020031{
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010032 class OnChangeContext
33 {
34 public:
Patrick Williams3a1c2972023-05-10 07:51:04 -050035 OnChangeContext(Report& report) : report(report) {}
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010036
37 ~OnChangeContext()
38 {
39 if (updated)
40 {
41 report.updateReadings();
42 }
43 }
44
45 void metricUpdated()
46 {
47 updated = true;
48 }
49
50 private:
51 Report& report;
52 bool updated = false;
53 };
54
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020055 public:
56 Report(boost::asio::io_context& ioc,
57 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010058 const std::string& reportId, const std::string& reportName,
59 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010060 std::vector<ReportAction> reportActions, const Milliseconds period,
Szymon Dompke3eb56862021-09-20 15:32:04 +020061 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020062 interfaces::ReportManager& reportManager,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010063 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020064 std::vector<std::shared_ptr<interfaces::Metric>> metrics,
Szymon Dompkefdb06a12022-02-11 11:04:44 +010065 const interfaces::ReportFactory& reportFactory, const bool enabled,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010066 std::unique_ptr<interfaces::Clock> clock, Readings);
67 ~Report();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020068
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020069 Report(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020070 Report(Report&&) = delete;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020071 Report& operator=(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020072 Report& operator=(Report&&) = delete;
73
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010074 std::string getId() const override
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020075 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010076 return id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020077 }
78
79 std::string getPath() const override
80 {
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020081 return path.str;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020082 }
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020083
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010084 void metricUpdated() override;
85
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +020086 void activate();
87 void deactivate();
88
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020089 private:
Szymon Dompkefdb06a12022-02-11 11:04:44 +010090 std::unique_ptr<sdbusplus::asio::dbus_interface>
91 makeReportInterface(const interfaces::ReportFactory& reportFactory);
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010092 static void timerProcForPeriodicReport(boost::system::error_code,
93 Report& self);
94 static void timerProcForOnChangeReport(boost::system::error_code,
95 Report& self);
96 void scheduleTimerForPeriodicReport(Milliseconds interval);
97 void scheduleTimerForOnChangeReport();
Szymon Dompke3eb56862021-09-20 15:32:04 +020098 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
99 const ReportingType reportingTypeIn) const;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100100 void setReadingBuffer(const ReportUpdates newReportUpdates);
Szymon Dompke3eb56862021-09-20 15:32:04 +0200101 void setReportUpdates(const ReportUpdates newReportUpdates);
Krzysztof Grobelny18e71012022-11-02 13:17:01 +0000102 static uint64_t getMetricCount(
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100103 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100104 interfaces::JsonStorage::FilePath reportFileName() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100105 std::unordered_set<std::string>
106 collectTriggerIds(boost::asio::io_context& ioc) const;
107 bool storeConfiguration() const;
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100108 bool shouldStoreMetricValues() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100109 void updateReadings();
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200110 void scheduleTimer();
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000111 static std::vector<ErrorMessage> verify(ReportingType, Milliseconds);
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200112
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100113 std::string id;
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200114 const sdbusplus::message::object_path path;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200115 std::string name;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200116 ReportingType reportingType;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000117 Milliseconds interval;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100118 std::unordered_set<ReportAction> reportActions;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100119 ReadingParameters readingParameters;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000120 bool persistency = false;
Krzysztof Grobelny18e71012022-11-02 13:17:01 +0000121 uint64_t metricCount;
122 uint64_t appendLimit;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200123 ReportUpdates reportUpdates;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200124 Readings readings = {};
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100125 CircularVector<ReadingData> readingsBuffer;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200126 std::shared_ptr<sdbusplus::asio::object_server> objServer;
127 std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
128 std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200129 std::vector<std::shared_ptr<interfaces::Metric>> metrics;
130 boost::asio::steady_timer timer;
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100131 std::unordered_set<std::string> triggerIds;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200132
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100133 interfaces::JsonStorage& reportStorage;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100134 std::unique_ptr<interfaces::Clock> clock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100135 utils::Messanger messanger;
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100136 std::optional<OnChangeContext> onChangeContext;
137 utils::Ensure<std::function<void()>> unregisterFromMetrics;
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200138 State<ReportFlags, Report, ReportFlags::enabled, ReportFlags::valid> state{
139 *this};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100140
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200141 public:
142 static constexpr const char* reportIfaceName =
143 "xyz.openbmc_project.Telemetry.Report";
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200144 static constexpr const char* deleteIfaceName =
145 "xyz.openbmc_project.Object.Delete";
Krzysztof Grobelny18e71012022-11-02 13:17:01 +0000146 static constexpr size_t reportVersion = 7;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200147};