| #pragma once |
| |
| #include "node_config.hpp" |
| #include "resource.hpp" |
| |
| #include <map> |
| #include <memory> |
| #include <string_view> |
| |
| namespace boot_time_monitor |
| { |
| |
| namespace api |
| { |
| |
| namespace btm = boot_time_monitor; |
| |
| /** |
| * @brief Interface for managing boot time monitoring data for different nodes. |
| * |
| * This interface defines the contract for setting and retrieving boot |
| * checkpoints and durations for specific nodes or groups of nodes identified |
| * by tags. It also handles notifications for boot completion and provides |
| * methods to check the reboot status of a node. |
| */ |
| class IApi |
| { |
| public: |
| virtual ~IApi() = default; |
| |
| /** |
| * @brief Sets a boot checkpoint for a specific node. |
| * |
| * @param nodeConfig The configuration of the target node. |
| * @param checkpointName The name of the checkpoint. |
| * @param wallTime The wall clock time (epoch time in ms) when the |
| * checkpoint was reached. If 0, the current time is used. |
| * @param selfMeasuredDuration The duration (in ms) measured by the |
| * component reporting the checkpoint. If |
| * non-zero, an additional checkpoint marking the beginning of this duration |
| * might be recorded. |
| */ |
| virtual void SetNodeCheckpoint(const btm::NodeConfig& nodeConfig, |
| std::string_view checkpointName, |
| int64_t wallTime, |
| int64_t selfMeasuredDuration) = 0; |
| |
| /** |
| * @brief Sets a boot checkpoint for all nodes matching a specific tag. |
| * |
| * @param tag The tag to identify the target nodes. |
| * @param checkpointName The name of the checkpoint. |
| * @param wallTime The wall clock time (epoch time in ms) when the |
| * checkpoint was reached. If 0, the current time is used. |
| * @param selfMeasuredDuration The duration (in ms) measured by the |
| * component reporting the checkpoint. |
| */ |
| virtual void SetNodesCheckpointByTag(std::string_view tag, |
| std::string_view checkpointName, |
| int64_t wallTime, |
| int64_t selfMeasuredDuration) = 0; |
| |
| /** |
| * @brief Sets a boot duration measurement for a specific node. |
| * |
| * @param nodeConfig The configuration of the target node. |
| * @param durationName The name of the duration measurement. |
| * @param duration The measured duration in milliseconds. |
| */ |
| virtual void SetNodeDuration(const btm::NodeConfig& nodeConfig, |
| std::string_view durationName, |
| int64_t duration) = 0; |
| |
| /** |
| * @brief Sets a boot duration measurement for all nodes matching a specific |
| * tag. |
| * |
| * @param tag The tag to identify the target nodes. |
| * @param durationName The name of the duration measurement. |
| * @param duration The measured duration in milliseconds. |
| */ |
| virtual void SetNodesDurationByTag(std::string_view tag, |
| std::string_view durationName, |
| int64_t duration) = 0; |
| |
| /** |
| * @brief Sets a Power Supply Unit (PSU) related boot checkpoint for all |
| * nodes. PSU events are typically broadcast to all nodes. |
| * |
| * @param checkpointName The name of the PSU checkpoint. |
| * @param wallTime The wall clock time (epoch time in ms). |
| * @param selfMeasuredDuration The self-measured duration (in ms). |
| */ |
| virtual void SetPSUCheckpoint(std::string_view checkpointName, |
| int64_t wallTime, |
| int64_t selfMeasuredDuration) = 0; |
| |
| /** |
| * @brief Sets a Power Supply Unit (PSU) related boot duration for all |
| * nodes. PSU events are typically broadcast to all nodes. |
| * |
| * @param durationName The name of the PSU duration. |
| * @param duration The measured duration in milliseconds. |
| */ |
| virtual void SetPSUDuration(std::string_view durationName, |
| int64_t duration) = 0; |
| |
| /** |
| * @brief Notifies that the boot process for a specific node is complete. |
| * This typically involves finalizing the current boot record. |
| * |
| * @param nodeConfig The configuration of the completed node. |
| */ |
| virtual void NotifyNodeComplete(const btm::NodeConfig& nodeConfig) = 0; |
| |
| /** |
| * @brief Notifies that the boot process for all nodes matching a specific |
| * tag is complete. |
| * |
| * @param tag The tag identifying the completed nodes. |
| */ |
| virtual void NotifyNodesCompleteByTag(std::string_view tag) = 0; |
| |
| /** |
| * @brief Retrieves the list of recorded checkpoints for a specific node. |
| * |
| * @param nodeConfig The configuration of the target node. |
| * @return A vector of tuples, each containing (checkpoint name, wall time |
| * ms, monotonic time ms). |
| */ |
| virtual std::vector<std::tuple<std::string, int64_t, int64_t>> |
| GetNodeCheckpointList(const btm::NodeConfig& nodeConfig) = 0; |
| |
| /** |
| * @brief Retrieves the list of recorded additional durations for a specific |
| * node. |
| * |
| * @param nodeConfig The configuration of the target node. |
| * @return A vector of tuples, each containing (duration name, duration ms). |
| */ |
| virtual std::vector<std::tuple<std::string, int64_t>> |
| GetNodeAdditionalDurations(const btm::NodeConfig& nodeConfig) = 0; |
| |
| /** |
| * @brief Checks if a specific node is currently in the process of |
| * rebooting. |
| * |
| * @param nodeConfig The configuration of the target node. |
| * @return True if the node is considered to be rebooting, false otherwise. |
| */ |
| virtual bool IsNodeRebooting(const btm::NodeConfig& nodeConfig) = 0; |
| }; |
| |
| /** |
| * @brief Concrete implementation of the IApi interface. |
| * |
| * This class manages multiple `IResource` instances (one per node) |
| * and provides thread-safe access to them via a mutex. It acts as the central |
| * point for interacting with boot time data storage. |
| */ |
| class Api : public IApi |
| { |
| public: |
| virtual ~Api() override; |
| void SetNodeCheckpoint(const btm::NodeConfig& nodeConfig, |
| std::string_view checkpointName, int64_t wallTime, |
| int64_t selfMeasuredDuration) override; |
| void SetNodesCheckpointByTag(std::string_view tag, |
| std::string_view checkpointName, |
| int64_t wallTime, |
| int64_t selfMeasuredDuration) override; |
| void SetNodeDuration(const btm::NodeConfig& nodeConfig, |
| std::string_view durationName, |
| int64_t duration) override; |
| void SetNodesDurationByTag(std::string_view tag, |
| std::string_view durationName, |
| int64_t duration) override; |
| void SetPSUCheckpoint(std::string_view checkpointName, int64_t wallTime, |
| int64_t selfMeasuredDuration) override; |
| void SetPSUDuration(std::string_view durationName, |
| int64_t duration) override; |
| void NotifyNodeComplete(const btm::NodeConfig& nodeConfig) override; |
| void NotifyNodesCompleteByTag(std::string_view tag) override; |
| std::vector<std::tuple<std::string, int64_t, int64_t>> |
| GetNodeCheckpointList(const btm::NodeConfig& nodeConfig) override; |
| std::vector<std::tuple<std::string, int64_t>> |
| GetNodeAdditionalDurations(const btm::NodeConfig& nodeConfig) override; |
| bool IsNodeRebooting(const btm::NodeConfig& nodeConfig) override; |
| |
| /** |
| * @brief Registers a new node with the API. |
| * |
| * This creates a corresponding resource handler (e.g., a File resource) |
| * for the given node configuration. |
| * |
| * @param nodeConfig The configuration of the node to register. |
| */ |
| void RegisterNode(const btm::NodeConfig& nodeConfig); |
| |
| private: |
| // Map storing the resource handler for each registered node. |
| std::map<NodeConfig, std::unique_ptr<resource::IResource>> mResourceMap; |
| std::mutex mApiMutex; // An api mutex is required for protecting |
| // misordering of the record. |
| }; |
| } // namespace api |
| } // namespace boot_time_monitor |