CPU Polling working properly and dynamically for all cores on Linux systems

This commit is contained in:
Ben
2018-12-10 12:24:05 +00:00
parent da20ea0626
commit 81cfff8a59
7 changed files with 56 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.7
# Generated by "Unix Makefiles" Generator, CMake Version 3.12
# Default target executed when no arguments are given to make.
default_target: all
@@ -48,10 +48,10 @@ RM = /usr/bin/cmake -E remove -f
EQUALS = =
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/ben/Desktop/universal-system-monitor
CMAKE_SOURCE_DIR = /home/ben/Programming/resource-monitor
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/ben/Desktop/universal-system-monitor
CMAKE_BINARY_DIR = /home/ben/Programming/resource-monitor
#=============================================================================
# Targets provided globally by CMake.
@@ -80,9 +80,9 @@ edit_cache/fast: edit_cache
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/ben/Desktop/universal-system-monitor/CMakeFiles /home/ben/Desktop/universal-system-monitor/CMakeFiles/progress.marks
$(CMAKE_COMMAND) -E cmake_progress_start /home/ben/Programming/resource-monitor/CMakeFiles /home/ben/Programming/resource-monitor/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/ben/Desktop/universal-system-monitor/CMakeFiles 0
$(CMAKE_COMMAND) -E cmake_progress_start /home/ben/Programming/resource-monitor/CMakeFiles 0
.PHONY : all
# The main clean target
@@ -184,8 +184,8 @@ help:
@echo "... clean"
@echo "... depend"
@echo "... rebuild_cache"
@echo "... edit_cache"
@echo "... ./bin/resource-monitor"
@echo "... edit_cache"
@echo "... platform/linux/monitoring/cpu.o"
@echo "... platform/linux/monitoring/cpu.i"
@echo "... platform/linux/monitoring/cpu.s"

Binary file not shown.

View File

@@ -1,9 +1,9 @@
#pragma once
#include <vector>
#include <map>
#include <thread>
#include <vector>
#include <mutex>
#include <map>
class CPU {
public:

View File

@@ -0,0 +1,31 @@
#pragma once
#include <vector>
#include <thread>
#include <mutex>
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;
int UPDATE_INTERVAL; // s
void START_MEMORY_POLLING();
static void MEMORY_POLL(Memory* mem);
void END_MEMORY_POLLING();
virtual ~Memory();
private:
std::thread* m_pollThread;
bool m_isPolling;
};
static Memory* Memory_Instance;

View File

@@ -2,6 +2,7 @@
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
@@ -35,12 +36,16 @@ void CPU::CPU_POLL(CPU* cpu) {
cpu->CPU_Mutex.unlock();
// READ TOTAL CPU
std::vector<std::string> ProcStatLines;
unsigned long long luser, nice, system, idle, iowait,
irq, softirq = 0;
FILE* ProcStatCPU = fopen("/proc/stat", "r");
fscanf(ProcStatCPU, "cpu %llu %llu %llu %llu %llu %llu %llu", &luser,
std::ifstream ProcStatFile("/proc/stat");
for (std::string str; std::getline(ProcStatFile, str); )
ProcStatLines.push_back(str);
sscanf(ProcStatLines[0].c_str(), "cpu %llu %llu %llu %llu %llu %llu %llu", &luser,
&nice, &system, &idle, &iowait, &irq, &softirq);
fclose(ProcStatCPU);
cpu->CPU_Mutex.lock();
@@ -54,15 +59,12 @@ void CPU::CPU_POLL(CPU* cpu) {
for (unsigned int thread = 0; thread < cpu->CPU_HARDWARE_THREADS; thread++) {
// READ TOTAL THREAD CPU
unsigned int currentThread = thread + 1;
unsigned long long tluser, tnice, tsystem, tidle, tiowait,
tirq, tsoftirq = 0;
FILE* ProcStatCPUThread = fopen("/proc/stat", "r");
fscanf(ProcStatCPUThread, "%*s %llu %llu %llu %llu %llu %llu %llu",
&tluser, &tnice, &tsystem, &tidle, &tiowait, &tirq, &tsoftirq); // WONT WORK
fclose(ProcStatCPUThread);
tirq, tsoftirq = 0;[[]]
std::cout << tluser << " " << tnice << " " << tsystem << " " << tidle
<< " " << tiowait << " " << tirq << " " << tsoftirq << std::endl;
sscanf(ProcStatLines[currentThread].c_str(), "%*s %llu %llu %llu %llu %llu %llu %llu",
&tluser, &tnice, &tsystem, &tidle, &tiowait, &tirq, &tsoftirq);
cpu->CPU_Mutex.lock();

View File

View File

@@ -1,19 +1,20 @@
#include <iostream>
#include <cpu.h>
#include <memory.h>
#include <unistd.h>
int main(int argc, char** argv) {
CPU* cpu = new CPU();
CPU_Instance = cpu;
// Memory* mem = new Memory();
// Memory_Instance = mem;
cpu->START_CPU_POLLING();
// mem->START_MEMORY_POLLING();
while(1) {
sleep(1);
// for (unsigned int thread = 0; thread < cpu->CPU_HARDWARE_THREADS; thread++) {
// std::cout << "CORE " << thread << " USAGE: " << cpu->CPU_PERCENT(thread) << std::endl;
// }
for (unsigned int thread = 0; thread <= cpu->CPU_HARDWARE_THREADS; thread++) {
std::cout << "CORE " << thread << " USAGE: " << cpu->CPU_PERCENT(thread) << std::endl;
}