diff --git a/bin/resource-monitor b/bin/resource-monitor new file mode 100755 index 0000000..5d333c1 Binary files /dev/null and b/bin/resource-monitor differ diff --git a/include/memory.h b/include/memstat.h similarity index 61% rename from include/memory.h rename to include/memstat.h index e5de65a..b8344cc 100644 --- a/include/memory.h +++ b/include/memstat.h @@ -2,17 +2,24 @@ #include #include +#include // For memcpy + +struct MemStat { + long double TOTAL_MB; + long double FREE_MB; + long double SHARED_MB; + long double BUFFER_MB; + short TOTAL_RUNNING_PROCS; +}; class Memory { public: Memory(); std::mutex Memory_Mutex; - long double MEMORY_TOTAL_MB; - long double MEMORY_FREE_MB; - long double MEMORY_AVAILABLE_MB; - long double MEMORY_ACTIVE_MB; - long double MEMORY_INACTIVE_MB; + MemStat* memStat; + + MemStat getMemStat(); int UPDATE_INTERVAL; // ms diff --git a/include/sys.h b/include/sys.h new file mode 100644 index 0000000..e69de29 diff --git a/platform/linux/monitoring/cpu.cpp b/platform/linux/monitoring/cpu.cpp index fe05dbf..a47eb69 100644 --- a/platform/linux/monitoring/cpu.cpp +++ b/platform/linux/monitoring/cpu.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -20,8 +19,7 @@ void CPU::START_CPU_POLLING() { } this->m_isPolling = true; std::cout << "Starting CPU worker thread" << std::endl; - std::thread* t = new std::thread(&CPU::CPU_POLL, this); - this->m_pollThread = t; + this->m_pollThread = new std::thread(&CPU::CPU_POLL, this); } void CPU::CPU_POLL(CPU* cpu) { diff --git a/platform/linux/monitoring/memory.cpp b/platform/linux/monitoring/memory.cpp index d484128..7850e3b 100644 --- a/platform/linux/monitoring/memory.cpp +++ b/platform/linux/monitoring/memory.cpp @@ -1,21 +1,90 @@ -#include +#include + +#include +#include +#include + +// struct sysinfo { +// long uptime; /* Seconds since boot */ +// unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ +// unsigned long totalram; /* Total usable main memory size */ +// unsigned long freeram; /* Available memory size */ +// unsigned long sharedram; /* Amount of shared memory */ +// unsigned long bufferram; /* Memory used by buffers */ +// unsigned long totalswap; /* Total swap space size */ +// unsigned long freeswap; /* swap space still available */ +// unsigned short procs; /* Number of current processes */ +// unsigned long totalhigh; /* Total high memory size */ +// unsigned long freehigh; /* Available high memory size */ +// unsigned int mem_unit; /* Memory unit size in bytes */ +// char _f[20-2*sizeof(long)-sizeof(int)]; /* IDFK what this does, it doesnt work without it*/ +// }; Memory::Memory() { + this->UPDATE_INTERVAL = 1000; + this->m_isPolling = false; + memStat = new MemStat(); + struct sysinfo info; + sysinfo(&info); + std::cout << "Total RAM Memory supported: " << info.totalram / 1.049e+6 << "MB" << std::endl; +} +MemStat Memory::getMemStat() { + MemStat stat; + this->Memory_Mutex.lock(); + memcpy(&stat, this->memStat, sizeof(MemStat)); + this->Memory_Mutex.unlock(); + return stat; } void Memory::START_MEMORY_POLLING() { - + if (this->m_isPolling) { + return; + } + this->m_isPolling = true; + std::cout << "Starting memory worker thread" << std::endl; + m_pollThread = new std::thread(&Memory::MEMORY_POLL, this); } -void Memory::MEMORY_POLL() { +void Memory::MEMORY_POLL(Memory* mem) { + std::cout << "New memory worker thread" << std::endl; + while (true) { + mem->Memory_Mutex.lock(); + if (!mem->m_isPolling) { + mem->Memory_Mutex.unlock(); + return; + } + mem->Memory_Mutex.unlock(); + + struct sysinfo info; + sysinfo(&info); + + mem->Memory_Mutex.lock(); + + mem->memStat->TOTAL_MB = info.totalram / 1.049e+6; + mem->memStat->FREE_MB = info.freeram / 1.049e+6; + mem->memStat->SHARED_MB = info.sharedram / 1.049e+6; + mem->memStat->BUFFER_MB = info.bufferram / 1.049e+6; + mem->memStat->TOTAL_RUNNING_PROCS = info.procs; + + mem->Memory_Mutex.unlock(); + + sleep(1); + } } -void Memory::END_MEMORY_POLLING(Memory* memory) { - +void Memory::END_MEMORY_POLLING() { + if (!this->m_isPolling) { + return; + } + this->m_isPolling = false; + m_pollThread->join(); + delete m_pollThread; } Memory::~Memory() { - + this->m_isPolling = false; + m_pollThread->join(); + delete m_pollThread; } diff --git a/src/main.cpp b/src/main.cpp index 396d9e9..2323a2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -16,9 +16,9 @@ int main(int argc, char** argv) { while(1) { sleep(1); + std::cout << std::endl; for (unsigned int thread = 0; thread <= cpu->CPU_HARDWARE_THREADS; thread++) { std::cout << "CORE " << thread << " USAGE: " << cpu->CPU_PERCENT(thread) << std::endl; } - std::cout << std::endl; } }