CPU Polling working properly and dynamically for all cores on Linux systems
This commit is contained in:
12
Makefile
12
Makefile
@@ -1,5 +1,5 @@
|
|||||||
# CMAKE generated file: DO NOT EDIT!
|
# 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 executed when no arguments are given to make.
|
||||||
default_target: all
|
default_target: all
|
||||||
@@ -48,10 +48,10 @@ RM = /usr/bin/cmake -E remove -f
|
|||||||
EQUALS = =
|
EQUALS = =
|
||||||
|
|
||||||
# The top-level source directory on which CMake was run.
|
# 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.
|
# 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.
|
# Targets provided globally by CMake.
|
||||||
@@ -80,9 +80,9 @@ edit_cache/fast: edit_cache
|
|||||||
|
|
||||||
# The main all target
|
# The main all target
|
||||||
all: cmake_check_build_system
|
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
|
$(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
|
.PHONY : all
|
||||||
|
|
||||||
# The main clean target
|
# The main clean target
|
||||||
@@ -184,8 +184,8 @@ help:
|
|||||||
@echo "... clean"
|
@echo "... clean"
|
||||||
@echo "... depend"
|
@echo "... depend"
|
||||||
@echo "... rebuild_cache"
|
@echo "... rebuild_cache"
|
||||||
@echo "... edit_cache"
|
|
||||||
@echo "... ./bin/resource-monitor"
|
@echo "... ./bin/resource-monitor"
|
||||||
|
@echo "... edit_cache"
|
||||||
@echo "... platform/linux/monitoring/cpu.o"
|
@echo "... platform/linux/monitoring/cpu.o"
|
||||||
@echo "... platform/linux/monitoring/cpu.i"
|
@echo "... platform/linux/monitoring/cpu.i"
|
||||||
@echo "... platform/linux/monitoring/cpu.s"
|
@echo "... platform/linux/monitoring/cpu.s"
|
||||||
|
|||||||
Binary file not shown.
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <map>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
class CPU {
|
class CPU {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@@ -35,12 +36,16 @@ void CPU::CPU_POLL(CPU* cpu) {
|
|||||||
cpu->CPU_Mutex.unlock();
|
cpu->CPU_Mutex.unlock();
|
||||||
|
|
||||||
// READ TOTAL CPU
|
// READ TOTAL CPU
|
||||||
|
std::vector<std::string> ProcStatLines;
|
||||||
unsigned long long luser, nice, system, idle, iowait,
|
unsigned long long luser, nice, system, idle, iowait,
|
||||||
irq, softirq = 0;
|
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);
|
&nice, &system, &idle, &iowait, &irq, &softirq);
|
||||||
fclose(ProcStatCPU);
|
|
||||||
|
|
||||||
cpu->CPU_Mutex.lock();
|
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++) {
|
for (unsigned int thread = 0; thread < cpu->CPU_HARDWARE_THREADS; thread++) {
|
||||||
// READ TOTAL THREAD CPU
|
// READ TOTAL THREAD CPU
|
||||||
|
unsigned int currentThread = thread + 1;
|
||||||
unsigned long long tluser, tnice, tsystem, tidle, tiowait,
|
unsigned long long tluser, tnice, tsystem, tidle, tiowait,
|
||||||
tirq, tsoftirq = 0;
|
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);
|
|
||||||
|
|
||||||
std::cout << tluser << " " << tnice << " " << tsystem << " " << tidle
|
sscanf(ProcStatLines[currentThread].c_str(), "%*s %llu %llu %llu %llu %llu %llu %llu",
|
||||||
<< " " << tiowait << " " << tirq << " " << tsoftirq << std::endl;
|
&tluser, &tnice, &tsystem, &tidle, &tiowait, &tirq, &tsoftirq);
|
||||||
|
|
||||||
cpu->CPU_Mutex.lock();
|
cpu->CPU_Mutex.lock();
|
||||||
|
|
||||||
|
|||||||
0
platform/linux/monitoring/memory.cpp
Normal file
0
platform/linux/monitoring/memory.cpp
Normal file
@@ -1,19 +1,20 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cpu.h>
|
#include <cpu.h>
|
||||||
|
#include <memory.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
CPU* cpu = new CPU();
|
CPU* cpu = new CPU();
|
||||||
CPU_Instance = cpu;
|
CPU_Instance = cpu;
|
||||||
|
|
||||||
|
// Memory* mem = new Memory();
|
||||||
|
// Memory_Instance = mem;
|
||||||
|
|
||||||
cpu->START_CPU_POLLING();
|
cpu->START_CPU_POLLING();
|
||||||
|
// mem->START_MEMORY_POLLING();
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
sleep(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++) {
|
for (unsigned int thread = 0; thread <= cpu->CPU_HARDWARE_THREADS; thread++) {
|
||||||
std::cout << "CORE " << thread << " USAGE: " << cpu->CPU_PERCENT(thread) << std::endl;
|
std::cout << "CORE " << thread << " USAGE: " << cpu->CPU_PERCENT(thread) << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user