sane makefile, sane structure, lots of updates
This commit is contained in:
190
main.cpp
190
main.cpp
@@ -13,32 +13,21 @@
|
||||
#include <thread>
|
||||
|
||||
#include "include/LCDDisplay.hpp"
|
||||
#include "include/linux-system-usage.hpp"
|
||||
#include "include/gpu.hpp"
|
||||
#include "include/utils.hpp"
|
||||
|
||||
#define LCD_PORT "/dev/ttyACM0"
|
||||
#define LCD_INTERVAL_MS 1000
|
||||
#define LCD_CONTRAST 225
|
||||
|
||||
struct gpuHwmon_t {
|
||||
std::string fan_speed;
|
||||
std::string core_clock;
|
||||
std::string mem_clock;
|
||||
std::string core_mv;
|
||||
std::string power_avg;
|
||||
std::string power_cap;
|
||||
std::string power_max;
|
||||
std::string fan_pwm;
|
||||
std::string edge_temp;
|
||||
std::string junction_temp;
|
||||
std::string mem_temp;
|
||||
};
|
||||
#define LCD_CONTRAST 215
|
||||
#define FILL_CHAR ' '
|
||||
|
||||
struct mem_usage_t {
|
||||
std::string free;
|
||||
std::string used;
|
||||
std::string total;
|
||||
uint64_t free;
|
||||
uint64_t used;
|
||||
uint64_t total;
|
||||
};
|
||||
|
||||
/* DISPLAY FUNCS */
|
||||
std::string pBytes(double i, bool isKbytes = false) {
|
||||
std::stringstream ss;
|
||||
std::array suffix = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'};
|
||||
@@ -46,24 +35,43 @@ std::string pBytes(double i, bool isKbytes = false) {
|
||||
if (s == 'K' && isKbytes) continue;
|
||||
i /= 1024.0;
|
||||
if (i < 1024) {
|
||||
ss << std::fixed << std::setprecision(i > 100 ? 1 : 2) << i << s;
|
||||
uint8_t p = 2;
|
||||
if (i > 10) p = 1;
|
||||
if (i > 100) p = 0;
|
||||
ss << std::fixed << std::setprecision(p) << i << s;
|
||||
return ss.str();
|
||||
}
|
||||
}
|
||||
return "++++";
|
||||
}
|
||||
|
||||
// converts number to string with given decimal places
|
||||
std::string pNum(auto num, uint8_t decs = 0) {
|
||||
std::stringstream ss;
|
||||
ss << std::fixed << std::setprecision(decs) << num;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// pads given string to size
|
||||
std::string pad(std::string s, uint8_t w, bool ljust = false) {
|
||||
if (w > s.size()) s.insert(ljust ? s.end() : s.begin(), w - s.size(), ' ');
|
||||
if (w > s.size()) s.insert(ljust ? s.end() : s.begin(), w - s.size(), FILL_CHAR);
|
||||
return s;
|
||||
}
|
||||
|
||||
// fills n spaces
|
||||
std::string fill(uint8_t n) {
|
||||
return std::string(n, FILL_CHAR);
|
||||
}
|
||||
|
||||
/* getters */
|
||||
std::string getAvgs() {
|
||||
std::stringstream ss;
|
||||
double avgs[3];
|
||||
getloadavg(avgs, 3);
|
||||
ss << std::fixed << std::setprecision(2);
|
||||
ss << avgs[0] << " " << avgs[1] << " " << avgs[2];
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
ss << std::fixed << std::setprecision(avgs[i] > 10 ? 1 : 2);
|
||||
ss << " " << avgs[i];
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
@@ -83,10 +91,15 @@ std::array<std::string, 2> getTimeDate() {
|
||||
|
||||
mem_usage_t getRam() {
|
||||
mem_usage_t mem;
|
||||
auto memory_data = get_system_usage_linux::read_memory_data();
|
||||
mem.free = pBytes(memory_data.available_memory, true);
|
||||
mem.used = pBytes(memory_data.total_memory - memory_data.available_memory, true);
|
||||
mem.total = pBytes(memory_data.total_memory, true);
|
||||
std::ifstream proc_meminfo("/proc/meminfo");
|
||||
|
||||
if (proc_meminfo.good()) {
|
||||
std::string content((std::istreambuf_iterator<char>(proc_meminfo)), std::istreambuf_iterator<char>());
|
||||
mem.free = get_val("MemAvailable:", content);
|
||||
mem.total = get_val("MemTotal:", content);
|
||||
mem.used = mem.total - mem.free;
|
||||
}
|
||||
proc_meminfo.close();
|
||||
return mem;
|
||||
}
|
||||
|
||||
@@ -94,87 +107,82 @@ mem_usage_t getDisk(std::string mount) {
|
||||
mem_usage_t disk;
|
||||
struct statvfs diskData;
|
||||
statvfs(mount.c_str(), &diskData);
|
||||
disk.free = pBytes(diskData.f_bfree * diskData.f_frsize);
|
||||
disk.used = pBytes((diskData.f_blocks - diskData.f_bfree) * diskData.f_frsize);
|
||||
disk.total = pBytes(diskData.f_blocks * diskData.f_frsize);
|
||||
disk.free = diskData.f_bfree * diskData.f_frsize;
|
||||
disk.used = (diskData.f_blocks - diskData.f_bfree) * diskData.f_frsize;
|
||||
disk.total = diskData.f_blocks * diskData.f_frsize;
|
||||
return disk;
|
||||
}
|
||||
|
||||
std::string readFile(std::string path, uint8_t trim = 0) {
|
||||
std::string ret;
|
||||
std::stringstream ss;
|
||||
std::ifstream fs(path);
|
||||
if (!fs.good()) {
|
||||
std::cerr << "Error reading " << path << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
ss << fs.rdbuf();
|
||||
fs.close();
|
||||
ret = ss.str();
|
||||
return ret.substr(0, ret.size() - trim - 1);
|
||||
}
|
||||
auto getCpuTemp(std::string hw) {
|
||||
struct {
|
||||
double tctl;
|
||||
double tccd1;
|
||||
} cpu;
|
||||
|
||||
gpuHwmon_t getGpu(std::string hw) {
|
||||
gpuHwmon_t gpu;
|
||||
|
||||
gpu.fan_speed = readFile(hw + "/fan1_input");
|
||||
gpu.core_clock = readFile(hw + "/freq1_input", 6);
|
||||
gpu.mem_clock = readFile(hw + "/freq2_input", 6);
|
||||
gpu.core_mv = readFile(hw + "/in0_input");
|
||||
gpu.power_avg = readFile(hw + "/power1_average", 6);
|
||||
gpu.power_cap = readFile(hw + "/power1_cap", 6);
|
||||
gpu.power_max = readFile(hw + "/power1_cap_max", 6);
|
||||
gpu.fan_pwm = readFile(hw + "/pwm1");
|
||||
gpu.edge_temp = readFile(hw + "/temp1_input", 3);
|
||||
gpu.junction_temp = readFile(hw + "/temp2_input", 3);
|
||||
gpu.mem_temp = readFile(hw + "/temp3_input", 3);
|
||||
|
||||
return gpu;
|
||||
}
|
||||
|
||||
std::string getCpuTemp() {
|
||||
return readFile("/sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon3/temp1_input");
|
||||
cpu.tctl = (double)(std::stoul(readFile(hw + "/temp1_input")) / 1000.0);
|
||||
cpu.tccd1 = (double)(std::stoul(readFile(hw + "/temp3_input")) / 1000.0);
|
||||
return cpu;
|
||||
}
|
||||
|
||||
void collectAndDraw(LCDDisplay *lcd) {
|
||||
auto td = getTimeDate();
|
||||
auto ram = getRam();
|
||||
auto anime = getDisk("/mnt/anime");
|
||||
auto home = getDisk("/var/home/drath");
|
||||
auto gpu = getGpu("/sys/devices/pci0000:00/0000:00:03.1/0000:08:00.0/0000:09:00.0/0000:0a:00.0/hwmon/hwmon2");
|
||||
|
||||
std::array<std::string, 4> lines;
|
||||
|
||||
lines[0] = td[1] + " " + td[0] + pad(getAvgs(), 20);
|
||||
|
||||
lines[1] = pad(ram.used + "/" + ram.total, 13, true) + " ";
|
||||
lines[1] += pad(gpu.fan_speed + "R", 5) + " ";
|
||||
lines[1] += pad(gpu.power_avg + "/" + gpu.power_cap + "W", 8) + " ";
|
||||
lines[1] += pad(gpu.edge_temp + "/" + gpu.junction_temp + "C", 8);
|
||||
|
||||
lines[2] = "/home/drath:" + pad(home.free + " free of " + home.total, 28);
|
||||
|
||||
lines[3] = "/mnt/anime: " + pad(anime.free + " free of " + anime.total, 28);
|
||||
|
||||
lcd->writeLines(lines);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
bool daemon_mode = (argc > 1 && std::string(argv[1]) == "-d");
|
||||
|
||||
/* init lcd, set contrast and backlight */
|
||||
LCDDisplay lcd(LCD_PORT, 0010002);
|
||||
|
||||
if (argc > 1 && std::string(argv[1]) == "-d") {
|
||||
std::cout << "[Chrono] Running in daemon mode, " << LCD_INTERVAL_MS << "ms interval" << std::endl;
|
||||
if (daemon_mode) {
|
||||
lcd.setContrast(LCD_CONTRAST);
|
||||
lcd.setBacklight(true);
|
||||
std::chrono::milliseconds interval(LCD_INTERVAL_MS);
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
while (true) {
|
||||
collectAndDraw(&lcd);
|
||||
}
|
||||
|
||||
/* initial vars */
|
||||
std::array<std::string, 4> lines;
|
||||
auto cpu_hwmon = findHwmonPath("k10temp");
|
||||
auto gpu_hwmon = "/sys/class/drm/card1/device";
|
||||
|
||||
/* loop setup */
|
||||
std::cout << "[Chrono] Running in daemon mode, " << LCD_INTERVAL_MS << "ms interval" << std::endl;
|
||||
std::chrono::milliseconds interval(LCD_INTERVAL_MS);
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
|
||||
/* main loop */
|
||||
while (true) {
|
||||
auto td = getTimeDate();
|
||||
auto ram = getRam();
|
||||
auto anime = getDisk("/mnt/anime");
|
||||
auto home = getDisk("/var/home/drath");
|
||||
auto gpu = getGpu(gpu_hwmon);
|
||||
auto cpu = getCpuTemp(cpu_hwmon);
|
||||
|
||||
lines[0] = td[1] + " " + td[0] + pad(getAvgs(), 20);
|
||||
|
||||
lines[1] = pad(pNum(cpu.tctl) + "/" + pNum(cpu.tccd1) + "C", 8, true);
|
||||
lines[1] += pad(pBytes(ram.used, true) + "B", 7, true);
|
||||
lines[1] += fill(7);
|
||||
lines[1] += pad(pBytes(gpu.vram_used) + "B", 7);
|
||||
lines[1] += pad(std::to_string(gpu.edge_temp) + "/" + std::to_string(gpu.hotspot_temp) + "/" + std::to_string(gpu.mem_temp) + "C", 11);
|
||||
|
||||
lines[2] = pad("home: " + pBytes(home.free), 11, true);
|
||||
lines[2] += fill(15);
|
||||
lines[2] += pad(pNum(gpu.fan_speed) + "R", 5);
|
||||
lines[2] += pad(std::to_string(gpu.power_avg) + "/" + std::to_string(gpu.power_cap) + "W", 9);
|
||||
|
||||
lines[3] = pad("anime: " + pBytes(anime.free), 13, true);
|
||||
lines[3] += fill(8);
|
||||
lines[3] += pad(pNum(gpu.core_clock) + "/" + pNum(gpu.mem_clock) + "MHz ", 13);
|
||||
lines[3] += pad(pNum(gpu.vcore, 3) + "V", 5);
|
||||
|
||||
lcd.writeLines(lines);
|
||||
|
||||
if (daemon_mode) {
|
||||
std::this_thread::sleep_until(start + interval);
|
||||
start += interval;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
collectAndDraw(&lcd);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user