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;
370ff00485SMatt Spinler 
380ff00485SMatt Spinler         PELAttributes() = delete;
390ff00485SMatt Spinler 
40dd325c32SMatt Spinler         PELAttributes(const std::filesystem::path& p, size_t size,
41afb1b46fSVijay Lobo                       uint8_t creator, uint8_t subsystem, uint8_t sev,
42afb1b46fSVijay Lobo                       uint16_t flags, TransmissionState hostState,
43afb1b46fSVijay Lobo                       TransmissionState hmcState) :
44346f99a1SMatt Spinler             path(p),
45afb1b46fSVijay Lobo             sizeOnDisk(size), creator(creator), subsystem(subsystem),
46afb1b46fSVijay Lobo             severity(sev), actionFlags(flags), hostState(hostState),
47afb1b46fSVijay Lobo             hmcState(hmcState)
480ff00485SMatt Spinler         {
490ff00485SMatt Spinler         }
500ff00485SMatt Spinler     };
510ff00485SMatt Spinler 
520ff00485SMatt Spinler     /**
53475e574dSMatt Spinler      * @brief A structure that holds both the PEL and corresponding
54475e574dSMatt Spinler      *        OpenBMC IDs.
55475e574dSMatt Spinler      * Used for correlating the IDs with their data files for quick
56475e574dSMatt Spinler      * lookup.  To find a PEL based on just one of the IDs, just use
57475e574dSMatt Spinler      * the constructor that takes that ID.
58475e574dSMatt Spinler      */
59475e574dSMatt Spinler     struct LogID
60475e574dSMatt Spinler     {
61475e574dSMatt Spinler         struct Pel
62475e574dSMatt Spinler         {
63475e574dSMatt Spinler             uint32_t id;
64475e574dSMatt Spinler             explicit Pel(uint32_t i) : id(i)
65475e574dSMatt Spinler             {
66475e574dSMatt Spinler             }
67475e574dSMatt Spinler         };
68475e574dSMatt Spinler         struct Obmc
69475e574dSMatt Spinler         {
70475e574dSMatt Spinler             uint32_t id;
71475e574dSMatt Spinler             explicit Obmc(uint32_t i) : id(i)
72475e574dSMatt Spinler             {
73475e574dSMatt Spinler             }
74475e574dSMatt Spinler         };
75475e574dSMatt Spinler 
76475e574dSMatt Spinler         Pel pelID;
77475e574dSMatt Spinler 
78475e574dSMatt Spinler         Obmc obmcID;
79475e574dSMatt Spinler 
80475e574dSMatt Spinler         LogID(Pel pel, Obmc obmc) : pelID(pel), obmcID(obmc)
81475e574dSMatt Spinler         {
82475e574dSMatt Spinler         }
83475e574dSMatt Spinler 
84475e574dSMatt Spinler         explicit LogID(Pel id) : pelID(id), obmcID(0)
85475e574dSMatt Spinler         {
86475e574dSMatt Spinler         }
87475e574dSMatt Spinler 
88475e574dSMatt Spinler         explicit LogID(Obmc id) : pelID(0), obmcID(id)
89475e574dSMatt Spinler         {
90475e574dSMatt Spinler         }
91475e574dSMatt Spinler 
92475e574dSMatt Spinler         LogID() = delete;
93475e574dSMatt Spinler 
94475e574dSMatt Spinler         /**
95475e574dSMatt Spinler          * @brief A == operator that will match on either ID
96475e574dSMatt Spinler          *        being equal if the other is zero, so that
97475e574dSMatt Spinler          *        one can look up a PEL with just one of the IDs.
98475e574dSMatt Spinler          */
99475e574dSMatt Spinler         bool operator==(const LogID& id) const
100475e574dSMatt Spinler         {
101475e574dSMatt Spinler             if (id.pelID.id != 0)
102475e574dSMatt Spinler             {
103475e574dSMatt Spinler                 return id.pelID.id == pelID.id;
104475e574dSMatt Spinler             }
105475e574dSMatt Spinler             if (id.obmcID.id != 0)
106475e574dSMatt Spinler             {
107475e574dSMatt Spinler                 return id.obmcID.id == obmcID.id;
108475e574dSMatt Spinler             }
109475e574dSMatt Spinler             return false;
110475e574dSMatt Spinler         }
111475e574dSMatt Spinler 
112475e574dSMatt Spinler         bool operator<(const LogID& id) const
113475e574dSMatt Spinler         {
114475e574dSMatt Spinler             return pelID.id < id.pelID.id;
115475e574dSMatt Spinler         }
116475e574dSMatt Spinler     };
117475e574dSMatt Spinler 
118b0a8df5bSMatt Spinler     using AttributesReference =
119b0a8df5bSMatt Spinler         std::reference_wrapper<const std::pair<const LogID, PELAttributes>>;
120b0a8df5bSMatt Spinler 
121b188f78aSMatt Spinler     /**
122b188f78aSMatt Spinler      * @brief A structure for keeping a breakdown of the sizes of PELs
123b188f78aSMatt Spinler      *        of different types in the repository.
124b188f78aSMatt Spinler      */
125b188f78aSMatt Spinler     struct SizeStats
126b188f78aSMatt Spinler     {
127b188f78aSMatt Spinler         uint64_t total;
128b188f78aSMatt Spinler         uint64_t bmc;
129b188f78aSMatt Spinler         uint64_t nonBMC;
130b188f78aSMatt Spinler         uint64_t bmcServiceable;
131b188f78aSMatt Spinler         uint64_t bmcInfo;
132b188f78aSMatt Spinler         uint64_t nonBMCServiceable;
133b188f78aSMatt Spinler         uint64_t nonBMCInfo;
134b188f78aSMatt Spinler 
135b188f78aSMatt Spinler         SizeStats() :
136b188f78aSMatt Spinler             total(0), bmc(0), nonBMC(0), bmcServiceable(0), bmcInfo(0),
137b188f78aSMatt Spinler             nonBMCServiceable(0), nonBMCInfo(0)
138b188f78aSMatt Spinler         {
139b188f78aSMatt Spinler         }
140b188f78aSMatt Spinler     };
141b188f78aSMatt Spinler 
14289fa082aSMatt Spinler     Repository() = delete;
14389fa082aSMatt Spinler     ~Repository() = default;
14489fa082aSMatt Spinler     Repository(const Repository&) = default;
14589fa082aSMatt Spinler     Repository& operator=(const Repository&) = default;
14689fa082aSMatt Spinler     Repository(Repository&&) = default;
14789fa082aSMatt Spinler     Repository& operator=(Repository&&) = default;
14889fa082aSMatt Spinler 
14989fa082aSMatt Spinler     /**
15089fa082aSMatt Spinler      * @brief Constructor
15189fa082aSMatt Spinler      *
15289fa082aSMatt Spinler      * @param[in] basePath - the base filesystem path for the repository
15389fa082aSMatt Spinler      */
1548d5f3a2bSMatt Spinler     Repository(const std::filesystem::path& basePath) :
1558d5f3a2bSMatt Spinler         Repository(basePath, getPELRepoSize(), getMaxNumPELs())
1568d5f3a2bSMatt Spinler     {
1578d5f3a2bSMatt Spinler     }
1588d5f3a2bSMatt Spinler 
1598d5f3a2bSMatt Spinler     /**
1608d5f3a2bSMatt Spinler      * @brief Constructor that takes the repository size
1618d5f3a2bSMatt Spinler      *
1628d5f3a2bSMatt Spinler      * @param[in] basePath - the base filesystem path for the repository
1638d5f3a2bSMatt Spinler      * @param[in] repoSize - The maximum amount of space to use for PELs,
1648d5f3a2bSMatt Spinler      *                       in bytes
1658d5f3a2bSMatt Spinler      * @param[in] maxNumPELs - The maximum number of PELs to allow
1668d5f3a2bSMatt Spinler      */
1678d5f3a2bSMatt Spinler     Repository(const std::filesystem::path& basePath, size_t repoSize,
1688d5f3a2bSMatt Spinler                size_t maxNumPELs);
16989fa082aSMatt Spinler 
17089fa082aSMatt Spinler     /**
17189fa082aSMatt Spinler      * @brief Adds a PEL to the repository
17289fa082aSMatt Spinler      *
17389fa082aSMatt Spinler      * Throws File.Error.Open or File.Error.Write exceptions on failure
17489fa082aSMatt Spinler      *
17589fa082aSMatt Spinler      * @param[in] pel - the PEL to add
17689fa082aSMatt Spinler      */
17789fa082aSMatt Spinler     void add(std::unique_ptr<PEL>& pel);
17889fa082aSMatt Spinler 
17989fa082aSMatt Spinler     /**
180475e574dSMatt Spinler      * @brief Removes a PEL from the repository
181475e574dSMatt Spinler      *
18252602e35SMatt Spinler      * Note that the returned LogID is the fully filled in LogID, i.e.
18352602e35SMatt Spinler      * it has both the PEL and OpenBMC IDs, unlike the passed in LogID
18452602e35SMatt Spinler      * which can just have one or the other.
18552602e35SMatt Spinler      *
186475e574dSMatt Spinler      * @param[in] id - the ID (either the pel ID, OBMC ID, or both) to remove
18752602e35SMatt Spinler      *
18852602e35SMatt Spinler      * @return std::optional<LogID> - The LogID of the removed PEL
189475e574dSMatt Spinler      */
19052602e35SMatt Spinler     std::optional<LogID> remove(const LogID& id);
191475e574dSMatt Spinler 
192475e574dSMatt Spinler     /**
19389fa082aSMatt Spinler      * @brief Generates the filename to use for the PEL ID and BCDTime.
19489fa082aSMatt Spinler      *
19589fa082aSMatt Spinler      * @param[in] pelID - the PEL ID
19689fa082aSMatt Spinler      * @param[in] time - the BCD time
19789fa082aSMatt Spinler      *
19889fa082aSMatt Spinler      * @return string - A filename string of <BCD_time>_<pelID>
19989fa082aSMatt Spinler      */
20089fa082aSMatt Spinler     static std::string getPELFilename(uint32_t pelID, const BCDTime& time);
20189fa082aSMatt Spinler 
202475e574dSMatt Spinler     /**
203475e574dSMatt Spinler      * @brief Returns true if the PEL with the specified ID is in the repo.
204475e574dSMatt Spinler      *
205475e574dSMatt Spinler      * @param[in] id - the ID (either the pel ID, OBMC ID, or both)
206475e574dSMatt Spinler      * @return bool - true if that PEL is present
207475e574dSMatt Spinler      */
208475e574dSMatt Spinler     inline bool hasPEL(const LogID& id)
209475e574dSMatt Spinler     {
2100ff00485SMatt Spinler         return findPEL(id) != _pelAttributes.end();
211475e574dSMatt Spinler     }
212475e574dSMatt Spinler 
2132813f36dSMatt Spinler     /**
2142813f36dSMatt Spinler      * @brief Returns the PEL data based on its ID.
2152813f36dSMatt Spinler      *
2162813f36dSMatt Spinler      * If the data can't be found for that ID, then the optional object
2172813f36dSMatt Spinler      * will be empty.
2182813f36dSMatt Spinler      *
2192813f36dSMatt Spinler      * @param[in] id - the LogID to get the PEL for, which can be either a
2202813f36dSMatt Spinler      *                 PEL ID or OpenBMC log ID.
2212813f36dSMatt Spinler      * @return std::optional<std::vector<uint8_t>> - the PEL data
2222813f36dSMatt Spinler      */
2232813f36dSMatt Spinler     std::optional<std::vector<uint8_t>> getPELData(const LogID& id);
2242813f36dSMatt Spinler 
2256d51224bSMatt Spinler     /**
2266d51224bSMatt Spinler      * @brief Get a file descriptor to the PEL data
2276d51224bSMatt Spinler      *
2286d51224bSMatt Spinler      * @param[in] id - The ID to get the FD for
2296d51224bSMatt Spinler      *
2306d51224bSMatt Spinler      * @return std::optional<sdbusplus::message::unix_fd> -
2316d51224bSMatt Spinler      *         The FD, or an empty optional object.
2326d51224bSMatt Spinler      */
2336d51224bSMatt Spinler     std::optional<sdbusplus::message::unix_fd> getPELFD(const LogID& id);
2346d51224bSMatt Spinler 
2351ea78801SMatt Spinler     using ForEachFunc = std::function<bool(const PEL&)>;
2361ea78801SMatt Spinler 
2371ea78801SMatt Spinler     /**
2381ea78801SMatt Spinler      * @brief Run a user defined function on every PEL in the repository.
2391ea78801SMatt Spinler      *
2401ea78801SMatt Spinler      * ForEachFunc takes a const PEL reference, and should return
2411ea78801SMatt Spinler      * true to stop iterating and return out of for_each.
2421ea78801SMatt Spinler      *
2431ea78801SMatt Spinler      * For example, to save up to 100 IDs in the repo into a vector:
2441ea78801SMatt Spinler      *
2451ea78801SMatt Spinler      *     std::vector<uint32_t> ids;
2461ea78801SMatt Spinler      *     ForEachFunc f = [&ids](const PEL& pel) {
2471ea78801SMatt Spinler      *         ids.push_back(pel.id());
2481ea78801SMatt Spinler      *         return ids.size() == 100 ? true : false;
2491ea78801SMatt Spinler      *     };
2501ea78801SMatt Spinler      *
2511ea78801SMatt Spinler      * @param[in] func - The function to run.
2521ea78801SMatt Spinler      */
2531ea78801SMatt Spinler     void for_each(ForEachFunc func) const;
2541ea78801SMatt Spinler 
255421f6531SMatt Spinler     using AddCallback = std::function<void(const PEL&)>;
256421f6531SMatt Spinler 
257421f6531SMatt Spinler     /**
258421f6531SMatt Spinler      * @brief Subscribe to PELs being added to the repository.
259421f6531SMatt Spinler      *
260421f6531SMatt Spinler      * Every time a PEL is added to the repository, the provided
261421f6531SMatt Spinler      * function will be called with the new PEL as the argument.
262421f6531SMatt Spinler      *
263421f6531SMatt Spinler      * The function must be of type void(const PEL&).
264421f6531SMatt Spinler      *
265421f6531SMatt Spinler      * @param[in] name - The subscription name
266421f6531SMatt Spinler      * @param[in] func - The callback function
267421f6531SMatt Spinler      */
268421f6531SMatt Spinler     void subscribeToAdds(const std::string& name, AddCallback func)
269421f6531SMatt Spinler     {
270421f6531SMatt Spinler         if (_addSubscriptions.find(name) == _addSubscriptions.end())
271421f6531SMatt Spinler         {
272421f6531SMatt Spinler             _addSubscriptions.emplace(name, func);
273421f6531SMatt Spinler         }
274421f6531SMatt Spinler     }
275421f6531SMatt Spinler 
276421f6531SMatt Spinler     /**
277421f6531SMatt Spinler      * @brief Unsubscribe from new PELs.
278421f6531SMatt Spinler      *
279421f6531SMatt Spinler      * @param[in] name - The subscription name
280421f6531SMatt Spinler      */
281421f6531SMatt Spinler     void unsubscribeFromAdds(const std::string& name)
282421f6531SMatt Spinler     {
283421f6531SMatt Spinler         _addSubscriptions.erase(name);
284421f6531SMatt Spinler     }
285421f6531SMatt Spinler 
286421f6531SMatt Spinler     using DeleteCallback = std::function<void(uint32_t)>;
287421f6531SMatt Spinler 
288421f6531SMatt Spinler     /**
289421f6531SMatt Spinler      * @brief Subscribe to PELs being deleted from the repository.
290421f6531SMatt Spinler      *
291421f6531SMatt Spinler      * Every time a PEL is deleted from the repository, the provided
292421f6531SMatt Spinler      * function will be called with the PEL ID as the argument.
293421f6531SMatt Spinler      *
294421f6531SMatt Spinler      * The function must be of type void(const uint32_t).
295421f6531SMatt Spinler      *
296421f6531SMatt Spinler      * @param[in] name - The subscription name
297421f6531SMatt Spinler      * @param[in] func - The callback function
298421f6531SMatt Spinler      */
299421f6531SMatt Spinler     void subscribeToDeletes(const std::string& name, DeleteCallback func)
300421f6531SMatt Spinler     {
301421f6531SMatt Spinler         if (_deleteSubscriptions.find(name) == _deleteSubscriptions.end())
302421f6531SMatt Spinler         {
303421f6531SMatt Spinler             _deleteSubscriptions.emplace(name, func);
304421f6531SMatt Spinler         }
305421f6531SMatt Spinler     }
306421f6531SMatt Spinler 
307421f6531SMatt Spinler     /**
308421f6531SMatt Spinler      * @brief Unsubscribe from deleted PELs.
309421f6531SMatt Spinler      *
310421f6531SMatt Spinler      * @param[in] name - The subscription name
311421f6531SMatt Spinler      */
312421f6531SMatt Spinler     void unsubscribeFromDeletes(const std::string& name)
313421f6531SMatt Spinler     {
314421f6531SMatt Spinler         _deleteSubscriptions.erase(name);
315421f6531SMatt Spinler     }
316421f6531SMatt Spinler 
3170ff00485SMatt Spinler     /**
3180ff00485SMatt Spinler      * @brief Get the PEL attributes for a PEL
3190ff00485SMatt Spinler      *
3200ff00485SMatt Spinler      * @param[in] id - The ID to find the attributes for
3210ff00485SMatt Spinler      *
3220ff00485SMatt Spinler      * @return The attributes or an empty optional if not found
3230ff00485SMatt Spinler      */
3240ff00485SMatt Spinler     std::optional<std::reference_wrapper<const PELAttributes>>
3250ff00485SMatt Spinler         getPELAttributes(const LogID& id) const;
3260ff00485SMatt Spinler 
32729d18c11SMatt Spinler     /**
32829d18c11SMatt Spinler      * @brief Sets the host transmission state on a PEL file
32929d18c11SMatt Spinler      *
33029d18c11SMatt Spinler      * Writes the host transmission state field in the User Header
33129d18c11SMatt Spinler      * section in the PEL data specified by the ID.
33229d18c11SMatt Spinler      *
33329d18c11SMatt Spinler      * @param[in] pelID - The PEL ID
33429d18c11SMatt Spinler      * @param[in] state - The state to write
33529d18c11SMatt Spinler      */
33629d18c11SMatt Spinler     void setPELHostTransState(uint32_t pelID, TransmissionState state);
33729d18c11SMatt Spinler 
33829d18c11SMatt Spinler     /**
33929d18c11SMatt Spinler      * @brief Sets the HMC transmission state on a PEL file
34029d18c11SMatt Spinler      *
34129d18c11SMatt Spinler      * Writes the HMC transmission state field in the User Header
34229d18c11SMatt Spinler      * section in the PEL data specified by the ID.
34329d18c11SMatt Spinler      *
34429d18c11SMatt Spinler      * @param[in] pelID - The PEL ID
34529d18c11SMatt Spinler      * @param[in] state - The state to write
34629d18c11SMatt Spinler      */
34729d18c11SMatt Spinler     void setPELHMCTransState(uint32_t pelID, TransmissionState state);
34829d18c11SMatt Spinler 
349b188f78aSMatt Spinler     /**
350b188f78aSMatt Spinler      * @brief Returns the size stats structure
351b188f78aSMatt Spinler      *
352b188f78aSMatt Spinler      * @return const SizeStats& - The stats structure
353b188f78aSMatt Spinler      */
354b188f78aSMatt Spinler     const SizeStats& getSizeStats() const
355b188f78aSMatt Spinler     {
356b188f78aSMatt Spinler         return _sizes;
357b188f78aSMatt Spinler     }
358b188f78aSMatt Spinler 
359b188f78aSMatt Spinler     /**
360b188f78aSMatt Spinler      * @brief Says if the PEL is considered serviceable (not just
361b188f78aSMatt Spinler      *        informational) as determined by its severity.
362b188f78aSMatt Spinler      *
363b188f78aSMatt Spinler      * @param[in] pel - The PELAttributes entry for the PEL
364b188f78aSMatt Spinler      * @return bool - If serviceable or not
365b188f78aSMatt Spinler      */
366b188f78aSMatt Spinler     static bool isServiceableSev(const PELAttributes& pel);
367b188f78aSMatt Spinler 
368b0a8df5bSMatt Spinler     /**
3697e727a39SMatt Spinler      * @brief Returns true if the total amount of disk space occupied
3707e727a39SMatt Spinler      *        by the PELs in the repo is over 95% of the maximum
3717e727a39SMatt Spinler      *        size, or if there are over the maximum number of
3727e727a39SMatt Spinler      *        PELs allowed.
3737e727a39SMatt Spinler      *
3747e727a39SMatt Spinler      * @return bool - true if repo is > 95% full or too many PELs
3757e727a39SMatt Spinler      */
376c296692bSSumit Kumar     bool sizeWarning();
3777e727a39SMatt Spinler 
3787e727a39SMatt Spinler     /**
379b0a8df5bSMatt Spinler      * @brief Deletes PELs to bring the repository size down
380b0a8df5bSMatt Spinler      *        to at most 90% full by placing PELs into 4 different
381b0a8df5bSMatt Spinler      *        catogories and then removing PELs until those catogories
382b0a8df5bSMatt Spinler      *        only take up certain percentages of the allowed space.
383b0a8df5bSMatt Spinler      *
384b0a8df5bSMatt Spinler      * This does not delete the corresponding OpenBMC event logs, which
385b0a8df5bSMatt Spinler      * is why those IDs are returned, so they can be deleted later.
386b0a8df5bSMatt Spinler      *
387b0a8df5bSMatt Spinler      * The categories and their rules are:
388b0a8df5bSMatt Spinler      *  1) Informational BMC PELs cannot take up more than 15% of
389b0a8df5bSMatt Spinler      *     the allocated space.
390b0a8df5bSMatt Spinler      *  2) Non-informational BMC PELs cannot take up more than 30%
391b0a8df5bSMatt Spinler      *     of the allocated space.
392b0a8df5bSMatt Spinler      *  3) Informational non-BMC PELs cannot take up more than 15% of
393b0a8df5bSMatt Spinler      *     the allocated space.
394b0a8df5bSMatt Spinler      *  4) Non-informational non-BMC PELs cannot take up more than 30%
395b0a8df5bSMatt Spinler      *     of the allocated space.
396b0a8df5bSMatt Spinler      *
397b0a8df5bSMatt Spinler      *  While removing PELs in a category, 4 passes will be made, with
398b0a8df5bSMatt Spinler      *  PELs being removed oldest first during each pass.
399b0a8df5bSMatt Spinler      *
400b0a8df5bSMatt Spinler      *   Pass 1: only delete HMC acked PELs
401b0a8df5bSMatt Spinler      *   Pass 2: only delete OS acked PELs
402b0a8df5bSMatt Spinler      *   Pass 3: only delete PHYP sent PELs
403b0a8df5bSMatt Spinler      *   Pass 4: delete all PELs
404b0a8df5bSMatt Spinler      *
405*027bf285SSumit Kumar      * @param[in] ids - The OpenBMC event log Ids with hardware isolation entry.
406*027bf285SSumit Kumar      *
407b0a8df5bSMatt Spinler      * @return std::vector<uint32_t> - The OpenBMC event log IDs of
408b0a8df5bSMatt Spinler      *                                 the PELs that were deleted.
409b0a8df5bSMatt Spinler      */
410*027bf285SSumit Kumar     std::vector<uint32_t> prune(const std::vector<uint32_t>& idsWithHwIsoEntry);
411b0a8df5bSMatt Spinler 
412ff9cec25SMatt Spinler     /**
413ff9cec25SMatt Spinler      * @brief Returns the path to the directory where the PEL
414ff9cec25SMatt Spinler      *        files are stored.
415ff9cec25SMatt Spinler      *
416ff9cec25SMatt Spinler      * @return std::filesystem::path - The directory path
417ff9cec25SMatt Spinler      */
418ff9cec25SMatt Spinler     const std::filesystem::path& repoPath() const
419ff9cec25SMatt Spinler     {
420ff9cec25SMatt Spinler         return _logPath;
421ff9cec25SMatt Spinler     }
422ff9cec25SMatt Spinler 
42344893cc9SMatt Spinler     /**
42444893cc9SMatt Spinler      * @brief Returns the ID of the most recently added PEL.
42544893cc9SMatt Spinler      *
42644893cc9SMatt Spinler      * @return uint32_t - The PEL ID
42744893cc9SMatt Spinler      */
42844893cc9SMatt Spinler     uint32_t lastPelID() const
42944893cc9SMatt Spinler     {
43044893cc9SMatt Spinler         return _lastPelID;
43144893cc9SMatt Spinler     }
43244893cc9SMatt Spinler 
43399f3717cSRamesh Iyyar     /**
43499f3717cSRamesh Iyyar      * @brief Get the LogID based on the given ObmcLogId or PelId.
43599f3717cSRamesh Iyyar      *
43699f3717cSRamesh Iyyar      * @param[in] id - The ID to find the LogID.
43799f3717cSRamesh Iyyar      *
43899f3717cSRamesh Iyyar      * @return The LogID or an empty optional if not found.
43999f3717cSRamesh Iyyar      *
44099f3717cSRamesh Iyyar      * @note The returned LogID is the fully filled in LogID, i.e.
44199f3717cSRamesh Iyyar      * it has both the PEL and OpenBMC Log IDs, unlike the passed in LogID
44299f3717cSRamesh Iyyar      * which can just have one or the other.
44399f3717cSRamesh Iyyar      */
44499f3717cSRamesh Iyyar     std::optional<LogID> getLogID(const LogID& id) const
44599f3717cSRamesh Iyyar     {
44699f3717cSRamesh Iyyar         if (auto logID = findPEL(id); logID != _pelAttributes.end())
44799f3717cSRamesh Iyyar         {
44899f3717cSRamesh Iyyar             return logID->first;
44999f3717cSRamesh Iyyar         }
45099f3717cSRamesh Iyyar         return std::nullopt;
45199f3717cSRamesh Iyyar     }
45299f3717cSRamesh Iyyar 
4532ccdcef9SSumit Kumar     /**
4542ccdcef9SSumit Kumar      * @brief Save the PEL to archive folder
4552ccdcef9SSumit Kumar      *
4562ccdcef9SSumit Kumar      * @param[in] pel - The PEL data
4572ccdcef9SSumit Kumar      */
4582ccdcef9SSumit Kumar     void archivePEL(const PEL& pel);
4592ccdcef9SSumit Kumar 
46089fa082aSMatt Spinler   private:
46129d18c11SMatt Spinler     using PELUpdateFunc = std::function<void(PEL&)>;
46229d18c11SMatt Spinler 
46329d18c11SMatt Spinler     /**
46429d18c11SMatt Spinler      * @brief Lets a function modify a PEL and saves the results
46529d18c11SMatt Spinler      *
46629d18c11SMatt Spinler      * Runs updateFunc (a void(PEL&) function) on the PEL data
46729d18c11SMatt Spinler      * on the file specified, and writes the results back to the file.
46829d18c11SMatt Spinler      *
46929d18c11SMatt Spinler      * @param[in] path - The file path to use
47029d18c11SMatt Spinler      * @param[in] updateFunc - The function to run to update the PEL.
47129d18c11SMatt Spinler      */
47229d18c11SMatt Spinler     void updatePEL(const std::filesystem::path& path, PELUpdateFunc updateFunc);
47329d18c11SMatt Spinler 
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
4820ff00485SMatt 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.
563*027bf285SSumit Kumar      * @param[in] ids - The OpenBMC event log Ids with hardware isolation
564*027bf285SSumit Kumar      *                   entry.
565b0a8df5bSMatt Spinler      *
566b0a8df5bSMatt Spinler      * @param[out] removedBMCLogIDs - The OpenBMC event log IDs of the
567b0a8df5bSMatt Spinler      *                                removed PELs.
568b0a8df5bSMatt Spinler      */
569b0a8df5bSMatt Spinler     void removePELs(IsOverLimitFunc& isOverLimit, IsPELTypeFunc& isPELType,
570*027bf285SSumit Kumar                     const std::vector<uint32_t>& idsWithHwIsoEntry,
571b0a8df5bSMatt Spinler                     std::vector<uint32_t>& removedBMCLogIDs);
572b188f78aSMatt Spinler     /**
57389fa082aSMatt Spinler      * @brief The filesystem path to the PEL logs.
57489fa082aSMatt Spinler      */
57589fa082aSMatt Spinler     const std::filesystem::path _logPath;
576475e574dSMatt Spinler 
577475e574dSMatt Spinler     /**
5780ff00485SMatt Spinler      * @brief A map of the PEL/OBMC IDs to PEL attributes.
579475e574dSMatt Spinler      */
5800ff00485SMatt Spinler     std::map<LogID, PELAttributes> _pelAttributes;
581421f6531SMatt Spinler 
582421f6531SMatt Spinler     /**
583421f6531SMatt Spinler      * @brief Subcriptions for new PELs.
584421f6531SMatt Spinler      */
585421f6531SMatt Spinler     std::map<std::string, AddCallback> _addSubscriptions;
586421f6531SMatt Spinler 
587421f6531SMatt Spinler     /**
588421f6531SMatt Spinler      * @brief Subscriptions for deleted PELs.
589421f6531SMatt Spinler      */
590421f6531SMatt Spinler     std::map<std::string, DeleteCallback> _deleteSubscriptions;
5918d5f3a2bSMatt Spinler 
5928d5f3a2bSMatt Spinler     /**
5938d5f3a2bSMatt Spinler      * @brief The maximum amount of space that the PELs in the
5948d5f3a2bSMatt Spinler      *        repository can occupy.
5958d5f3a2bSMatt Spinler      */
5968d5f3a2bSMatt Spinler     const uint64_t _maxRepoSize;
5978d5f3a2bSMatt Spinler 
5988d5f3a2bSMatt Spinler     /**
5998d5f3a2bSMatt Spinler      * @brief The maximum number of PELs to allow in the repo
6008d5f3a2bSMatt Spinler      *        before pruning.
6018d5f3a2bSMatt Spinler      */
6028d5f3a2bSMatt Spinler     const size_t _maxNumPELs;
603b188f78aSMatt Spinler 
604b188f78aSMatt Spinler     /**
605b188f78aSMatt Spinler      * @brief Statistics on the sizes of the stored PELs.
606b188f78aSMatt Spinler      */
607b188f78aSMatt Spinler     SizeStats _sizes;
60844893cc9SMatt Spinler 
60944893cc9SMatt Spinler     /**
61044893cc9SMatt Spinler      * @brief The ID of the most recently added PEL.
61144893cc9SMatt Spinler      */
61244893cc9SMatt Spinler     uint32_t _lastPelID = 0;
6131d8835bbSSumit Kumar 
6141d8835bbSSumit Kumar     /**
6151d8835bbSSumit Kumar      * @brief The filesystem path to the archive PEL logs.
6161d8835bbSSumit Kumar      */
6171d8835bbSSumit Kumar     const std::filesystem::path _archivePath;
6181d8835bbSSumit Kumar 
6191d8835bbSSumit Kumar     /**
6201d8835bbSSumit Kumar      * @brief The size of archive folder.
6211d8835bbSSumit Kumar      */
6221d8835bbSSumit Kumar     uint64_t _archiveSize = 0;
62389fa082aSMatt Spinler };
62489fa082aSMatt Spinler 
62589fa082aSMatt Spinler } // namespace pels
62689fa082aSMatt Spinler } // namespace openpower
627