-
Notifications
You must be signed in to change notification settings - Fork 3
/
system_monitor.hpp
155 lines (130 loc) · 3.96 KB
/
system_monitor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#include <atomic>
#include <cstdint>
#include <memory>
namespace fly::task {
class SequencedTaskRunner;
} // namespace fly::task
namespace fly::system {
class SystemConfig;
/**
* Virtual interface for monitoring system-level resources. Provides CPU and memory monitoring. This
* interface is platform independent - OS dependent implementations should inherit from this class.
*
* @author Timothy Flynn (trflynn89@pm.me)
* @version September 15, 2017
*/
class SystemMonitor : public std::enable_shared_from_this<SystemMonitor>
{
public:
/**
* Create and start a system monitor.
*
* @param task_runner Task runner for posting monitor-related tasks onto.
* @param config Reference to system system monitor
*
* @return The created system monitor.
*/
static std::shared_ptr<SystemMonitor> create(
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<SystemConfig> config);
/**
* Destructor.
*/
virtual ~SystemMonitor() = default;
/**
* Get the system's CPU count.
*
* @return System CPU count.
*/
std::uint32_t get_system_cpu_count() const;
/**
* Get the system's CPU usage percentage (0-100%) over the last second.
*
* @return Current system CPU usage.
*/
double get_system_cpu_usage() const;
/**
* Get the process's CPU usage percentage (0-100%) over the last second.
*
* @return Current process CPU usage.
*/
double get_process_cpu_usage() const;
/**
* Get the system's total physical memory available in bytes.
*
* @return Total system memory.
*/
std::uint64_t get_total_system_memory() const;
/**
* Get the system's physical memory usage in bytes.
*
* @return Current system memory usage.
*/
std::uint64_t get_system_memory_usage() const;
/**
* Get the process's physical memory usage in bytes.
*
* @return Current process memory usage.
*/
std::uint64_t get_process_memory_usage() const;
protected:
/**
* Constructor.
*
* @param task_runner Task runner for posting monitor-related tasks onto.
* @param config Reference to system configuration.
*/
SystemMonitor(
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<SystemConfig> config) noexcept;
/**
* Update the system's current CPU count.
*/
virtual void update_system_cpu_count() = 0;
/**
* Update the system's current CPU usage.
*/
virtual void update_system_cpu_usage() = 0;
/**
* Update the process's current CPU usage.
*/
virtual void update_process_cpu_usage() = 0;
/**
* Update the system's current memory usage.
*/
virtual void update_system_memory_usage() = 0;
/**
* Update the process's current memory usage.
*/
virtual void update_process_memory_usage() = 0;
std::atomic<std::uint32_t> m_system_cpu_count {0};
std::atomic<double> m_system_cpu_usage {0.0};
std::atomic<double> m_process_cpu_usage {0.0};
std::atomic<std::uint64_t> m_total_system_memory {0};
std::atomic<std::uint64_t> m_system_memory_usage {0};
std::atomic<std::uint64_t> m_process_memory_usage {0};
private:
/**
* Queue a task to poll system-level resources.
*
* @return True if the system monitor is in a valid state.
*/
bool start();
/**
* Check if the system CPU count was successfully set.
*
* @return True if the CPU count is valid.
*/
bool is_valid() const;
/**
* Queue a task to poll system-level resources. When the task is completed, it re-arms itself
* (if the system monitor is still in a valid state).
*
* @return True if task was able to be queued.
*/
bool poll_system_later();
std::shared_ptr<fly::task::SequencedTaskRunner> m_task_runner;
std::shared_ptr<SystemConfig> m_config;
};
} // namespace fly::system