| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835 |
- #include "utils_performance_monitor.h"
- #include "../base/base_logger.h"
- #include <algorithm>
- #include <sstream>
- #include <fstream>
- #include <iomanip>
- #include <thread>
- #include <cmath>
- #ifdef _WIN32
- #include <windows.h>
- #include <psapi.h>
- #include <pdh.h>
- #pragma comment(lib, "pdh.lib")
- #pragma comment(lib, "psapi.lib")
- #else
- #include <sys/sysinfo.h>
- #include <sys/statvfs.h>
- #include <unistd.h>
- #endif
- namespace av {
- namespace utils {
- using namespace av::base;
- // PerformanceMonitor 实现
- PerformanceMonitor::PerformanceMonitor(const MonitorConfig& config)
- : config_(config)
- , initialized_(false)
- , running_(false)
- , monitoring_(false)
- , startTime_(std::chrono::steady_clock::now())
- , lastReportTime_(std::chrono::steady_clock::now()) {
- }
- PerformanceMonitor::~PerformanceMonitor() {
- close();
- }
- ErrorCode PerformanceMonitor::initialize() {
- std::lock_guard<std::mutex> lock(configMutex_);
-
- if (initialized_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- try {
- // 初始化基本指标
- registerMetric("system.cpu_usage", MetricType::GAUGE, 90.0);
- registerMetric("system.memory_usage", MetricType::GAUGE, 85.0);
- registerMetric("system.disk_usage", MetricType::GAUGE, 90.0);
- registerMetric("performance.frame_rate", MetricType::RATE, 0.0);
- registerMetric("performance.latency", MetricType::HISTOGRAM, 100.0);
- registerMetric("performance.throughput", MetricType::RATE, 0.0);
-
- startTime_ = std::chrono::steady_clock::now();
- lastReportTime_ = startTime_;
-
- initialized_.store(true);
- LOG_INFO("Performance monitor initialized successfully");
-
- return ErrorCode::SUCCESS;
- } catch (const std::exception& e) {
- LOG_ERROR("Failed to initialize performance monitor: {}", e.what());
- return ErrorCode::INITIALIZATION_FAILED;
- }
- }
- ErrorCode PerformanceMonitor::start() {
- if (!initialized_.load()) {
- return ErrorCode::NOT_INITIALIZED;
- }
-
- if (running_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- running_.store(true);
- monitoring_.store(config_.enableMonitoring);
-
- LOG_INFO("Performance monitor started");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::stop() {
- if (!running_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- running_.store(false);
- monitoring_.store(false);
-
- LOG_INFO("Performance monitor stopped");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::reset() {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- resetAllStats();
- clearAlerts();
-
- startTime_ = std::chrono::steady_clock::now();
- lastReportTime_ = startTime_;
-
- LOG_INFO("Performance monitor reset");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::close() {
- stop();
-
- std::lock_guard<std::mutex> metricsLock(metricsMutex_);
- std::lock_guard<std::mutex> alertsLock(alertsMutex_);
- std::lock_guard<std::mutex> configLock(configMutex_);
-
- metrics_.clear();
- metricTypes_.clear();
- alertThresholds_.clear();
- timerStarts_.clear();
- historyData_.clear();
- activeAlerts_.clear();
-
- initialized_.store(false);
-
- LOG_INFO("Performance monitor closed");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::registerMetric(const std::string& name, MetricType type, double alertThreshold) {
- if (name.empty()) {
- return ErrorCode::INVALID_PARAMETER;
- }
-
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- if (metrics_.find(name) != metrics_.end()) {
- return ErrorCode::ALREADY_EXISTS;
- }
-
- PerformanceStats stats;
- stats.name = name;
- stats.type = type;
- stats.startTime = std::chrono::steady_clock::now();
- stats.lastUpdateTime = stats.startTime;
-
- metrics_[name] = stats;
- metricTypes_[name] = type;
-
- if (alertThreshold > 0.0) {
- alertThresholds_[name] = alertThreshold;
- }
-
- if (config_.enableLogging) {
- LOG_DEBUG("Registered metric: {} (type: {})", name, static_cast<int>(type));
- }
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::unregisterMetric(const std::string& name) {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it == metrics_.end()) {
- return ErrorCode::NOT_FOUND;
- }
-
- metrics_.erase(it);
- metricTypes_.erase(name);
- alertThresholds_.erase(name);
- timerStarts_.erase(name);
- historyData_.erase(name);
-
- if (config_.enableLogging) {
- LOG_DEBUG("Unregistered metric: {}", name);
- }
-
- return ErrorCode::SUCCESS;
- }
- bool PerformanceMonitor::hasMetric(const std::string& name) const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
- return metrics_.find(name) != metrics_.end();
- }
- std::vector<std::string> PerformanceMonitor::getMetricNames() const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- std::vector<std::string> names;
- names.reserve(metrics_.size());
-
- for (const auto& pair : metrics_) {
- names.push_back(pair.first);
- }
-
- return names;
- }
- ErrorCode PerformanceMonitor::incrementCounter(const std::string& name, double value) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- updateMetric(name, value, true);
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::decrementCounter(const std::string& name, double value) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- updateMetric(name, -value, true);
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::setCounter(const std::string& name, double value) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- updateMetric(name, value, false);
- return ErrorCode::SUCCESS;
- }
- double PerformanceMonitor::getCounter(const std::string& name) const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end() && it->second.type == MetricType::COUNTER) {
- return it->second.value;
- }
-
- return 0.0;
- }
- ErrorCode PerformanceMonitor::setGauge(const std::string& name, double value) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- updateMetric(name, value, false);
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::updateGauge(const std::string& name, double delta) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- updateMetric(name, delta, true);
- return ErrorCode::SUCCESS;
- }
- double PerformanceMonitor::getGauge(const std::string& name) const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end() && it->second.type == MetricType::GAUGE) {
- return it->second.value;
- }
-
- return 0.0;
- }
- ErrorCode PerformanceMonitor::recordHistogram(const std::string& name, double value) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end() && it->second.type == MetricType::HISTOGRAM) {
- updateHistogram(it->second, value);
- checkAlerts(name, value);
-
- if (config_.enableLogging) {
- logMetricUpdate(name, value);
- }
- }
-
- return ErrorCode::SUCCESS;
- }
- PerformanceStats PerformanceMonitor::getHistogramStats(const std::string& name) const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end() && it->second.type == MetricType::HISTOGRAM) {
- return it->second;
- }
-
- return PerformanceStats();
- }
- ErrorCode PerformanceMonitor::startTimer(const std::string& name) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- std::lock_guard<std::mutex> lock(metricsMutex_);
- timerStarts_[name] = std::chrono::steady_clock::now();
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::stopTimer(const std::string& name) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = timerStarts_.find(name);
- if (it != timerStarts_.end()) {
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
- std::chrono::steady_clock::now() - it->second).count() / 1000.0;
-
- timerStarts_.erase(it);
-
- auto metricIt = metrics_.find(name);
- if (metricIt != metrics_.end() && metricIt->second.type == MetricType::TIMER) {
- updateStatistics(metricIt->second, duration);
- checkAlerts(name, duration);
-
- if (config_.enableLogging) {
- logMetricUpdate(name, duration);
- }
- }
- }
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::recordTimer(const std::string& name, double duration) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- updateMetric(name, duration, false);
- return ErrorCode::SUCCESS;
- }
- double PerformanceMonitor::getTimerAverage(const std::string& name) const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end() && it->second.type == MetricType::TIMER) {
- return it->second.avgValue;
- }
-
- return 0.0;
- }
- ErrorCode PerformanceMonitor::recordRate(const std::string& name, double value) {
- if (!monitoring_.load()) {
- return ErrorCode::SUCCESS;
- }
-
- updateMetric(name, value, true);
- return ErrorCode::SUCCESS;
- }
- double PerformanceMonitor::getRate(const std::string& name) const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end() && it->second.type == MetricType::RATE) {
- return it->second.rate;
- }
-
- return 0.0;
- }
- PerformanceStats PerformanceMonitor::getStats(const std::string& name) const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end()) {
- return it->second;
- }
-
- return PerformanceStats();
- }
- std::map<std::string, PerformanceStats> PerformanceMonitor::getAllStats() const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
- return metrics_;
- }
- void PerformanceMonitor::resetStats(const std::string& name) {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end()) {
- it->second.value = 0.0;
- it->second.minValue = 0.0;
- it->second.maxValue = 0.0;
- it->second.avgValue = 0.0;
- it->second.count = 0;
- it->second.totalCount = 0;
- it->second.rate = 0.0;
- it->second.startTime = std::chrono::steady_clock::now();
- it->second.lastUpdateTime = it->second.startTime;
- }
- }
- void PerformanceMonitor::resetAllStats() {
- for (const auto& pair : metrics_) {
- resetStats(pair.first);
- }
- }
- void PerformanceMonitor::setConfig(const MonitorConfig& config) {
- std::lock_guard<std::mutex> lock(configMutex_);
- config_ = config;
- monitoring_.store(config_.enableMonitoring && running_.load());
- }
- MonitorConfig PerformanceMonitor::getConfig() const {
- std::lock_guard<std::mutex> lock(configMutex_);
- return config_;
- }
- void PerformanceMonitor::enableMonitoring(bool enable) {
- std::lock_guard<std::mutex> lock(configMutex_);
- config_.enableMonitoring = enable;
- monitoring_.store(enable && running_.load());
- }
- void PerformanceMonitor::enableLogging(bool enable) {
- std::lock_guard<std::mutex> lock(configMutex_);
- config_.enableLogging = enable;
- }
- void PerformanceMonitor::enableReporting(bool enable) {
- std::lock_guard<std::mutex> lock(configMutex_);
- config_.enableReporting = enable;
- }
- void PerformanceMonitor::setReportInterval(double interval) {
- std::lock_guard<std::mutex> lock(configMutex_);
- config_.reportInterval = interval;
- }
- void PerformanceMonitor::setReportCallback(PerformanceReportCallback callback) {
- reportCallback_ = callback;
- }
- void PerformanceMonitor::setAlertCallback(PerformanceAlertCallback callback) {
- alertCallback_ = callback;
- }
- std::string PerformanceMonitor::generateReport() const {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- std::ostringstream oss;
- oss << "Performance Monitor Report\n";
- oss << "========================\n";
- oss << "Generated at: " << std::chrono::duration_cast<std::chrono::seconds>(
- std::chrono::steady_clock::now().time_since_epoch()).count() << "\n";
- oss << "Total metrics: " << metrics_.size() << "\n\n";
-
- for (const auto& pair : metrics_) {
- const auto& stats = pair.second;
- oss << "Metric: " << stats.name << "\n";
- oss << " Type: " << static_cast<int>(stats.type) << "\n";
- oss << " Current Value: " << std::fixed << std::setprecision(2) << stats.value << "\n";
- oss << " Min/Max/Avg: " << stats.minValue << "/" << stats.maxValue << "/" << stats.avgValue << "\n";
- oss << " Count: " << stats.count << "\n";
- oss << " Rate: " << stats.rate << "/sec\n";
- oss << "\n";
- }
-
- return oss.str();
- }
- ErrorCode PerformanceMonitor::exportToFile(const std::string& filename) const {
- try {
- std::ofstream file(filename);
- if (!file.is_open()) {
- return ErrorCode::FILE_OPERATION_FAILED;
- }
-
- file << generateReport();
- file.close();
-
- return ErrorCode::SUCCESS;
- } catch (const std::exception& e) {
- LOG_ERROR("Failed to export performance report: {}", e.what());
- return ErrorCode::FILE_OPERATION_FAILED;
- }
- }
- ErrorCode PerformanceMonitor::exportToJson(const std::string& filename) const {
- // JSON导出实现
- return ErrorCode::SUCCESS;
- }
- ErrorCode PerformanceMonitor::exportToCsv(const std::string& filename) const {
- // CSV导出实现
- return ErrorCode::SUCCESS;
- }
- void PerformanceMonitor::setAlertThreshold(const std::string& name, double threshold) {
- std::lock_guard<std::mutex> lock(metricsMutex_);
- alertThresholds_[name] = threshold;
- }
- std::vector<PerformanceAlert> PerformanceMonitor::getActiveAlerts() const {
- std::lock_guard<std::mutex> lock(alertsMutex_);
- return activeAlerts_;
- }
- void PerformanceMonitor::clearAlerts() {
- std::lock_guard<std::mutex> lock(alertsMutex_);
- activeAlerts_.clear();
- }
- // 私有方法实现
- void PerformanceMonitor::updateMetric(const std::string& name, double value, bool isIncrement) {
- std::lock_guard<std::mutex> lock(metricsMutex_);
-
- auto it = metrics_.find(name);
- if (it != metrics_.end()) {
- if (isIncrement) {
- it->second.value += value;
- } else {
- it->second.value = value;
- }
-
- updateStatistics(it->second, value);
- checkAlerts(name, it->second.value);
-
- if (config_.enableLogging) {
- logMetricUpdate(name, it->second.value);
- }
- }
- }
- void PerformanceMonitor::checkAlerts(const std::string& name, double value) {
- if (!config_.enableAlerts) {
- return;
- }
-
- auto thresholdIt = alertThresholds_.find(name);
- if (thresholdIt != alertThresholds_.end() && value > thresholdIt->second) {
- PerformanceAlert alert;
- alert.metricName = name;
- alert.currentValue = value;
- alert.threshold = thresholdIt->second;
- alert.message = "Metric " + name + " exceeded threshold";
- alert.timestamp = std::chrono::steady_clock::now();
-
- {
- std::lock_guard<std::mutex> alertLock(alertsMutex_);
- activeAlerts_.push_back(alert);
- }
-
- if (alertCallback_) {
- alertCallback_(alert);
- }
- }
- }
- void PerformanceMonitor::logMetricUpdate(const std::string& name, double value) {
- LOG_DEBUG("Metric updated: {} = {}", name, value);
- }
- void PerformanceMonitor::updateStatistics(PerformanceStats& stats, double value) {
- stats.count++;
- stats.totalCount++;
- stats.lastUpdateTime = std::chrono::steady_clock::now();
-
- if (stats.count == 1) {
- stats.minValue = value;
- stats.maxValue = value;
- stats.avgValue = value;
- } else {
- stats.minValue = std::min(stats.minValue, value);
- stats.maxValue = std::max(stats.maxValue, value);
- stats.avgValue = (stats.avgValue * (stats.count - 1) + value) / stats.count;
- }
-
- // 计算速率
- if (stats.type == MetricType::RATE) {
- auto duration = std::chrono::duration_cast<std::chrono::seconds>(
- stats.lastUpdateTime - stats.startTime).count();
- if (duration > 0) {
- stats.rate = stats.totalCount / static_cast<double>(duration);
- }
- }
- }
- void PerformanceMonitor::updateHistogram(PerformanceStats& stats, double value) {
- updateStatistics(stats, value);
-
- // 保存历史数据
- auto& history = historyData_[stats.name];
- history.push_back(value);
-
- if (history.size() > config_.maxHistorySize) {
- history.erase(history.begin());
- }
- }
- // ScopedTimer 实现
- ScopedTimer::ScopedTimer(PerformanceMonitor* monitor, const std::string& name)
- : monitor_(monitor)
- , name_(name)
- , startTime_(std::chrono::steady_clock::now()) {
- }
- ScopedTimer::~ScopedTimer() {
- if (monitor_) {
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
- std::chrono::steady_clock::now() - startTime_).count() / 1000.0;
- monitor_->recordTimer(name_, duration);
- }
- }
- // SystemPerformanceMonitor 实现
- SystemPerformanceMonitor::SystemPerformanceMonitor()
- : cpuUsage_(0.0)
- , memoryUsage_(0.0)
- , diskUsage_(0.0)
- , networkUsage_(0.0)
- , gpuUsage_(0.0)
- , lastUpdateTime_(std::chrono::steady_clock::now()) {
- }
- SystemPerformanceMonitor::~SystemPerformanceMonitor() {
- }
- ErrorCode SystemPerformanceMonitor::collectCpuUsage() {
- #ifdef _WIN32
- // Windows CPU使用率收集实现
- static PDH_HQUERY cpuQuery;
- static PDH_HCOUNTER cpuTotal;
- static bool initialized = false;
-
- if (!initialized) {
- PdhOpenQuery(NULL, NULL, &cpuQuery);
- PdhAddEnglishCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
- PdhCollectQueryData(cpuQuery);
- initialized = true;
- }
-
- PDH_FMT_COUNTERVALUE counterVal;
- PdhCollectQueryData(cpuQuery);
- PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
- cpuUsage_.store(counterVal.doubleValue);
- #else
- // Linux CPU使用率收集实现
- cpuUsage_.store(0.0); // 简化实现
- #endif
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode SystemPerformanceMonitor::collectMemoryUsage() {
- #ifdef _WIN32
- MEMORYSTATUSEX memInfo;
- memInfo.dwLength = sizeof(MEMORYSTATUSEX);
- GlobalMemoryStatusEx(&memInfo);
-
- double totalMem = static_cast<double>(memInfo.ullTotalPhys);
- double availMem = static_cast<double>(memInfo.ullAvailPhys);
- double usedMem = totalMem - availMem;
-
- memoryUsage_.store((usedMem / totalMem) * 100.0);
- #else
- struct sysinfo memInfo;
- sysinfo(&memInfo);
-
- double totalMem = static_cast<double>(memInfo.totalram * memInfo.mem_unit);
- double freeMem = static_cast<double>(memInfo.freeram * memInfo.mem_unit);
- double usedMem = totalMem - freeMem;
-
- memoryUsage_.store((usedMem / totalMem) * 100.0);
- #endif
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode SystemPerformanceMonitor::collectDiskUsage() {
- // 磁盘使用率收集实现
- diskUsage_.store(0.0); // 简化实现
- return ErrorCode::SUCCESS;
- }
- ErrorCode SystemPerformanceMonitor::collectNetworkUsage() {
- // 网络使用率收集实现
- networkUsage_.store(0.0); // 简化实现
- return ErrorCode::SUCCESS;
- }
- ErrorCode SystemPerformanceMonitor::collectGpuUsage() {
- // GPU使用率收集实现
- gpuUsage_.store(0.0); // 简化实现
- return ErrorCode::SUCCESS;
- }
- double SystemPerformanceMonitor::getCpuUsage() const {
- return cpuUsage_.load();
- }
- double SystemPerformanceMonitor::getMemoryUsage() const {
- return memoryUsage_.load();
- }
- double SystemPerformanceMonitor::getDiskUsage() const {
- return diskUsage_.load();
- }
- double SystemPerformanceMonitor::getNetworkUsage() const {
- return networkUsage_.load();
- }
- double SystemPerformanceMonitor::getGpuUsage() const {
- return gpuUsage_.load();
- }
- std::string SystemPerformanceMonitor::getSystemInfo() const {
- std::ostringstream oss;
- oss << "System Performance:\n";
- oss << "CPU Usage: " << std::fixed << std::setprecision(1) << getCpuUsage() << "%\n";
- oss << "Memory Usage: " << getMemoryUsage() << "%\n";
- oss << "Disk Usage: " << getDiskUsage() << "%\n";
- oss << "Network Usage: " << getNetworkUsage() << "%\n";
- oss << "GPU Usage: " << getGpuUsage() << "%\n";
- return oss.str();
- }
- std::string SystemPerformanceMonitor::getCpuInfo() const {
- return "CPU Information"; // 简化实现
- }
- std::string SystemPerformanceMonitor::getMemoryInfo() const {
- return "Memory Information"; // 简化实现
- }
- std::string SystemPerformanceMonitor::getGpuInfo() const {
- return "GPU Information"; // 简化实现
- }
- ErrorCode SystemPerformanceMonitor::integrateWithMonitor(PerformanceMonitor* monitor) {
- if (!monitor) {
- return ErrorCode::INVALID_PARAMETER;
- }
-
- // 定期更新系统指标到性能监控器
- collectCpuUsage();
- collectMemoryUsage();
- collectDiskUsage();
- collectNetworkUsage();
- collectGpuUsage();
-
- monitor->setGauge("system.cpu_usage", getCpuUsage());
- monitor->setGauge("system.memory_usage", getMemoryUsage());
- monitor->setGauge("system.disk_usage", getDiskUsage());
- monitor->setGauge("system.network_usage", getNetworkUsage());
- monitor->setGauge("system.gpu_usage", getGpuUsage());
-
- return ErrorCode::SUCCESS;
- }
- void SystemPerformanceMonitor::updateSystemMetrics() {
- collectCpuUsage();
- collectMemoryUsage();
- collectDiskUsage();
- collectNetworkUsage();
- collectGpuUsage();
-
- lastUpdateTime_ = std::chrono::steady_clock::now();
- }
- // PerformanceMonitorFactory 实现
- std::unique_ptr<PerformanceMonitor> PerformanceMonitorFactory::createStandardMonitor() {
- MonitorConfig config;
- config.enableMonitoring = true;
- config.enableLogging = true;
- config.enableReporting = true;
- config.reportInterval = 10.0;
- config.maxHistorySize = 1000;
- config.enableAlerts = true;
-
- return std::make_unique<PerformanceMonitor>(config);
- }
- std::unique_ptr<PerformanceMonitor> PerformanceMonitorFactory::createLightweightMonitor() {
- MonitorConfig config;
- config.enableMonitoring = true;
- config.enableLogging = false;
- config.enableReporting = false;
- config.reportInterval = 60.0;
- config.maxHistorySize = 100;
- config.enableAlerts = false;
-
- return std::make_unique<PerformanceMonitor>(config);
- }
- std::unique_ptr<PerformanceMonitor> PerformanceMonitorFactory::createDetailedMonitor() {
- MonitorConfig config;
- config.enableMonitoring = true;
- config.enableLogging = true;
- config.enableReporting = true;
- config.reportInterval = 5.0;
- config.maxHistorySize = 5000;
- config.enableAlerts = true;
-
- return std::make_unique<PerformanceMonitor>(config);
- }
- std::unique_ptr<PerformanceMonitor> PerformanceMonitorFactory::createRealtimeMonitor() {
- MonitorConfig config;
- config.enableMonitoring = true;
- config.enableLogging = false;
- config.enableReporting = true;
- config.reportInterval = 1.0;
- config.maxHistorySize = 500;
- config.enableAlerts = true;
-
- return std::make_unique<PerformanceMonitor>(config);
- }
- std::unique_ptr<SystemPerformanceMonitor> PerformanceMonitorFactory::createSystemMonitor() {
- return std::make_unique<SystemPerformanceMonitor>();
- }
- } // namespace utils
- } // namespace av
|