diff --git a/src/cpu.c b/src/cpu.c index 514abbe..6b0d084 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -6,6 +6,8 @@ #include #include +static uint64_t max_energy_range_uj = 0; + void cpu_detect(struct cpu_sensors_t *t, const char *pci_dev) { t->module = unknown; t->num_cores = sysconf(_SC_NPROCESSORS_ONLN); @@ -18,6 +20,8 @@ void cpu_detect(struct cpu_sensors_t *t, const char *pci_dev) { return; } + static char buf[32]; + fast_read_num_u64("/sys/class/powercap/intel-rapl:0/", "max_energy_range_uj", buf, &max_energy_range_uj, 1); t->module = read_file(t->hwmon, "/in1_input", nullptr, 0) ? zenpower : k10temp; // if we can read zenpower file then zenpower. idk if this is reliable, we're trying to avoid just reading the name applog(LOG_INF, "Detected %d cores, module enum #%d in path %s", t->num_cores, t->module, t->hwmon); } @@ -31,13 +35,20 @@ void cpu_update(struct cpu_sensors_t *c) { static struct energy_t prev; struct energy_t curr = {}; + uint64_t energy_delta; // read power from intel-rapl? works on amd too, btop uses this trick, seems accurate // it tracks with "socket power" in hwinfo on windows, its actually the same thing i had in lcdsmartie clock_gettime(CLOCK_MONOTONIC, &curr.ts); fast_read_num_u64("/sys/class/powercap/intel-rapl:0/", "energy_uj", buf, &curr.energy, 1); - c->ptotal = (float)(curr.energy - prev.energy) / (float)timespec_diff_us(&curr.ts, &prev.ts); + if (curr.energy >= prev.energy) + energy_delta = curr.energy - prev.energy; + else + energy_delta = (max_energy_range_uj - prev.energy) + curr.energy; + + c->ptotal = (float)energy_delta / (float)timespec_diff_us(&curr.ts, &prev.ts); + if (c->ptotal > 500.0f || c->ptotal < 0.0f) c->ptotal = 0.0f; else diff --git a/src/sys.c b/src/sys.c index 540d2ec..9203387 100644 --- a/src/sys.c +++ b/src/sys.c @@ -69,10 +69,12 @@ void sys_get_network(struct network_t *network) { fast_read_num_u64(path, "rx_bytes", buf, ¤t.rx, 1); fast_read_num_u64(path, "tx_bytes", buf, ¤t.tx, 1); const uint64_t diff_us = timespec_diff_us(¤t.ts, &prev.ts); + const uint64_t rx_delta = current.rx >= prev.rx ? current.rx - prev.rx : 0; + const uint64_t tx_delta = current.tx >= prev.tx ? current.tx - prev.tx : 0; const double diff_s = (double)diff_us / 1000000.0; - network->download = (uint32_t)((double)(current.rx - prev.rx) / diff_s); - network->upload = (uint32_t)((double)(current.tx - prev.tx) / diff_s); - if (network->upload < 2000000000ULL && network->download < 2000000000ULL) // 2gb/s + network->download = (uint32_t)((double)rx_delta / diff_s); + network->upload = (uint32_t)((double)tx_delta / diff_s); + if (rx_delta < 2000000000ULL && tx_delta < 2000000000ULL) prev = current; }