26using namespace std::chrono_literals;
32 std::cout <<
"\n=== Platform Information ===" << std::endl;
35 std::cout <<
"Platform: " <<
info.name << std::endl;
36 std::cout <<
"Version: " <<
info.version << std::endl;
37 std::cout <<
"Architecture: " <<
info.architecture << std::endl;
39 std::cout <<
"Platform information not available" << std::endl;
47 std::cout <<
"\n=== Uptime Metrics ===" << std::endl;
52 int64_t days = seconds / (24 * 3600);
53 seconds %= (24 * 3600);
54 int64_t hours = seconds / 3600;
56 int64_t minutes = seconds / 60;
59 std::cout <<
"System Uptime: " << days <<
"d " << hours <<
"h "
60 << minutes <<
"m " << seconds <<
"s" << std::endl;
61 std::cout <<
"Total Uptime: " << uptime.
uptime_seconds <<
" seconds" << std::endl;
62 std::cout <<
"Idle Time: " << uptime.
idle_seconds <<
" seconds" << std::endl;
66 auto boot_time = std::chrono::system_clock::from_time_t(uptime.
boot_timestamp);
67 auto boot_tt = std::chrono::system_clock::to_time_t(boot_time);
70 localtime_s(&boot_tm, &boot_tt);
72 localtime_r(&boot_tt, &boot_tm);
74 std::cout <<
"Boot Time: " << std::put_time(&boot_tm,
"%Y-%m-%d %H:%M:%S") << std::endl;
77 std::cout <<
"Uptime metrics not available on this platform" << std::endl;
85 std::cout <<
"\n=== Context Switch Statistics ===" << std::endl;
88 std::cout <<
"Total Switches: " << switches.
total_switches << std::endl;
91 std::cout <<
"Switches Per Second: " << std::fixed << std::setprecision(2)
94 std::cout <<
"Context switch statistics not available on this platform" << std::endl;
102 std::cout <<
"\n=== TCP Connection States ===" << std::endl;
105 std::cout <<
"ESTABLISHED: " << tcp.
established << std::endl;
106 std::cout <<
"SYN_SENT: " << tcp.
syn_sent << std::endl;
107 std::cout <<
"SYN_RECV: " << tcp.
syn_recv << std::endl;
108 std::cout <<
"FIN_WAIT1: " << tcp.
fin_wait1 << std::endl;
109 std::cout <<
"FIN_WAIT2: " << tcp.
fin_wait2 << std::endl;
110 std::cout <<
"TIME_WAIT: " << tcp.
time_wait << std::endl;
111 std::cout <<
"CLOSE_WAIT: " << tcp.
close_wait << std::endl;
112 std::cout <<
"LISTEN: " << tcp.
listen << std::endl;
113 std::cout <<
"Total Connections: " << tcp.
total << std::endl;
115 std::cout <<
"TCP state information not available on this platform" << std::endl;
123 std::cout <<
"\n=== Socket Buffer Information ===" << std::endl;
126 std::cout <<
"RX Buffer Size: " << (socket.
rx_buffer_size / 1024.0) <<
" KB" << std::endl;
127 std::cout <<
"TX Buffer Size: " << (socket.
tx_buffer_size / 1024.0) <<
" KB" << std::endl;
128 std::cout <<
"RX Buffer Used: " << (socket.
rx_buffer_used / 1024.0) <<
" KB ("
129 << std::fixed << std::setprecision(1)
131 <<
"%)" << std::endl;
132 std::cout <<
"TX Buffer Used: " << (socket.
tx_buffer_used / 1024.0) <<
" KB ("
134 <<
"%)" << std::endl;
136 std::cout <<
"Socket buffer information not available on this platform" << std::endl;
144 std::cout <<
"\n=== Interrupt Statistics ===" << std::endl;
147 std::cout <<
"Total Interrupts: " << interrupts.
total_interrupts << std::endl;
149 std::cout <<
"Interrupt statistics not available on this platform" << std::endl;
157 std::cout <<
"\n=== Platform-Specific Feature Detection ===" << std::endl;
162 std::cout <<
"\nDetected Platform: " <<
info.
name << std::endl;
165 std::cout <<
"\nFeature Availability:" << std::endl;
168 std::cout <<
" Platform Available: " << (collector.
is_platform_available() ?
"Yes" :
"No") << std::endl;
169 std::cout <<
" Collector Health: " << (collector.
is_available() ?
"Healthy" :
"Unhealthy") << std::endl;
171 std::cout <<
"\nNote: The Strategy pattern abstracts platform-specific implementations." << std::endl;
172 std::cout <<
" Features not supported on a platform return empty/unavailable values." << std::endl;
174#if defined(__linux__)
175 std::cout <<
"\nLinux-specific features:" << std::endl;
176 std::cout <<
" - Reading /proc/stat for context switches" << std::endl;
177 std::cout <<
" - Reading /proc/net/tcp for TCP state info" << std::endl;
178 std::cout <<
" - Reading /proc/uptime for system uptime" << std::endl;
179#elif defined(__APPLE__)
180 std::cout <<
"\nmacOS-specific features:" << std::endl;
181 std::cout <<
" - Using sysctl for system metrics" << std::endl;
182 std::cout <<
" - Limited TCP state information" << std::endl;
183 std::cout <<
" - Using kern.boottime for uptime" << std::endl;
185 std::cout <<
"\nWindows-specific features:" << std::endl;
186 std::cout <<
" - Using GetTickCount64 for uptime" << std::endl;
187 std::cout <<
" - Using Performance Counters for metrics" << std::endl;
188 std::cout <<
" - Limited context switch information" << std::endl;
190 std::cout <<
"\nUnknown platform - limited feature support" << std::endl;
198 std::cout <<
"\n=== Cross-Platform Metric Normalization ===" << std::endl;
200 std::cout <<
"\nAll metrics use standardized naming conventions:" << std::endl;
201 std::cout <<
" platform.uptime.* - Uptime metrics" << std::endl;
202 std::cout <<
" platform.context_switches.* - Context switch metrics" << std::endl;
203 std::cout <<
" platform.tcp.* - TCP state metrics" << std::endl;
204 std::cout <<
" platform.socket.* - Socket buffer metrics" << std::endl;
205 std::cout <<
" platform.interrupts.* - Interrupt metrics" << std::endl;
207 std::cout <<
"\nCollected Metrics (" << metrics.size() <<
" total):" << std::endl;
210 for (
const auto& m : metrics) {
211 std::cout <<
" " << m.name <<
": ";
212 std::visit([](
const auto& val) { std::cout << val; }, m.value);
213 auto unit_it = m.tags.find(
"unit");
214 if (unit_it != m.tags.end() && !unit_it->second.empty()) {
215 std::cout <<
" " << unit_it->second;
217 std::cout << std::endl;
222 std::cout <<
"=== Platform Metrics Example ===" << std::endl;
226 std::cout <<
"\n1. Creating platform_metrics_collector..." << std::endl;
238 std::unordered_map<std::string, std::string> init_config;
240 std::cerr <<
"Failed to initialize platform_metrics_collector" << std::endl;
244 std::cout <<
" Initialized: " << collector.
name() << std::endl;
245 std::cout <<
" Health: " << (collector.
is_available() ?
"OK" :
"UNHEALTHY") << std::endl;
248 std::cout <<
"\n2. Retrieving platform information..." << std::endl;
253 std::cout <<
"\n3. Demonstrating platform-specific features..." << std::endl;
257 std::cout <<
"\n4. Collecting platform metrics (3 iterations)..." << std::endl;
259 for (
int i = 0; i < 3; ++i) {
260 std::cout <<
"\n--- Iteration " << (i + 1) <<
"/3 ---" << std::endl;
263 auto metrics = collector.
collect();
264 std::cout <<
"Metrics collected: " << metrics.size() << std::endl;
278 std::cout <<
"\nWaiting 2 seconds before next collection..." << std::endl;
279 std::this_thread::sleep_for(2s);
284 std::cout <<
"\n5. Demonstrating cross-platform metric normalization..." << std::endl;
285 auto final_metrics = collector.
collect();
289 std::cout <<
"\n6. Collector Statistics:" << std::endl;
292 for (
const auto& [key, value] : stats) {
293 std::cout <<
" " << key <<
": " << value << std::endl;
297 std::cout <<
"\n7. Configuration note:" << std::endl;
298 std::cout <<
" Platform metrics collector configuration is set at initialization." << std::endl;
299 std::cout <<
" To change configuration, recreate the collector with new config." << std::endl;
301 std::cout <<
"\n=== Example completed successfully ===" << std::endl;
303 }
catch (
const std::exception& e) {
304 std::cerr <<
"Exception: " << e.what() << std::endl;
@ info
Informational, no action required.
Platform context switch statistics.
uint64_t total_switches
Total context switches.
bool available
Whether info is available.
double switches_per_second
Context switches per second.
uint64_t voluntary_switches
Voluntary context switches.
uint64_t involuntary_switches
Involuntary context switches.