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 
407*ff9cec25SMatt Spinler     /**
408*ff9cec25SMatt Spinler      * @brief Returns the path to the directory where the PEL
409*ff9cec25SMatt Spinler      *        files are stored.
410*ff9cec25SMatt Spinler      *
411*ff9cec25SMatt Spinler      * @return std::filesystem::path - The directory path
412*ff9cec25SMatt Spinler      */
413*ff9cec25SMatt Spinler     const std::filesystem::path& repoPath() const
414*ff9cec25SMatt Spinler     {
415*ff9cec25SMatt Spinler         return _logPath;
416*ff9cec25SMatt Spinler     }
417*ff9cec25SMatt Spinler 
41889fa082aSMatt Spinler   private:
41929d18c11SMatt Spinler     using PELUpdateFunc = std::function<void(PEL&)>;
42029d18c11SMatt Spinler 
42129d18c11SMatt Spinler     /**
42229d18c11SMatt Spinler      * @brief Lets a function modify a PEL and saves the results
42329d18c11SMatt Spinler      *
42429d18c11SMatt Spinler      * Runs updateFunc (a void(PEL&) function) on the PEL data
42529d18c11SMatt Spinler      * on the file specified, and writes the results back to the file.
42629d18c11SMatt Spinler      *
42729d18c11SMatt Spinler      * @param[in] path - The file path to use
42829d18c11SMatt Spinler      * @param[in] updateFunc - The function to run to update the PEL.
42929d18c11SMatt Spinler      */
43029d18c11SMatt Spinler     void updatePEL(const std::filesystem::path& path, PELUpdateFunc updateFunc);
43129d18c11SMatt Spinler 
43289fa082aSMatt Spinler     /**
4330ff00485SMatt Spinler      * @brief Finds an entry in the _pelAttributes map.
434475e574dSMatt Spinler      *
435475e574dSMatt Spinler      * @param[in] id - the ID (either the pel ID, OBMC ID, or both)
436475e574dSMatt Spinler      *
437475e574dSMatt Spinler      * @return an iterator to the entry
438475e574dSMatt Spinler      */
4390ff00485SMatt Spinler     std::map<LogID, PELAttributes>::const_iterator
4400ff00485SMatt Spinler         findPEL(const LogID& id) const
441475e574dSMatt Spinler     {
4420ff00485SMatt Spinler         return std::find_if(_pelAttributes.begin(), _pelAttributes.end(),
4430ff00485SMatt Spinler                             [&id](const auto& a) { return a.first == id; });
444475e574dSMatt Spinler     }
445475e574dSMatt Spinler 
446475e574dSMatt Spinler     /**
447421f6531SMatt Spinler      * @brief Call any subscribed functions for new PELs
448421f6531SMatt Spinler      *
449421f6531SMatt Spinler      * @param[in] pel - The new PEL
450421f6531SMatt Spinler      */
451421f6531SMatt Spinler     void processAddCallbacks(const PEL& pel) const;
452421f6531SMatt Spinler 
453421f6531SMatt Spinler     /**
454421f6531SMatt Spinler      * @brief Call any subscribed functions for deleted PELs
455421f6531SMatt Spinler      *
456421f6531SMatt Spinler      * @param[in] id - The ID of the deleted PEL
457421f6531SMatt Spinler      */
458421f6531SMatt Spinler     void processDeleteCallbacks(uint32_t id) const;
459421f6531SMatt Spinler 
460421f6531SMatt Spinler     /**
4610ff00485SMatt Spinler      * @brief Restores the _pelAttributes map on startup based on the existing
462475e574dSMatt Spinler      *        PEL data files.
463475e574dSMatt Spinler      */
464475e574dSMatt Spinler     void restore();
465475e574dSMatt Spinler 
466475e574dSMatt Spinler     /**
467ab1b97feSMatt Spinler      * @brief Stores a PEL object in the filesystem.
468ab1b97feSMatt Spinler      *
469ab1b97feSMatt Spinler      * @param[in] pel - The PEL to write
470ab1b97feSMatt Spinler      * @param[in] path - The file to write to
471ab1b97feSMatt Spinler      *
472ab1b97feSMatt Spinler      * Throws exceptions on failures.
473ab1b97feSMatt Spinler      */
474ab1b97feSMatt Spinler     void write(const PEL& pel, const std::filesystem::path& path);
475ab1b97feSMatt Spinler 
476ab1b97feSMatt Spinler     /**
477b188f78aSMatt Spinler      * @brief Updates the repository statistics after a PEL is
478b188f78aSMatt Spinler      *        added or removed.
479b188f78aSMatt Spinler      *
480b188f78aSMatt Spinler      * @param[in] pel - The PELAttributes entry for the PEL
481b188f78aSMatt Spinler      * @param[in] pelAdded - true if the PEL was added, false if removed
482b188f78aSMatt Spinler      */
483b188f78aSMatt Spinler     void updateRepoStats(const PELAttributes& pel, bool pelAdded);
484b188f78aSMatt Spinler 
485b0a8df5bSMatt Spinler     enum class SortOrder
486b0a8df5bSMatt Spinler     {
487b0a8df5bSMatt Spinler         ascending,
488b0a8df5bSMatt Spinler         descending
489b0a8df5bSMatt Spinler     };
490b0a8df5bSMatt Spinler 
491b0a8df5bSMatt Spinler     /**
492b0a8df5bSMatt Spinler      * @brief Returns a vector of all the _pelAttributes entries sorted
493b0a8df5bSMatt Spinler      *        as specified
494b0a8df5bSMatt Spinler      *
495b0a8df5bSMatt Spinler      * @param[in] order - If the PELs should be returned in ascending
496b0a8df5bSMatt Spinler      *                    (oldest first) or descending order.
497b0a8df5bSMatt Spinler      *
498b0a8df5bSMatt Spinler      * @return std::vector<AttributesReference> - The sorted vector of
499b0a8df5bSMatt Spinler      *         references to the pair<LogID, PELAttributes> entries of
500b0a8df5bSMatt Spinler      *         _pelAttributes.
501b0a8df5bSMatt Spinler      */
502b0a8df5bSMatt Spinler     std::vector<AttributesReference> getAllPELAttributes(SortOrder order) const;
503b0a8df5bSMatt Spinler 
504b0a8df5bSMatt Spinler     using IsOverLimitFunc = std::function<bool()>;
505b0a8df5bSMatt Spinler     using IsPELTypeFunc = std::function<bool(const PELAttributes&)>;
506b0a8df5bSMatt Spinler 
507b0a8df5bSMatt Spinler     /**
508b0a8df5bSMatt Spinler      * @brief Makes 4 passes on the PELs that meet the IsPELTypeFunc
509b0a8df5bSMatt Spinler      *        criteria removing PELs until IsOverLimitFunc returns false.
510b0a8df5bSMatt Spinler      *
511b0a8df5bSMatt Spinler      *   Pass 1: only delete HMC acked PELs
512b0a8df5bSMatt Spinler      *   Pass 2: only delete Os acked PELs
513b0a8df5bSMatt Spinler      *   Pass 3: only delete PHYP sent PELs
514b0a8df5bSMatt Spinler      *   Pass 4: delete all PELs
515b0a8df5bSMatt Spinler      *
516b0a8df5bSMatt Spinler      * @param[in] isOverLimit - The bool(void) function that should
517b0a8df5bSMatt Spinler      *                          return true if PELs still need to be
518b0a8df5bSMatt Spinler      *                           removed.
519b0a8df5bSMatt Spinler      * @param[in] isPELType - The bool(const PELAttributes&) function
520b0a8df5bSMatt Spinler      *                         used to select the PELs to operate on.
521b0a8df5bSMatt Spinler      *
522b0a8df5bSMatt Spinler      * @param[out] removedBMCLogIDs - The OpenBMC event log IDs of the
523b0a8df5bSMatt Spinler      *                                removed PELs.
524b0a8df5bSMatt Spinler      */
525b0a8df5bSMatt Spinler     void removePELs(IsOverLimitFunc& isOverLimit, IsPELTypeFunc& isPELType,
526b0a8df5bSMatt Spinler                     std::vector<uint32_t>& removedBMCLogIDs);
527b188f78aSMatt Spinler     /**
52889fa082aSMatt Spinler      * @brief The filesystem path to the PEL logs.
52989fa082aSMatt Spinler      */
53089fa082aSMatt Spinler     const std::filesystem::path _logPath;
531475e574dSMatt Spinler 
532475e574dSMatt Spinler     /**
5330ff00485SMatt Spinler      * @brief A map of the PEL/OBMC IDs to PEL attributes.
534475e574dSMatt Spinler      */
5350ff00485SMatt Spinler     std::map<LogID, PELAttributes> _pelAttributes;
536421f6531SMatt Spinler 
537421f6531SMatt Spinler     /**
538421f6531SMatt Spinler      * @brief Subcriptions for new PELs.
539421f6531SMatt Spinler      */
540421f6531SMatt Spinler     std::map<std::string, AddCallback> _addSubscriptions;
541421f6531SMatt Spinler 
542421f6531SMatt Spinler     /**
543421f6531SMatt Spinler      * @brief Subscriptions for deleted PELs.
544421f6531SMatt Spinler      */
545421f6531SMatt Spinler     std::map<std::string, DeleteCallback> _deleteSubscriptions;
5468d5f3a2bSMatt Spinler 
5478d5f3a2bSMatt Spinler     /**
5488d5f3a2bSMatt Spinler      * @brief The maximum amount of space that the PELs in the
5498d5f3a2bSMatt Spinler      *        repository can occupy.
5508d5f3a2bSMatt Spinler      */
5518d5f3a2bSMatt Spinler     const uint64_t _maxRepoSize;
5528d5f3a2bSMatt Spinler 
5538d5f3a2bSMatt Spinler     /**
5548d5f3a2bSMatt Spinler      * @brief The maximum number of PELs to allow in the repo
5558d5f3a2bSMatt Spinler      *        before pruning.
5568d5f3a2bSMatt Spinler      */
5578d5f3a2bSMatt Spinler     const size_t _maxNumPELs;
558b188f78aSMatt Spinler 
559b188f78aSMatt Spinler     /**
560b188f78aSMatt Spinler      * @brief Statistics on the sizes of the stored PELs.
561b188f78aSMatt Spinler      */
562b188f78aSMatt Spinler     SizeStats _sizes;
56389fa082aSMatt Spinler };
56489fa082aSMatt Spinler 
56589fa082aSMatt Spinler } // namespace pels
56689fa082aSMatt Spinler } // namespace openpower
567