Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,9 +41,23 @@ 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
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}
)
17 changes: 17 additions & 0 deletions src/CPUSnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,3 +29,18 @@ CPUSnapshot::CPUSnapshot()

fileStat.close();
}

double CPUSnapshot::GetDurationFromConstruct()
{
auto currentTime = std::chrono::high_resolution_clock::now();

std::chrono::duration<double, std::milli> ms_double = currentTime - timeTaken;

return ms_double.count();
}

bool CPUSnapshot::didPassDurationFromConstructMs(double durationMs)
{
return (durationMs > GetDurationFromConstruct()) ;
}

7 changes: 7 additions & 0 deletions src/CPUSnapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "CPUData.h"

#include <vector>
#include <chrono>

class CPUSnapshot
{
Expand All @@ -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<std::chrono::high_resolution_clock> timeTaken = std::chrono::high_resolution_clock::now();

private:
std::vector<CPUData> mEntries;
};
Expand Down
73 changes: 73 additions & 0 deletions src/CPUStat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once

#include "CPUData.h"
#include "CPUSnapshot.h"
#include "CPUStatsPrinter.h"

#include <chrono>


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<unsigned int> 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;

};
27 changes: 27 additions & 0 deletions src/CPUStatsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/CPUStatsPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// CPUStatsPrinter - object which prints stats extracted by 2 CPUSnapshots.

#include "CPUData.h"
//#include "CPUSnapshot.h"
#include <sstream>

class CPUSnapshot;

Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#include "CPUSnapshot.h"
#include "CPUStatsPrinter.h"
#include "OptionsParser.h"
#include "CPUStat.h"

#include <chrono>
#include <thread>
#include <iostream>

// -- PROGRAM DATA --
const char * STR_APP_NAME = "cpu-stat";
Expand All @@ -36,6 +38,8 @@ int main(int argc, char * argv[])
if(options.ExitRequested())
return 0;

CPUStat cpuStat;

// -- GET SNAPSHOTS --
// snapshot 1
CPUSnapshot s1;
Expand All @@ -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);

Expand All @@ -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
{
Expand Down