Finished memory usage API for linux systems

This commit is contained in:
Ben
2018-12-10 19:49:31 +00:00
parent 5bde13909e
commit 4a12d50114
6 changed files with 90 additions and 16 deletions

BIN
bin/resource-monitor Executable file

Binary file not shown.

View File

@@ -2,17 +2,24 @@
#include <thread>
#include <mutex>
#include <cstring> // 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

0
include/sys.h Normal file
View File

View File

@@ -3,7 +3,6 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
@@ -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) {

View File

@@ -1,21 +1,90 @@
#include <memory.h>
#include <memstat.h>
#include <iostream>
#include <unistd.h>
#include <sys/sysinfo.h>
// 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;
}

View File

@@ -1,5 +1,5 @@
#include <cpu.h>
#include <memory.h>
#include <memstat.h>
#include <iostream>
#include <unistd.h>
@@ -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;
}
}