From 16fe06e9fa997504b269ae1e9bf451eef351e7fe Mon Sep 17 00:00:00 2001 From: Alastair Poole Date: Mon, 17 May 2021 11:00:29 +0100 Subject: [PATCH] sensors: Add thermal and FANRPM types. OpenBSD actually read this properly now. --- src/bin/evisum_watcher.c | 19 +++++-- src/bin/next/machine.h | 54 +++++++++--------- src/bin/next/machine/memory.x | 4 +- src/bin/next/machine/network.x | 2 +- src/bin/next/machine/sensors.x | 101 ++++++++++++++++++++++----------- 5 files changed, 113 insertions(+), 67 deletions(-) diff --git a/src/bin/evisum_watcher.c b/src/bin/evisum_watcher.c index 3377946..fbe261d 100644 --- a/src/bin/evisum_watcher.c +++ b/src/bin/evisum_watcher.c @@ -1,5 +1,7 @@ #include "next/machine.h" #include +#define __STDC_FORMAT_MACROS +#include static Eina_List *batteries = NULL; static Eina_List *sensors = NULL; @@ -19,7 +21,7 @@ main(int argc, char **argv) int ncpu = 0; cores = system_cpu_usage_delayed_get(&ncpu, 1000000); for (int i = 0; i < ncpu; i++) - printf("core %i = %1.2f%\n", cores[i]->id, cores[i]->percent); + printf("core %i = %1.2f%%\n", cores[i]->id, cores[i]->percent); batteries = batteries_find(); EINA_LIST_FOREACH(batteries, l, bat) @@ -30,20 +32,29 @@ main(int argc, char **argv) EINA_LIST_FREE(batteries, bat) battery_free(bat); - printf("POWER %i\n", power_ac()); + printf("POWER %i\n", power_ac_check()); sensors = sensors_find(); EINA_LIST_FREE(sensors, sensor) { if (sensor_check(sensor)) - printf("sensor %s = %1.2f\n", sensor->name, sensor->value); + printf("sensor %s (%s) = %1.1f", sensor->name, sensor->child_name, sensor->value); + if (sensor->type == FANRPM) + printf("RPM\n"); + else if (sensor->type == THERMAL) + printf("C\n"); + else + { + printf(" - UNHANDLED!\n"); + exit(1); + } sensor_free(sensor); } network_interfaces = network_interfaces_find(); EINA_LIST_FREE(network_interfaces, iface) { - printf("name => %s => %i %i\n", iface->name, iface->total_in, iface->total_out); + printf("name => %s => %"PRIi64" %"PRIi64"\n", iface->name, iface->total_in, iface->total_out); free(iface); } diff --git a/src/bin/next/machine.h b/src/bin/next/machine.h index 6cedfd4..3386cd1 100644 --- a/src/bin/next/machine.h +++ b/src/bin/next/machine.h @@ -19,35 +19,43 @@ typedef struct { uint64_t total; uint64_t used; -} meminfo_video_t; +} Meminfo_Video; typedef struct { - uint64_t total; - uint64_t used; - uint64_t cached; - uint64_t buffered; - uint64_t shared; - uint64_t swap_total; - uint64_t swap_used; + uint64_t total; + uint64_t used; + uint64_t cached; + uint64_t buffered; + uint64_t shared; + uint64_t swap_total; + uint64_t swap_used; - uint64_t zfs_arc_used; + uint64_t zfs_arc_used; uint64_t video_count; - meminfo_video_t video[MEM_VIDEO_CARD_MAX]; -} meminfo_t; + Meminfo_Video video[MEM_VIDEO_CARD_MAX]; +} Meminfo; + +typedef enum +{ + THERMAL = 0, + FANRPM = 1, +} Sensor_Type; typedef struct { - char *name; - char *child_name; + char *name; + char *child_name; #if defined(__linux__) - char *path; + char *path; #elif defined(__OpenBSD__) - int mibs[5]; + int mibs[5]; #endif - double value; - bool invalid; + double value; + bool invalid; + int id; + Sensor_Type type; } Sensor; typedef struct @@ -64,14 +72,6 @@ typedef struct #endif } Battery; -typedef struct -{ - bool have_ac; -#if defined(__OpenBSD__) - int mibs[5]; -#endif -} AC_Adapter; - typedef struct { char name[255]; @@ -86,7 +86,7 @@ typedef struct } Network_Interface; Eina_Bool -power_ac(void); +power_ac_check(void); Eina_List * batteries_find(void); @@ -143,6 +143,6 @@ void system_cpu_topology_get(int *ids, int ncpus); void -system_memory_usage_get(meminfo_t *memory); +system_memory_usage_get(Meminfo *memory); #endif diff --git a/src/bin/next/machine/memory.x b/src/bin/next/machine/memory.x index 6e9aa80..501ef7b 100644 --- a/src/bin/next/machine/memory.x +++ b/src/bin/next/machine/memory.x @@ -16,9 +16,9 @@ _meminfo_parse_line(const char *line) #endif void -system_memory_usage_get(meminfo_t *memory) +system_memory_usage_get(Meminfo *memory) { - memset(memory, 0, sizeof(meminfo_t)); + memset(memory, 0, sizeof(Meminfo)); #if defined(__linux__) FILE *f; unsigned long swap_free = 0, tmp_free = 0, tmp_slab = 0; diff --git a/src/bin/next/machine/network.x b/src/bin/next/machine/network.x index f1c6f29..2dc0b8a 100644 --- a/src/bin/next/machine/network.x +++ b/src/bin/next/machine/network.x @@ -42,7 +42,7 @@ _freebsd_generic_network_status(int *n) #if defined(__OpenBSD__) static Eina_List * -_openbsd_generic_network_status(int *n) +_openbsd_generic_network_status(void) { struct ifaddrs *interfaces, *ifa; diff --git a/src/bin/next/machine/sensors.x b/src/bin/next/machine/sensors.x index 0e1c5fb..96b0302 100644 --- a/src/bin/next/machine/sensors.x +++ b/src/bin/next/machine/sensors.x @@ -30,9 +30,18 @@ sensor_check(Sensor *sensor) struct sensor snsr; size_t slen = sizeof(struct sensor); + if (sensor->invalid) + { + sensor->value = 0; + return 0; + } + if (sysctl(sensor->mibs, 5, &snsr, &slen, NULL, 0) == -1) return 0; - sensor->value = (snsr.value - 273150000) / 1000000.0; + if (sensor->type == THERMAL) + sensor->value = (snsr.value - 273150000) / 1000000.0; + else if (sensor->type == FANRPM) + sensor->value = snsr.value; return 1; #elif defined(__FreeBSD__) || defined(__DragonFly__) @@ -59,6 +68,8 @@ sensors_find(void) struct sensordev snsrdev; size_t slen = sizeof(struct sensor); size_t sdlen = sizeof(struct sensordev); + char buf[32]; + enum sensor_type type; for (devn = 0;; devn++) { @@ -69,30 +80,35 @@ sensors_find(void) if (errno == ENOENT) break; continue; } - - for (n = 0; n < snsrdev.maxnumt[SENSOR_TEMP]; n++) { - mibs[4] = n; - - if (sysctl(mibs, 5, &snsr, &slen, NULL, 0) == -1) - continue; - - if (slen > 0 && (snsr.flags & SENSOR_FINVALID) == 0) - break; - } - - if (sysctl(mibs, 5, &snsr, &slen, NULL, 0) == -1) - continue; - if (snsr.type != SENSOR_TEMP) - continue; - - sensor = calloc(1, sizeof(Sensor)); - if (sensor) + for (type = 0; type < SENSOR_MAX_TYPES; type++) { - sensor->name = strdup(snsrdev.xname); - sensor->value = (snsr.value - 273150000) / 1000000.0; // (uK -> C) - memcpy(sensor->mibs, &mibs, sizeof(mibs)); + mibs[3] = type; + for (n = 0; n < snsrdev.sensors_count; n++) { + mibs[4] = n; - sensors = eina_list_append(sensors, sensor); + if (sysctl(mibs, 5, &snsr, &slen, NULL, 0) == -1) + continue; + + if (!slen || ((snsr.type != SENSOR_TEMP) && (snsr.type != SENSOR_FANRPM))) + continue; + + if ((snsr.flags & SENSOR_FINVALID)) continue; + + sensor = calloc(1, sizeof(Sensor)); + if (sensor) + { + sensor->name = strdup(snsrdev.xname); + snprintf(buf, sizeof(buf), "%i", n); + sensor->child_name = strdup(buf); + if (snsr.type == SENSOR_TEMP) + sensor->type = THERMAL; + else if (snsr.type == SENSOR_FANRPM) + sensor->type = FANRPM; + memcpy(sensor->mibs, &mibs, sizeof(mibs)); + + sensors = eina_list_append(sensors, sensor); + } + } } } #elif defined(__FreeBSD__) || defined(__DragonFly__) @@ -391,7 +407,6 @@ battery_check(Battery *bat) } else { - // Fallback to "coarse" representation. snprintf(path, sizeof(path), "%s/capacity_level", link); buf = file_contents(path); if (buf) @@ -420,22 +435,42 @@ done: if (charge_full && charge_current) bat->percent = 100 * (charge_full / charge_current); + else + bat->percent = 0; } - Eina_Bool -power_ac(void) +power_ac_check(void) { Eina_Bool have_ac = 0; #if defined(__OpenBSD__) - struct sensor snsr; - size_t slen = sizeof(struct sensor); + struct sensordev snsrdev; + size_t sdlen = sizeof(struct sensordev); + static int mibs[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 }; + int devn; - power->mibs[3] = 9; - power->mibs[4] = 0; - - if (sysctl(power->mibs, 5, &snsr, &slen, NULL, 0) != -1) - have_ac = (int)snsr.value; + for (devn = 0; !mibs[3] ; devn++) + { + mibs[2] = devn; + if (sysctl(mibs, 3, &snsrdev, &sdlen, NULL, 0) == -1) + { + if (errno == ENXIO) continue; + if (errno == ENOENT) break; + } + if (!strncmp(snsrdev.xname, "acpiac", 6)) + { + mibs[3] = 9; + mibs[4] = 0; + break; + } + } + if (mibs[3] == 9) + { + struct sensor snsr; + size_t slen = sizeof(struct sensor); + if (sysctl(mibs, 5, &snsr, &slen, NULL, 0) != -1) + have_ac = (int)snsr.value; + } #elif defined(__FreeBSD__) || defined(__DragonFly__) int val, fd;