Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/utils/PerformanceMonitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "PerformanceMonitor.hpp"

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: ";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually get any data?

memoryUsage += std::to_string(physMemUsed / 1024 / 1024) + " MB / ";
memoryUsage += std::to_string(totalPhysMem / 1024 / 1024) + " MB";
return memoryUsage;
}

std::string PerformanceMonitor::getCPUUsage()
{
std::string cpuUsage = "CPU Usage: ";
return cpuUsage;
}

void PerformanceMonitor::logPerformanceMetrics() {
logger->logPerformance("Memory", getMemoryUsage());
logger->logPerformance("CPU", getCpuUsage());
}
14 changes: 14 additions & 0 deletions src/utils/PerformanceMonitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <string>
#include "ThreadLogger.hpp"

class PerformanceMonitor {
public:
PerformanceMonitor();
std::string getMemoryUsage();
std::string getCPUUsage();
void logPerformanceMetrics();

private:
ThreadLogger* logger;
};

21 changes: 21 additions & 0 deletions src/utils/ThreadLogger.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ThreadLogger.hpp"
#include <iostream>


ThreadLogger::ThreadLogger()
{
ThreadLogger::logQueue = new std::queue<std::string>();
Expand Down Expand Up @@ -106,3 +107,23 @@ void ThreadLogger::logMessage()
}
}

std::queue<std::string> logQueue;
std::mutex logMutex;

void ThreadLogger::logPerformance(const std::string& category, const std::string& data) {
std::ostringstream ss;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the main log function to log messages. this is not threadsafe (hence the whole point of the logger), and using multiple methods to queue messages would not be too intuitive. You could opt to use a helper method instead

ss << category << ": " << data;
log(ss.str());
}

void ThreadLogger::log(const std::string& message) {
std::lock_guard<std::mutex> guard(logMutex);
logQueue.push(message);

while (!logQueue.empty()) {
std::cout << logQueue.front() << std::endl;
logQueue.pop();
}
}


2 changes: 2 additions & 0 deletions src/utils/ThreadLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ 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();

void log(const char* format, ...);
void log(LoggingLevelWrapper LoggingLevel,const char* format, ...);
void logPerformance(const std::string& category, const std::string& data);

};

Expand Down