diff options
author | Ondrej Čerman | 2019-10-12 19:24:19 +0200 |
---|---|---|
committer | Ondrej Čerman | 2019-10-12 19:44:54 +0200 |
commit | e1beb5791ef4608d20ad8992578a75ecae401eeb (patch) | |
tree | ce4c2be4efd1a6372a45838e5f6dedef4e447beb /src | |
parent | ec849b74627cd6ad890714a0c3215fcf3c9c0dad (diff) |
FIX: Core ID to cpu device id mapping.
Diffstat (limited to 'src')
-rw-r--r-- | src/include/sysfs.h | 1 | ||||
-rw-r--r-- | src/include/zenmonitor.h | 1 | ||||
-rw-r--r-- | src/ss/msr.c | 28 | ||||
-rw-r--r-- | src/sysfs.c | 48 | ||||
-rw-r--r-- | src/zenmonitor.c | 18 |
5 files changed, 75 insertions, 21 deletions
diff --git a/src/include/sysfs.h b/src/include/sysfs.h new file mode 100644 index 0000000..ec838bf --- /dev/null +++ b/src/include/sysfs.h @@ -0,0 +1 @@ +gshort* get_cpu_dev_ids(); diff --git a/src/include/zenmonitor.h b/src/include/zenmonitor.h index c0b1f73..21cc9fc 100644 --- a/src/include/zenmonitor.h +++ b/src/include/zenmonitor.h @@ -24,3 +24,4 @@ SensorInit* sensor_init_new(void); void sensor_init_free(SensorInit *s); gboolean check_zen(); gchar *cpu_model(); +guint get_core_count(); diff --git a/src/ss/msr.c b/src/ss/msr.c index d26f703..aeca262 100644 --- a/src/ss/msr.c +++ b/src/ss/msr.c @@ -6,6 +6,7 @@ #include <math.h> #include "zenmonitor.h" #include "msr.h" +#include "sysfs.h" #define MSR_PWR_PRINTF_FORMAT " %8.3f W" #define MESUREMENT_TIME 0.1 @@ -14,7 +15,6 @@ // AMD OSRR = https://developer.amd.com/wp-content/resources/56255_3_03.PDF guint cores = 0; -guint threads_per_code = 0; gdouble energy_unit = 0; gint *msr_files = NULL; @@ -31,27 +31,10 @@ gfloat *core_power; gfloat *core_power_min; gfloat *core_power_max; -static guint get_core_count() { - guint eax = 0, ebx = 0, ecx = 0, edx = 0; - guint logical_cpus; - // AMD PPR: page 57 - CPUID_Fn00000001_EBX - __get_cpuid(1, &eax, &ebx, &ecx, &edx); - logical_cpus = (ebx >> 16) & 0xFF; - - // AMD PPR: page 82 - CPUID_Fn8000001E_EBX - __get_cpuid(0x8000001E, &eax, &ebx, &ecx, &edx); - threads_per_code = ((ebx >> 8) & 0xF) + 1; - - if (threads_per_code == 0) - return logical_cpus; - - return logical_cpus / threads_per_code; -} - -static gint open_msr(gshort core) { +static gint open_msr(gshort devid) { gchar msr_path[20]; - sprintf(msr_path, "/dev/cpu/%d/msr", core * threads_per_code); + sprintf(msr_path, "/dev/cpu/%d/msr", devid); return open(msr_path, O_RDONLY); } @@ -90,6 +73,7 @@ gulong get_core_energy(gint core) { } gboolean msr_init() { + gshort *cpu_dev_ids = NULL; int i; size_t sz; @@ -100,10 +84,12 @@ gboolean msr_init() { if (cores == 0) return FALSE; + cpu_dev_ids = get_cpu_dev_ids(); msr_files = malloc(cores * sizeof (gint)); for (i = 0; i < cores; i++) { - msr_files[i] = open_msr(i); + msr_files[i] = open_msr(cpu_dev_ids[i]); } + g_free(cpu_dev_ids); energy_unit = get_energy_unit(); if (energy_unit == 0) diff --git a/src/sysfs.c b/src/sysfs.c new file mode 100644 index 0000000..692ef47 --- /dev/null +++ b/src/sysfs.c @@ -0,0 +1,48 @@ +#include <glib.h> +#include <stdio.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; + GDir *dir; + const gchar *entry; + gchar *filename, *buffer; + guint cores, i; + + cores = get_core_count(); + cpu_dev_ids = malloc(cores * sizeof (gshort)); + memset(cpu_dev_ids, -1, cores * sizeof (gshort)); + + dir = g_dir_open(SYSFS_DIR_CPUS, 0, NULL); + if (dir) { + while ((entry = g_dir_read_name(dir))) { + if (sscanf(entry, "cpu%hd", &cpuid) != 1) { + continue; + } + + filename = g_build_filename(SYSFS_DIR_CPUS, entry, "topology", "core_id", NULL); + if (g_file_get_contents(filename, &buffer, NULL, NULL)) { + coreid = (gshort) atoi(buffer); + + if (coreid < cores && cpu_dev_ids[coreid] == -1) { + cpu_dev_ids[coreid] = cpuid; + } + } + + g_free(filename); + g_free(buffer); + } + } + + for (i = 0; i < cores; i++) { + if (cpu_dev_ids[i] < 0) { + cpu_dev_ids[i] = i; + } + } + + return cpu_dev_ids; +} + diff --git a/src/zenmonitor.c b/src/zenmonitor.c index 4ce4bb9..e001804 100644 --- a/src/zenmonitor.c +++ b/src/zenmonitor.c @@ -62,6 +62,24 @@ gchar *cpu_model() { return g_strdup(g_strchomp(model)); } +guint get_core_count() { + guint eax = 0, ebx = 0, ecx = 0, edx = 0; + guint logical_cpus, threads_per_code; + + // AMD PPR: page 57 - CPUID_Fn00000001_EBX + __get_cpuid(1, &eax, &ebx, &ecx, &edx); + logical_cpus = (ebx >> 16) & 0xFF; + + // AMD PPR: page 82 - CPUID_Fn8000001E_EBX + __get_cpuid(0x8000001E, &eax, &ebx, &ecx, &edx); + threads_per_code = ((ebx >> 8) & 0xF) + 1; + + if (threads_per_code == 0) + return logical_cpus; + + return logical_cpus / threads_per_code; +} + static SensorSource sensor_sources[] = { { "zenpower", |