Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | #include "bcd_time.hpp" |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 3 | #include "paths.hpp" |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 4 | #include "pel.hpp" |
| 5 | |
| 6 | #include <algorithm> |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 7 | #include <bitset> |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 8 | #include <filesystem> |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 9 | #include <map> |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace pels |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @class Repository |
| 18 | * |
| 19 | * The class handles saving and retrieving PELs on the BMC. |
| 20 | */ |
| 21 | class Repository |
| 22 | { |
| 23 | public: |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 24 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 25 | * @brief Structure of commonly used PEL attributes. |
| 26 | */ |
| 27 | struct PELAttributes |
| 28 | { |
| 29 | std::filesystem::path path; |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 30 | size_t sizeOnDisk; |
| 31 | uint8_t creator; |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 32 | uint8_t subsystem; |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 33 | uint8_t severity; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 34 | std::bitset<16> actionFlags; |
Matt Spinler | 346f99a | 2019-11-21 13:06:35 -0600 | [diff] [blame] | 35 | TransmissionState hostState; |
| 36 | TransmissionState hmcState; |
Matt Spinler | 8e65f4e | 2023-05-02 13:40:08 -0500 | [diff] [blame] | 37 | uint32_t plid; |
| 38 | bool deconfig; |
| 39 | bool guard; |
| 40 | uint64_t creationTime; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 41 | |
| 42 | PELAttributes() = delete; |
| 43 | |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 44 | PELAttributes(const std::filesystem::path& p, size_t size, |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 45 | uint8_t creator, uint8_t subsystem, uint8_t sev, |
| 46 | uint16_t flags, TransmissionState hostState, |
Matt Spinler | 8e65f4e | 2023-05-02 13:40:08 -0500 | [diff] [blame] | 47 | TransmissionState hmcState, uint32_t plid, bool deconfig, |
| 48 | bool guard, uint64_t creationTime) : |
Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 49 | path(p), sizeOnDisk(size), creator(creator), subsystem(subsystem), |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 50 | severity(sev), actionFlags(flags), hostState(hostState), |
Matt Spinler | 8e65f4e | 2023-05-02 13:40:08 -0500 | [diff] [blame] | 51 | hmcState(hmcState), plid(plid), deconfig(deconfig), guard(guard), |
| 52 | creationTime(creationTime) |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 53 | {} |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /** |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 57 | * @brief A structure that holds both the PEL and corresponding |
| 58 | * OpenBMC IDs. |
| 59 | * Used for correlating the IDs with their data files for quick |
| 60 | * lookup. To find a PEL based on just one of the IDs, just use |
| 61 | * the constructor that takes that ID. |
| 62 | */ |
| 63 | struct LogID |
| 64 | { |
| 65 | struct Pel |
| 66 | { |
| 67 | uint32_t id; |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 68 | explicit Pel(uint32_t i) : id(i) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 69 | }; |
| 70 | struct Obmc |
| 71 | { |
| 72 | uint32_t id; |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 73 | explicit Obmc(uint32_t i) : id(i) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | Pel pelID; |
| 77 | |
| 78 | Obmc obmcID; |
| 79 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 80 | LogID(Pel pel, Obmc obmc) : pelID(pel), obmcID(obmc) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 81 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 82 | explicit LogID(Pel id) : pelID(id), obmcID(0) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 83 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 84 | explicit LogID(Obmc id) : pelID(0), obmcID(id) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 85 | |
| 86 | LogID() = delete; |
| 87 | |
| 88 | /** |
| 89 | * @brief A == operator that will match on either ID |
| 90 | * being equal if the other is zero, so that |
| 91 | * one can look up a PEL with just one of the IDs. |
| 92 | */ |
| 93 | bool operator==(const LogID& id) const |
| 94 | { |
| 95 | if (id.pelID.id != 0) |
| 96 | { |
| 97 | return id.pelID.id == pelID.id; |
| 98 | } |
| 99 | if (id.obmcID.id != 0) |
| 100 | { |
| 101 | return id.obmcID.id == obmcID.id; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | bool operator<(const LogID& id) const |
| 107 | { |
| 108 | return pelID.id < id.pelID.id; |
| 109 | } |
| 110 | }; |
| 111 | |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 112 | using AttributesReference = |
| 113 | std::reference_wrapper<const std::pair<const LogID, PELAttributes>>; |
| 114 | |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 115 | /** |
| 116 | * @brief A structure for keeping a breakdown of the sizes of PELs |
| 117 | * of different types in the repository. |
| 118 | */ |
| 119 | struct SizeStats |
| 120 | { |
| 121 | uint64_t total; |
| 122 | uint64_t bmc; |
| 123 | uint64_t nonBMC; |
| 124 | uint64_t bmcServiceable; |
| 125 | uint64_t bmcInfo; |
| 126 | uint64_t nonBMCServiceable; |
| 127 | uint64_t nonBMCInfo; |
| 128 | |
| 129 | SizeStats() : |
| 130 | total(0), bmc(0), nonBMC(0), bmcServiceable(0), bmcInfo(0), |
| 131 | nonBMCServiceable(0), nonBMCInfo(0) |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 132 | {} |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 133 | }; |
| 134 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 135 | Repository() = delete; |
| 136 | ~Repository() = default; |
| 137 | Repository(const Repository&) = default; |
| 138 | Repository& operator=(const Repository&) = default; |
| 139 | Repository(Repository&&) = default; |
| 140 | Repository& operator=(Repository&&) = default; |
| 141 | |
| 142 | /** |
| 143 | * @brief Constructor |
| 144 | * |
| 145 | * @param[in] basePath - the base filesystem path for the repository |
| 146 | */ |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 147 | explicit Repository(const std::filesystem::path& basePath) : |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 148 | Repository(basePath, getPELRepoSize(), getMaxNumPELs()) |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 149 | {} |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * @brief Constructor that takes the repository size |
| 153 | * |
| 154 | * @param[in] basePath - the base filesystem path for the repository |
| 155 | * @param[in] repoSize - The maximum amount of space to use for PELs, |
| 156 | * in bytes |
| 157 | * @param[in] maxNumPELs - The maximum number of PELs to allow |
| 158 | */ |
| 159 | Repository(const std::filesystem::path& basePath, size_t repoSize, |
| 160 | size_t maxNumPELs); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * @brief Adds a PEL to the repository |
| 164 | * |
| 165 | * Throws File.Error.Open or File.Error.Write exceptions on failure |
| 166 | * |
| 167 | * @param[in] pel - the PEL to add |
| 168 | */ |
| 169 | void add(std::unique_ptr<PEL>& pel); |
| 170 | |
| 171 | /** |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 172 | * @brief Removes a PEL from the repository |
| 173 | * |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 174 | * Note that the returned LogID is the fully filled in LogID, i.e. |
| 175 | * it has both the PEL and OpenBMC IDs, unlike the passed in LogID |
| 176 | * which can just have one or the other. |
| 177 | * |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 178 | * @param[in] id - the ID (either the pel ID, OBMC ID, or both) to remove |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 179 | * |
| 180 | * @return std::optional<LogID> - The LogID of the removed PEL |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 181 | */ |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 182 | std::optional<LogID> remove(const LogID& id); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 183 | |
| 184 | /** |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 185 | * @brief Generates the filename to use for the PEL ID and BCDTime. |
| 186 | * |
| 187 | * @param[in] pelID - the PEL ID |
| 188 | * @param[in] time - the BCD time |
| 189 | * |
| 190 | * @return string - A filename string of <BCD_time>_<pelID> |
| 191 | */ |
| 192 | static std::string getPELFilename(uint32_t pelID, const BCDTime& time); |
| 193 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 194 | /** |
| 195 | * @brief Returns true if the PEL with the specified ID is in the repo. |
| 196 | * |
| 197 | * @param[in] id - the ID (either the pel ID, OBMC ID, or both) |
| 198 | * @return bool - true if that PEL is present |
| 199 | */ |
| 200 | inline bool hasPEL(const LogID& id) |
| 201 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 202 | return findPEL(id) != _pelAttributes.end(); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 205 | /** |
| 206 | * @brief Returns the PEL data based on its ID. |
| 207 | * |
| 208 | * If the data can't be found for that ID, then the optional object |
| 209 | * will be empty. |
| 210 | * |
| 211 | * @param[in] id - the LogID to get the PEL for, which can be either a |
| 212 | * PEL ID or OpenBMC log ID. |
| 213 | * @return std::optional<std::vector<uint8_t>> - the PEL data |
| 214 | */ |
| 215 | std::optional<std::vector<uint8_t>> getPELData(const LogID& id); |
| 216 | |
Matt Spinler | 6d51224 | 2019-12-09 13:44:17 -0600 | [diff] [blame] | 217 | /** |
| 218 | * @brief Get a file descriptor to the PEL data |
| 219 | * |
| 220 | * @param[in] id - The ID to get the FD for |
| 221 | * |
| 222 | * @return std::optional<sdbusplus::message::unix_fd> - |
| 223 | * The FD, or an empty optional object. |
| 224 | */ |
| 225 | std::optional<sdbusplus::message::unix_fd> getPELFD(const LogID& id); |
| 226 | |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 227 | using ForEachFunc = std::function<bool(const PEL&)>; |
| 228 | |
| 229 | /** |
| 230 | * @brief Run a user defined function on every PEL in the repository. |
| 231 | * |
| 232 | * ForEachFunc takes a const PEL reference, and should return |
| 233 | * true to stop iterating and return out of for_each. |
| 234 | * |
| 235 | * For example, to save up to 100 IDs in the repo into a vector: |
| 236 | * |
| 237 | * std::vector<uint32_t> ids; |
| 238 | * ForEachFunc f = [&ids](const PEL& pel) { |
| 239 | * ids.push_back(pel.id()); |
| 240 | * return ids.size() == 100 ? true : false; |
| 241 | * }; |
| 242 | * |
| 243 | * @param[in] func - The function to run. |
| 244 | */ |
| 245 | void for_each(ForEachFunc func) const; |
| 246 | |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 247 | using AddCallback = std::function<void(const PEL&)>; |
| 248 | |
| 249 | /** |
| 250 | * @brief Subscribe to PELs being added to the repository. |
| 251 | * |
| 252 | * Every time a PEL is added to the repository, the provided |
| 253 | * function will be called with the new PEL as the argument. |
| 254 | * |
| 255 | * The function must be of type void(const PEL&). |
| 256 | * |
| 257 | * @param[in] name - The subscription name |
| 258 | * @param[in] func - The callback function |
| 259 | */ |
| 260 | void subscribeToAdds(const std::string& name, AddCallback func) |
| 261 | { |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 262 | _addSubscriptions.emplace(name, func); |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @brief Unsubscribe from new PELs. |
| 267 | * |
| 268 | * @param[in] name - The subscription name |
| 269 | */ |
| 270 | void unsubscribeFromAdds(const std::string& name) |
| 271 | { |
| 272 | _addSubscriptions.erase(name); |
| 273 | } |
| 274 | |
| 275 | using DeleteCallback = std::function<void(uint32_t)>; |
| 276 | |
| 277 | /** |
| 278 | * @brief Subscribe to PELs being deleted from the repository. |
| 279 | * |
| 280 | * Every time a PEL is deleted from the repository, the provided |
| 281 | * function will be called with the PEL ID as the argument. |
| 282 | * |
| 283 | * The function must be of type void(const uint32_t). |
| 284 | * |
| 285 | * @param[in] name - The subscription name |
| 286 | * @param[in] func - The callback function |
| 287 | */ |
| 288 | void subscribeToDeletes(const std::string& name, DeleteCallback func) |
| 289 | { |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 290 | _deleteSubscriptions.emplace(name, func); |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @brief Unsubscribe from deleted PELs. |
| 295 | * |
| 296 | * @param[in] name - The subscription name |
| 297 | */ |
| 298 | void unsubscribeFromDeletes(const std::string& name) |
| 299 | { |
| 300 | _deleteSubscriptions.erase(name); |
| 301 | } |
| 302 | |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 303 | /** |
| 304 | * @brief Get the PEL attributes for a PEL |
| 305 | * |
| 306 | * @param[in] id - The ID to find the attributes for |
| 307 | * |
| 308 | * @return The attributes or an empty optional if not found |
| 309 | */ |
| 310 | std::optional<std::reference_wrapper<const PELAttributes>> |
| 311 | getPELAttributes(const LogID& id) const; |
| 312 | |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 313 | /** |
Matt Spinler | 0dd22c8 | 2023-05-04 15:28:12 -0500 | [diff] [blame] | 314 | * @brief Returns the attributes map so that others can traverse PELs. |
| 315 | * |
| 316 | * @return - A const reference to the attributes map. |
| 317 | */ |
| 318 | const std::map<LogID, PELAttributes>& getAttributesMap() const |
| 319 | { |
| 320 | return _pelAttributes; |
| 321 | } |
| 322 | |
| 323 | /** |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 324 | * @brief Sets the host transmission state on a PEL file |
| 325 | * |
| 326 | * Writes the host transmission state field in the User Header |
| 327 | * section in the PEL data specified by the ID. |
| 328 | * |
| 329 | * @param[in] pelID - The PEL ID |
| 330 | * @param[in] state - The state to write |
| 331 | */ |
| 332 | void setPELHostTransState(uint32_t pelID, TransmissionState state); |
| 333 | |
| 334 | /** |
| 335 | * @brief Sets the HMC transmission state on a PEL file |
| 336 | * |
| 337 | * Writes the HMC transmission state field in the User Header |
| 338 | * section in the PEL data specified by the ID. |
| 339 | * |
| 340 | * @param[in] pelID - The PEL ID |
| 341 | * @param[in] state - The state to write |
| 342 | */ |
| 343 | void setPELHMCTransState(uint32_t pelID, TransmissionState state); |
| 344 | |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 345 | /** |
| 346 | * @brief Returns the size stats structure |
| 347 | * |
| 348 | * @return const SizeStats& - The stats structure |
| 349 | */ |
| 350 | const SizeStats& getSizeStats() const |
| 351 | { |
| 352 | return _sizes; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * @brief Says if the PEL is considered serviceable (not just |
| 357 | * informational) as determined by its severity. |
| 358 | * |
| 359 | * @param[in] pel - The PELAttributes entry for the PEL |
| 360 | * @return bool - If serviceable or not |
| 361 | */ |
| 362 | static bool isServiceableSev(const PELAttributes& pel); |
| 363 | |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 364 | /** |
Matt Spinler | 7e727a3 | 2020-07-07 15:00:17 -0500 | [diff] [blame] | 365 | * @brief Returns true if the total amount of disk space occupied |
| 366 | * by the PELs in the repo is over 95% of the maximum |
| 367 | * size, or if there are over the maximum number of |
| 368 | * PELs allowed. |
| 369 | * |
| 370 | * @return bool - true if repo is > 95% full or too many PELs |
| 371 | */ |
Sumit Kumar | c296692 | 2021-07-21 10:14:03 -0500 | [diff] [blame] | 372 | bool sizeWarning(); |
Matt Spinler | 7e727a3 | 2020-07-07 15:00:17 -0500 | [diff] [blame] | 373 | |
| 374 | /** |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 375 | * @brief Deletes PELs to bring the repository size down |
| 376 | * to at most 90% full by placing PELs into 4 different |
| 377 | * catogories and then removing PELs until those catogories |
| 378 | * only take up certain percentages of the allowed space. |
| 379 | * |
| 380 | * This does not delete the corresponding OpenBMC event logs, which |
| 381 | * is why those IDs are returned, so they can be deleted later. |
| 382 | * |
| 383 | * The categories and their rules are: |
| 384 | * 1) Informational BMC PELs cannot take up more than 15% of |
| 385 | * the allocated space. |
| 386 | * 2) Non-informational BMC PELs cannot take up more than 30% |
| 387 | * of the allocated space. |
| 388 | * 3) Informational non-BMC PELs cannot take up more than 15% of |
| 389 | * the allocated space. |
| 390 | * 4) Non-informational non-BMC PELs cannot take up more than 30% |
| 391 | * of the allocated space. |
| 392 | * |
| 393 | * While removing PELs in a category, 4 passes will be made, with |
| 394 | * PELs being removed oldest first during each pass. |
| 395 | * |
| 396 | * Pass 1: only delete HMC acked PELs |
| 397 | * Pass 2: only delete OS acked PELs |
| 398 | * Pass 3: only delete PHYP sent PELs |
| 399 | * Pass 4: delete all PELs |
| 400 | * |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 401 | * @param[in] ids - The OpenBMC event log Ids with hardware isolation entry. |
| 402 | * |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 403 | * @return std::vector<uint32_t> - The OpenBMC event log IDs of |
| 404 | * the PELs that were deleted. |
| 405 | */ |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 406 | std::vector<uint32_t> prune(const std::vector<uint32_t>& idsWithHwIsoEntry); |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 407 | |
Matt Spinler | ff9cec2 | 2020-07-15 13:06:35 -0500 | [diff] [blame] | 408 | /** |
| 409 | * @brief Returns the path to the directory where the PEL |
| 410 | * files are stored. |
| 411 | * |
| 412 | * @return std::filesystem::path - The directory path |
| 413 | */ |
| 414 | const std::filesystem::path& repoPath() const |
| 415 | { |
| 416 | return _logPath; |
| 417 | } |
| 418 | |
Matt Spinler | 44893cc | 2020-08-26 11:34:17 -0500 | [diff] [blame] | 419 | /** |
| 420 | * @brief Returns the ID of the most recently added PEL. |
| 421 | * |
| 422 | * @return uint32_t - The PEL ID |
| 423 | */ |
| 424 | uint32_t lastPelID() const |
| 425 | { |
| 426 | return _lastPelID; |
| 427 | } |
| 428 | |
Ramesh Iyyar | 99f3717 | 2021-06-24 05:41:51 -0500 | [diff] [blame] | 429 | /** |
| 430 | * @brief Get the LogID based on the given ObmcLogId or PelId. |
| 431 | * |
| 432 | * @param[in] id - The ID to find the LogID. |
| 433 | * |
| 434 | * @return The LogID or an empty optional if not found. |
| 435 | * |
| 436 | * @note The returned LogID is the fully filled in LogID, i.e. |
| 437 | * it has both the PEL and OpenBMC Log IDs, unlike the passed in LogID |
| 438 | * which can just have one or the other. |
| 439 | */ |
| 440 | std::optional<LogID> getLogID(const LogID& id) const |
| 441 | { |
| 442 | if (auto logID = findPEL(id); logID != _pelAttributes.end()) |
| 443 | { |
| 444 | return logID->first; |
| 445 | } |
| 446 | return std::nullopt; |
| 447 | } |
| 448 | |
Sumit Kumar | 2ccdcef | 2021-07-31 10:04:58 -0500 | [diff] [blame] | 449 | /** |
| 450 | * @brief Save the PEL to archive folder |
| 451 | * |
| 452 | * @param[in] pel - The PEL data |
| 453 | */ |
| 454 | void archivePEL(const PEL& pel); |
| 455 | |
Matt Spinler | d0ccda3 | 2023-05-04 09:59:59 -0500 | [diff] [blame] | 456 | using PELUpdateFunc = std::function<bool(PEL&)>; |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 457 | |
| 458 | /** |
| 459 | * @brief Lets a function modify a PEL and saves the results |
| 460 | * |
Matt Spinler | d0ccda3 | 2023-05-04 09:59:59 -0500 | [diff] [blame] | 461 | * Runs updateFunc (a bool(PEL&) function) on the PEL data |
| 462 | * on the file specified, and writes the results back to the file |
| 463 | * if the function returned true. |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 464 | * |
| 465 | * @param[in] path - The file path to use |
| 466 | * @param[in] updateFunc - The function to run to update the PEL. |
Matt Spinler | 1cb59f7 | 2023-07-20 09:49:50 -0500 | [diff] [blame] | 467 | * |
| 468 | * @return bool - If the PEL was updated or not. |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 469 | */ |
Matt Spinler | 1cb59f7 | 2023-07-20 09:49:50 -0500 | [diff] [blame] | 470 | bool updatePEL(const std::filesystem::path& path, PELUpdateFunc updateFunc); |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 471 | |
Matt Spinler | 0dd22c8 | 2023-05-04 15:28:12 -0500 | [diff] [blame] | 472 | private: |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 473 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 474 | * @brief Finds an entry in the _pelAttributes map. |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 475 | * |
| 476 | * @param[in] id - the ID (either the pel ID, OBMC ID, or both) |
| 477 | * |
| 478 | * @return an iterator to the entry |
| 479 | */ |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 480 | std::map<LogID, PELAttributes>::const_iterator |
| 481 | findPEL(const LogID& id) const |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 482 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 483 | return std::find_if(_pelAttributes.begin(), _pelAttributes.end(), |
| 484 | [&id](const auto& a) { return a.first == id; }); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /** |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 488 | * @brief Call any subscribed functions for new PELs |
| 489 | * |
| 490 | * @param[in] pel - The new PEL |
| 491 | */ |
| 492 | void processAddCallbacks(const PEL& pel) const; |
| 493 | |
| 494 | /** |
| 495 | * @brief Call any subscribed functions for deleted PELs |
| 496 | * |
| 497 | * @param[in] id - The ID of the deleted PEL |
| 498 | */ |
| 499 | void processDeleteCallbacks(uint32_t id) const; |
| 500 | |
| 501 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 502 | * @brief Restores the _pelAttributes map on startup based on the existing |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 503 | * PEL data files. |
| 504 | */ |
| 505 | void restore(); |
| 506 | |
| 507 | /** |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame] | 508 | * @brief Stores a PEL object in the filesystem. |
| 509 | * |
| 510 | * @param[in] pel - The PEL to write |
| 511 | * @param[in] path - The file to write to |
| 512 | * |
| 513 | * Throws exceptions on failures. |
| 514 | */ |
| 515 | void write(const PEL& pel, const std::filesystem::path& path); |
| 516 | |
| 517 | /** |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 518 | * @brief Updates the repository statistics after a PEL is |
| 519 | * added or removed. |
| 520 | * |
| 521 | * @param[in] pel - The PELAttributes entry for the PEL |
| 522 | * @param[in] pelAdded - true if the PEL was added, false if removed |
| 523 | */ |
| 524 | void updateRepoStats(const PELAttributes& pel, bool pelAdded); |
| 525 | |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 526 | enum class SortOrder |
| 527 | { |
| 528 | ascending, |
| 529 | descending |
| 530 | }; |
| 531 | |
| 532 | /** |
| 533 | * @brief Returns a vector of all the _pelAttributes entries sorted |
| 534 | * as specified |
| 535 | * |
| 536 | * @param[in] order - If the PELs should be returned in ascending |
| 537 | * (oldest first) or descending order. |
| 538 | * |
| 539 | * @return std::vector<AttributesReference> - The sorted vector of |
| 540 | * references to the pair<LogID, PELAttributes> entries of |
| 541 | * _pelAttributes. |
| 542 | */ |
| 543 | std::vector<AttributesReference> getAllPELAttributes(SortOrder order) const; |
| 544 | |
| 545 | using IsOverLimitFunc = std::function<bool()>; |
| 546 | using IsPELTypeFunc = std::function<bool(const PELAttributes&)>; |
| 547 | |
| 548 | /** |
| 549 | * @brief Makes 4 passes on the PELs that meet the IsPELTypeFunc |
| 550 | * criteria removing PELs until IsOverLimitFunc returns false. |
| 551 | * |
| 552 | * Pass 1: only delete HMC acked PELs |
| 553 | * Pass 2: only delete Os acked PELs |
| 554 | * Pass 3: only delete PHYP sent PELs |
| 555 | * Pass 4: delete all PELs |
| 556 | * |
| 557 | * @param[in] isOverLimit - The bool(void) function that should |
| 558 | * return true if PELs still need to be |
| 559 | * removed. |
| 560 | * @param[in] isPELType - The bool(const PELAttributes&) function |
| 561 | * used to select the PELs to operate on. |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 562 | * @param[in] ids - The OpenBMC event log Ids with hardware isolation |
| 563 | * entry. |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 564 | * |
| 565 | * @param[out] removedBMCLogIDs - The OpenBMC event log IDs of the |
| 566 | * removed PELs. |
| 567 | */ |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 568 | void removePELs(const IsOverLimitFunc& isOverLimit, |
| 569 | const IsPELTypeFunc& isPELType, |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 570 | const std::vector<uint32_t>& idsWithHwIsoEntry, |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 571 | std::vector<uint32_t>& removedBMCLogIDs); |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 572 | /** |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 573 | * @brief The filesystem path to the PEL logs. |
| 574 | */ |
| 575 | const std::filesystem::path _logPath; |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 576 | |
| 577 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 578 | * @brief A map of the PEL/OBMC IDs to PEL attributes. |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 579 | */ |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 580 | std::map<LogID, PELAttributes> _pelAttributes; |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 581 | |
| 582 | /** |
| 583 | * @brief Subcriptions for new PELs. |
| 584 | */ |
| 585 | std::map<std::string, AddCallback> _addSubscriptions; |
| 586 | |
| 587 | /** |
| 588 | * @brief Subscriptions for deleted PELs. |
| 589 | */ |
| 590 | std::map<std::string, DeleteCallback> _deleteSubscriptions; |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 591 | |
| 592 | /** |
| 593 | * @brief The maximum amount of space that the PELs in the |
| 594 | * repository can occupy. |
| 595 | */ |
| 596 | const uint64_t _maxRepoSize; |
| 597 | |
| 598 | /** |
| 599 | * @brief The maximum number of PELs to allow in the repo |
| 600 | * before pruning. |
| 601 | */ |
| 602 | const size_t _maxNumPELs; |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 603 | |
| 604 | /** |
| 605 | * @brief Statistics on the sizes of the stored PELs. |
| 606 | */ |
| 607 | SizeStats _sizes; |
Matt Spinler | 44893cc | 2020-08-26 11:34:17 -0500 | [diff] [blame] | 608 | |
| 609 | /** |
| 610 | * @brief The ID of the most recently added PEL. |
| 611 | */ |
| 612 | uint32_t _lastPelID = 0; |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 613 | |
| 614 | /** |
| 615 | * @brief The filesystem path to the archive PEL logs. |
| 616 | */ |
| 617 | const std::filesystem::path _archivePath; |
| 618 | |
| 619 | /** |
| 620 | * @brief The size of archive folder. |
| 621 | */ |
| 622 | uint64_t _archiveSize = 0; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 623 | }; |
| 624 | |
| 625 | } // namespace pels |
| 626 | } // namespace openpower |