summaryrefslogtreecommitdiffstats
path: root/linux/LibSensors.c
blob: f94bcff72e62bd2fd1a804072a6789fdcf520de3 (plain) (blame)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "LibSensors.h"

#ifdef HAVE_SENSORS_SENSORS_H

#include <dlfcn.h>
#include <errno.h>
#include <math.h>
#include <sensors/sensors.h>

#include "XUtils.h"


#ifdef BUILD_STATIC

#define sym_sensors_init sensors_init
#define sym_sensors_cleanup sensors_cleanup
#define sym_sensors_get_detected_chips sensors_get_detected_chips
#define sym_sensors_snprintf_chip_name sensors_snprintf_chip_name
#define sym_sensors_get_features sensors_get_features
#define sym_sensors_get_subfeature sensors_get_subfeature
#define sym_sensors_get_value sensors_get_value
#define sym_sensors_get_label sensors_get_label

#else

static int (*sym_sensors_init)(FILE*);
static void (*sym_sensors_cleanup)(void);
static const sensors_chip_name* (*sym_sensors_get_detected_chips)(const sensors_chip_name*, int*);
static int (*sym_sensors_snprintf_chip_name)(char*, size_t, const sensors_chip_name*);
static const sensors_feature* (*sym_sensors_get_features)(const sensors_chip_name*, int*);
static const sensors_subfeature* (*sym_sensors_get_subfeature)(const sensors_chip_name*, const sensors_feature*, sensors_subfeature_type);
static int (*sym_sensors_get_value)(const sensors_chip_name*, int, double*);
static char* (*sym_sensors_get_label)(const sensors_chip_name*, const sensors_feature*);

static void* dlopenHandle = NULL;

#endif /* BUILD_STATIC */

int LibSensors_init(FILE* input) {
#ifdef BUILD_STATIC

   return sym_sensors_init(input);

#else

   if (!dlopenHandle) {
      /* Find the unversioned libsensors.so (symlink) and prefer that, but Debian has .so.5 and Fedora .so.4 without
         matching symlinks (unless people install the -dev packages) */
      dlopenHandle = dlopen("libsensors.so", RTLD_LAZY);
      if (!dlopenHandle)
         dlopenHandle = dlopen("libsensors.so.5", RTLD_LAZY);
      if (!dlopenHandle)
         dlopenHandle = dlopen("libsensors.so.4", RTLD_LAZY);
      if (!dlopenHandle)
         goto dlfailure;

      /* Clear any errors */
      dlerror();

      #define resolve(symbolname) do {                                      \
         *(void **)(&sym_##symbolname) = dlsym(dlopenHandle, #symbolname);  \
         if (!sym_##symbolname || dlerror() != NULL)                        \
            goto dlfailure;                                                 \
      } while(0)

      resolve(sensors_init);
      resolve(sensors_cleanup);
      resolve(sensors_get_detected_chips);
      resolve(sensors_snprintf_chip_name);
      resolve(sensors_get_features);
      resolve(sensors_get_subfeature);
      resolve(sensors_get_value);
      resolve(sensors_get_label);

      #undef resolve
   }

   return sym_sensors_init(input);


dlfailure:
   if (dlopenHandle) {
      dlclose(dlopenHandle);
      dlopenHandle = NULL;
   }
   return -1;

#endif /* BUILD_STATIC */
}

void LibSensors_cleanup(void) {
#ifdef BUILD_STATIC

   sym_sensors_cleanup();

#else

   if (dlopenHandle) {
      sym_sensors_cleanup();

      dlclose(dlopenHandle);
      dlopenHandle = NULL;
   }

#endif /* BUILD_STATIC */
}

void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int cpuCount) {
   for (unsigned int i = 0; i <= cpuCount; i++)
      cpus[i].temperature = NAN;

#ifndef BUILD_STATIC
   if (!dlopenHandle)
      return;
#endif /* !BUILD_STATIC */

   unsigned int coreTempCount = 0;

   int n = 0;
   for (const sensors_chip_name *chip = sym_sensors_get_detected_chips(NULL, &n); chip; chip = sym_sensors_get_detected_chips(NULL, &n)) {
      char buffer[32];
      sym_sensors_snprintf_chip_name(buffer, sizeof(buffer), chip);
      if (!String_startsWith(buffer, "coretemp") &&
          !String_startsWith(buffer, "cpu_thermal") &&
          !String_startsWith(buffer, "k10temp") &&
          !String_startsWith(buffer, "zenpower"))
         continue;

      int m = 0;
      for (const sensors_feature *feature = sym_sensors_get_features(chip, &m); feature; feature = sym_sensors_get_features(chip, &m)) {
         if (feature->type != SENSORS_FEATURE_TEMP)
            continue;

         char* label = sym_sensors_get_label(chip, feature);
         if (!label)
            continue;

         unsigned int tempId;
         if (String_startsWith(label, "Package ")) {
            tempId = 0;
         } else if (String_startsWith(label, "temp")) {
            /* Raspberry Pi has only temp1 */
            tempId = 0;
         } else if (String_startsWith(label, "Tdie")) {
            tempId = 0;
         } else if (String_startsWith(label, "Core ")) {
            tempId = 1 + atoi(label + strlen("Core "));
         } else {
            tempId = UINT_MAX;
         }

         free(label);

         if (tempId > cpuCount)
            continue;

         const sensors_subfeature *sub_feature = sym_sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
         if (sub_feature) {
            double temp;
            int r = sym_sensors_get_value(chip, sub_feature->number, &temp);
            if (r != 0)
               continue;

            cpus[tempId].temperature = temp;
            if (tempId > 0)
               coreTempCount++;
         }
      }
   }

   const double packageTemp = cpus[0].temperature;

   /* Only package temperature - copy to all cpus */
   if (coreTempCount == 0 && !isnan(packageTemp)) {
      for (unsigned int i = 1; i <= cpuCount; i++)
         cpus[i].temperature = packageTemp;

      return;
   }

   /* No package temperature - set to max core temperature */
   if (isnan(packageTemp) && coreTempCount != 0) {
      double maxTemp = NAN;
      for (unsigned int i = 1; i <= cpuCount; i++) {
         const double coreTemp = cpus[i].temperature;
         if (isnan(coreTemp))
            continue;

         maxTemp = MAXIMUM(maxTemp, coreTemp);
      }

      cpus[0].temperature = maxTemp;
   }

   /* Half the temperatures, probably HT/SMT - copy to second half */
   const unsigned int delta = cpuCount / 2;
   if (coreTempCount == delta) {
      for (unsigned int i = 1; i <= delta; i++)
         cpus[i + delta].temperature = cpus[i].temperature;
   }
}

#endif /* HAVE_SENSORS_SENSORS_H */

© 2014-2024 Faster IT GmbH | imprint | privacy policy