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;
37*8e65f4eaSMatt Spinler         uint32_t plid;
38*8e65f4eaSMatt Spinler         bool deconfig;
39*8e65f4eaSMatt Spinler         bool guard;
40*8e65f4eaSMatt Spinler         uint64_t creationTime;
410ff00485SMatt Spinler 
420ff00485SMatt Spinler         PELAttributes() = delete;
430ff00485SMatt Spinler 
44dd325c32SMatt 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,
47*8e65f4eaSMatt Spinler                       TransmissionState hmcState, uint32_t plid, bool deconfig,
48*8e65f4eaSMatt 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),
52*8e65f4eaSMatt Spinler             hmcState(hmcState), plid(plid), deconfig(deconfig), guard(guard),
53*8e65f4eaSMatt 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;
692544b419SPatrick Williams             explicit Pel(uint32_t i) : id(i) {}
70475e574dSMatt Spinler         };
71475e574dSMatt Spinler         struct Obmc
72475e574dSMatt Spinler         {
73475e574dSMatt Spinler             uint32_t id;
742544b419SPatrick Williams             explicit Obmc(uint32_t i) : id(i) {}
75475e574dSMatt Spinler         };
76475e574dSMatt Spinler 
77475e574dSMatt Spinler         Pel pelID;
78475e574dSMatt Spinler 
79475e574dSMatt Spinler         Obmc obmcID;
80475e574dSMatt Spinler 
812544b419SPatrick Williams         LogID(Pel pel, Obmc obmc) : pelID(pel), obmcID(obmc) {}
82475e574dSMatt Spinler 
832544b419SPatrick Williams         explicit LogID(Pel id) : pelID(id), obmcID(0) {}
84475e574dSMatt Spinler 
852544b419SPatrick 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          */
94475e574dSMatt 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 
107475e574dSMatt 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 
130b188f78aSMatt 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      */
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      */
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      */
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      */
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      */
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      */
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     /**
31529d18c11SMatt Spinler      * @brief Sets the host transmission state on a PEL file
31629d18c11SMatt Spinler      *
31729d18c11SMatt Spinler      * Writes the host transmission state field in the User Header
31829d18c11SMatt Spinler      * section in the PEL data specified by the ID.
31929d18c11SMatt Spinler      *
32029d18c11SMatt Spinler      * @param[in] pelID - The PEL ID
32129d18c11SMatt Spinler      * @param[in] state - The state to write
32229d18c11SMatt Spinler      */
32329d18c11SMatt Spinler     void setPELHostTransState(uint32_t pelID, TransmissionState state);
32429d18c11SMatt Spinler 
32529d18c11SMatt Spinler     /**
32629d18c11SMatt Spinler      * @brief Sets the HMC transmission state on a PEL file
32729d18c11SMatt Spinler      *
32829d18c11SMatt Spinler      * Writes the HMC transmission state field in the User Header
32929d18c11SMatt Spinler      * section in the PEL data specified by the ID.
33029d18c11SMatt Spinler      *
33129d18c11SMatt Spinler      * @param[in] pelID - The PEL ID
33229d18c11SMatt Spinler      * @param[in] state - The state to write
33329d18c11SMatt Spinler      */
33429d18c11SMatt Spinler     void setPELHMCTransState(uint32_t pelID, TransmissionState state);
33529d18c11SMatt Spinler 
336b188f78aSMatt Spinler     /**
337b188f78aSMatt Spinler      * @brief Returns the size stats structure
338b188f78aSMatt Spinler      *
339b188f78aSMatt Spinler      * @return const SizeStats& - The stats structure
340b188f78aSMatt Spinler      */
341b188f78aSMatt Spinler     const SizeStats& getSizeStats() const
342b188f78aSMatt Spinler     {
343b188f78aSMatt Spinler         return _sizes;
344b188f78aSMatt Spinler     }
345b188f78aSMatt Spinler 
346b188f78aSMatt Spinler     /**
347b188f78aSMatt Spinler      * @brief Says if the PEL is considered serviceable (not just
348b188f78aSMatt Spinler      *        informational) as determined by its severity.
349b188f78aSMatt Spinler      *
350b188f78aSMatt Spinler      * @param[in] pel - The PELAttributes entry for the PEL
351b188f78aSMatt Spinler      * @return bool - If serviceable or not
352b188f78aSMatt Spinler      */
353b188f78aSMatt Spinler     static bool isServiceableSev(const PELAttributes& pel);
354b188f78aSMatt Spinler 
355b0a8df5bSMatt Spinler     /**
3567e727a39SMatt Spinler      * @brief Returns true if the total amount of disk space occupied
3577e727a39SMatt Spinler      *        by the PELs in the repo is over 95% of the maximum
3587e727a39SMatt Spinler      *        size, or if there are over the maximum number of
3597e727a39SMatt Spinler      *        PELs allowed.
3607e727a39SMatt Spinler      *
3617e727a39SMatt Spinler      * @return bool - true if repo is > 95% full or too many PELs
3627e727a39SMatt Spinler      */
363c296692bSSumit Kumar     bool sizeWarning();
3647e727a39SMatt Spinler 
3657e727a39SMatt Spinler     /**
366b0a8df5bSMatt Spinler      * @brief Deletes PELs to bring the repository size down
367b0a8df5bSMatt Spinler      *        to at most 90% full by placing PELs into 4 different
368b0a8df5bSMatt Spinler      *        catogories and then removing PELs until those catogories
369b0a8df5bSMatt Spinler      *        only take up certain percentages of the allowed space.
370b0a8df5bSMatt Spinler      *
371b0a8df5bSMatt Spinler      * This does not delete the corresponding OpenBMC event logs, which
372b0a8df5bSMatt Spinler      * is why those IDs are returned, so they can be deleted later.
373b0a8df5bSMatt Spinler      *
374b0a8df5bSMatt Spinler      * The categories and their rules are:
375b0a8df5bSMatt Spinler      *  1) Informational BMC PELs cannot take up more than 15% of
376b0a8df5bSMatt Spinler      *     the allocated space.
377b0a8df5bSMatt Spinler      *  2) Non-informational BMC PELs cannot take up more than 30%
378b0a8df5bSMatt Spinler      *     of the allocated space.
379b0a8df5bSMatt Spinler      *  3) Informational non-BMC PELs cannot take up more than 15% of
380b0a8df5bSMatt Spinler      *     the allocated space.
381b0a8df5bSMatt Spinler      *  4) Non-informational non-BMC PELs cannot take up more than 30%
382b0a8df5bSMatt Spinler      *     of the allocated space.
383b0a8df5bSMatt Spinler      *
384b0a8df5bSMatt Spinler      *  While removing PELs in a category, 4 passes will be made, with
385b0a8df5bSMatt Spinler      *  PELs being removed oldest first during each pass.
386b0a8df5bSMatt Spinler      *
387b0a8df5bSMatt Spinler      *   Pass 1: only delete HMC acked PELs
388b0a8df5bSMatt Spinler      *   Pass 2: only delete OS acked PELs
389b0a8df5bSMatt Spinler      *   Pass 3: only delete PHYP sent PELs
390b0a8df5bSMatt Spinler      *   Pass 4: delete all PELs
391b0a8df5bSMatt Spinler      *
392027bf285SSumit Kumar      * @param[in] ids - The OpenBMC event log Ids with hardware isolation entry.
393027bf285SSumit Kumar      *
394b0a8df5bSMatt Spinler      * @return std::vector<uint32_t> - The OpenBMC event log IDs of
395b0a8df5bSMatt Spinler      *                                 the PELs that were deleted.
396b0a8df5bSMatt Spinler      */
397027bf285SSumit Kumar     std::vector<uint32_t> prune(const std::vector<uint32_t>& idsWithHwIsoEntry);
398b0a8df5bSMatt Spinler 
399ff9cec25SMatt Spinler     /**
400ff9cec25SMatt Spinler      * @brief Returns the path to the directory where the PEL
401ff9cec25SMatt Spinler      *        files are stored.
402ff9cec25SMatt Spinler      *
403ff9cec25SMatt Spinler      * @return std::filesystem::path - The directory path
404ff9cec25SMatt Spinler      */
405ff9cec25SMatt Spinler     const std::filesystem::path& repoPath() const
406ff9cec25SMatt Spinler     {
407ff9cec25SMatt Spinler         return _logPath;
408ff9cec25SMatt Spinler     }
409ff9cec25SMatt Spinler 
41044893cc9SMatt Spinler     /**
41144893cc9SMatt Spinler      * @brief Returns the ID of the most recently added PEL.
41244893cc9SMatt Spinler      *
41344893cc9SMatt Spinler      * @return uint32_t - The PEL ID
41444893cc9SMatt Spinler      */
41544893cc9SMatt Spinler     uint32_t lastPelID() const
41644893cc9SMatt Spinler     {
41744893cc9SMatt Spinler         return _lastPelID;
41844893cc9SMatt Spinler     }
41944893cc9SMatt Spinler 
42099f3717cSRamesh Iyyar     /**
42199f3717cSRamesh Iyyar      * @brief Get the LogID based on the given ObmcLogId or PelId.
42299f3717cSRamesh Iyyar      *
42399f3717cSRamesh Iyyar      * @param[in] id - The ID to find the LogID.
42499f3717cSRamesh Iyyar      *
42599f3717cSRamesh Iyyar      * @return The LogID or an empty optional if not found.
42699f3717cSRamesh Iyyar      *
42799f3717cSRamesh Iyyar      * @note The returned LogID is the fully filled in LogID, i.e.
42899f3717cSRamesh Iyyar      * it has both the PEL and OpenBMC Log IDs, unlike the passed in LogID
42999f3717cSRamesh Iyyar      * which can just have one or the other.
43099f3717cSRamesh Iyyar      */
43199f3717cSRamesh Iyyar     std::optional<LogID> getLogID(const LogID& id) const
43299f3717cSRamesh Iyyar     {
43399f3717cSRamesh Iyyar         if (auto logID = findPEL(id); logID != _pelAttributes.end())
43499f3717cSRamesh Iyyar         {
43599f3717cSRamesh Iyyar             return logID->first;
43699f3717cSRamesh Iyyar         }
43799f3717cSRamesh Iyyar         return std::nullopt;
43899f3717cSRamesh Iyyar     }
43999f3717cSRamesh Iyyar 
4402ccdcef9SSumit Kumar     /**
4412ccdcef9SSumit Kumar      * @brief Save the PEL to archive folder
4422ccdcef9SSumit Kumar      *
4432ccdcef9SSumit Kumar      * @param[in] pel - The PEL data
4442ccdcef9SSumit Kumar      */
4452ccdcef9SSumit Kumar     void archivePEL(const PEL& pel);
4462ccdcef9SSumit Kumar 
44789fa082aSMatt Spinler   private:
44829d18c11SMatt Spinler     using PELUpdateFunc = std::function<void(PEL&)>;
44929d18c11SMatt Spinler 
45029d18c11SMatt Spinler     /**
45129d18c11SMatt Spinler      * @brief Lets a function modify a PEL and saves the results
45229d18c11SMatt Spinler      *
45329d18c11SMatt Spinler      * Runs updateFunc (a void(PEL&) function) on the PEL data
45429d18c11SMatt Spinler      * on the file specified, and writes the results back to the file.
45529d18c11SMatt Spinler      *
45629d18c11SMatt Spinler      * @param[in] path - The file path to use
45729d18c11SMatt Spinler      * @param[in] updateFunc - The function to run to update the PEL.
45829d18c11SMatt Spinler      */
45929d18c11SMatt Spinler     void updatePEL(const std::filesystem::path& path, PELUpdateFunc updateFunc);
46029d18c11SMatt Spinler 
46189fa082aSMatt Spinler     /**
4620ff00485SMatt Spinler      * @brief Finds an entry in the _pelAttributes map.
463475e574dSMatt Spinler      *
464475e574dSMatt Spinler      * @param[in] id - the ID (either the pel ID, OBMC ID, or both)
465475e574dSMatt Spinler      *
466475e574dSMatt Spinler      * @return an iterator to the entry
467475e574dSMatt Spinler      */
4680ff00485SMatt Spinler     std::map<LogID, PELAttributes>::const_iterator
4690ff00485SMatt Spinler         findPEL(const LogID& id) const
470475e574dSMatt Spinler     {
4710ff00485SMatt Spinler         return std::find_if(_pelAttributes.begin(), _pelAttributes.end(),
4720ff00485SMatt Spinler                             [&id](const auto& a) { return a.first == id; });
473475e574dSMatt Spinler     }
474475e574dSMatt Spinler 
475475e574dSMatt Spinler     /**
476421f6531SMatt Spinler      * @brief Call any subscribed functions for new PELs
477421f6531SMatt Spinler      *
478421f6531SMatt Spinler      * @param[in] pel - The new PEL
479421f6531SMatt Spinler      */
480421f6531SMatt Spinler     void processAddCallbacks(const PEL& pel) const;
481421f6531SMatt Spinler 
482421f6531SMatt Spinler     /**
483421f6531SMatt Spinler      * @brief Call any subscribed functions for deleted PELs
484421f6531SMatt Spinler      *
485421f6531SMatt Spinler      * @param[in] id - The ID of the deleted PEL
486421f6531SMatt Spinler      */
487421f6531SMatt Spinler     void processDeleteCallbacks(uint32_t id) const;
488421f6531SMatt Spinler 
489421f6531SMatt Spinler     /**
4900ff00485SMatt Spinler      * @brief Restores the _pelAttributes map on startup based on the existing
491475e574dSMatt Spinler      *        PEL data files.
492475e574dSMatt Spinler      */
493475e574dSMatt Spinler     void restore();
494475e574dSMatt Spinler 
495475e574dSMatt Spinler     /**
496ab1b97feSMatt Spinler      * @brief Stores a PEL object in the filesystem.
497ab1b97feSMatt Spinler      *
498ab1b97feSMatt Spinler      * @param[in] pel - The PEL to write
499ab1b97feSMatt Spinler      * @param[in] path - The file to write to
500ab1b97feSMatt Spinler      *
501ab1b97feSMatt Spinler      * Throws exceptions on failures.
502ab1b97feSMatt Spinler      */
503ab1b97feSMatt Spinler     void write(const PEL& pel, const std::filesystem::path& path);
504ab1b97feSMatt Spinler 
505ab1b97feSMatt Spinler     /**
506b188f78aSMatt Spinler      * @brief Updates the repository statistics after a PEL is
507b188f78aSMatt Spinler      *        added or removed.
508b188f78aSMatt Spinler      *
509b188f78aSMatt Spinler      * @param[in] pel - The PELAttributes entry for the PEL
510b188f78aSMatt Spinler      * @param[in] pelAdded - true if the PEL was added, false if removed
511b188f78aSMatt Spinler      */
512b188f78aSMatt Spinler     void updateRepoStats(const PELAttributes& pel, bool pelAdded);
513b188f78aSMatt Spinler 
514b0a8df5bSMatt Spinler     enum class SortOrder
515b0a8df5bSMatt Spinler     {
516b0a8df5bSMatt Spinler         ascending,
517b0a8df5bSMatt Spinler         descending
518b0a8df5bSMatt Spinler     };
519b0a8df5bSMatt Spinler 
520b0a8df5bSMatt Spinler     /**
521b0a8df5bSMatt Spinler      * @brief Returns a vector of all the _pelAttributes entries sorted
522b0a8df5bSMatt Spinler      *        as specified
523b0a8df5bSMatt Spinler      *
524b0a8df5bSMatt Spinler      * @param[in] order - If the PELs should be returned in ascending
525b0a8df5bSMatt Spinler      *                    (oldest first) or descending order.
526b0a8df5bSMatt Spinler      *
527b0a8df5bSMatt Spinler      * @return std::vector<AttributesReference> - The sorted vector of
528b0a8df5bSMatt Spinler      *         references to the pair<LogID, PELAttributes> entries of
529b0a8df5bSMatt Spinler      *         _pelAttributes.
530b0a8df5bSMatt Spinler      */
531b0a8df5bSMatt Spinler     std::vector<AttributesReference> getAllPELAttributes(SortOrder order) const;
532b0a8df5bSMatt Spinler 
533b0a8df5bSMatt Spinler     using IsOverLimitFunc = std::function<bool()>;
534b0a8df5bSMatt Spinler     using IsPELTypeFunc = std::function<bool(const PELAttributes&)>;
535b0a8df5bSMatt Spinler 
536b0a8df5bSMatt Spinler     /**
537b0a8df5bSMatt Spinler      * @brief Makes 4 passes on the PELs that meet the IsPELTypeFunc
538b0a8df5bSMatt Spinler      *        criteria removing PELs until IsOverLimitFunc returns false.
539b0a8df5bSMatt Spinler      *
540b0a8df5bSMatt Spinler      *   Pass 1: only delete HMC acked PELs
541b0a8df5bSMatt Spinler      *   Pass 2: only delete Os acked PELs
542b0a8df5bSMatt Spinler      *   Pass 3: only delete PHYP sent PELs
543b0a8df5bSMatt Spinler      *   Pass 4: delete all PELs
544b0a8df5bSMatt Spinler      *
545b0a8df5bSMatt Spinler      * @param[in] isOverLimit - The bool(void) function that should
546b0a8df5bSMatt Spinler      *                          return true if PELs still need to be
547b0a8df5bSMatt Spinler      *                           removed.
548b0a8df5bSMatt Spinler      * @param[in] isPELType - The bool(const PELAttributes&) function
549b0a8df5bSMatt Spinler      *                         used to select the PELs to operate on.
550027bf285SSumit Kumar      * @param[in] ids - The OpenBMC event log Ids with hardware isolation
551027bf285SSumit Kumar      *                   entry.
552b0a8df5bSMatt Spinler      *
553b0a8df5bSMatt Spinler      * @param[out] removedBMCLogIDs - The OpenBMC event log IDs of the
554b0a8df5bSMatt Spinler      *                                removed PELs.
555b0a8df5bSMatt Spinler      */
55645796e82SMatt Spinler     void removePELs(const IsOverLimitFunc& isOverLimit,
55745796e82SMatt Spinler                     const IsPELTypeFunc& isPELType,
558027bf285SSumit Kumar                     const std::vector<uint32_t>& idsWithHwIsoEntry,
559b0a8df5bSMatt Spinler                     std::vector<uint32_t>& removedBMCLogIDs);
560b188f78aSMatt Spinler     /**
56189fa082aSMatt Spinler      * @brief The filesystem path to the PEL logs.
56289fa082aSMatt Spinler      */
56389fa082aSMatt Spinler     const std::filesystem::path _logPath;
564475e574dSMatt Spinler 
565475e574dSMatt Spinler     /**
5660ff00485SMatt Spinler      * @brief A map of the PEL/OBMC IDs to PEL attributes.
567475e574dSMatt Spinler      */
5680ff00485SMatt Spinler     std::map<LogID, PELAttributes> _pelAttributes;
569421f6531SMatt Spinler 
570421f6531SMatt Spinler     /**
571421f6531SMatt Spinler      * @brief Subcriptions for new PELs.
572421f6531SMatt Spinler      */
573421f6531SMatt Spinler     std::map<std::string, AddCallback> _addSubscriptions;
574421f6531SMatt Spinler 
575421f6531SMatt Spinler     /**
576421f6531SMatt Spinler      * @brief Subscriptions for deleted PELs.
577421f6531SMatt Spinler      */
578421f6531SMatt Spinler     std::map<std::string, DeleteCallback> _deleteSubscriptions;
5798d5f3a2bSMatt Spinler 
5808d5f3a2bSMatt Spinler     /**
5818d5f3a2bSMatt Spinler      * @brief The maximum amount of space that the PELs in the
5828d5f3a2bSMatt Spinler      *        repository can occupy.
5838d5f3a2bSMatt Spinler      */
5848d5f3a2bSMatt Spinler     const uint64_t _maxRepoSize;
5858d5f3a2bSMatt Spinler 
5868d5f3a2bSMatt Spinler     /**
5878d5f3a2bSMatt Spinler      * @brief The maximum number of PELs to allow in the repo
5888d5f3a2bSMatt Spinler      *        before pruning.
5898d5f3a2bSMatt Spinler      */
5908d5f3a2bSMatt Spinler     const size_t _maxNumPELs;
591b188f78aSMatt Spinler 
592b188f78aSMatt Spinler     /**
593b188f78aSMatt Spinler      * @brief Statistics on the sizes of the stored PELs.
594b188f78aSMatt Spinler      */
595b188f78aSMatt Spinler     SizeStats _sizes;
59644893cc9SMatt Spinler 
59744893cc9SMatt Spinler     /**
59844893cc9SMatt Spinler      * @brief The ID of the most recently added PEL.
59944893cc9SMatt Spinler      */
60044893cc9SMatt Spinler     uint32_t _lastPelID = 0;
6011d8835bbSSumit Kumar 
6021d8835bbSSumit Kumar     /**
6031d8835bbSSumit Kumar      * @brief The filesystem path to the archive PEL logs.
6041d8835bbSSumit Kumar      */
6051d8835bbSSumit Kumar     const std::filesystem::path _archivePath;
6061d8835bbSSumit Kumar 
6071d8835bbSSumit Kumar     /**
6081d8835bbSSumit Kumar      * @brief The size of archive folder.
6091d8835bbSSumit Kumar      */
6101d8835bbSSumit Kumar     uint64_t _archiveSize = 0;
61189fa082aSMatt Spinler };
61289fa082aSMatt Spinler 
61389fa082aSMatt Spinler } // namespace pels
61489fa082aSMatt Spinler } // namespace openpower
615