189fa082aSMatt Spinler #pragma once 289fa082aSMatt Spinler #include "bcd_time.hpp" 38d5f3a2bSMatt Spinler #include "paths.hpp" 489fa082aSMatt Spinler #include "pel.hpp" 589fa082aSMatt Spinler 689fa082aSMatt Spinler #include <algorithm> 70ff00485SMatt Spinler #include <bitset> 889fa082aSMatt Spinler #include <filesystem> 9475e574dSMatt Spinler #include <map> 1089fa082aSMatt Spinler 1189fa082aSMatt Spinler namespace openpower 1289fa082aSMatt Spinler { 1389fa082aSMatt Spinler namespace pels 1489fa082aSMatt Spinler { 1589fa082aSMatt Spinler 1689fa082aSMatt Spinler /** 1789fa082aSMatt Spinler * @class Repository 1889fa082aSMatt Spinler * 1989fa082aSMatt Spinler * The class handles saving and retrieving PELs on the BMC. 2089fa082aSMatt Spinler */ 2189fa082aSMatt Spinler class Repository 2289fa082aSMatt Spinler { 2389fa082aSMatt Spinler public: 24475e574dSMatt Spinler /** 250ff00485SMatt Spinler * @brief Structure of commonly used PEL attributes. 260ff00485SMatt Spinler */ 270ff00485SMatt Spinler struct PELAttributes 280ff00485SMatt Spinler { 290ff00485SMatt Spinler std::filesystem::path path; 30dd325c32SMatt Spinler size_t sizeOnDisk; 31dd325c32SMatt Spinler uint8_t creator; 32dd325c32SMatt Spinler uint8_t severity; 330ff00485SMatt Spinler std::bitset<16> actionFlags; 34346f99a1SMatt Spinler TransmissionState hostState; 35346f99a1SMatt Spinler TransmissionState hmcState; 360ff00485SMatt Spinler 370ff00485SMatt Spinler PELAttributes() = delete; 380ff00485SMatt Spinler 39dd325c32SMatt Spinler PELAttributes(const std::filesystem::path& p, size_t size, 40dd325c32SMatt Spinler uint8_t creator, uint8_t sev, uint16_t flags, 41346f99a1SMatt Spinler TransmissionState hostState, TransmissionState hmcState) : 42346f99a1SMatt Spinler path(p), 43dd325c32SMatt Spinler sizeOnDisk(size), creator(creator), severity(sev), 44346f99a1SMatt Spinler actionFlags(flags), hostState(hostState), hmcState(hmcState) 450ff00485SMatt Spinler { 460ff00485SMatt Spinler } 470ff00485SMatt Spinler }; 480ff00485SMatt Spinler 490ff00485SMatt Spinler /** 50475e574dSMatt Spinler * @brief A structure that holds both the PEL and corresponding 51475e574dSMatt Spinler * OpenBMC IDs. 52475e574dSMatt Spinler * Used for correlating the IDs with their data files for quick 53475e574dSMatt Spinler * lookup. To find a PEL based on just one of the IDs, just use 54475e574dSMatt Spinler * the constructor that takes that ID. 55475e574dSMatt Spinler */ 56475e574dSMatt Spinler struct LogID 57475e574dSMatt Spinler { 58475e574dSMatt Spinler struct Pel 59475e574dSMatt Spinler { 60475e574dSMatt Spinler uint32_t id; 61475e574dSMatt Spinler explicit Pel(uint32_t i) : id(i) 62475e574dSMatt Spinler { 63475e574dSMatt Spinler } 64475e574dSMatt Spinler }; 65475e574dSMatt Spinler struct Obmc 66475e574dSMatt Spinler { 67475e574dSMatt Spinler uint32_t id; 68475e574dSMatt Spinler explicit Obmc(uint32_t i) : id(i) 69475e574dSMatt Spinler { 70475e574dSMatt Spinler } 71475e574dSMatt Spinler }; 72475e574dSMatt Spinler 73475e574dSMatt Spinler Pel pelID; 74475e574dSMatt Spinler 75475e574dSMatt Spinler Obmc obmcID; 76475e574dSMatt Spinler 77475e574dSMatt Spinler LogID(Pel pel, Obmc obmc) : pelID(pel), obmcID(obmc) 78475e574dSMatt Spinler { 79475e574dSMatt Spinler } 80475e574dSMatt Spinler 81475e574dSMatt Spinler explicit LogID(Pel id) : pelID(id), obmcID(0) 82475e574dSMatt Spinler { 83475e574dSMatt Spinler } 84475e574dSMatt Spinler 85475e574dSMatt Spinler explicit LogID(Obmc id) : pelID(0), obmcID(id) 86475e574dSMatt Spinler { 87475e574dSMatt Spinler } 88475e574dSMatt Spinler 89475e574dSMatt Spinler LogID() = delete; 90475e574dSMatt Spinler 91475e574dSMatt Spinler /** 92475e574dSMatt Spinler * @brief A == operator that will match on either ID 93475e574dSMatt Spinler * being equal if the other is zero, so that 94475e574dSMatt Spinler * one can look up a PEL with just one of the IDs. 95475e574dSMatt Spinler */ 96475e574dSMatt Spinler bool operator==(const LogID& id) const 97475e574dSMatt Spinler { 98475e574dSMatt Spinler if (id.pelID.id != 0) 99475e574dSMatt Spinler { 100475e574dSMatt Spinler return id.pelID.id == pelID.id; 101475e574dSMatt Spinler } 102475e574dSMatt Spinler if (id.obmcID.id != 0) 103475e574dSMatt Spinler { 104475e574dSMatt Spinler return id.obmcID.id == obmcID.id; 105475e574dSMatt Spinler } 106475e574dSMatt Spinler return false; 107475e574dSMatt Spinler } 108475e574dSMatt Spinler 109475e574dSMatt Spinler bool operator<(const LogID& id) const 110475e574dSMatt Spinler { 111475e574dSMatt Spinler return pelID.id < id.pelID.id; 112475e574dSMatt Spinler } 113475e574dSMatt Spinler }; 114475e574dSMatt Spinler 115b0a8df5bSMatt Spinler using AttributesReference = 116b0a8df5bSMatt Spinler std::reference_wrapper<const std::pair<const LogID, PELAttributes>>; 117b0a8df5bSMatt Spinler 118b188f78aSMatt Spinler /** 119b188f78aSMatt Spinler * @brief A structure for keeping a breakdown of the sizes of PELs 120b188f78aSMatt Spinler * of different types in the repository. 121b188f78aSMatt Spinler */ 122b188f78aSMatt Spinler struct SizeStats 123b188f78aSMatt Spinler { 124b188f78aSMatt Spinler uint64_t total; 125b188f78aSMatt Spinler uint64_t bmc; 126b188f78aSMatt Spinler uint64_t nonBMC; 127b188f78aSMatt Spinler uint64_t bmcServiceable; 128b188f78aSMatt Spinler uint64_t bmcInfo; 129b188f78aSMatt Spinler uint64_t nonBMCServiceable; 130b188f78aSMatt Spinler uint64_t nonBMCInfo; 131b188f78aSMatt Spinler 132b188f78aSMatt Spinler SizeStats() : 133b188f78aSMatt Spinler total(0), bmc(0), nonBMC(0), bmcServiceable(0), bmcInfo(0), 134b188f78aSMatt Spinler nonBMCServiceable(0), nonBMCInfo(0) 135b188f78aSMatt Spinler { 136b188f78aSMatt Spinler } 137b188f78aSMatt Spinler }; 138b188f78aSMatt Spinler 13989fa082aSMatt Spinler Repository() = delete; 14089fa082aSMatt Spinler ~Repository() = default; 14189fa082aSMatt Spinler Repository(const Repository&) = default; 14289fa082aSMatt Spinler Repository& operator=(const Repository&) = default; 14389fa082aSMatt Spinler Repository(Repository&&) = default; 14489fa082aSMatt Spinler Repository& operator=(Repository&&) = default; 14589fa082aSMatt Spinler 14689fa082aSMatt Spinler /** 14789fa082aSMatt Spinler * @brief Constructor 14889fa082aSMatt Spinler * 14989fa082aSMatt Spinler * @param[in] basePath - the base filesystem path for the repository 15089fa082aSMatt Spinler */ 1518d5f3a2bSMatt Spinler Repository(const std::filesystem::path& basePath) : 1528d5f3a2bSMatt Spinler Repository(basePath, getPELRepoSize(), getMaxNumPELs()) 1538d5f3a2bSMatt Spinler { 1548d5f3a2bSMatt Spinler } 1558d5f3a2bSMatt Spinler 1568d5f3a2bSMatt Spinler /** 1578d5f3a2bSMatt Spinler * @brief Constructor that takes the repository size 1588d5f3a2bSMatt Spinler * 1598d5f3a2bSMatt Spinler * @param[in] basePath - the base filesystem path for the repository 1608d5f3a2bSMatt Spinler * @param[in] repoSize - The maximum amount of space to use for PELs, 1618d5f3a2bSMatt Spinler * in bytes 1628d5f3a2bSMatt Spinler * @param[in] maxNumPELs - The maximum number of PELs to allow 1638d5f3a2bSMatt Spinler */ 1648d5f3a2bSMatt Spinler Repository(const std::filesystem::path& basePath, size_t repoSize, 1658d5f3a2bSMatt Spinler size_t maxNumPELs); 16689fa082aSMatt Spinler 16789fa082aSMatt Spinler /** 16889fa082aSMatt Spinler * @brief Adds a PEL to the repository 16989fa082aSMatt Spinler * 17089fa082aSMatt Spinler * Throws File.Error.Open or File.Error.Write exceptions on failure 17189fa082aSMatt Spinler * 17289fa082aSMatt Spinler * @param[in] pel - the PEL to add 17389fa082aSMatt Spinler */ 17489fa082aSMatt Spinler void add(std::unique_ptr<PEL>& pel); 17589fa082aSMatt Spinler 17689fa082aSMatt Spinler /** 177475e574dSMatt Spinler * @brief Removes a PEL from the repository 178475e574dSMatt Spinler * 17952602e35SMatt Spinler * Note that the returned LogID is the fully filled in LogID, i.e. 18052602e35SMatt Spinler * it has both the PEL and OpenBMC IDs, unlike the passed in LogID 18152602e35SMatt Spinler * which can just have one or the other. 18252602e35SMatt Spinler * 183475e574dSMatt Spinler * @param[in] id - the ID (either the pel ID, OBMC ID, or both) to remove 18452602e35SMatt Spinler * 18552602e35SMatt Spinler * @return std::optional<LogID> - The LogID of the removed PEL 186475e574dSMatt Spinler */ 18752602e35SMatt Spinler std::optional<LogID> remove(const LogID& id); 188475e574dSMatt Spinler 189475e574dSMatt Spinler /** 19089fa082aSMatt Spinler * @brief Generates the filename to use for the PEL ID and BCDTime. 19189fa082aSMatt Spinler * 19289fa082aSMatt Spinler * @param[in] pelID - the PEL ID 19389fa082aSMatt Spinler * @param[in] time - the BCD time 19489fa082aSMatt Spinler * 19589fa082aSMatt Spinler * @return string - A filename string of <BCD_time>_<pelID> 19689fa082aSMatt Spinler */ 19789fa082aSMatt Spinler static std::string getPELFilename(uint32_t pelID, const BCDTime& time); 19889fa082aSMatt Spinler 199475e574dSMatt Spinler /** 200475e574dSMatt Spinler * @brief Returns true if the PEL with the specified ID is in the repo. 201475e574dSMatt Spinler * 202475e574dSMatt Spinler * @param[in] id - the ID (either the pel ID, OBMC ID, or both) 203475e574dSMatt Spinler * @return bool - true if that PEL is present 204475e574dSMatt Spinler */ 205475e574dSMatt Spinler inline bool hasPEL(const LogID& id) 206475e574dSMatt Spinler { 2070ff00485SMatt Spinler return findPEL(id) != _pelAttributes.end(); 208475e574dSMatt Spinler } 209475e574dSMatt Spinler 2102813f36dSMatt Spinler /** 2112813f36dSMatt Spinler * @brief Returns the PEL data based on its ID. 2122813f36dSMatt Spinler * 2132813f36dSMatt Spinler * If the data can't be found for that ID, then the optional object 2142813f36dSMatt Spinler * will be empty. 2152813f36dSMatt Spinler * 2162813f36dSMatt Spinler * @param[in] id - the LogID to get the PEL for, which can be either a 2172813f36dSMatt Spinler * PEL ID or OpenBMC log ID. 2182813f36dSMatt Spinler * @return std::optional<std::vector<uint8_t>> - the PEL data 2192813f36dSMatt Spinler */ 2202813f36dSMatt Spinler std::optional<std::vector<uint8_t>> getPELData(const LogID& id); 2212813f36dSMatt Spinler 2226d51224bSMatt Spinler /** 2236d51224bSMatt Spinler * @brief Get a file descriptor to the PEL data 2246d51224bSMatt Spinler * 2256d51224bSMatt Spinler * @param[in] id - The ID to get the FD for 2266d51224bSMatt Spinler * 2276d51224bSMatt Spinler * @return std::optional<sdbusplus::message::unix_fd> - 2286d51224bSMatt Spinler * The FD, or an empty optional object. 2296d51224bSMatt Spinler */ 2306d51224bSMatt Spinler std::optional<sdbusplus::message::unix_fd> getPELFD(const LogID& id); 2316d51224bSMatt Spinler 2321ea78801SMatt Spinler using ForEachFunc = std::function<bool(const PEL&)>; 2331ea78801SMatt Spinler 2341ea78801SMatt Spinler /** 2351ea78801SMatt Spinler * @brief Run a user defined function on every PEL in the repository. 2361ea78801SMatt Spinler * 2371ea78801SMatt Spinler * ForEachFunc takes a const PEL reference, and should return 2381ea78801SMatt Spinler * true to stop iterating and return out of for_each. 2391ea78801SMatt Spinler * 2401ea78801SMatt Spinler * For example, to save up to 100 IDs in the repo into a vector: 2411ea78801SMatt Spinler * 2421ea78801SMatt Spinler * std::vector<uint32_t> ids; 2431ea78801SMatt Spinler * ForEachFunc f = [&ids](const PEL& pel) { 2441ea78801SMatt Spinler * ids.push_back(pel.id()); 2451ea78801SMatt Spinler * return ids.size() == 100 ? true : false; 2461ea78801SMatt Spinler * }; 2471ea78801SMatt Spinler * 2481ea78801SMatt Spinler * @param[in] func - The function to run. 2491ea78801SMatt Spinler */ 2501ea78801SMatt Spinler void for_each(ForEachFunc func) const; 2511ea78801SMatt Spinler 252421f6531SMatt Spinler using AddCallback = std::function<void(const PEL&)>; 253421f6531SMatt Spinler 254421f6531SMatt Spinler /** 255421f6531SMatt Spinler * @brief Subscribe to PELs being added to the repository. 256421f6531SMatt Spinler * 257421f6531SMatt Spinler * Every time a PEL is added to the repository, the provided 258421f6531SMatt Spinler * function will be called with the new PEL as the argument. 259421f6531SMatt Spinler * 260421f6531SMatt Spinler * The function must be of type void(const PEL&). 261421f6531SMatt Spinler * 262421f6531SMatt Spinler * @param[in] name - The subscription name 263421f6531SMatt Spinler * @param[in] func - The callback function 264421f6531SMatt Spinler */ 265421f6531SMatt Spinler void subscribeToAdds(const std::string& name, AddCallback func) 266421f6531SMatt Spinler { 267421f6531SMatt Spinler if (_addSubscriptions.find(name) == _addSubscriptions.end()) 268421f6531SMatt Spinler { 269421f6531SMatt Spinler _addSubscriptions.emplace(name, func); 270421f6531SMatt Spinler } 271421f6531SMatt Spinler } 272421f6531SMatt Spinler 273421f6531SMatt Spinler /** 274421f6531SMatt Spinler * @brief Unsubscribe from new PELs. 275421f6531SMatt Spinler * 276421f6531SMatt Spinler * @param[in] name - The subscription name 277421f6531SMatt Spinler */ 278421f6531SMatt Spinler void unsubscribeFromAdds(const std::string& name) 279421f6531SMatt Spinler { 280421f6531SMatt Spinler _addSubscriptions.erase(name); 281421f6531SMatt Spinler } 282421f6531SMatt Spinler 283421f6531SMatt Spinler using DeleteCallback = std::function<void(uint32_t)>; 284421f6531SMatt Spinler 285421f6531SMatt Spinler /** 286421f6531SMatt Spinler * @brief Subscribe to PELs being deleted from the repository. 287421f6531SMatt Spinler * 288421f6531SMatt Spinler * Every time a PEL is deleted from the repository, the provided 289421f6531SMatt Spinler * function will be called with the PEL ID as the argument. 290421f6531SMatt Spinler * 291421f6531SMatt Spinler * The function must be of type void(const uint32_t). 292421f6531SMatt Spinler * 293421f6531SMatt Spinler * @param[in] name - The subscription name 294421f6531SMatt Spinler * @param[in] func - The callback function 295421f6531SMatt Spinler */ 296421f6531SMatt Spinler void subscribeToDeletes(const std::string& name, DeleteCallback func) 297421f6531SMatt Spinler { 298421f6531SMatt Spinler if (_deleteSubscriptions.find(name) == _deleteSubscriptions.end()) 299421f6531SMatt Spinler { 300421f6531SMatt Spinler _deleteSubscriptions.emplace(name, func); 301421f6531SMatt Spinler } 302421f6531SMatt Spinler } 303421f6531SMatt Spinler 304421f6531SMatt Spinler /** 305421f6531SMatt Spinler * @brief Unsubscribe from deleted PELs. 306421f6531SMatt Spinler * 307421f6531SMatt Spinler * @param[in] name - The subscription name 308421f6531SMatt Spinler */ 309421f6531SMatt Spinler void unsubscribeFromDeletes(const std::string& name) 310421f6531SMatt Spinler { 311421f6531SMatt Spinler _deleteSubscriptions.erase(name); 312421f6531SMatt Spinler } 313421f6531SMatt Spinler 3140ff00485SMatt Spinler /** 3150ff00485SMatt Spinler * @brief Get the PEL attributes for a PEL 3160ff00485SMatt Spinler * 3170ff00485SMatt Spinler * @param[in] id - The ID to find the attributes for 3180ff00485SMatt Spinler * 3190ff00485SMatt Spinler * @return The attributes or an empty optional if not found 3200ff00485SMatt Spinler */ 3210ff00485SMatt Spinler std::optional<std::reference_wrapper<const PELAttributes>> 3220ff00485SMatt Spinler getPELAttributes(const LogID& id) const; 3230ff00485SMatt Spinler 32429d18c11SMatt Spinler /** 32529d18c11SMatt Spinler * @brief Sets the host transmission state on a PEL file 32629d18c11SMatt Spinler * 32729d18c11SMatt Spinler * Writes the host transmission state field in the User Header 32829d18c11SMatt Spinler * section in the PEL data specified by the ID. 32929d18c11SMatt Spinler * 33029d18c11SMatt Spinler * @param[in] pelID - The PEL ID 33129d18c11SMatt Spinler * @param[in] state - The state to write 33229d18c11SMatt Spinler */ 33329d18c11SMatt Spinler void setPELHostTransState(uint32_t pelID, TransmissionState state); 33429d18c11SMatt Spinler 33529d18c11SMatt Spinler /** 33629d18c11SMatt Spinler * @brief Sets the HMC transmission state on a PEL file 33729d18c11SMatt Spinler * 33829d18c11SMatt Spinler * Writes the HMC transmission state field in the User Header 33929d18c11SMatt Spinler * section in the PEL data specified by the ID. 34029d18c11SMatt Spinler * 34129d18c11SMatt Spinler * @param[in] pelID - The PEL ID 34229d18c11SMatt Spinler * @param[in] state - The state to write 34329d18c11SMatt Spinler */ 34429d18c11SMatt Spinler void setPELHMCTransState(uint32_t pelID, TransmissionState state); 34529d18c11SMatt Spinler 346b188f78aSMatt Spinler /** 347b188f78aSMatt Spinler * @brief Returns the size stats structure 348b188f78aSMatt Spinler * 349b188f78aSMatt Spinler * @return const SizeStats& - The stats structure 350b188f78aSMatt Spinler */ 351b188f78aSMatt Spinler const SizeStats& getSizeStats() const 352b188f78aSMatt Spinler { 353b188f78aSMatt Spinler return _sizes; 354b188f78aSMatt Spinler } 355b188f78aSMatt Spinler 356b188f78aSMatt Spinler /** 357b188f78aSMatt Spinler * @brief Says if the PEL is considered serviceable (not just 358b188f78aSMatt Spinler * informational) as determined by its severity. 359b188f78aSMatt Spinler * 360b188f78aSMatt Spinler * @param[in] pel - The PELAttributes entry for the PEL 361b188f78aSMatt Spinler * @return bool - If serviceable or not 362b188f78aSMatt Spinler */ 363b188f78aSMatt Spinler static bool isServiceableSev(const PELAttributes& pel); 364b188f78aSMatt Spinler 365b0a8df5bSMatt Spinler /** 3667e727a39SMatt Spinler * @brief Returns true if the total amount of disk space occupied 3677e727a39SMatt Spinler * by the PELs in the repo is over 95% of the maximum 3687e727a39SMatt Spinler * size, or if there are over the maximum number of 3697e727a39SMatt Spinler * PELs allowed. 3707e727a39SMatt Spinler * 3717e727a39SMatt Spinler * @return bool - true if repo is > 95% full or too many PELs 3727e727a39SMatt Spinler */ 3737e727a39SMatt Spinler bool sizeWarning() const; 3747e727a39SMatt Spinler 3757e727a39SMatt Spinler /** 376b0a8df5bSMatt Spinler * @brief Deletes PELs to bring the repository size down 377b0a8df5bSMatt Spinler * to at most 90% full by placing PELs into 4 different 378b0a8df5bSMatt Spinler * catogories and then removing PELs until those catogories 379b0a8df5bSMatt Spinler * only take up certain percentages of the allowed space. 380b0a8df5bSMatt Spinler * 381b0a8df5bSMatt Spinler * This does not delete the corresponding OpenBMC event logs, which 382b0a8df5bSMatt Spinler * is why those IDs are returned, so they can be deleted later. 383b0a8df5bSMatt Spinler * 384b0a8df5bSMatt Spinler * The categories and their rules are: 385b0a8df5bSMatt Spinler * 1) Informational BMC PELs cannot take up more than 15% of 386b0a8df5bSMatt Spinler * the allocated space. 387b0a8df5bSMatt Spinler * 2) Non-informational BMC PELs cannot take up more than 30% 388b0a8df5bSMatt Spinler * of the allocated space. 389b0a8df5bSMatt Spinler * 3) Informational non-BMC PELs cannot take up more than 15% of 390b0a8df5bSMatt Spinler * the allocated space. 391b0a8df5bSMatt Spinler * 4) Non-informational non-BMC PELs cannot take up more than 30% 392b0a8df5bSMatt Spinler * of the allocated space. 393b0a8df5bSMatt Spinler * 394b0a8df5bSMatt Spinler * While removing PELs in a category, 4 passes will be made, with 395b0a8df5bSMatt Spinler * PELs being removed oldest first during each pass. 396b0a8df5bSMatt Spinler * 397b0a8df5bSMatt Spinler * Pass 1: only delete HMC acked PELs 398b0a8df5bSMatt Spinler * Pass 2: only delete OS acked PELs 399b0a8df5bSMatt Spinler * Pass 3: only delete PHYP sent PELs 400b0a8df5bSMatt Spinler * Pass 4: delete all PELs 401b0a8df5bSMatt Spinler * 402b0a8df5bSMatt Spinler * @return std::vector<uint32_t> - The OpenBMC event log IDs of 403b0a8df5bSMatt Spinler * the PELs that were deleted. 404b0a8df5bSMatt Spinler */ 405b0a8df5bSMatt Spinler std::vector<uint32_t> prune(); 406b0a8df5bSMatt Spinler 407ff9cec25SMatt Spinler /** 408ff9cec25SMatt Spinler * @brief Returns the path to the directory where the PEL 409ff9cec25SMatt Spinler * files are stored. 410ff9cec25SMatt Spinler * 411ff9cec25SMatt Spinler * @return std::filesystem::path - The directory path 412ff9cec25SMatt Spinler */ 413ff9cec25SMatt Spinler const std::filesystem::path& repoPath() const 414ff9cec25SMatt Spinler { 415ff9cec25SMatt Spinler return _logPath; 416ff9cec25SMatt Spinler } 417ff9cec25SMatt Spinler 41844893cc9SMatt Spinler /** 41944893cc9SMatt Spinler * @brief Returns the ID of the most recently added PEL. 42044893cc9SMatt Spinler * 42144893cc9SMatt Spinler * @return uint32_t - The PEL ID 42244893cc9SMatt Spinler */ 42344893cc9SMatt Spinler uint32_t lastPelID() const 42444893cc9SMatt Spinler { 42544893cc9SMatt Spinler return _lastPelID; 42644893cc9SMatt Spinler } 42744893cc9SMatt Spinler 428*99f3717cSRamesh Iyyar /** 429*99f3717cSRamesh Iyyar * @brief Get the LogID based on the given ObmcLogId or PelId. 430*99f3717cSRamesh Iyyar * 431*99f3717cSRamesh Iyyar * @param[in] id - The ID to find the LogID. 432*99f3717cSRamesh Iyyar * 433*99f3717cSRamesh Iyyar * @return The LogID or an empty optional if not found. 434*99f3717cSRamesh Iyyar * 435*99f3717cSRamesh Iyyar * @note The returned LogID is the fully filled in LogID, i.e. 436*99f3717cSRamesh Iyyar * it has both the PEL and OpenBMC Log IDs, unlike the passed in LogID 437*99f3717cSRamesh Iyyar * which can just have one or the other. 438*99f3717cSRamesh Iyyar */ 439*99f3717cSRamesh Iyyar std::optional<LogID> getLogID(const LogID& id) const 440*99f3717cSRamesh Iyyar { 441*99f3717cSRamesh Iyyar if (auto logID = findPEL(id); logID != _pelAttributes.end()) 442*99f3717cSRamesh Iyyar { 443*99f3717cSRamesh Iyyar return logID->first; 444*99f3717cSRamesh Iyyar } 445*99f3717cSRamesh Iyyar return std::nullopt; 446*99f3717cSRamesh Iyyar } 447*99f3717cSRamesh Iyyar 44889fa082aSMatt Spinler private: 44929d18c11SMatt Spinler using PELUpdateFunc = std::function<void(PEL&)>; 45029d18c11SMatt Spinler 45129d18c11SMatt Spinler /** 45229d18c11SMatt Spinler * @brief Lets a function modify a PEL and saves the results 45329d18c11SMatt Spinler * 45429d18c11SMatt Spinler * Runs updateFunc (a void(PEL&) function) on the PEL data 45529d18c11SMatt Spinler * on the file specified, and writes the results back to the file. 45629d18c11SMatt Spinler * 45729d18c11SMatt Spinler * @param[in] path - The file path to use 45829d18c11SMatt Spinler * @param[in] updateFunc - The function to run to update the PEL. 45929d18c11SMatt Spinler */ 46029d18c11SMatt Spinler void updatePEL(const std::filesystem::path& path, PELUpdateFunc updateFunc); 46129d18c11SMatt Spinler 46289fa082aSMatt Spinler /** 4630ff00485SMatt Spinler * @brief Finds an entry in the _pelAttributes map. 464475e574dSMatt Spinler * 465475e574dSMatt Spinler * @param[in] id - the ID (either the pel ID, OBMC ID, or both) 466475e574dSMatt Spinler * 467475e574dSMatt Spinler * @return an iterator to the entry 468475e574dSMatt Spinler */ 4690ff00485SMatt Spinler std::map<LogID, PELAttributes>::const_iterator 4700ff00485SMatt Spinler findPEL(const LogID& id) const 471475e574dSMatt Spinler { 4720ff00485SMatt Spinler return std::find_if(_pelAttributes.begin(), _pelAttributes.end(), 4730ff00485SMatt Spinler [&id](const auto& a) { return a.first == id; }); 474475e574dSMatt Spinler } 475475e574dSMatt Spinler 476475e574dSMatt Spinler /** 477421f6531SMatt Spinler * @brief Call any subscribed functions for new PELs 478421f6531SMatt Spinler * 479421f6531SMatt Spinler * @param[in] pel - The new PEL 480421f6531SMatt Spinler */ 481421f6531SMatt Spinler void processAddCallbacks(const PEL& pel) const; 482421f6531SMatt Spinler 483421f6531SMatt Spinler /** 484421f6531SMatt Spinler * @brief Call any subscribed functions for deleted PELs 485421f6531SMatt Spinler * 486421f6531SMatt Spinler * @param[in] id - The ID of the deleted PEL 487421f6531SMatt Spinler */ 488421f6531SMatt Spinler void processDeleteCallbacks(uint32_t id) const; 489421f6531SMatt Spinler 490421f6531SMatt Spinler /** 4910ff00485SMatt Spinler * @brief Restores the _pelAttributes map on startup based on the existing 492475e574dSMatt Spinler * PEL data files. 493475e574dSMatt Spinler */ 494475e574dSMatt Spinler void restore(); 495475e574dSMatt Spinler 496475e574dSMatt Spinler /** 497ab1b97feSMatt Spinler * @brief Stores a PEL object in the filesystem. 498ab1b97feSMatt Spinler * 499ab1b97feSMatt Spinler * @param[in] pel - The PEL to write 500ab1b97feSMatt Spinler * @param[in] path - The file to write to 501ab1b97feSMatt Spinler * 502ab1b97feSMatt Spinler * Throws exceptions on failures. 503ab1b97feSMatt Spinler */ 504ab1b97feSMatt Spinler void write(const PEL& pel, const std::filesystem::path& path); 505ab1b97feSMatt Spinler 506ab1b97feSMatt Spinler /** 507b188f78aSMatt Spinler * @brief Updates the repository statistics after a PEL is 508b188f78aSMatt Spinler * added or removed. 509b188f78aSMatt Spinler * 510b188f78aSMatt Spinler * @param[in] pel - The PELAttributes entry for the PEL 511b188f78aSMatt Spinler * @param[in] pelAdded - true if the PEL was added, false if removed 512b188f78aSMatt Spinler */ 513b188f78aSMatt Spinler void updateRepoStats(const PELAttributes& pel, bool pelAdded); 514b188f78aSMatt Spinler 515b0a8df5bSMatt Spinler enum class SortOrder 516b0a8df5bSMatt Spinler { 517b0a8df5bSMatt Spinler ascending, 518b0a8df5bSMatt Spinler descending 519b0a8df5bSMatt Spinler }; 520b0a8df5bSMatt Spinler 521b0a8df5bSMatt Spinler /** 522b0a8df5bSMatt Spinler * @brief Returns a vector of all the _pelAttributes entries sorted 523b0a8df5bSMatt Spinler * as specified 524b0a8df5bSMatt Spinler * 525b0a8df5bSMatt Spinler * @param[in] order - If the PELs should be returned in ascending 526b0a8df5bSMatt Spinler * (oldest first) or descending order. 527b0a8df5bSMatt Spinler * 528b0a8df5bSMatt Spinler * @return std::vector<AttributesReference> - The sorted vector of 529b0a8df5bSMatt Spinler * references to the pair<LogID, PELAttributes> entries of 530b0a8df5bSMatt Spinler * _pelAttributes. 531b0a8df5bSMatt Spinler */ 532b0a8df5bSMatt Spinler std::vector<AttributesReference> getAllPELAttributes(SortOrder order) const; 533b0a8df5bSMatt Spinler 534b0a8df5bSMatt Spinler using IsOverLimitFunc = std::function<bool()>; 535b0a8df5bSMatt Spinler using IsPELTypeFunc = std::function<bool(const PELAttributes&)>; 536b0a8df5bSMatt Spinler 537b0a8df5bSMatt Spinler /** 538b0a8df5bSMatt Spinler * @brief Makes 4 passes on the PELs that meet the IsPELTypeFunc 539b0a8df5bSMatt Spinler * criteria removing PELs until IsOverLimitFunc returns false. 540b0a8df5bSMatt Spinler * 541b0a8df5bSMatt Spinler * Pass 1: only delete HMC acked PELs 542b0a8df5bSMatt Spinler * Pass 2: only delete Os acked PELs 543b0a8df5bSMatt Spinler * Pass 3: only delete PHYP sent PELs 544b0a8df5bSMatt Spinler * Pass 4: delete all PELs 545b0a8df5bSMatt Spinler * 546b0a8df5bSMatt Spinler * @param[in] isOverLimit - The bool(void) function that should 547b0a8df5bSMatt Spinler * return true if PELs still need to be 548b0a8df5bSMatt Spinler * removed. 549b0a8df5bSMatt Spinler * @param[in] isPELType - The bool(const PELAttributes&) function 550b0a8df5bSMatt Spinler * used to select the PELs to operate on. 551b0a8df5bSMatt Spinler * 552b0a8df5bSMatt Spinler * @param[out] removedBMCLogIDs - The OpenBMC event log IDs of the 553b0a8df5bSMatt Spinler * removed PELs. 554b0a8df5bSMatt Spinler */ 555b0a8df5bSMatt Spinler void removePELs(IsOverLimitFunc& isOverLimit, IsPELTypeFunc& isPELType, 556b0a8df5bSMatt Spinler std::vector<uint32_t>& removedBMCLogIDs); 557b188f78aSMatt Spinler /** 55889fa082aSMatt Spinler * @brief The filesystem path to the PEL logs. 55989fa082aSMatt Spinler */ 56089fa082aSMatt Spinler const std::filesystem::path _logPath; 561475e574dSMatt Spinler 562475e574dSMatt Spinler /** 5630ff00485SMatt Spinler * @brief A map of the PEL/OBMC IDs to PEL attributes. 564475e574dSMatt Spinler */ 5650ff00485SMatt Spinler std::map<LogID, PELAttributes> _pelAttributes; 566421f6531SMatt Spinler 567421f6531SMatt Spinler /** 568421f6531SMatt Spinler * @brief Subcriptions for new PELs. 569421f6531SMatt Spinler */ 570421f6531SMatt Spinler std::map<std::string, AddCallback> _addSubscriptions; 571421f6531SMatt Spinler 572421f6531SMatt Spinler /** 573421f6531SMatt Spinler * @brief Subscriptions for deleted PELs. 574421f6531SMatt Spinler */ 575421f6531SMatt Spinler std::map<std::string, DeleteCallback> _deleteSubscriptions; 5768d5f3a2bSMatt Spinler 5778d5f3a2bSMatt Spinler /** 5788d5f3a2bSMatt Spinler * @brief The maximum amount of space that the PELs in the 5798d5f3a2bSMatt Spinler * repository can occupy. 5808d5f3a2bSMatt Spinler */ 5818d5f3a2bSMatt Spinler const uint64_t _maxRepoSize; 5828d5f3a2bSMatt Spinler 5838d5f3a2bSMatt Spinler /** 5848d5f3a2bSMatt Spinler * @brief The maximum number of PELs to allow in the repo 5858d5f3a2bSMatt Spinler * before pruning. 5868d5f3a2bSMatt Spinler */ 5878d5f3a2bSMatt Spinler const size_t _maxNumPELs; 588b188f78aSMatt Spinler 589b188f78aSMatt Spinler /** 590b188f78aSMatt Spinler * @brief Statistics on the sizes of the stored PELs. 591b188f78aSMatt Spinler */ 592b188f78aSMatt Spinler SizeStats _sizes; 59344893cc9SMatt Spinler 59444893cc9SMatt Spinler /** 59544893cc9SMatt Spinler * @brief The ID of the most recently added PEL. 59644893cc9SMatt Spinler */ 59744893cc9SMatt Spinler uint32_t _lastPelID = 0; 5981d8835bbSSumit Kumar 5991d8835bbSSumit Kumar /** 6001d8835bbSSumit Kumar * @brief The filesystem path to the archive PEL logs. 6011d8835bbSSumit Kumar */ 6021d8835bbSSumit Kumar const std::filesystem::path _archivePath; 6031d8835bbSSumit Kumar 6041d8835bbSSumit Kumar /** 6051d8835bbSSumit Kumar * @brief The size of archive folder. 6061d8835bbSSumit Kumar */ 6071d8835bbSSumit Kumar uint64_t _archiveSize = 0; 60889fa082aSMatt Spinler }; 60989fa082aSMatt Spinler 61089fa082aSMatt Spinler } // namespace pels 61189fa082aSMatt Spinler } // namespace openpower 612