#include <iostream>
#include <thread>
#include <chrono>
#include <iomanip>
using namespace std::chrono_literals;
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;
}
}
std::cout << "\n=== Uptime Metrics ===" << std::endl;
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;
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;
}
}
std::cout << "\n=== Context Switch Statistics ===" << std::endl;
std::cout <<
"Total Switches: " << switches.
total_switches << std::endl;
std::cout << "Switches Per Second: " << std::fixed << std::setprecision(2)
} else {
std::cout << "Context switch statistics not available on this platform" << std::endl;
}
}
std::cout << "\n=== TCP Connection States ===" << std::endl;
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;
}
}
std::cout << "\n=== Socket Buffer Information ===" << std::endl;
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)
<< "%)" << std::endl;
std::cout <<
"TX Buffer Used: " << (socket.
tx_buffer_used / 1024.0) <<
" KB ("
<< "%)" << std::endl;
} else {
std::cout << "Socket buffer information not available on this platform" << std::endl;
}
}
std::cout << "\n=== Interrupt Statistics ===" << std::endl;
} else {
std::cout << "Interrupt statistics not available on this platform" << std::endl;
}
}
std::cout << "\n=== Platform-Specific Feature Detection ===" << std::endl;
std::cout <<
"\nDetected Platform: " <<
info.name << std::endl;
std::cout << "\nFeature Availability:" << 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
}
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;
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;
}
}
std::cout << "=== Platform Metrics Example ===" << std::endl;
try {
std::cout << "\n1. Creating platform_metrics_collector..." << std::endl;
std::unordered_map<std::string, std::string> 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;
std::cout << "\n2. Retrieving platform information..." << std::endl;
std::cout << "\n3. Demonstrating platform-specific features..." << std::endl;
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;
auto metrics = collector.
collect();
std::cout << "Metrics collected: " << metrics.size() << std::endl;
if (i < 2) {
std::cout << "\nWaiting 2 seconds before next collection..." << std::endl;
std::this_thread::sleep_for(2s);
}
}
std::cout << "\n5. Demonstrating cross-platform metric normalization..." << std::endl;
auto final_metrics = collector.
collect();
std::cout << "\n6. Collector Statistics:" << std::endl;
for (const auto& [key, value] : stats) {
std::cout << " " << key << ": " << value << std::endl;
}
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;
}
@ 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.