sensors: Add thermal and FANRPM types.

OpenBSD actually read this properly now.
This commit is contained in:
Alastair Poole 2021-05-17 11:00:29 +01:00
parent b580266dc0
commit 16fe06e9fa
5 changed files with 113 additions and 67 deletions

View File

@ -1,5 +1,7 @@
#include "next/machine.h" #include "next/machine.h"
#include <Ecore.h> #include <Ecore.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
static Eina_List *batteries = NULL; static Eina_List *batteries = NULL;
static Eina_List *sensors = NULL; static Eina_List *sensors = NULL;
@ -19,7 +21,7 @@ main(int argc, char **argv)
int ncpu = 0; int ncpu = 0;
cores = system_cpu_usage_delayed_get(&ncpu, 1000000); cores = system_cpu_usage_delayed_get(&ncpu, 1000000);
for (int i = 0; i < ncpu; i++) 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(); batteries = batteries_find();
EINA_LIST_FOREACH(batteries, l, bat) EINA_LIST_FOREACH(batteries, l, bat)
@ -30,20 +32,29 @@ main(int argc, char **argv)
EINA_LIST_FREE(batteries, bat) EINA_LIST_FREE(batteries, bat)
battery_free(bat); battery_free(bat);
printf("POWER %i\n", power_ac()); printf("POWER %i\n", power_ac_check());
sensors = sensors_find(); sensors = sensors_find();
EINA_LIST_FREE(sensors, sensor) EINA_LIST_FREE(sensors, sensor)
{ {
if (sensor_check(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); sensor_free(sensor);
} }
network_interfaces = network_interfaces_find(); network_interfaces = network_interfaces_find();
EINA_LIST_FREE(network_interfaces, iface) 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); free(iface);
} }

View File

@ -19,35 +19,43 @@ typedef struct
{ {
uint64_t total; uint64_t total;
uint64_t used; uint64_t used;
} meminfo_video_t; } Meminfo_Video;
typedef struct typedef struct
{ {
uint64_t total; uint64_t total;
uint64_t used; uint64_t used;
uint64_t cached; uint64_t cached;
uint64_t buffered; uint64_t buffered;
uint64_t shared; uint64_t shared;
uint64_t swap_total; uint64_t swap_total;
uint64_t swap_used; uint64_t swap_used;
uint64_t zfs_arc_used; uint64_t zfs_arc_used;
uint64_t video_count; uint64_t video_count;
meminfo_video_t video[MEM_VIDEO_CARD_MAX]; Meminfo_Video video[MEM_VIDEO_CARD_MAX];
} meminfo_t; } Meminfo;
typedef enum
{
THERMAL = 0,
FANRPM = 1,
} Sensor_Type;
typedef struct typedef struct
{ {
char *name; char *name;
char *child_name; char *child_name;
#if defined(__linux__) #if defined(__linux__)
char *path; char *path;
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
int mibs[5]; int mibs[5];
#endif #endif
double value; double value;
bool invalid; bool invalid;
int id;
Sensor_Type type;
} Sensor; } Sensor;
typedef struct typedef struct
@ -64,14 +72,6 @@ typedef struct
#endif #endif
} Battery; } Battery;
typedef struct
{
bool have_ac;
#if defined(__OpenBSD__)
int mibs[5];
#endif
} AC_Adapter;
typedef struct typedef struct
{ {
char name[255]; char name[255];
@ -86,7 +86,7 @@ typedef struct
} Network_Interface; } Network_Interface;
Eina_Bool Eina_Bool
power_ac(void); power_ac_check(void);
Eina_List * Eina_List *
batteries_find(void); batteries_find(void);
@ -143,6 +143,6 @@ void
system_cpu_topology_get(int *ids, int ncpus); system_cpu_topology_get(int *ids, int ncpus);
void void
system_memory_usage_get(meminfo_t *memory); system_memory_usage_get(Meminfo *memory);
#endif #endif

View File

@ -16,9 +16,9 @@ _meminfo_parse_line(const char *line)
#endif #endif
void 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__) #if defined(__linux__)
FILE *f; FILE *f;
unsigned long swap_free = 0, tmp_free = 0, tmp_slab = 0; unsigned long swap_free = 0, tmp_free = 0, tmp_slab = 0;

View File

@ -42,7 +42,7 @@ _freebsd_generic_network_status(int *n)
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
static Eina_List * static Eina_List *
_openbsd_generic_network_status(int *n) _openbsd_generic_network_status(void)
{ {
struct ifaddrs *interfaces, *ifa; struct ifaddrs *interfaces, *ifa;

View File

@ -30,9 +30,18 @@ sensor_check(Sensor *sensor)
struct sensor snsr; struct sensor snsr;
size_t slen = sizeof(struct sensor); 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; 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; return 1;
#elif defined(__FreeBSD__) || defined(__DragonFly__) #elif defined(__FreeBSD__) || defined(__DragonFly__)
@ -59,6 +68,8 @@ sensors_find(void)
struct sensordev snsrdev; struct sensordev snsrdev;
size_t slen = sizeof(struct sensor); size_t slen = sizeof(struct sensor);
size_t sdlen = sizeof(struct sensordev); size_t sdlen = sizeof(struct sensordev);
char buf[32];
enum sensor_type type;
for (devn = 0;; devn++) for (devn = 0;; devn++)
{ {
@ -69,30 +80,35 @@ sensors_find(void)
if (errno == ENOENT) break; if (errno == ENOENT) break;
continue; continue;
} }
for (type = 0; type < SENSOR_MAX_TYPES; type++)
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)
{ {
sensor->name = strdup(snsrdev.xname); mibs[3] = type;
sensor->value = (snsr.value - 273150000) / 1000000.0; // (uK -> C) for (n = 0; n < snsrdev.sensors_count; n++) {
memcpy(sensor->mibs, &mibs, sizeof(mibs)); 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__) #elif defined(__FreeBSD__) || defined(__DragonFly__)
@ -391,7 +407,6 @@ battery_check(Battery *bat)
} }
else else
{ {
// Fallback to "coarse" representation.
snprintf(path, sizeof(path), "%s/capacity_level", link); snprintf(path, sizeof(path), "%s/capacity_level", link);
buf = file_contents(path); buf = file_contents(path);
if (buf) if (buf)
@ -420,22 +435,42 @@ done:
if (charge_full && charge_current) if (charge_full && charge_current)
bat->percent = 100 * (charge_full / charge_current); bat->percent = 100 * (charge_full / charge_current);
else
bat->percent = 0;
} }
Eina_Bool Eina_Bool
power_ac(void) power_ac_check(void)
{ {
Eina_Bool have_ac = 0; Eina_Bool have_ac = 0;
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
struct sensor snsr; struct sensordev snsrdev;
size_t slen = sizeof(struct sensor); size_t sdlen = sizeof(struct sensordev);
static int mibs[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
int devn;
power->mibs[3] = 9; for (devn = 0; !mibs[3] ; devn++)
power->mibs[4] = 0; {
mibs[2] = devn;
if (sysctl(power->mibs, 5, &snsr, &slen, NULL, 0) != -1) if (sysctl(mibs, 3, &snsrdev, &sdlen, NULL, 0) == -1)
have_ac = (int)snsr.value; {
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__) #elif defined(__FreeBSD__) || defined(__DragonFly__)
int val, fd; int val, fd;