Files
externals/linux/include/ActivMiddleware/ActivBase/SystemMonitor.h
T
2023-12-15 13:15:08 -05:00

150 lines
4.5 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/SystemMonitor.h
*
* @brief Header file for the system monitor class.
*/
#if (!defined ACTIV_BASE_SYSTEM_MONITOR_H)
#define ACTIV_BASE_SYSTEM_MONITOR_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/ThreadUtils.h"
#include "ActivMiddleware/ActivBase/Process.h"
#include ACTIV_BOOST_INCLUDE(scoped_ptr.hpp)
#include <vector>
#include <map>
//! Activ namespace.
namespace Activ
{
/**
* @brief System monitor class.
*
* Monitors cpu usage, memory, disk usage, context switching and paging.
*
* Note this class is not thread aware - it is not safe to call the methods from different threads
* without some user-provided locking.
*/
class SystemMonitor : private Boost::noncopyable
{
public:
//! Collected stats.
class Stats
{
public:
//! Per-disk info.
class DiskInfo
{
public:
String m_diskName; ///< Name of disk.
uint8_t m_usagePercentage; ///< Usage % of disk.
};
//! Per-thread info.
class ThreadInfo
{
public:
ThreadUtils::OsId m_threadId; ///< Os id of the thread.
uint8_t m_cpuUsage; ///< Cpu usage monitored in last interval.
uint8_t m_userCpuUsage; ///< User cpu usage monitored in last interval.
uint8_t m_systemCpuUsage; ///< System cpu usage monitored in last interval.
uint32_t m_contextSwitchRate; ///< Number of context switches in the last interval.
uint32_t m_forcedContextSwitchRate; ///< Number of forced context switches in the last interval.
};
typedef std::vector<ThreadInfo> ThreadInfoList;
//! Per-process info.
class ProcessInfo
{
public:
String m_name; ///< Process name.
uint8_t m_averageCpuUsage; ///< Average % cpu usage across all threads in this process.
uint8_t m_highestThreadCpuUsage; ///< Highest % cpu usage across all threads in this process.
uint32_t m_highestThreadCpuUsageId; ///< Id of thread with highest cpu usage in this process.
uint64_t m_virtualMemorySize; ///< VM size of this process. NB: not populated on Windows.
uint64_t m_workingSetSize; ///< Working set size of this process.
uint64_t m_numberOfPageFaults;
ThreadInfoList m_threadInfoList; ///< List of threads in the process.
};
//! Default constructor.
ACTIV_BASE_API Stats();
typedef std::vector<uint8_t> CpuUsageList;
typedef std::map<Process::Id, ProcessInfo> ProcessInfoMap;
typedef std::vector<DiskInfo> DiskInfoList;
CpuUsageList m_cpuUsageList; ///< List of total cpu usage percentages per cpu.
uint8_t m_averageCpuUsage; ///< Average usage across all cpus.
uint8_t m_peakCpuUsage; ///< Peak usage across all cpus.
ProcessInfoMap m_processInfoMap; ///< Map of per-process info.
DiskInfoList m_diskInfoList; ///< List of disk usages.
uint32_t m_contextSwitchRate; ///< Context switches / sec.
uint32_t m_pagesInOutRate; ///< Hard or major page faults / sec.
uint64_t m_swapUsedBytes; ///< Swap used bytes.
uint8_t m_freeMemoryPercent; ///< % of memory free.
uint64_t m_packetReceiveErrors; ///< Socket packet receive errors.
};
/**
* @brief Constructor.
*
* @param diskNameList list of disks to watch. If empty, watch the disk containing the current working directory.
*
* @throw Exception.
*/
ACTIV_BASE_API SystemMonitor(const std::vector<String> &diskNameList = std::vector<String>());
/**
* @brief Destructor.
*/
ACTIV_BASE_API ~SystemMonitor();
/**
* @brief Add an individual process to the watch.
*
* @param processId os-pid of the process to monitor.
* @param name optional name of the process, defaults to executable name.
*
* @return STATUS_CODE_SUCCESS or error code.
*/
ACTIV_BASE_API StatusCode AddProcess(const Process::Id processId, const String &name = String());
/**
* @brief Remove an individual process from the watch.
*
* @param processId os-pid of the process to monitor.
*/
ACTIV_BASE_API void RemoveProcess(const Process::Id processId);
/**
* @brief Collate stats.
*
* This should be called on a timer with a regular interval. GetStats() can then be called
* at will, and will return the state at the point CollateStats() was last called.
*/
ACTIV_BASE_API void CollateStats();
/**
* @brief Get the stats at the point CollateStats() was last called.
*
* @return Stats reference.
*/
ACTIV_BASE_API const Stats &GetStats();
private:
class Impl;
Boost::scoped_ptr<Impl> m_pImpl;
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_SYSTEM_MONITOR_H)