From 2d05ed99b44fedfd4e85524a35481f31d045673f Mon Sep 17 00:00:00 2001 From: Or Hirshfeld Date: Thu, 16 May 2024 14:58:13 +0200 Subject: [PATCH 1/2] [feat] add easy handler wrapper to get CPU directy Created this to use this libaray inside other project easily Or Hirshfeld 2024-May-16 14:57 CET on Ubuntu18 --- CMakeLists.txt | 25 ++++++++++++++ src/CPUSnapshot.cpp | 17 ++++++++++ src/CPUSnapshot.h | 7 ++++ src/CPUStat.h | 73 +++++++++++++++++++++++++++++++++++++++++ src/CPUStatsPrinter.cpp | 27 +++++++++++++++ src/CPUStatsPrinter.h | 4 +++ src/main.cpp | 10 ++++++ 7 files changed, 163 insertions(+) create mode 100644 src/CPUStat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 110dc07..20dd1e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,23 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable( cpu-stat src/main.cpp + src/CPUStat.h + src/CPUData.h + src/CPUSnapshot.h + src/CPUStatsPrinter.h + src/OptionsParser.h + src/CPUData.cpp + src/CPUSnapshot.cpp + src/CPUStatsPrinter.cpp + src/OptionsParser.cpp +) + + + +ADD_LIBRARY( + cpu-stat-lib STATIC + #src/main.cpp + src/CPUStat.h src/CPUData.h src/CPUSnapshot.h src/CPUStatsPrinter.h @@ -24,6 +41,14 @@ target_include_directories( PRIVATE src/ ) +target_include_directories( + cpu-stat-lib + PUBLIC src/ +) + +# Add the -fPIC flag to the compilation options for your_library_name +set_target_properties(cpu-stat-lib PROPERTIES POSITION_INDEPENDENT_CODE ON) + # installation include(GNUInstallDirs) install(TARGETS cpu-stat diff --git a/src/CPUSnapshot.cpp b/src/CPUSnapshot.cpp index b9819bf..99add13 100644 --- a/src/CPUSnapshot.cpp +++ b/src/CPUSnapshot.cpp @@ -8,6 +8,8 @@ const int CPUSnapshot::INDEX_TOT = 0; CPUSnapshot::CPUSnapshot() { + timeTaken = std::chrono::high_resolution_clock::now(); + std::ifstream fileStat("/proc/stat"); std::string line; @@ -27,3 +29,18 @@ CPUSnapshot::CPUSnapshot() fileStat.close(); } + +double CPUSnapshot::GetDurationFromConstruct() +{ + auto currentTime = std::chrono::high_resolution_clock::now(); + + std::chrono::duration ms_double = currentTime - timeTaken; + + return ms_double.count(); +} + +bool CPUSnapshot::didPassDurationFromConstructMs(double durationMs) +{ + return (durationMs > GetDurationFromConstruct()) ; +} + diff --git a/src/CPUSnapshot.h b/src/CPUSnapshot.h index 8f0a900..dfd1225 100644 --- a/src/CPUSnapshot.h +++ b/src/CPUSnapshot.h @@ -5,6 +5,7 @@ #include "CPUData.h" #include +#include class CPUSnapshot { @@ -28,9 +29,15 @@ class CPUSnapshot std::size_t GetTotalTimeTotal() const; std::size_t GetTotalTime(unsigned int cpu) const; + double GetDurationFromConstruct(); + + bool didPassDurationFromConstructMs(double durationMs); + private: static const int INDEX_TOT; + std::chrono::time_point timeTaken = std::chrono::high_resolution_clock::now(); + private: std::vector mEntries; }; diff --git a/src/CPUStat.h b/src/CPUStat.h new file mode 100644 index 0000000..fc8a999 --- /dev/null +++ b/src/CPUStat.h @@ -0,0 +1,73 @@ +#pragma once + +#include "CPUData.h" +#include "CPUSnapshot.h" +#include "CPUStatsPrinter.h" + +#include + + +class CPUStat +{ + public: + CPUStat() + { + lastSnapshot = CPUSnapshot(); + steadySnapshot = lastSnapshot; + }; + + std::string GetActivePercentageCPU(unsigned int cpu) + { + + auto newSnapshot = CPUSnapshot(); + + auto cpuStatsPrinter = CPUStatsPrinter(steadySnapshot,newSnapshot); + + auto retrunString = cpuStatsPrinter.GetActivePercentageCPU(cpu); + + if(lastSnapshot.didPassDurationFromConstructMs(100)) + steadySnapshot = lastSnapshot; + lastSnapshot = newSnapshot; + + + return retrunString; + } + + std::string GetActivePercentageCPU(unsigned int cpuA, unsigned int cpuB) + { + + auto newSnapshot = CPUSnapshot(); + + auto cpuStatsPrinter = CPUStatsPrinter(lastSnapshot,newSnapshot); + + auto retrunString = "CPU" + std::to_string(cpuA)+ ":" + cpuStatsPrinter.GetActivePercentageCPU(cpuA); + + retrunString += ",CPU" + std::to_string(cpuB) + ":" +cpuStatsPrinter.GetActivePercentageCPU(cpuB); + + lastSnapshot = newSnapshot; + + return retrunString; + } + + std::string GetActivePercentageCPU(std::vector cpus) + { + auto newSnapshot = CPUSnapshot(); + + auto cpuStatsPrinter = CPUStatsPrinter(lastSnapshot,newSnapshot); + + std::string retrunString=""; + + for(auto& cpu : cpus) + retrunString += cpuStatsPrinter.GetActivePercentageCPU(cpu) + ";"; + + lastSnapshot = newSnapshot; + + return retrunString; + } + + + private: + CPUSnapshot lastSnapshot; + CPUSnapshot steadySnapshot; + +}; diff --git a/src/CPUStatsPrinter.cpp b/src/CPUStatsPrinter.cpp index 1d9944d..6c8c0bb 100644 --- a/src/CPUStatsPrinter.cpp +++ b/src/CPUStatsPrinter.cpp @@ -69,6 +69,33 @@ void CPUStatsPrinter::PrintActivePercentageCPU(unsigned int cpu) std::cout << std::endl; } +std::string CPUStatsPrinter::GetActivePercentageCPU(unsigned int cpu) +{ + std::stringstream ss; + + if(cpu >= mS1.GetNumEntries()) + { + ss << "ERROR - CPU " << cpu << " not available."; + return ss.str(); + } + + if(mVerbose) + { + ss.width(CPU_LABEL_W); + ss << mS1.GetLabel(cpu) << "] "; + } + + ss.setf(std::ios::fixed, std::ios::floatfield); + ss.precision(mPrecision); + ss << GetPercActive(cpu); + + if(mVerbose) + ss << "%"; + + return ss.str(); +} + + void CPUStatsPrinter::PrintActivePercentageAll() { // PRINT TOTAL diff --git a/src/CPUStatsPrinter.h b/src/CPUStatsPrinter.h index 9e91388..aabb7ff 100644 --- a/src/CPUStatsPrinter.h +++ b/src/CPUStatsPrinter.h @@ -3,6 +3,8 @@ // CPUStatsPrinter - object which prints stats extracted by 2 CPUSnapshots. #include "CPUData.h" +//#include "CPUSnapshot.h" +#include class CPUSnapshot; @@ -13,6 +15,8 @@ class CPUStatsPrinter void PrintActivePercentageTotal(); void PrintActivePercentageCPU(unsigned int cpu); + std::string GetActivePercentageCPU(unsigned int cpu); + void PrintActivePercentageAll(); void PrintStatePercentageTotal(unsigned int state); diff --git a/src/main.cpp b/src/main.cpp index 628ec3c..dd3250c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,9 +15,11 @@ #include "CPUSnapshot.h" #include "CPUStatsPrinter.h" #include "OptionsParser.h" +#include "CPUStat.h" #include #include +#include // -- PROGRAM DATA -- const char * STR_APP_NAME = "cpu-stat"; @@ -36,6 +38,8 @@ int main(int argc, char * argv[]) if(options.ExitRequested()) return 0; + CPUStat cpuStat; + // -- GET SNAPSHOTS -- // snapshot 1 CPUSnapshot s1; @@ -46,6 +50,10 @@ int main(int argc, char * argv[]) // snapshot 2 CPUSnapshot s2; + std::cout << cpuStat.GetActivePercentageCPU(4,5) << std::endl; + + return 0; + // -- PRINT STATS -- CPUStatsPrinter printer(s1, s2); @@ -69,6 +77,8 @@ int main(int argc, char * argv[]) printer.PrintStatePercentageCPU(options.GetOptionState(), options.GetOptionCPU()); else printer.PrintActivePercentageCPU(options.GetOptionCPU()); + std::cout << "GetActivePercentageCPU:" << printer.GetActivePercentageCPU(options.GetOptionCPU()) << std::endl; + } else { From 4465aba3985696fd3abb0e101070f94a62386172 Mon Sep 17 00:00:00 2001 From: Or Hirshfeld Date: Thu, 16 May 2024 15:07:51 +0200 Subject: [PATCH 2/2] [feat] add instllation for cpus-stat-lib as well --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 20dd1e2..85d87a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,3 +55,9 @@ install(TARGETS cpu-stat LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) + +# Install the library +install(TARGETS cpu-stat-lib + EXPORT cpu-stat-lib-targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +)