use std::array instead of c-style bufs

This commit is contained in:
2026-01-02 13:44:01 +01:00
parent 27350122b2
commit 0eb401cdcc
3 changed files with 21 additions and 18 deletions

View File

@@ -1,3 +1,4 @@
#include <array>
#include <chrono>
#include <cpu.hpp>
#include <cstdint>
@@ -22,7 +23,7 @@ void detectHwmon(cpuSensors* target, const std::string& pci) {
}
void update(cpuSensors* c) {
static char buf[32];
static std::array<char, 32> buf;
struct EnergyTs {
uint64_t energy = 0;
std::chrono::time_point<std::chrono::steady_clock> ts;
@@ -41,8 +42,9 @@ void update(cpuSensors* c) {
// lots of reads but still faster than reading /proc/cpuinfo
c->avg_freq = 0;
for (uint8_t i = 0; i < N_CPU; i++) {
utils::readFile("/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_avg_freq", buf, sizeof(buf));
c->avg_freq += strtol(buf, nullptr, 10) / 1000;
uint32_t t = 0;
utils::fastReadNum<1000>("/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_avg_freq", buf, t);
c->avg_freq += t;
}
c->avg_freq /= N_CPU;

View File

@@ -1,3 +1,4 @@
#include <array>
#include <gpu.hpp>
#include <gpu_metrics.hpp>
#include <log.hpp>
@@ -24,8 +25,8 @@ void detectHwmon(gpuSensors* target, const std::string& pci) {
}
/* some metrics are missing in gpu_metrics, we always fill them from fs, unused metrics are commented out */
void fillMissingMetrics(gpuSensors* g) {
static char buf[32];
void fillMissingMetrics(gpuSensors* g) {;
static std::array<char, 32> buf;
utils::fastReadNum<1000>(g->hwmon + "/in0_input", buf, g->vcore);
// g->fan_pwm = std::stoi(readFile(g->hwmon + "/pwm1", 4)) * 100 / 256;
utils::fastReadNum(g->hwmon + "/fan1_input", buf, g->fan_speed);
@@ -37,7 +38,7 @@ void fillMissingMetrics(gpuSensors* g) {
/* get sensors for pre-vega cards (rx480/580) */
void updateFromFs(gpuSensors* g) {
static char buf[32];
static std::array<char, 32> buf;
utils::fastReadNum<1000000>(g->hwmon + "/freq1_input", buf, g->clock_core);
utils::fastReadNum<1000000>(g->hwmon + "/freq2_input", buf, g->clock_mem);
utils::fastReadNum<1000000>(g->hwmon + "/power1_input", buf, g->power_avg);
@@ -47,13 +48,13 @@ void updateFromFs(gpuSensors* g) {
/* get sensors for vega and newer (rx5700xt, rx7800xt [*]) */
void updateFromMetrics(gpuSensors* g) {
char raw_metrics[max_metrics_length];
if (!utils::readFile(g->hwmon + rel_metrics_path, raw_metrics, sizeof(raw_metrics))) return;
std::array<char, max_metrics_length> raw_metrics{};
if (!utils::readFile(g->hwmon + rel_metrics_path, raw_metrics)) return;
switch (const auto header = reinterpret_cast<metrics::metrics_table_header*>(raw_metrics); header->format_revision * 10 + header->content_revision) {
switch (const auto header = reinterpret_cast<metrics::metrics_table_header*>(raw_metrics.data()); header->format_revision * 10 + header->content_revision) {
/* rx5700xt, rx7800xt */
case 13: {
const auto metrics = reinterpret_cast<metrics::gpu_metrics_v1_3*>(raw_metrics);
const auto metrics = reinterpret_cast<metrics::gpu_metrics_v1_3*>(raw_metrics.data());
g->usage_core = metrics->average_gfx_activity;
g->usage_umc = metrics->average_umc_activity;
g->usage_media = metrics->average_mm_activity;
@@ -80,7 +81,7 @@ void updateFromMetrics(gpuSensors* g) {
/* ryzen 5 5600g */
case 22: {
const auto metrics = reinterpret_cast<metrics::gpu_metrics_v2_2*>(raw_metrics);
const auto metrics = reinterpret_cast<metrics::gpu_metrics_v2_2*>(raw_metrics.data());
g->temp_edge = metrics->temperature_gfx / 100;
break;
}

View File

@@ -30,8 +30,8 @@ void sys::getTimeDate(TimeDate* td) {
}
void sys::getRam(Memory* mem) {
static char buf[128]; // file is longer but we only need first 128 chars
utils::readFile("/proc/meminfo", buf, sizeof(buf));
static std::array<char, 128> buf; // file is longer but we only need first 128 chars
utils::readFile("/proc/meminfo", buf);
mem->free = getVal("MemAvailable:", std::string_view(buf));
mem->total = getVal("MemTotal:", std::string_view(buf)); // shouldn't really update this on each loop
mem->used = mem->total - mem->free;
@@ -46,15 +46,15 @@ void sys::getDisk(Disk* disk) {
}
void sys::getNetwork(Network* net, const std::string& device) {
struct _net {
struct Network {
std::chrono::time_point<std::chrono::steady_clock> ts;
uint64_t rx = 0;
uint64_t tx = 0;
};
static char buf[32];
static _net prev;
_net current;
static std::array<char, 32> buf;
static Network prev;
Network current;
current.ts = std::chrono::steady_clock::now();
utils::fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf, current.rx);
@@ -78,7 +78,7 @@ void sys::detectHwmon(sysSensors* s, const std::string& platform) {
void sys::updateSensors(sysSensors* s) {
if (s->hwmon.empty())
return;
static char buf[8];
static std::array<char, 8> buf;
utils::fastReadNum(s->hwmon + "/fan2_input", buf, s->fan_speed[0]);
utils::fastReadNum(s->hwmon + "/fan7_input", buf, s->fan_speed[1]);
utils::fastReadNum<1000>(s->hwmon + "/temp5_input", buf, s->t_sensor);