aboutsummaryrefslogtreecommitdiff
path: root/src/ss
diff options
context:
space:
mode:
authorOndrej Čerman2019-10-12 21:12:55 +0200
committerOndrej Čerman2019-10-12 21:12:55 +0200
commit38aaa630df9a88d6387ebc2b95f50da7bad6b03d (patch)
tree63ea3a45e2fd5ef56987c9018a5caa9cc9aca15a /src/ss
parentf3ca3475d0500734421b738f944edbf55802abca (diff)
parente409f26106b4a60a64a122e1cb5884360c2cca34 (diff)
Merge branch 'nove'
Diffstat (limited to 'src/ss')
-rw-r--r--src/ss/msr.c44
-rw-r--r--src/ss/os.c91
-rw-r--r--src/ss/zenpower.c3
3 files changed, 106 insertions, 32 deletions
diff --git a/src/ss/msr.c b/src/ss/msr.c
index 2da5d7a..8b26472 100644
--- a/src/ss/msr.c
+++ b/src/ss/msr.c
@@ -8,6 +8,7 @@
#include "string.h"
#include "zenmonitor.h"
#include "msr.h"
+#include "sysfs.h"
#define MSR_PWR_PRINTF_FORMAT " %8.3f W"
#define MESUREMENT_TIME 0.1
@@ -15,16 +16,15 @@
// AMD PPR = https://www.amd.com/system/files/TechDocs/54945_PPR_Family_17h_Models_00h-0Fh.pdf
// 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;
+static guint cores = 0;
+static gdouble energy_unit = 0;
-gint *msr_files = NULL;
+static gint *msr_files = NULL;
-gulong package_eng_b = 0;
-gulong package_eng_a = 0;
-gulong *core_eng_b = NULL;
-gulong *core_eng_a = NULL;
+static gulong package_eng_b = 0;
+static gulong package_eng_a = 0;
+static gulong *core_eng_b = NULL;
+static gulong *core_eng_a = NULL;
gfloat package_power;
gfloat package_power_min;
@@ -33,27 +33,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);
}
@@ -92,8 +75,8 @@ gulong get_core_energy(gint core) {
}
gboolean msr_init() {
+ gshort *cpu_dev_ids = NULL;
int i;
- size_t sz;
if (!check_zen())
return FALSE;
@@ -102,10 +85,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)
@@ -127,7 +112,6 @@ gboolean msr_init() {
}
void msr_update() {
- GSList *list = NULL;
gint i;
package_eng_b = get_package_energy();
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 <glib.h>
+#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/ss/zenpower.c b/src/ss/zenpower.c
index 07a6b54..6af86dc 100644
--- a/src/ss/zenpower.c
+++ b/src/ss/zenpower.c
@@ -74,8 +74,7 @@ gboolean zenpower_init() {
const gchar *entry;
gchar *name = NULL;
HwmonSensorType *type;
- HwmonSensor *sensor;
-
+
hwmon = g_dir_open("/sys/class/hwmon", 0, NULL);
if (!hwmon)
return FALSE;