From 9795d0c5b8f6cbc0bace0d6b819cbad705f71f10 Mon Sep 17 00:00:00 2001 From: Ondrej Čerman Date: Sat, 12 Oct 2019 20:48:29 +0200 Subject: Added CPU Frequency (reported by OS) --- src/include/os.h | 4 +++ src/include/sysfs.h | 2 ++ src/ss/os.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sysfs.c | 3 +- src/zenmonitor.c | 6 ++++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/include/os.h create mode 100644 src/ss/os.c diff --git a/src/include/os.h b/src/include/os.h new file mode 100644 index 0000000..d6af111 --- /dev/null +++ b/src/include/os.h @@ -0,0 +1,4 @@ +gboolean os_init(); +void os_update(); +void os_clear_minmax(); +GSList* os_get_sensors(); diff --git a/src/include/sysfs.h b/src/include/sysfs.h index ec838bf..a079c7d 100644 --- a/src/include/sysfs.h +++ b/src/include/sysfs.h @@ -1 +1,3 @@ +#define SYSFS_DIR_CPUS "/sys/devices/system/cpu" + gshort* get_cpu_dev_ids(); diff --git a/src/ss/os.c b/src/ss/os.c new file mode 100644 index 0000000..d232a1f --- /dev/null +++ b/src/ss/os.c @@ -0,0 +1,91 @@ +#include +#include "zenmonitor.h" +#include "sysfs.h" +#include "os.h" + +#define OS_FREQ_PRINTF_FORMAT " %8.3f GHz" + +static gchar **frq_files = NULL; +static guint cores; + +gfloat *core_freq; +gfloat *core_freq_min; +gfloat *core_freq_max; + +static gdouble get_frequency(guint coreid) { + gchar *data; + if (!g_file_get_contents(frq_files[coreid], &data, NULL, NULL)) + return 0.0; + + return atoi(data) / 1000000.0; +} + +gboolean os_init() { + gshort *cpu_dev_ids = NULL; + gint i; + + if (!check_zen()) + return FALSE; + + cores = get_core_count(); + if (cores == 0) + return FALSE; + + cpu_dev_ids = get_cpu_dev_ids(); + frq_files = malloc(cores * sizeof (gchar*)); + for (i = 0; i < cores; i++) { + frq_files[i] = g_strdup_printf( + "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", + cpu_dev_ids[i]); + } + g_free(cpu_dev_ids); + + core_freq = malloc(cores * sizeof (gfloat)); + core_freq_min = malloc(cores * sizeof (gfloat)); + core_freq_max = malloc(cores * sizeof (gfloat)); + + os_update(); + memcpy(core_freq_min, core_freq, cores * sizeof (gfloat)); + memcpy(core_freq_max, core_freq, cores * sizeof (gfloat)); + + return TRUE; +} + +void os_update() { + gint i; + + for (i = 0; i < cores; i++) { + core_freq[i] = get_frequency(i); + if (core_freq[i] < core_freq_min[i]) + core_freq_min[i] = core_freq[i]; + if (core_freq[i] > core_freq_max[i]) + core_freq_max[i] = core_freq[i]; + } +} + +void os_clear_minmax() { + gint i; + + for (i = 0; i < cores; i++) { + core_freq_min[i] = core_freq[i]; + core_freq_max[i] = core_freq[i]; + } +} + +GSList* os_get_sensors() { + GSList *list = NULL; + SensorInit *data; + gint i; + + for (i = 0; i < cores; i++) { + data = sensor_init_new(); + data->label = g_strdup_printf("Core %d Frequency", i); + data->value = &(core_freq[i]); + data->min = &(core_freq_min[i]); + data->max = &(core_freq_max[i]); + data->printf_format = OS_FREQ_PRINTF_FORMAT; + list = g_slist_append(list, data); + } + + return list; +} diff --git a/src/sysfs.c b/src/sysfs.c index 692ef47..ea59c1b 100644 --- a/src/sysfs.c +++ b/src/sysfs.c @@ -1,9 +1,8 @@ #include #include +#include "sysfs.h" #include "zenmonitor.h" -#define SYSFS_DIR_CPUS "/sys/bus/cpu/devices" - gshort* get_cpu_dev_ids(){ gshort *cpu_dev_ids = NULL; gshort coreid, cpuid; diff --git a/src/zenmonitor.c b/src/zenmonitor.c index e001804..88e676a 100644 --- a/src/zenmonitor.c +++ b/src/zenmonitor.c @@ -3,6 +3,7 @@ #include "zenmonitor.h" #include "zenpower.h" #include "msr.h" +#include "os.h" #include "gui.h" #define AMD_STRING "AuthenticAMD" @@ -91,6 +92,11 @@ static SensorSource sensor_sources[] = { msr_init, msr_get_sensors, msr_update, msr_clear_minmax, FALSE, NULL }, + { + "os", + os_init, os_get_sensors, os_update, os_clear_minmax, + FALSE, NULL + }, { NULL } -- cgit v1.2.3-70-g09d2