aboutsummaryrefslogtreecommitdiff
path: root/src/ss/zenpower.c
blob: 2bf762b4054a8eba984346fee8ac81d6d14de00e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "zenmonitor.h"
#include "zenpower.h"

GSList *zp_sensors = NULL;
static int nodes = 0;

typedef struct
{
    const gchar *label;
    const gchar *hint;
    const gchar *file;
    const gchar *printf_format;
    const double adjust_ratio;
} HwmonSensorType;

typedef struct
{
    float current_value;
    float min;
    float max;
    HwmonSensorType *type;
    gchar *hwmon_dir;
    int node;
} HwmonSensor;

static HwmonSensorType hwmon_stype[] = {
  {"CPU Temperature (tCtl)",    "Reported CPU Temperature",                  "temp1_input",  " %6.2f°C", 1000.0},
  {"CPU Temperature (tDie)",    "Reported CPU Temperature - offset",         "temp2_input",  " %6.2f°C", 1000.0},
  {"CCD1 Temperature",          "Core Complex Die 1 Temperature",            "temp3_input",  " %6.2f°C", 1000.0},
  {"CCD2 Temperature",          "Core Complex Die 2 Temperature",            "temp4_input",  " %6.2f°C", 1000.0},
  {"CCD3 Temperature",          "Core Complex Die 3 Temperature",            "temp5_input",  " %6.2f°C", 1000.0},
  {"CCD4 Temperature",          "Core Complex Die 4 Temperature",            "temp6_input",  " %6.2f°C", 1000.0},
  {"CCD5 Temperature",          "Core Complex Die 5 Temperature",            "temp7_input",  " %6.2f°C", 1000.0},
  {"CCD6 Temperature",          "Core Complex Die 6 Temperature",            "temp8_input",  " %6.2f°C", 1000.0},
  {"CCD7 Temperature",          "Core Complex Die 7 Temperature",            "temp9_input",  " %6.2f°C", 1000.0},
  {"CCD8 Temperature",          "Core Complex Die 8 Temperature",            "temp10_input", " %6.2f°C", 1000.0},
  {"CPU Core Voltage (SVI2)",   "Core Voltage reported by SVI2 telemetry",   "in1_input",    " %8.3f V", 1000.0},
  {"SOC Voltage (SVI2)",        "SOC Voltage reported by SVI2 telemetry",    "in2_input",    " %8.3f V", 1000.0},
  {"CPU Core Current (SVI2)",   "Core Current reported by SVI2 telemetry\n"
                                "Note: May not be accurate on some systems", "curr1_input",  " %8.3f A", 1000.0},
  {"SOC Current (SVI2)",        "SOC Current reported by SVI2 telemetry\n"
                                "Note: May not be accurate on some systems", "curr2_input",  " %8.3f A", 1000.0},
  {"CPU Core Power (SVI2)",     "Core Voltage * Current\n"
                                "Note: May not be accurate on some systems", "power1_input", " %8.3f W", 1000000.0},
  {"SOC Power (SVI2)",          "Core Voltage * Current\n"
                                "Note: May not be accurate on some systems", "power2_input", " %8.3f W", 1000000.0},
  {0, NULL}
};

static gboolean hwmon_file_exists(const gchar *dir, const gchar *file) {
    gchar *full_path;
    gboolean result;

    full_path = g_strdup_printf("/sys/class/hwmon/%s/%s", dir, file);
    result = g_file_test(full_path, G_FILE_TEST_EXISTS);

    g_free(full_path);
    return result;
}

static gboolean read_raw_hwmon_value(const gchar *dir, const gchar *file, gchar **result) {
    gchar *full_path;
    gboolean file_result;

    full_path = g_strdup_printf("/sys/class/hwmon/%s/%s", dir, file);
    file_result = g_file_get_contents(full_path, result, NULL, NULL);

    g_free(full_path);
    return file_result;
}

static HwmonSensor *hwmon_sensor_new(HwmonSensorType *type, const gchar *dir, gint node) {
    HwmonSensor *s;
    s = g_new0(HwmonSensor, 1);
    s->min = 999.0;
    s->type = type;
    s->hwmon_dir = g_strdup(dir);
    s->node = node;
    return s;
}

gboolean zenpower_init() {
    GDir *hwmon;
    const gchar *entry;
    gchar *name = NULL;
    HwmonSensorType *type;

    hwmon = g_dir_open("/sys/class/hwmon", 0, NULL);
    if (!hwmon)
        return FALSE;

    while ((entry = g_dir_read_name(hwmon))) {
        read_raw_hwmon_value(entry, "name", &name);

        if (strcmp(g_strchomp(name), "zenpower") == 0) {

            for (type = hwmon_stype; type->label; type++) {
                if (hwmon_file_exists(entry, type->file)) {
                    zp_sensors = g_slist_append(zp_sensors, hwmon_sensor_new(type, entry, nodes));
                }
            }
            nodes++;

        }
        g_free(name);
    }

    if (zp_sensors == NULL)
        return FALSE;

    return TRUE;
}

void zenpower_update() {
    gchar *tmp = NULL;
    GSList *node;
    HwmonSensor *sensor;

    node = zp_sensors;
    while(node) {
        sensor = (HwmonSensor *)node->data;

        if (read_raw_hwmon_value(sensor->hwmon_dir, sensor->type->file, &tmp)){
            sensor->current_value = atof(tmp) / sensor->type->adjust_ratio;

            if (sensor->current_value < sensor->min)
                sensor->min = sensor->current_value;

            if (sensor->current_value > sensor->max)
                sensor->max = sensor->current_value;

            g_free(tmp);
        }
        else{
            sensor->current_value = ERROR_VALUE;
        }
        node = node->next;
    }
}

void zenpower_clear_minmax() {
    HwmonSensor *sensor;
    GSList *node;
    node = zp_sensors;
    while(node) {
        sensor = (HwmonSensor *)node->data;
        sensor->min = sensor->current_value;
        sensor->max = sensor->current_value;
        node = node->next;
    }
}

GSList* zenpower_get_sensors() {
    GSList *list = NULL;
    HwmonSensor *sensor;
    GSList *node;
    SensorInit *data;

    node = zp_sensors;
    while(node) {
        sensor = (HwmonSensor *)node->data;

        data = sensor_init_new();
        if (nodes > 1){
            data->label = g_strdup_printf("Node %d - %s", sensor->node, sensor->type->label);
        }
        else{
            data->label = g_strdup(sensor->type->label);
        }
        data->hint = g_strdup_printf("%s\nSource: zenpower %s/%s", sensor->type->hint, sensor->hwmon_dir, sensor->type->file);
        data->value = &sensor->current_value;
        data->min = &sensor->min;
        data->max = &sensor->max;
        data->printf_format = sensor->type->printf_format;
        list = g_slist_append(list, data);

        node = node->next;
    }

    return list;
}