diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..753c815 --- /dev/null +++ b/Makefile @@ -0,0 +1,208 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.7 + +# Default target executed when no arguments are given to make. +default_target: all + +.PHONY : default_target + +# Allow only one "make -f Makefile2" at a time, but pass parallelism. +.NOTPARALLEL: + + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canonical targets will work. +.SUFFIXES: + + +# Remove some rules from gmake that .SUFFIXES does not remove. +SUFFIXES = + +.SUFFIXES: .hpux_make_needs_suffix_list + + +# Suppress display of executed commands. +$(VERBOSE).SILENT: + + +# A target that is always out of date. +cmake_force: + +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/bin/cmake + +# The command to remove a file. +RM = /usr/bin/cmake -E remove -f + +# Escaping for special characters. +EQUALS = = + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /home/ben/Desktop/universal-system-monitor + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /home/ben/Desktop/universal-system-monitor + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." + /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache + +.PHONY : rebuild_cache/fast + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..." + /usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache + +.PHONY : edit_cache/fast + +# 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 + $(MAKE) -f CMakeFiles/Makefile2 all + $(CMAKE_COMMAND) -E cmake_progress_start /home/ben/Desktop/universal-system-monitor/CMakeFiles 0 +.PHONY : all + +# The main clean target +clean: + $(MAKE) -f CMakeFiles/Makefile2 clean +.PHONY : clean + +# The main clean target +clean/fast: clean + +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +#============================================================================= +# Target rules for targets named ./bin/resource-monitor + +# Build rule for target. +./bin/resource-monitor: cmake_check_build_system + $(MAKE) -f CMakeFiles/Makefile2 ./bin/resource-monitor +.PHONY : ./bin/resource-monitor + +# fast build rule for target. +./bin/resource-monitor/fast: + $(MAKE) -f CMakeFiles/./bin/resource-monitor.dir/build.make CMakeFiles/./bin/resource-monitor.dir/build +.PHONY : ./bin/resource-monitor/fast + +platform/linux/monitoring/cpu.o: platform/linux/monitoring/cpu.cpp.o + +.PHONY : platform/linux/monitoring/cpu.o + +# target to build an object file +platform/linux/monitoring/cpu.cpp.o: + $(MAKE) -f CMakeFiles/./bin/resource-monitor.dir/build.make CMakeFiles/./bin/resource-monitor.dir/platform/linux/monitoring/cpu.cpp.o +.PHONY : platform/linux/monitoring/cpu.cpp.o + +platform/linux/monitoring/cpu.i: platform/linux/monitoring/cpu.cpp.i + +.PHONY : platform/linux/monitoring/cpu.i + +# target to preprocess a source file +platform/linux/monitoring/cpu.cpp.i: + $(MAKE) -f CMakeFiles/./bin/resource-monitor.dir/build.make CMakeFiles/./bin/resource-monitor.dir/platform/linux/monitoring/cpu.cpp.i +.PHONY : platform/linux/monitoring/cpu.cpp.i + +platform/linux/monitoring/cpu.s: platform/linux/monitoring/cpu.cpp.s + +.PHONY : platform/linux/monitoring/cpu.s + +# target to generate assembly for a file +platform/linux/monitoring/cpu.cpp.s: + $(MAKE) -f CMakeFiles/./bin/resource-monitor.dir/build.make CMakeFiles/./bin/resource-monitor.dir/platform/linux/monitoring/cpu.cpp.s +.PHONY : platform/linux/monitoring/cpu.cpp.s + +src/main.o: src/main.cpp.o + +.PHONY : src/main.o + +# target to build an object file +src/main.cpp.o: + $(MAKE) -f CMakeFiles/./bin/resource-monitor.dir/build.make CMakeFiles/./bin/resource-monitor.dir/src/main.cpp.o +.PHONY : src/main.cpp.o + +src/main.i: src/main.cpp.i + +.PHONY : src/main.i + +# target to preprocess a source file +src/main.cpp.i: + $(MAKE) -f CMakeFiles/./bin/resource-monitor.dir/build.make CMakeFiles/./bin/resource-monitor.dir/src/main.cpp.i +.PHONY : src/main.cpp.i + +src/main.s: src/main.cpp.s + +.PHONY : src/main.s + +# target to generate assembly for a file +src/main.cpp.s: + $(MAKE) -f CMakeFiles/./bin/resource-monitor.dir/build.make CMakeFiles/./bin/resource-monitor.dir/src/main.cpp.s +.PHONY : src/main.cpp.s + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... rebuild_cache" + @echo "... edit_cache" + @echo "... ./bin/resource-monitor" + @echo "... platform/linux/monitoring/cpu.o" + @echo "... platform/linux/monitoring/cpu.i" + @echo "... platform/linux/monitoring/cpu.s" + @echo "... src/main.o" + @echo "... src/main.i" + @echo "... src/main.s" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system + diff --git a/bin/core b/bin/core new file mode 100644 index 0000000..d324344 Binary files /dev/null and b/bin/core differ diff --git a/bin/resource-monitor b/bin/resource-monitor index 24d2188..05cf2bb 100755 Binary files a/bin/resource-monitor and b/bin/resource-monitor differ diff --git a/include/cpu.h b/include/cpu.h index d78d858..3601593 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -16,8 +17,12 @@ public: int UPDATE_INTERVAL; // s - std::vector> CPU_PREVIOUS_CORES_WORK_AND_TOTAL; - std::vector> CPU_CORES_WORK_AND_TOTAL; + // int CPU_PREVIOUS_CORES_WORK_AND_TOTAL[2048][8]; + // int CPU_CORES_WORK_AND_TOTAL[2048][8]; + + std::map> CPU_PREVIOUS_CORES_WORK_AND_TOTAL; + std::map> CPU_CORES_WORK_AND_TOTAL; + int CPU_HARDWARE_THREADS = std::thread::hardware_concurrency(); void START_CPU_POLLING(); static void CPU_POLL(CPU* cpu); @@ -26,8 +31,6 @@ public: double CPU_PERCENT(int core); std::vector CPU_CORE_PERCENT(); - unsigned int CPU_HARDWARE_THREADS = std::thread::hardware_concurrency(); - virtual ~CPU(); private: std::thread* m_pollThread; diff --git a/platform/linux/monitoring/cpu.cpp b/platform/linux/monitoring/cpu.cpp index 8d62d0a..5318a11 100644 --- a/platform/linux/monitoring/cpu.cpp +++ b/platform/linux/monitoring/cpu.cpp @@ -7,20 +7,10 @@ #include CPU::CPU() { + CPU_HARDWARE_THREADS = std::thread::hardware_concurrency(); this->UPDATE_INTERVAL = 1000; this->m_isPolling = false; std::cout << "Number of hardware threads supported: " << CPU_HARDWARE_THREADS << std::endl; - for (unsigned int thread; thread < CPU_HARDWARE_THREADS; thread++) { - std::vector temp1, temp2; - - for (int i = 0; i <= 2; i++) { - temp1.push_back(0); - temp2.push_back(0); - } - - CPU_PREVIOUS_CORES_WORK_AND_TOTAL.push_back(temp1); - CPU_CORES_WORK_AND_TOTAL.push_back(temp2); - } } void CPU::START_CPU_POLLING() { @@ -46,10 +36,11 @@ void CPU::CPU_POLL(CPU* cpu) { // READ TOTAL CPU unsigned long long luser, nice, system, idle, iowait, - irq, softirq; - FILE* ProcStat = fopen("/proc/stat", "r"); - fscanf(ProcStat, "cpu %llu %llu %llu %llu %llu %llu %llu", &luser, + irq, softirq = 0; + FILE* ProcStatCPU = fopen("/proc/stat", "r"); + fscanf(ProcStatCPU, "cpu %llu %llu %llu %llu %llu %llu %llu", &luser, &nice, &system, &idle, &iowait, &irq, &softirq); + fclose(ProcStatCPU); cpu->CPU_Mutex.lock(); @@ -62,17 +53,25 @@ void CPU::CPU_POLL(CPU* cpu) { cpu->CPU_Mutex.unlock(); for (unsigned int thread = 0; thread < cpu->CPU_HARDWARE_THREADS; thread++) { + // READ TOTAL THREAD CPU unsigned long long tluser, tnice, tsystem, tidle, tiowait, - tirq, tsoftirq; - - std::stringstream pattern; - pattern << "cpu" << thread << " %llu %llu %llu %llu %llu %llu %llu"; + tirq, tsoftirq = 0; + FILE* ProcStatCPUThread = fopen("/proc/stat", "r"); + // char c = fgetc(ProcStatCPU); + // while (c != -1) { + // printf ("%c", c); + // c = fgetc(ProcStatCPU); + // } + fscanf(ProcStatCPUThread, "cpu0 %llu %llu %llu %llu %llu %llu %llu", + &tluser, &tnice, &tsystem, &tidle, &tiowait, &tirq, &tsoftirq); + fclose(ProcStatCPUThread); - fscanf(ProcStat, pattern.str().c_str(), &tluser, &tnice, &tsystem, - &tidle, &tiowait, &tirq, &tsoftirq); + std::cout << tluser << " " << tnice << " " << tsystem << " " << tidle + << " " << tiowait << " " << tirq << " " << tsoftirq << std::endl; cpu->CPU_Mutex.lock(); + // CALCULATE TOTAL THREAD CPU cpu->CPU_PREVIOUS_CORES_WORK_AND_TOTAL[thread][0] = cpu->CPU_CORES_WORK_AND_TOTAL[thread][0]; cpu->CPU_PREVIOUS_CORES_WORK_AND_TOTAL[thread][1] = cpu->CPU_CORES_WORK_AND_TOTAL[thread][1]; cpu->CPU_CORES_WORK_AND_TOTAL[thread][0] = tluser + tnice + tsystem + tidle + tiowait + tirq + tsoftirq; @@ -81,7 +80,6 @@ void CPU::CPU_POLL(CPU* cpu) { cpu->CPU_Mutex.unlock(); } - fclose(ProcStat); sleep(1); } } @@ -96,12 +94,12 @@ void CPU::END_CPU_POLLING() { } double CPU::CPU_PERCENT(int core) { - core--; - - this->CPU_Mutex.lock(); + --core; long double totalOverTime = 0.0; long double workOverTime = 0.0; + this->CPU_Mutex.lock(); + if (core == -1) { totalOverTime = this->CPU_PREVIOUS_TOTAL - this->CPU_TOTAL; workOverTime = this->CPU_PREVIOUS_WORK - this->CPU_WORK; diff --git a/src/main.cpp b/src/main.cpp index a5a5733..ad850a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,12 +9,14 @@ int main(int argc, char** argv) { cpu->START_CPU_POLLING(); while(1) { - std::cout << "TOTAL CPU USAGE: " << cpu->CPU_PERCENT(0) << std::endl; - // std::cout << "CORE 1 USAGE: " << cpu->CPU_PERCENT(1) << std::endl; - // std::cout << "CORE 2 USAGE: " << cpu->CPU_PERCENT(2) << std::endl; - // std::cout << "CORE 3 USAGE: " << cpu->CPU_PERCENT(3) << std::endl; - // std::cout << "CORE 4 USAGE: " << cpu->CPU_PERCENT(4) << std::endl; - std::cout << std::endl; 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; + } + std::cout << std::endl; } }