Skip to content
Merged
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
Binary file added bin/sysMood
Binary file not shown.
180 changes: 154 additions & 26 deletions include/cpu_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,176 @@
// - this file is mostly chill, unless your system's on fire 🔥
// - not responsible if your computer gets feelings 🥲
// - if you find a bug, please report it to me on github
//
//
#define _WIN32_WINNT 0x0602
#include <stdio.h>
#include <cstdint>
#include <windows.h>
#include <stdlib.h>
uint64_t FromFileTime( const FILETIME& ft ) {
ULARGE_INTEGER uli = { 0 };
#ifdef _WIN32
#include <windows.h>
#elif __linux__
#include <unistd.h>
#include <sys/time.h>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#endif

#ifdef _WIN32
uint64_t FromFileTime(const FILETIME &ft)
{
ULARGE_INTEGER uli = {0};
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart;
}
#elif __linux__
struct CpuTimes
{
uint64_t user, nice, system, idle, iowait, irq, softirq, steal;
uint64_t getTotalTime() const
{
return user + nice + system + idle + iowait + irq + softirq + steal;
}
uint64_t getIdleTime() const
{
return idle + iowait;
}
};

struct CPUStats
{
uint64_t ctxt;
uint64_t btime;
uint64_t processes;
uint64_t procs_running;
uint64_t procs_blocked;
};

CpuTimes getCpuTimes()
{
std::ifstream file("/proc/stat");
if (!file.is_open())
{
std::cerr << "Could not open /proc/stat" << std::endl;
return CpuTimes{0};
}
std::string line;
std::getline(file, line);

std::istringstream ss(line);
std::string cpu_label;
CpuTimes times = {0};

ss >> cpu_label >> times.user >> times.nice >> times.system >> times.idle >> times.iowait >> times.irq >> times.softirq >> times.steal;

return times;
}

CPUStats getCPUstats()
{
std::ifstream file("/proc/stat");
CPUStats stats = {0};
if (!file.is_open())
{
std::cerr << "Could not open /proc/stat" << std::endl;
return stats;
}
std::string line;
while (std::getline(file, line))
{
std::istringstream ss(line);
std::string key;
ss >> key;
if (key == "ctxt")
{
ss >> stats.ctxt;
}
else if (key == "btime")
{
ss >> stats.btime;
}
else if (key == "processes")
{
ss >> stats.processes;
}
else if (key == "procs_running")
{
ss >> stats.procs_running;
}
else if (key == "procs_blocked")
{
ss >> stats.procs_blocked;
}
}
return stats;
}
#endif

class Processor{};
class Processor
{
};

class Usage: public Processor
class Usage : public Processor
{
public:
#ifdef _WIN32
uint64_t FromFileTime(const FILETIME &ft)
{
public:
ULARGE_INTEGER uli = {0};
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart;
}

int now(){
FILETIME i0, i1, k0, k1, u0, u1;
GetSystemTimes(&i0, &k0, &u0);
SleepEx(1000, false);
GetSystemTimes(&i1, &k1, &u1);
int now()
{
FILETIME i0, i1, k0, k1, u0, u1;
GetSystemTimes(&i0, &k0, &u0);
SleepEx(1000, false);
GetSystemTimes(&i1, &k1, &u1);

uint64_t idle0 = FromFileTime(i0);
uint64_t idle1 = FromFileTime(i1);
uint64_t kernel0 = FromFileTime(k0);
uint64_t kernel1 = FromFileTime(k1);
uint64_t user0 = FromFileTime(u0);
uint64_t user1 = FromFileTime(u1);

uint64_t idle0 = FromFileTime(i0);
uint64_t idle1 = FromFileTime(i1);
uint64_t kernel0 = FromFileTime(k0);
uint64_t kernel1 = FromFileTime(k1);
uint64_t user0 = FromFileTime(u0);
uint64_t user1 = FromFileTime(u1);
uint64_t idle = idle1 - idle0;
uint64_t kernel = kernel1 - kernel0;
uint64_t user = user1 - user0;

uint64_t idle = idle1 - idle0;
uint64_t kernel = kernel1 - kernel0;
uint64_t user = user1 - user0;
uint64_t total = (kernel + user) - idle;
double cpu = (1.0 - (double)idle / (kernel + user)) * 100.0;
return static_cast<int>(cpu);
}

double cpu = (1.0 - (double)idle / (kernel + user)) * 100.0;
#elif __linux__
int now()
{
CpuTimes times1 = getCpuTimes();
sleep(1);
CpuTimes times2 = getCpuTimes();

return static_cast<int>(cpu);
uint64_t idle1 = times1.getIdleTime();
uint64_t idle2 = times2.getIdleTime();
uint64_t total1 = times1.getTotalTime();
uint64_t total2 = times2.getTotalTime();

}
uint64_t idle_diff = idle2 - idle1;
uint64_t total_diff = total2 - total1;

if (total_diff == 0)
return 0; // Avoid division by zero

double cpu = (double)(total_diff - idle_diff) / total_diff * 100.0;
return static_cast<int>(cpu);
}
CPUStats getCPUstats()
{
return ::getCPUstats();
}

};
#endif
};
90 changes: 77 additions & 13 deletions include/memory_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,104 @@
//
#include <stdio.h>
#include <cstdint>
#include <windows.h>
#include <stdlib.h>

#ifdef _WIN32
#include <windows.h>
#define DIV 1024
#elif __linux__
#include <fstream>
#include <string>
#include <sstream>
#include <unistd.h>
#define DIV 1024
#endif

class Memory {
public:
#ifdef _WIN32
int memory_percent(){
MEMORYSTATUSEX statusex;

statusex.dwLength = sizeof(statusex);

GlobalMemoryStatusEx(&statusex);
long long percent = statusex.dwMemoryLoad;

return percent;
return statusex.dwMemoryLoad;
}

int memory_available(){
MEMORYSTATUSEX statusex;

statusex.dwLength = sizeof(statusex);

GlobalMemoryStatusEx(&statusex);
long long available = statusex.ullAvailPhys / DIV / DIV;

return available;
return statusex.ullAvailPhys / DIV / DIV;
}

int memory_total(){
MEMORYSTATUSEX statusex;
statusex.dwLength = sizeof(statusex);
GlobalMemoryStatusEx(&statusex);
long long total = statusex.ullTotalPhys / DIV / DIV;
return total;
return statusex.ullTotalPhys / DIV / DIV;
}

int memory_used(){
MEMORYSTATUSEX statusex;
statusex.dwLength = sizeof(statusex);
GlobalMemoryStatusEx(&statusex);
long long used = statusex.ullTotalPhys - statusex.ullAvailPhys;
return used / DIV / DIV;
}

#elif __linux__
private:
struct MemInfo {
long long total;
long long free;
long long available;
long long buffers;
long long cached;
};

MemInfo getMemInfo() {
MemInfo info = {0};
std::ifstream file("/proc/meminfo");
std::string line;

while (std::getline(file, line)) {
std::istringstream iss(line);
std::string key;
long long value;
std::string unit;

if (iss >> key >> value >> unit) {
if (key == "MemTotal:") info.total = value;
else if (key == "MemFree:") info.free = value;
else if (key == "MemAvailable:") info.available = value;
else if (key == "Buffers:") info.buffers = value;
else if (key == "Cached:") info.cached = value;
}
}
return info;
}

public:
int memory_percent(){
MemInfo info = getMemInfo();
if (info.total == 0) return 0;
long long used = info.total - info.available;
return (used * 100) / info.total;
}

int memory_available(){
MemInfo info = getMemInfo();
return info.available / DIV;
}

int memory_total(){
MemInfo info = getMemInfo();
return info.total / DIV;
}

int memory_used(){
MemInfo info = getMemInfo();
return (info.total - info.available) / DIV;
}
#endif
};
Loading