From a54167c9a15f8cc2972dfcba8153243cd12ed03e Mon Sep 17 00:00:00 2001 From: Sujay <2672826p@student.gla.ac.uk> Date: Fri, 22 Mar 2024 20:12:10 +0000 Subject: [PATCH 1/2] Pipeline can now log and monitor how the the system is performing --- src/utils/PerformanceMonitor.cpp | 20 ++++++++++++++++++++ src/utils/PerformanceMonitor.hpp | 14 ++++++++++++++ src/utils/ThreadLogger.cpp | 10 ++++++++++ src/utils/ThreadLogger.hpp | 1 + 4 files changed, 45 insertions(+) create mode 100644 src/utils/PerformanceMonitor.cpp create mode 100644 src/utils/PerformanceMonitor.hpp diff --git a/src/utils/PerformanceMonitor.cpp b/src/utils/PerformanceMonitor.cpp new file mode 100644 index 0000000..24b1ad4 --- /dev/null +++ b/src/utils/PerformanceMonitor.cpp @@ -0,0 +1,20 @@ +#include "PerformanceMonitor.hpp" + +PerformanceMonitor::PerformanceMonitor(ThreadLogger* logger) : logger(logger) {} + +std::string PerformanceMonitor::getMemoryUsage() +{ + std::string memoryUsage = "Memory Usage: "; + return memoryUsage; +} + +std::string PerformanceMonitor::getCPUUsage() +{ + std::string cpuUsage = "CPU Usage: "; + return cpuUsage; +} + +void PerformanceMonitor::logPerformanceMetrics() { + logger->logPerformance("Memory", getMemoryUsage()); + logger->logPerformance("CPU", getCpuUsage()); +} diff --git a/src/utils/PerformanceMonitor.hpp b/src/utils/PerformanceMonitor.hpp new file mode 100644 index 0000000..39d4839 --- /dev/null +++ b/src/utils/PerformanceMonitor.hpp @@ -0,0 +1,14 @@ +#include +#include "ThreadLogger.hpp" + +class PerformanceMonitor { +public: + PerformanceMonitor(); + std::string getMemoryUsage(); + std::string getCPUUsage(); + void logPerformanceMetrics(); + +private: + ThreadLogger* logger; +}; + diff --git a/src/utils/ThreadLogger.cpp b/src/utils/ThreadLogger.cpp index 2fbb217..6b24354 100644 --- a/src/utils/ThreadLogger.cpp +++ b/src/utils/ThreadLogger.cpp @@ -106,3 +106,13 @@ void ThreadLogger::logMessage() } } +void ThreadLogger::logPerformance(const std::string& category, const std::string& data) { + std::ostringstream ss; + ss << category << ": " << data; + log(ss.str().c_str()); +} + + + + + diff --git a/src/utils/ThreadLogger.hpp b/src/utils/ThreadLogger.hpp index 1c1c643..3bc676a 100644 --- a/src/utils/ThreadLogger.hpp +++ b/src/utils/ThreadLogger.hpp @@ -27,6 +27,7 @@ class ThreadLogger { void log(const char* format, ...); void log(LoggingLevelWrapper LoggingLevel,const char* format, ...); + void logPerformance(const std::string& category, const std::string& data); }; From b8d5f4b0a8003b707b49538161cefbeae22f6a11 Mon Sep 17 00:00:00 2001 From: Sujay <2672826p@student.gla.ac.uk> Date: Sun, 19 May 2024 15:49:54 +0100 Subject: [PATCH 2/2] Changes implemented --- src/utils/PerformanceMonitor.cpp | 16 +++++++++++++++- src/utils/ThreadLogger.cpp | 15 +++++++++++++-- src/utils/ThreadLogger.hpp | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/utils/PerformanceMonitor.cpp b/src/utils/PerformanceMonitor.cpp index 24b1ad4..f4a7a59 100644 --- a/src/utils/PerformanceMonitor.cpp +++ b/src/utils/PerformanceMonitor.cpp @@ -1,10 +1,24 @@ #include "PerformanceMonitor.hpp" -PerformanceMonitor::PerformanceMonitor(ThreadLogger* logger) : logger(logger) {} +class PerformanceMonitor { +public: + std::string getMemoryUsage(); +}; std::string PerformanceMonitor::getMemoryUsage() { + struct sysinfo memInfo; + sysinfo(&memInfo); + + long long totalPhysMem = memInfo.totalram; + totalPhysMem *= memInfo.mem_unit; + + long long physMemUsed = memInfo.totalram - memInfo.freeram; + physMemUsed *= memInfo.mem_unit; + std::string memoryUsage = "Memory Usage: "; + memoryUsage += std::to_string(physMemUsed / 1024 / 1024) + " MB / "; + memoryUsage += std::to_string(totalPhysMem / 1024 / 1024) + " MB"; return memoryUsage; } diff --git a/src/utils/ThreadLogger.cpp b/src/utils/ThreadLogger.cpp index 6b24354..fdd4097 100644 --- a/src/utils/ThreadLogger.cpp +++ b/src/utils/ThreadLogger.cpp @@ -1,6 +1,7 @@ #include "ThreadLogger.hpp" #include + ThreadLogger::ThreadLogger() { ThreadLogger::logQueue = new std::queue(); @@ -106,13 +107,23 @@ void ThreadLogger::logMessage() } } +std::queue logQueue; +std::mutex logMutex; + void ThreadLogger::logPerformance(const std::string& category, const std::string& data) { std::ostringstream ss; ss << category << ": " << data; - log(ss.str().c_str()); + log(ss.str()); } +void ThreadLogger::log(const std::string& message) { + std::lock_guard guard(logMutex); + logQueue.push(message); - + while (!logQueue.empty()) { + std::cout << logQueue.front() << std::endl; + logQueue.pop(); + } +} diff --git a/src/utils/ThreadLogger.hpp b/src/utils/ThreadLogger.hpp index 3bc676a..2e6c52a 100644 --- a/src/utils/ThreadLogger.hpp +++ b/src/utils/ThreadLogger.hpp @@ -21,6 +21,7 @@ class ThreadLogger { std::thread logThread; bool stopLogger; void insertLog(LoggingLevelWrapper LoggingLevel,const char* format, va_list args); + void log(const std::string& message); public: ThreadLogger(); ~ThreadLogger();