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;
32afb1b46fSVijay Lobo         uint8_t subsystem;
33dd325c32SMatt Spinler         uint8_t severity;
340ff00485SMatt Spinler         std::bitset<16> actionFlags;
35346f99a1SMatt Spinler         TransmissionState hostState;
36346f99a1SMatt Spinler         TransmissionState hmcState;
378e65f4eaSMatt Spinler         uint32_t plid;
388e65f4eaSMatt Spinler         bool deconfig;
398e65f4eaSMatt Spinler         bool guard;
408e65f4eaSMatt Spinler         uint64_t creationTime;
410ff00485SMatt Spinler 
420ff00485SMatt Spinler         PELAttributes() = delete;
430ff00485SMatt Spinler 
PELAttributesopenpower::pels::Repository::PELAttributes44dd325c32SMatt Spinler         PELAttributes(const std::filesystem::path& p, size_t size,
45afb1b46fSVijay Lobo                       uint8_t creator, uint8_t subsystem, uint8_t sev,
46afb1b46fSVijay Lobo                       uint16_t flags, TransmissionState hostState,
478e65f4eaSMatt Spinler                       TransmissionState hmcState, uint32_t plid, bool deconfig,
488e65f4eaSMatt Spinler                       bool guard, uint64_t creationTime) :
49346f99a1SMatt Spinler             path(p),
50afb1b46fSVijay Lobo             sizeOnDisk(size), creator(creator), subsystem(subsystem),
51afb1b46fSVijay Lobo             severity(sev), actionFlags(flags), hostState(hostState),
528e65f4eaSMatt Spinler             hmcState(hmcState), plid(plid), deconfig(deconfig), guard(guard),
538e65f4eaSMatt Spinler             creationTime(creationTime)
542544b419SPatrick Williams         {}
550ff00485SMatt Spinler     };
560ff00485SMatt Spinler 
570ff00485SMatt Spinler     /**
58475e574dSMatt Spinler      * @brief A structure that holds both the PEL and corresponding
59475e574dSMatt Spinler      *        OpenBMC IDs.
60475e574dSMatt Spinler      * Used for correlating the IDs with their data files for quick
61475e574dSMatt Spinler      * lookup.  To find a PEL based on just one of the IDs, just use
62475e574dSMatt Spinler      * the constructor that takes that ID.
63475e574dSMatt Spinler      */
64475e574dSMatt Spinler     struct LogID
65475e574dSMatt Spinler     {
66475e574dSMatt Spinler         struct Pel
67475e574dSMatt Spinler         {
68475e574dSMatt Spinler             uint32_t id;
Pelopenpower::pels::Repository::LogID::Pel692544b419SPatrick Williams             explicit Pel(uint32_t i) : id(i) {}
70475e574dSMatt Spinler         };
71475e574dSMatt Spinler         struct Obmc
72475e574dSMatt Spinler         {
73475e574dSMatt Spinler             uint32_t id;
Obmcopenpower::pels::Repository::LogID::Obmc742544b419SPatrick Williams             explicit Obmc(uint32_t i) : id(i) {}
75475e574dSMatt Spinler         };
76475e574dSMatt Spinler 
77475e574dSMatt Spinler         Pel pelID;
78475e574dSMatt Spinler 
79475e574dSMatt Spinler         Obmc obmcID;
80475e574dSMatt Spinler 
LogIDopenpower::pels::Repository::LogID812544b419SPatrick Williams         LogID(Pel pel, Obmc obmc) : pelID(pel), obmcID(obmc) {}
82475e574dSMatt Spinler 
LogIDopenpower::pels::Repository::LogID832544b419SPatrick Williams         explicit LogID(Pel id) : pelID(id), obmcID(0) {}
84475e574dSMatt Spinler 
LogIDopenpower::pels::Repository::LogID852544b419SPatrick Williams         explicit LogID(Obmc id) : pelID(0), obmcID(id) {}
86475e574dSMatt Spinler 
87475e574dSMatt Spinler         LogID() = delete;
88475e574dSMatt Spinler 
89475e574dSMatt Spinler         /**
90475e574dSMatt Spinler          * @brief A == operator that will match on either ID
91475e574dSMatt Spinler          *        being equal if the other is zero, so that
92475e574dSMatt Spinler          *        one can look up a PEL with just one of the IDs.
93475e574dSMatt Spinler          */
operator ==openpower::pels::Repository::LogID94475e574dSMatt Spinler         bool operator==(const LogID& id) const
95475e574dSMatt Spinler         {
96475e574dSMatt Spinler             if (id.pelID.id != 0)
97475e574dSMatt Spinler             {
98475e574dSMatt Spinler                 return id.pelID.id == pelID.id;
99475e574dSMatt Spinler             }
100475e574dSMatt Spinler             if (id.obmcID.id != 0)
101475e574dSMatt Spinler             {
102475e574dSMatt Spinler                 return id.obmcID.id == obmcID.id;
103475e574dSMatt Spinler             }
104475e574dSMatt Spinler             return false;
105475e574dSMatt Spinler         }
106475e574dSMatt Spinler 
operator <openpower::pels::Repository::LogID107475e574dSMatt Spinler         bool operator<(const LogID& id) const
108475e574dSMatt Spinler         {
109475e574dSMatt Spinler             return pelID.id < id.pelID.id;
110475e574dSMatt Spinler         }
111475e574dSMatt Spinler     };
112475e574dSMatt Spinler 
113b0a8df5bSMatt Spinler     using AttributesReference =
114b0a8df5bSMatt Spinler         std::reference_wrapper<const std::pair<const LogID, PELAttributes>>;
115b0a8df5bSMatt Spinler 
116b188f78aSMatt Spinler     /**
117b188f78aSMatt Spinler      * @brief A structure for keeping a breakdown of the sizes of PELs
118b188f78aSMatt Spinler      *        of different types in the repository.
119b188f78aSMatt Spinler      */
120b188f78aSMatt Spinler     struct SizeStats
121b188f78aSMatt Spinler     {
122b188f78aSMatt Spinler         uint64_t total;
123b188f78aSMatt Spinler         uint64_t bmc;
124b188f78aSMatt Spinler         uint64_t nonBMC;
125b188f78aSMatt Spinler         uint64_t bmcServiceable;
126b188f78aSMatt Spinler         uint64_t bmcInfo;
127b188f78aSMatt Spinler         uint64_t nonBMCServiceable;
128b188f78aSMatt Spinler         uint64_t nonBMCInfo;
129b188f78aSMatt Spinler 
SizeStatsopenpower::pels::Repository::SizeStats130b188f78aSMatt Spinler         SizeStats() :
131b188f78aSMatt Spinler             total(0), bmc(0), nonBMC(0), bmcServiceable(0), bmcInfo(0),
132b188f78aSMatt Spinler             nonBMCServiceable(0), nonBMCInfo(0)
1332544b419SPatrick Williams         {}
134b188f78aSMatt Spinler     };
135b188f78aSMatt Spinler 
13689fa082aSMatt Spinler     Repository() = delete;
13789fa082aSMatt Spinler     ~Repository() = default;
13889fa082aSMatt Spinler     Repository(const Repository&) = default;
13989fa082aSMatt Spinler     Repository& operator=(const Repository&) = default;
14089fa082aSMatt Spinler     Repository(Repository&&) = default;
14189fa082aSMatt Spinler     Repository& operator=(Repository&&) = default;
14289fa082aSMatt Spinler 
14389fa082aSMatt Spinler     /**
14489fa082aSMatt Spinler      * @brief Constructor
14589fa082aSMatt Spinler      *
14689fa082aSMatt Spinler      * @param[in] basePath - the base filesystem path for the repository
14789fa082aSMatt Spinler      */
Repository(const std::filesystem::path & basePath)14845796e82SMatt Spinler     explicit Repository(const std::filesystem::path& basePath) :
1498d5f3a2bSMatt Spinler         Repository(basePath, getPELRepoSize(), getMaxNumPELs())
1502544b419SPatrick Williams     {}
1518d5f3a2bSMatt Spinler 
1528d5f3a2bSMatt Spinler     /**
1538d5f3a2bSMatt Spinler      * @brief Constructor that takes the repository size
1548d5f3a2bSMatt Spinler      *
1558d5f3a2bSMatt Spinler      * @param[in] basePath - the base filesystem path for the repository
1568d5f3a2bSMatt Spinler      * @param[in] repoSize - The maximum amount of space to use for PELs,
1578d5f3a2bSMatt Spinler      *                       in bytes
1588d5f3a2bSMatt Spinler      * @param[in] maxNumPELs - The maximum number of PELs to allow
1598d5f3a2bSMatt Spinler      */
1608d5f3a2bSMatt Spinler     Repository(const std::filesystem::path& basePath, size_t repoSize,
1618d5f3a2bSMatt Spinler                size_t maxNumPELs);
16289fa082aSMatt Spinler 
16389fa082aSMatt Spinler     /**
16489fa082aSMatt Spinler      * @brief Adds a PEL to the repository
16589fa082aSMatt Spinler      *
16689fa082aSMatt Spinler      * Throws File.Error.Open or File.Error.Write exceptions on failure
16789fa082aSMatt Spinler      *
16889fa082aSMatt Spinler      * @param[in] pel - the PEL to add
16989fa082aSMatt Spinler      */
17089fa082aSMatt Spinler     void add(std::unique_ptr<PEL>& pel);
17189fa082aSMatt Spinler 
17289fa082aSMatt Spinler     /**
173475e574dSMatt Spinler      * @brief Removes a PEL from the repository
174475e574dSMatt Spinler      *
17552602e35SMatt Spinler      * Note that the returned LogID is the fully filled in LogID, i.e.
17652602e35SMatt Spinler      * it has both the PEL and OpenBMC IDs, unlike the passed in LogID
17752602e35SMatt Spinler      * which can just have one or the other.
17852602e35SMatt Spinler      *
179475e574dSMatt Spinler      * @param[in] id - the ID (either the pel ID, OBMC ID, or both) to remove
18052602e35SMatt Spinler      *
18152602e35SMatt Spinler      * @return std::optional<LogID> - The LogID of the removed PEL
182475e574dSMatt Spinler      */
18352602e35SMatt Spinler     std::optional<LogID> remove(const LogID& id);
184475e574dSMatt Spinler 
185475e574dSMatt Spinler     /**
18689fa082aSMatt Spinler      * @brief Generates the filename to use for the PEL ID and BCDTime.
18789fa082aSMatt Spinler      *
18889fa082aSMatt Spinler      * @param[in] pelID - the PEL ID
18989fa082aSMatt Spinler      * @param[in] time - the BCD time
19089fa082aSMatt Spinler      *
19189fa082aSMatt Spinler      * @return string - A filename string of <BCD_time>_<pelID>
19289fa082aSMatt Spinler      */
19389fa082aSMatt Spinler     static std::string getPELFilename(uint32_t pelID, const BCDTime& time);
19489fa082aSMatt Spinler 
195475e574dSMatt Spinler     /**
196475e574dSMatt Spinler      * @brief Returns true if the PEL with the specified ID is in the repo.
197475e574dSMatt Spinler      *
198475e574dSMatt Spinler      * @param[in] id - the ID (either the pel ID, OBMC ID, or both)
199475e574dSMatt Spinler      * @return bool - true if that PEL is present
200475e574dSMatt Spinler      */
hasPEL(const LogID & id)201475e574dSMatt Spinler     inline bool hasPEL(const LogID& id)
202475e574dSMatt Spinler     {
2030ff00485SMatt Spinler         return findPEL(id) != _pelAttributes.end();
204475e574dSMatt Spinler     }
205475e574dSMatt Spinler 
2062813f36dSMatt Spinler     /**
2072813f36dSMatt Spinler      * @brief Returns the PEL data based on its ID.
2082813f36dSMatt Spinler      *
2092813f36dSMatt Spinler      * If the data can't be found for that ID, then the optional object
2102813f36dSMatt Spinler      * will be empty.
2112813f36dSMatt Spinler      *
2122813f36dSMatt Spinler      * @param[in] id - the LogID to get the PEL for, which can be either a
2132813f36dSMatt Spinler      *                 PEL ID or OpenBMC log ID.
2142813f36dSMatt Spinler      * @return std::optional<std::vector<uint8_t>> - the PEL data
2152813f36dSMatt Spinler      */
2162813f36dSMatt Spinler     std::optional<std::vector<uint8_t>> getPELData(const LogID& id);
2172813f36dSMatt Spinler 
2186d51224bSMatt Spinler     /**
2196d51224bSMatt Spinler      * @brief Get a file descriptor to the PEL data
2206d51224bSMatt Spinler      *
2216d51224bSMatt Spinler      * @param[in] id - The ID to get the FD for
2226d51224bSMatt Spinler      *
2236d51224bSMatt Spinler      * @return std::optional<sdbusplus::message::unix_fd> -
2246d51224bSMatt Spinler      *         The FD, or an empty optional object.
2256d51224bSMatt Spinler      */
2266d51224bSMatt Spinler     std::optional<sdbusplus::message::unix_fd> getPELFD(const LogID& id);
2276d51224bSMatt Spinler 
2281ea78801SMatt Spinler     using ForEachFunc = std::function<bool(const PEL&)>;
2291ea78801SMatt Spinler 
2301ea78801SMatt Spinler     /**
2311ea78801SMatt Spinler      * @brief Run a user defined function on every PEL in the repository.
2321ea78801SMatt Spinler      *
2331ea78801SMatt Spinler      * ForEachFunc takes a const PEL reference, and should return
2341ea78801SMatt Spinler      * true to stop iterating and return out of for_each.
2351ea78801SMatt Spinler      *
2361ea78801SMatt Spinler      * For example, to save up to 100 IDs in the repo into a vector:
2371ea78801SMatt Spinler      *
2381ea78801SMatt Spinler      *     std::vector<uint32_t> ids;
2391ea78801SMatt Spinler      *     ForEachFunc f = [&ids](const PEL& pel) {
2401ea78801SMatt Spinler      *         ids.push_back(pel.id());
2411ea78801SMatt Spinler      *         return ids.size() == 100 ? true : false;
2421ea78801SMatt Spinler      *     };
2431ea78801SMatt Spinler      *
2441ea78801SMatt Spinler      * @param[in] func - The function to run.
2451ea78801SMatt Spinler      */
2461ea78801SMatt Spinler     void for_each(ForEachFunc func) const;
2471ea78801SMatt Spinler 
248421f6531SMatt Spinler     using AddCallback = std::function<void(const PEL&)>;
249421f6531SMatt Spinler 
250421f6531SMatt Spinler     /**
251421f6531SMatt Spinler      * @brief Subscribe to PELs being added to the repository.
252421f6531SMatt Spinler      *
253421f6531SMatt Spinler      * Every time a PEL is added to the repository, the provided
254421f6531SMatt Spinler      * function will be called with the new PEL as the argument.
255421f6531SMatt Spinler      *
256421f6531SMatt Spinler      * The function must be of type void(const PEL&).
257421f6531SMatt Spinler      *
258421f6531SMatt Spinler      * @param[in] name - The subscription name
259421f6531SMatt Spinler      * @param[in] func - The callback function
260421f6531SMatt Spinler      */
subscribeToAdds(const std::string & name,AddCallback func)261421f6531SMatt Spinler     void subscribeToAdds(const std::string& name, AddCallback func)
262421f6531SMatt Spinler     {
263421f6531SMatt Spinler         _addSubscriptions.emplace(name, func);
264421f6531SMatt Spinler     }
265421f6531SMatt Spinler 
266421f6531SMatt Spinler     /**
267421f6531SMatt Spinler      * @brief Unsubscribe from new PELs.
268421f6531SMatt Spinler      *
269421f6531SMatt Spinler      * @param[in] name - The subscription name
270421f6531SMatt Spinler      */
unsubscribeFromAdds(const std::string & name)271421f6531SMatt Spinler     void unsubscribeFromAdds(const std::string& name)
272421f6531SMatt Spinler     {
273421f6531SMatt Spinler         _addSubscriptions.erase(name);
274421f6531SMatt Spinler     }
275421f6531SMatt Spinler 
276421f6531SMatt Spinler     using DeleteCallback = std::function<void(uint32_t)>;
277421f6531SMatt Spinler 
278421f6531SMatt Spinler     /**
279421f6531SMatt Spinler      * @brief Subscribe to PELs being deleted from the repository.
280421f6531SMatt Spinler      *
281421f6531SMatt Spinler      * Every time a PEL is deleted from the repository, the provided
282421f6531SMatt Spinler      * function will be called with the PEL ID as the argument.
283421f6531SMatt Spinler      *
284421f6531SMatt Spinler      * The function must be of type void(const uint32_t).
285421f6531SMatt Spinler      *
286421f6531SMatt Spinler      * @param[in] name - The subscription name
287421f6531SMatt Spinler      * @param[in] func - The callback function
288421f6531SMatt Spinler      */
subscribeToDeletes(const std::string & name,DeleteCallback func)289421f6531SMatt Spinler     void subscribeToDeletes(const std::string& name, DeleteCallback func)
290421f6531SMatt Spinler     {
291421f6531SMatt Spinler         _deleteSubscriptions.emplace(name, func);
292421f6531SMatt Spinler     }
293421f6531SMatt Spinler 
294421f6531SMatt Spinler     /**
295421f6531SMatt Spinler      * @brief Unsubscribe from deleted PELs.
296421f6531SMatt Spinler      *
297421f6531SMatt Spinler      * @param[in] name - The subscription name
298421f6531SMatt Spinler      */
unsubscribeFromDeletes(const std::string & name)299421f6531SMatt Spinler     void unsubscribeFromDeletes(const std::string& name)
300421f6531SMatt Spinler     {
301421f6531SMatt Spinler         _deleteSubscriptions.erase(name);
302421f6531SMatt Spinler     }
303421f6531SMatt Spinler 
3040ff00485SMatt Spinler     /**
3050ff00485SMatt Spinler      * @brief Get the PEL attributes for a PEL
3060ff00485SMatt Spinler      *
3070ff00485SMatt Spinler      * @param[in] id - The ID to find the attributes for
3080ff00485SMatt Spinler      *
3090ff00485SMatt Spinler      * @return The attributes or an empty optional if not found
3100ff00485SMatt Spinler      */
3110ff00485SMatt Spinler     std::optional<std::reference_wrapper<const PELAttributes>>
3120ff00485SMatt Spinler         getPELAttributes(const LogID& id) const;
3130ff00485SMatt Spinler 
31429d18c11SMatt Spinler     /**
3150dd22c83SMatt Spinler      * @brief Returns the attributes map so that others can traverse PELs.
3160dd22c83SMatt Spinler      *
3170dd22c83SMatt Spinler      * @return - A const reference to the attributes map.
3180dd22c83SMatt Spinler      */
getAttributesMap() const3190dd22c83SMatt Spinler     const std::map<LogID, PELAttributes>& getAttributesMap() const
3200dd22c83SMatt Spinler     {
3210dd22c83SMatt Spinler         return _pelAttributes;
3220dd22c83SMatt Spinler     }
3230dd22c83SMatt Spinler 
3240dd22c83SMatt 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      */
getSizeStats() const351b188f78aSMatt 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      */
373c296692bSSumit Kumar     bool sizeWarning();
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      *
402027bf285SSumit Kumar      * @param[in] ids - The OpenBMC event log Ids with hardware isolation entry.
403027bf285SSumit Kumar      *
404b0a8df5bSMatt Spinler      * @return std::vector<uint32_t> - The OpenBMC event log IDs of
405b0a8df5bSMatt Spinler      *                                 the PELs that were deleted.
406b0a8df5bSMatt Spinler      */
407027bf285SSumit Kumar     std::vector<uint32_t> prune(const std::vector<uint32_t>& idsWithHwIsoEntry);
408b0a8df5bSMatt Spinler 
409ff9cec25SMatt Spinler     /**
410ff9cec25SMatt Spinler      * @brief Returns the path to the directory where the PEL
411ff9cec25SMatt Spinler      *        files are stored.
412ff9cec25SMatt Spinler      *
413ff9cec25SMatt Spinler      * @return std::filesystem::path - The directory path
414ff9cec25SMatt Spinler      */
repoPath() const415ff9cec25SMatt Spinler     const std::filesystem::path& repoPath() const
416ff9cec25SMatt Spinler     {
417ff9cec25SMatt Spinler         return _logPath;
418ff9cec25SMatt Spinler     }
419ff9cec25SMatt Spinler 
42044893cc9SMatt Spinler     /**
42144893cc9SMatt Spinler      * @brief Returns the ID of the most recently added PEL.
42244893cc9SMatt Spinler      *
42344893cc9SMatt Spinler      * @return uint32_t - The PEL ID
42444893cc9SMatt Spinler      */
lastPelID() const42544893cc9SMatt Spinler     uint32_t lastPelID() const
42644893cc9SMatt Spinler     {
42744893cc9SMatt Spinler         return _lastPelID;
42844893cc9SMatt Spinler     }
42944893cc9SMatt Spinler 
43099f3717cSRamesh Iyyar     /**
43199f3717cSRamesh Iyyar      * @brief Get the LogID based on the given ObmcLogId or PelId.
43299f3717cSRamesh Iyyar      *
43399f3717cSRamesh Iyyar      * @param[in] id - The ID to find the LogID.
43499f3717cSRamesh Iyyar      *
43599f3717cSRamesh Iyyar      * @return The LogID or an empty optional if not found.
43699f3717cSRamesh Iyyar      *
43799f3717cSRamesh Iyyar      * @note The returned LogID is the fully filled in LogID, i.e.
43899f3717cSRamesh Iyyar      * it has both the PEL and OpenBMC Log IDs, unlike the passed in LogID
43999f3717cSRamesh Iyyar      * which can just have one or the other.
44099f3717cSRamesh Iyyar      */
getLogID(const LogID & id) const44199f3717cSRamesh Iyyar     std::optional<LogID> getLogID(const LogID& id) const
44299f3717cSRamesh Iyyar     {
44399f3717cSRamesh Iyyar         if (auto logID = findPEL(id); logID != _pelAttributes.end())
44499f3717cSRamesh Iyyar         {
44599f3717cSRamesh Iyyar             return logID->first;
44699f3717cSRamesh Iyyar         }
44799f3717cSRamesh Iyyar         return std::nullopt;
44899f3717cSRamesh Iyyar     }
44999f3717cSRamesh Iyyar 
4502ccdcef9SSumit Kumar     /**
4512ccdcef9SSumit Kumar      * @brief Save the PEL to archive folder
4522ccdcef9SSumit Kumar      *
4532ccdcef9SSumit Kumar      * @param[in] pel - The PEL data
4542ccdcef9SSumit Kumar      */
4552ccdcef9SSumit Kumar     void archivePEL(const PEL& pel);
4562ccdcef9SSumit Kumar 
457d0ccda3cSMatt Spinler     using PELUpdateFunc = std::function<bool(PEL&)>;
45829d18c11SMatt Spinler 
45929d18c11SMatt Spinler     /**
46029d18c11SMatt Spinler      * @brief Lets a function modify a PEL and saves the results
46129d18c11SMatt Spinler      *
462d0ccda3cSMatt Spinler      * Runs updateFunc (a bool(PEL&) function) on the PEL data
463d0ccda3cSMatt Spinler      * on the file specified, and writes the results back to the file
464d0ccda3cSMatt Spinler      * if the function returned true.
46529d18c11SMatt Spinler      *
46629d18c11SMatt Spinler      * @param[in] path - The file path to use
46729d18c11SMatt Spinler      * @param[in] updateFunc - The function to run to update the PEL.
468*1cb59f70SMatt Spinler      *
469*1cb59f70SMatt Spinler      * @return bool - If the PEL was updated or not.
47029d18c11SMatt Spinler      */
471*1cb59f70SMatt Spinler     bool updatePEL(const std::filesystem::path& path, PELUpdateFunc updateFunc);
47229d18c11SMatt Spinler 
4730dd22c83SMatt Spinler   private:
47489fa082aSMatt Spinler     /**
4750ff00485SMatt Spinler      * @brief Finds an entry in the _pelAttributes map.
476475e574dSMatt Spinler      *
477475e574dSMatt Spinler      * @param[in] id - the ID (either the pel ID, OBMC ID, or both)
478475e574dSMatt Spinler      *
479475e574dSMatt Spinler      * @return an iterator to the entry
480475e574dSMatt Spinler      */
4810ff00485SMatt Spinler     std::map<LogID, PELAttributes>::const_iterator
findPEL(const LogID & id) const4820ff00485SMatt Spinler         findPEL(const LogID& id) const
483475e574dSMatt Spinler     {
4840ff00485SMatt Spinler         return std::find_if(_pelAttributes.begin(), _pelAttributes.end(),
4850ff00485SMatt Spinler                             [&id](const auto& a) { return a.first == id; });
486475e574dSMatt Spinler     }
487475e574dSMatt Spinler 
488475e574dSMatt Spinler     /**
489421f6531SMatt Spinler      * @brief Call any subscribed functions for new PELs
490421f6531SMatt Spinler      *
491421f6531SMatt Spinler      * @param[in] pel - The new PEL
492421f6531SMatt Spinler      */
493421f6531SMatt Spinler     void processAddCallbacks(const PEL& pel) const;
494421f6531SMatt Spinler 
495421f6531SMatt Spinler     /**
496421f6531SMatt Spinler      * @brief Call any subscribed functions for deleted PELs
497421f6531SMatt Spinler      *
498421f6531SMatt Spinler      * @param[in] id - The ID of the deleted PEL
499421f6531SMatt Spinler      */
500421f6531SMatt Spinler     void processDeleteCallbacks(uint32_t id) const;
501421f6531SMatt Spinler 
502421f6531SMatt Spinler     /**
5030ff00485SMatt Spinler      * @brief Restores the _pelAttributes map on startup based on the existing
504475e574dSMatt Spinler      *        PEL data files.
505475e574dSMatt Spinler      */
506475e574dSMatt Spinler     void restore();
507475e574dSMatt Spinler 
508475e574dSMatt Spinler     /**
509ab1b97feSMatt Spinler      * @brief Stores a PEL object in the filesystem.
510ab1b97feSMatt Spinler      *
511ab1b97feSMatt Spinler      * @param[in] pel - The PEL to write
512ab1b97feSMatt Spinler      * @param[in] path - The file to write to
513ab1b97feSMatt Spinler      *
514ab1b97feSMatt Spinler      * Throws exceptions on failures.
515ab1b97feSMatt Spinler      */
516ab1b97feSMatt Spinler     void write(const PEL& pel, const std::filesystem::path& path);
517ab1b97feSMatt Spinler 
518ab1b97feSMatt Spinler     /**
519b188f78aSMatt Spinler      * @brief Updates the repository statistics after a PEL is
520b188f78aSMatt Spinler      *        added or removed.
521b188f78aSMatt Spinler      *
522b188f78aSMatt Spinler      * @param[in] pel - The PELAttributes entry for the PEL
523b188f78aSMatt Spinler      * @param[in] pelAdded - true if the PEL was added, false if removed
524b188f78aSMatt Spinler      */
525b188f78aSMatt Spinler     void updateRepoStats(const PELAttributes& pel, bool pelAdded);
526b188f78aSMatt Spinler 
527b0a8df5bSMatt Spinler     enum class SortOrder
528b0a8df5bSMatt Spinler     {
529b0a8df5bSMatt Spinler         ascending,
530b0a8df5bSMatt Spinler         descending
531b0a8df5bSMatt Spinler     };
532b0a8df5bSMatt Spinler 
533b0a8df5bSMatt Spinler     /**
534b0a8df5bSMatt Spinler      * @brief Returns a vector of all the _pelAttributes entries sorted
535b0a8df5bSMatt Spinler      *        as specified
536b0a8df5bSMatt Spinler      *
537b0a8df5bSMatt Spinler      * @param[in] order - If the PELs should be returned in ascending
538b0a8df5bSMatt Spinler      *                    (oldest first) or descending order.
539b0a8df5bSMatt Spinler      *
540b0a8df5bSMatt Spinler      * @return std::vector<AttributesReference> - The sorted vector of
541b0a8df5bSMatt Spinler      *         references to the pair<LogID, PELAttributes> entries of
542b0a8df5bSMatt Spinler      *         _pelAttributes.
543b0a8df5bSMatt Spinler      */
544b0a8df5bSMatt Spinler     std::vector<AttributesReference> getAllPELAttributes(SortOrder order) const;
545b0a8df5bSMatt Spinler 
546b0a8df5bSMatt Spinler     using IsOverLimitFunc = std::function<bool()>;
547b0a8df5bSMatt Spinler     using IsPELTypeFunc = std::function<bool(const PELAttributes&)>;
548b0a8df5bSMatt Spinler 
549b0a8df5bSMatt Spinler     /**
550b0a8df5bSMatt Spinler      * @brief Makes 4 passes on the PELs that meet the IsPELTypeFunc
551b0a8df5bSMatt Spinler      *        criteria removing PELs until IsOverLimitFunc returns false.
552b0a8df5bSMatt Spinler      *
553b0a8df5bSMatt Spinler      *   Pass 1: only delete HMC acked PELs
554b0a8df5bSMatt Spinler      *   Pass 2: only delete Os acked PELs
555b0a8df5bSMatt Spinler      *   Pass 3: only delete PHYP sent PELs
556b0a8df5bSMatt Spinler      *   Pass 4: delete all PELs
557b0a8df5bSMatt Spinler      *
558b0a8df5bSMatt Spinler      * @param[in] isOverLimit - The bool(void) function that should
559b0a8df5bSMatt Spinler      *                          return true if PELs still need to be
560b0a8df5bSMatt Spinler      *                           removed.
561b0a8df5bSMatt Spinler      * @param[in] isPELType - The bool(const PELAttributes&) function
562b0a8df5bSMatt Spinler      *                         used to select the PELs to operate on.
563027bf285SSumit Kumar      * @param[in] ids - The OpenBMC event log Ids with hardware isolation
564027bf285SSumit Kumar      *                   entry.
565b0a8df5bSMatt Spinler      *
566b0a8df5bSMatt Spinler      * @param[out] removedBMCLogIDs - The OpenBMC event log IDs of the
567b0a8df5bSMatt Spinler      *                                removed PELs.
568b0a8df5bSMatt Spinler      */
56945796e82SMatt Spinler     void removePELs(const IsOverLimitFunc& isOverLimit,
57045796e82SMatt Spinler                     const IsPELTypeFunc& isPELType,
571027bf285SSumit Kumar                     const std::vector<uint32_t>& idsWithHwIsoEntry,
572b0a8df5bSMatt Spinler                     std::vector<uint32_t>& removedBMCLogIDs);
573b188f78aSMatt Spinler     /**
57489fa082aSMatt Spinler      * @brief The filesystem path to the PEL logs.
57589fa082aSMatt Spinler      */
57689fa082aSMatt Spinler     const std::filesystem::path _logPath;
577475e574dSMatt Spinler 
578475e574dSMatt Spinler     /**
5790ff00485SMatt Spinler      * @brief A map of the PEL/OBMC IDs to PEL attributes.
580475e574dSMatt Spinler      */
5810ff00485SMatt Spinler     std::map<LogID, PELAttributes> _pelAttributes;
582421f6531SMatt Spinler 
583421f6531SMatt Spinler     /**
584421f6531SMatt Spinler      * @brief Subcriptions for new PELs.
585421f6531SMatt Spinler      */
586421f6531SMatt Spinler     std::map<std::string, AddCallback> _addSubscriptions;
587421f6531SMatt Spinler 
588421f6531SMatt Spinler     /**
589421f6531SMatt Spinler      * @brief Subscriptions for deleted PELs.
590421f6531SMatt Spinler      */
591421f6531SMatt Spinler     std::map<std::string, DeleteCallback> _deleteSubscriptions;
5928d5f3a2bSMatt Spinler 
5938d5f3a2bSMatt Spinler     /**
5948d5f3a2bSMatt Spinler      * @brief The maximum amount of space that the PELs in the
5958d5f3a2bSMatt Spinler      *        repository can occupy.
5968d5f3a2bSMatt Spinler      */
5978d5f3a2bSMatt Spinler     const uint64_t _maxRepoSize;
5988d5f3a2bSMatt Spinler 
5998d5f3a2bSMatt Spinler     /**
6008d5f3a2bSMatt Spinler      * @brief The maximum number of PELs to allow in the repo
6018d5f3a2bSMatt Spinler      *        before pruning.
6028d5f3a2bSMatt Spinler      */
6038d5f3a2bSMatt Spinler     const size_t _maxNumPELs;
604b188f78aSMatt Spinler 
605b188f78aSMatt Spinler     /**
606b188f78aSMatt Spinler      * @brief Statistics on the sizes of the stored PELs.
607b188f78aSMatt Spinler      */
608b188f78aSMatt Spinler     SizeStats _sizes;
60944893cc9SMatt Spinler 
61044893cc9SMatt Spinler     /**
61144893cc9SMatt Spinler      * @brief The ID of the most recently added PEL.
61244893cc9SMatt Spinler      */
61344893cc9SMatt Spinler     uint32_t _lastPelID = 0;
6141d8835bbSSumit Kumar 
6151d8835bbSSumit Kumar     /**
6161d8835bbSSumit Kumar      * @brief The filesystem path to the archive PEL logs.
6171d8835bbSSumit Kumar      */
6181d8835bbSSumit Kumar     const std::filesystem::path _archivePath;
6191d8835bbSSumit Kumar 
6201d8835bbSSumit Kumar     /**
6211d8835bbSSumit Kumar      * @brief The size of archive folder.
6221d8835bbSSumit Kumar      */
6231d8835bbSSumit Kumar     uint64_t _archiveSize = 0;
62489fa082aSMatt Spinler };
62589fa082aSMatt Spinler 
62689fa082aSMatt Spinler } // namespace pels
62789fa082aSMatt Spinler } // namespace openpower
628