-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_metrics_example.cpp
More file actions
309 lines (259 loc) · 12.4 KB
/
platform_metrics_example.cpp
File metadata and controls
309 lines (259 loc) · 12.4 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
/**
* @file platform_metrics_example.cpp
* @brief Demonstrates platform_metrics_collector usage
* @example platform_metrics_example.cpp
*
* This example shows how to use the unified platform metrics collector:
* - platform_metrics_collector initialization
* - Platform-specific metric access patterns
* - Strategy pattern usage for OS abstraction
* - Handling platform-specific features gracefully
* - Cross-platform metric normalization
*/
#include <iostream>
#include <thread>
#include <chrono>
#include <iomanip>
#include "kcenon/monitoring/collectors/platform_metrics_collector.h"
using namespace kcenon::monitoring;
using namespace std::chrono_literals;
/**
* Display platform information
*/
void display_platform_info(const platform_info& info) {
std::cout << "\n=== Platform Information ===" << std::endl;
if (info.available) {
std::cout << "Platform: " << info.name << std::endl;
std::cout << "Version: " << info.version << std::endl;
std::cout << "Architecture: " << info.architecture << std::endl;
} else {
std::cout << "Platform information not available" << std::endl;
}
}
/**
* Display uptime metrics
*/
void display_uptime_metrics(const platform_uptime& uptime) {
std::cout << "\n=== Uptime Metrics ===" << std::endl;
if (uptime.available) {
// Convert seconds to days, hours, minutes
int64_t seconds = uptime.uptime_seconds;
int64_t days = seconds / (24 * 3600);
seconds %= (24 * 3600);
int64_t hours = seconds / 3600;
seconds %= 3600;
int64_t minutes = seconds / 60;
seconds %= 60;
std::cout << "System Uptime: " << days << "d " << hours << "h "
<< minutes << "m " << seconds << "s" << std::endl;
std::cout << "Total Uptime: " << uptime.uptime_seconds << " seconds" << std::endl;
std::cout << "Idle Time: " << uptime.idle_seconds << " seconds" << std::endl;
// Display boot timestamp as ISO 8601 format if available
if (uptime.boot_timestamp > 0) {
auto boot_time = std::chrono::system_clock::from_time_t(uptime.boot_timestamp);
auto boot_tt = std::chrono::system_clock::to_time_t(boot_time);
std::tm boot_tm{};
#ifdef _MSC_VER
localtime_s(&boot_tm, &boot_tt);
#else
localtime_r(&boot_tt, &boot_tm);
#endif
std::cout << "Boot Time: " << std::put_time(&boot_tm, "%Y-%m-%d %H:%M:%S") << std::endl;
}
} else {
std::cout << "Uptime metrics not available on this platform" << std::endl;
}
}
/**
* Display context switch statistics
*/
void display_context_switch_stats(const platform_context_switches& switches) {
std::cout << "\n=== Context Switch Statistics ===" << std::endl;
if (switches.available) {
std::cout << "Total Switches: " << switches.total_switches << std::endl;
std::cout << "Voluntary Switches: " << switches.voluntary_switches << std::endl;
std::cout << "Involuntary Switches: " << switches.involuntary_switches << std::endl;
std::cout << "Switches Per Second: " << std::fixed << std::setprecision(2)
<< switches.switches_per_second << std::endl;
} else {
std::cout << "Context switch statistics not available on this platform" << std::endl;
}
}
/**
* Display TCP connection state information
*/
void display_tcp_info(const platform_tcp_info& tcp) {
std::cout << "\n=== TCP Connection States ===" << std::endl;
if (tcp.available) {
std::cout << "ESTABLISHED: " << tcp.established << std::endl;
std::cout << "SYN_SENT: " << tcp.syn_sent << std::endl;
std::cout << "SYN_RECV: " << tcp.syn_recv << std::endl;
std::cout << "FIN_WAIT1: " << tcp.fin_wait1 << std::endl;
std::cout << "FIN_WAIT2: " << tcp.fin_wait2 << std::endl;
std::cout << "TIME_WAIT: " << tcp.time_wait << std::endl;
std::cout << "CLOSE_WAIT: " << tcp.close_wait << std::endl;
std::cout << "LISTEN: " << tcp.listen << std::endl;
std::cout << "Total Connections: " << tcp.total << std::endl;
} else {
std::cout << "TCP state information not available on this platform" << std::endl;
}
}
/**
* Display socket buffer information
*/
void display_socket_info(const platform_socket_info& socket) {
std::cout << "\n=== Socket Buffer Information ===" << std::endl;
if (socket.available) {
std::cout << "RX Buffer Size: " << (socket.rx_buffer_size / 1024.0) << " KB" << std::endl;
std::cout << "TX Buffer Size: " << (socket.tx_buffer_size / 1024.0) << " KB" << std::endl;
std::cout << "RX Buffer Used: " << (socket.rx_buffer_used / 1024.0) << " KB ("
<< std::fixed << std::setprecision(1)
<< (socket.rx_buffer_size > 0 ? (socket.rx_buffer_used * 100.0 / socket.rx_buffer_size) : 0.0)
<< "%)" << std::endl;
std::cout << "TX Buffer Used: " << (socket.tx_buffer_used / 1024.0) << " KB ("
<< (socket.tx_buffer_size > 0 ? (socket.tx_buffer_used * 100.0 / socket.tx_buffer_size) : 0.0)
<< "%)" << std::endl;
} else {
std::cout << "Socket buffer information not available on this platform" << std::endl;
}
}
/**
* Display interrupt statistics
*/
void display_interrupt_info(const platform_interrupt_info& interrupts) {
std::cout << "\n=== Interrupt Statistics ===" << std::endl;
if (interrupts.available) {
std::cout << "Total Interrupts: " << interrupts.total_interrupts << std::endl;
} else {
std::cout << "Interrupt statistics not available on this platform" << std::endl;
}
}
/**
* Demonstrate platform-specific feature detection and handling
*/
void demonstrate_platform_features(platform_metrics_collector& collector) {
std::cout << "\n=== Platform-Specific Feature Detection ===" << std::endl;
// Collect platform info first
auto info = collector.get_platform_info();
std::cout << "\nDetected Platform: " << info.name << std::endl;
// Demonstrate feature availability checks before collection
std::cout << "\nFeature Availability:" << std::endl;
// Display feature availability
std::cout << " Platform Available: " << (collector.is_platform_available() ? "Yes" : "No") << std::endl;
std::cout << " Collector Health: " << (collector.is_available() ? "Healthy" : "Unhealthy") << std::endl;
std::cout << "\nNote: The Strategy pattern abstracts platform-specific implementations." << std::endl;
std::cout << " Features not supported on a platform return empty/unavailable values." << std::endl;
#if defined(__linux__)
std::cout << "\nLinux-specific features:" << std::endl;
std::cout << " - Reading /proc/stat for context switches" << std::endl;
std::cout << " - Reading /proc/net/tcp for TCP state info" << std::endl;
std::cout << " - Reading /proc/uptime for system uptime" << std::endl;
#elif defined(__APPLE__)
std::cout << "\nmacOS-specific features:" << std::endl;
std::cout << " - Using sysctl for system metrics" << std::endl;
std::cout << " - Limited TCP state information" << std::endl;
std::cout << " - Using kern.boottime for uptime" << std::endl;
#elif defined(_WIN32)
std::cout << "\nWindows-specific features:" << std::endl;
std::cout << " - Using GetTickCount64 for uptime" << std::endl;
std::cout << " - Using Performance Counters for metrics" << std::endl;
std::cout << " - Limited context switch information" << std::endl;
#else
std::cout << "\nUnknown platform - limited feature support" << std::endl;
#endif
}
/**
* Demonstrate cross-platform metric normalization
*/
void demonstrate_metric_normalization(const std::vector<metric>& metrics) {
std::cout << "\n=== Cross-Platform Metric Normalization ===" << std::endl;
std::cout << "\nAll metrics use standardized naming conventions:" << std::endl;
std::cout << " platform.uptime.* - Uptime metrics" << std::endl;
std::cout << " platform.context_switches.* - Context switch metrics" << std::endl;
std::cout << " platform.tcp.* - TCP state metrics" << std::endl;
std::cout << " platform.socket.* - Socket buffer metrics" << std::endl;
std::cout << " platform.interrupts.* - Interrupt metrics" << std::endl;
std::cout << "\nCollected Metrics (" << metrics.size() << " total):" << std::endl;
// Display metrics directly without grouping to avoid compilation issues
for (const auto& m : metrics) {
std::cout << " " << m.name << ": ";
std::visit([](const auto& val) { std::cout << val; }, m.value);
auto unit_it = m.tags.find("unit");
if (unit_it != m.tags.end() && !unit_it->second.empty()) {
std::cout << " " << unit_it->second;
}
std::cout << std::endl;
}
}
int main() {
std::cout << "=== Platform Metrics Example ===" << std::endl;
try {
// Step 1: Create platform_metrics_collector with configuration
std::cout << "\n1. Creating platform_metrics_collector..." << std::endl;
platform_metrics_config config;
config.collect_uptime = true;
config.collect_context_switches = true;
config.collect_tcp_states = true;
config.collect_socket_buffers = true;
config.collect_interrupts = true;
platform_metrics_collector collector(config);
// Initialize the collector
std::unordered_map<std::string, std::string> init_config;
if (!collector.initialize(init_config)) {
std::cerr << "Failed to initialize platform_metrics_collector" << std::endl;
return 1;
}
std::cout << " Initialized: " << collector.name() << std::endl;
std::cout << " Health: " << (collector.is_available() ? "OK" : "UNHEALTHY") << std::endl;
// Step 2: Display platform information
std::cout << "\n2. Retrieving platform information..." << std::endl;
auto platform_info = collector.get_platform_info();
display_platform_info(platform_info);
// Step 3: Demonstrate platform-specific feature detection
std::cout << "\n3. Demonstrating platform-specific features..." << std::endl;
demonstrate_platform_features(collector);
// Step 4: Collect and display metrics (3 iterations)
std::cout << "\n4. Collecting platform metrics (3 iterations)..." << std::endl;
for (int i = 0; i < 3; ++i) {
std::cout << "\n--- Iteration " << (i + 1) << "/3 ---" << std::endl;
// Collect all platform metrics
auto metrics = collector.collect();
std::cout << "Metrics collected: " << metrics.size() << std::endl;
// Get last collected metrics for detailed display
auto last_metrics = collector.get_last_metrics();
// Display each metric category
display_uptime_metrics(last_metrics.uptime);
display_context_switch_stats(last_metrics.context_switches);
display_tcp_info(last_metrics.tcp);
display_socket_info(last_metrics.socket);
display_interrupt_info(last_metrics.interrupts);
// Wait before next collection
if (i < 2) {
std::cout << "\nWaiting 2 seconds before next collection..." << std::endl;
std::this_thread::sleep_for(2s);
}
}
// Step 5: Demonstrate metric normalization
std::cout << "\n5. Demonstrating cross-platform metric normalization..." << std::endl;
auto final_metrics = collector.collect();
demonstrate_metric_normalization(final_metrics);
// Step 6: Display collector statistics
std::cout << "\n6. Collector Statistics:" << std::endl;
auto stats = collector.get_statistics();
for (const auto& [key, value] : stats) {
std::cout << " " << key << ": " << value << std::endl;
}
// Step 7: Demonstrate dynamic configuration updates
std::cout << "\n7. Configuration note:" << std::endl;
std::cout << " Platform metrics collector configuration is set at initialization." << std::endl;
std::cout << " To change configuration, recreate the collector with new config." << std::endl;
std::cout << "\n=== Example completed successfully ===" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}