Skip to content

Commit

Permalink
ram and cpu usage for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 23, 2024
1 parent 5d810be commit 3f70969
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 35 deletions.
89 changes: 64 additions & 25 deletions src/systeminfo/SystemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,55 +47,94 @@ tsunami_lab::systeminfo::SystemInfo::SystemInfo()
#endif
}

void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(long long &o_totalRAM, long long &o_usedRAM)
void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double &o_usedRAM)
{
#ifdef __linux__
struct sysinfo memInfo;
sysinfo(&memInfo);
long long totalPhysMem = memInfo.totalram;
// Multiply in next statement to avoid int overflow on right hand side...
totalPhysMem *= memInfo.mem_unit;
totalPhysMem *= m_const;
o_totalRAM = totalPhysMem;

long long physMemUsed = memInfo.totalram - memInfo.freeram;
// Multiply in next statement to avoid int overflow on right hand side...
physMemUsed *= memInfo.mem_unit;
physMemUsed *= m_const;
o_usedRAM = physMemUsed;

#elif __APPLE__ || __MACH__
int mib[2];
int64_t physical_memory;
long long physical_memoryLong;
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
length = sizeof(int64_t);
sysctl(mib, 2, &physical_memory, &length, NULL, 0);
o_totalRAM = HW_MEMSIZE;

vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics64_data_t vm_stats;

mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO,
(host_info64_t)&vm_stats, &count))
unsigned long length = sizeof(int64_t);
sysctl(mib, 2, &physical_memoryLong, &length, NULL, 0);
double physical_memory = physical_memoryLong;
physical_memory *= m_const;
o_totalRAM = physical_memory;

std::array<char, 128> buffer;
std::string result;
// get page size
int pagesize = 0;
std::unique_ptr<FILE, decltype(&pclose)> pipePagesize(popen("pagesize", "r"), pclose);
if (!pipePagesize)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipePagesize.get()) != nullptr)
{
pagesize = std::stoi(buffer.data());
}

long pages = 0;
// get app memory
// = anonymous - pages purgeable
std::unique_ptr<FILE, decltype(&pclose)> pipeAppMemPages(popen("vm_stat | LC_NUMERIC='C' awk '{if (NR==8) { sum=-$3 } else if(NR==15) {sum+=$3}} END { print sum }'", "r"), pclose);
if (!pipeAppMemPages)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipeAppMemPages.get()) != nullptr)
{
result = buffer.data();
}
pages += std::stol(result);

// get wired memory
std::unique_ptr<FILE, decltype(&pclose)> pipeWiredMem(popen("vm_stat | LC_NUMERIC='C' awk 'NR==7 { sum+=$4 }; END { print sum }'", "r"), pclose);
if (!pipeWiredMem)
{
long long used_memory = ((int64_t)vm_stats.active_count +
(int64_t)vm_stats.inactive_count +
(int64_t)vm_stats.wire_count) *
(int64_t)page_size;
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipeWiredMem.get()) != nullptr)
{
result = buffer.data();
}
pages += std::stol(result);

o_usedRAM = used_memory;
// get compressed memory
std::unique_ptr<FILE, decltype(&pclose)> pipeCompMem(popen("vm_stat | LC_NUMERIC='C' awk 'NR==17 { sum+=$5 }; END { print sum }'", "r"), pclose);
if (!pipeCompMem)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipeCompMem.get()) != nullptr)
{
result = buffer.data();
}
pages += std::stol(result);

o_usedRAM = (pages * (double)pagesize) * m_const;
#endif
}

std::string tsunami_lab::systeminfo::SystemInfo::getCPUUsage()
std::vector<float> tsunami_lab::systeminfo::SystemInfo::getCPUUsage()
{
std::array<char, 128> buffer;
std::string result;
std::vector<float> result;

#ifdef __linux__
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("top -bn 1 -1 | grep '^%Cpu'", "r"), pclose);
Expand All @@ -108,14 +147,14 @@ std::string tsunami_lab::systeminfo::SystemInfo::getCPUUsage()
result += buffer.data();
}
#elif __APPLE__ || __MACH__
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("top -l 2 | grep -E '^CPU' | tail -1", "r"), pclose);
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("top -l 2 | grep -E '^CPU' | tail -1 | LC_NUMERIC='C' awk '{s=$3+$5} END {print s}' &", "r"), pclose);
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
result += buffer.data();
result.push_back(std::stof(buffer.data()));
}
#endif

Expand Down
6 changes: 4 additions & 2 deletions src/systeminfo/SystemInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ namespace tsunami_lab

class tsunami_lab::systeminfo::SystemInfo
{
private:
double m_const = 0.00000000093132257;// 1/(1024*1024*1024) Converts bytes to GB
public:
SystemInfo();

void getRAMUsage(long long &o_totalRAM, long long &o_usedRAM);
std::string getCPUUsage();
void getRAMUsage(double &o_totalRAM, double &o_usedRAM);
std::vector<float> getCPUUsage();
};
#endif
29 changes: 21 additions & 8 deletions src/ui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include <chrono>
#include <algorithm>

#include <thread>
#include <future>

#include "../systeminfo/SystemInfo.h"

static void glfw_error_callback(int error, const char *description)
Expand Down Expand Up @@ -186,10 +189,10 @@ int tsunami_lab::ui::GUI::launch()

tsunami_lab::systeminfo::SystemInfo systemInfo;
auto fiveseconds = std::chrono::system_clock::now() + std::chrono::seconds(5);
std::string cpu = "no info";
long long totalRAM = 0;
long long usedRAM = 0;

std::vector<float> cpu;
double totalRAM = 0;
double usedRAM = 0;
std::thread t1;
// Main loop
while (!glfwWindowShouldClose(window))
{
Expand All @@ -208,10 +211,12 @@ int tsunami_lab::ui::GUI::launch()

if (fiveseconds <= std::chrono::system_clock::now())
{
cpu = systemInfo.getCPUUsage();
systemInfo.getRAMUsage(totalRAM, usedRAM);
std::thread t1([&systemInfo, &cpu] {cpu = systemInfo.getCPUUsage();});
t1.detach();

std::thread t2([&systemInfo, &totalRAM, &usedRAM] {systemInfo.getRAMUsage(totalRAM, usedRAM);});
t2.detach();
fiveseconds = std::chrono::system_clock::now() + std::chrono::seconds(5);
std::cout << usedRAM/1000000 << " / " << totalRAM/1000000 << "MB ("<< (double)usedRAM/totalRAM<< std::endl;
}

updateData();
Expand All @@ -222,7 +227,15 @@ int tsunami_lab::ui::GUI::launch()
// Main window
{
ImGui::Begin("Welcome to the Tsunami Simulator GUI!");
ImGui::Text("%s", cpu.c_str());

for (unsigned long i = 0; i < cpu.size(); ++i)
{
ImGui::ProgressBar(cpu[i] / 100, ImVec2(0.0f, 0.0f));
ImGui::SameLine();
ImGui::Text("%s %lu", "CPU: ", i);
}
ImGui::Text("%f / %f GB RAM usage", usedRAM, totalRAM);

//-------------------------------------------//
//-----------------MAIN TABS-----------------//
//-------------------------------------------//
Expand Down

0 comments on commit 3f70969

Please sign in to comment.