LibSensors.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. htop - linux/LibSensors.c
  3. (C) 2020-2023 htop dev team
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "linux/LibSensors.h"
  9. #ifdef HAVE_SENSORS_SENSORS_H
  10. #include <assert.h>
  11. #include <dlfcn.h>
  12. #include <errno.h>
  13. #include <limits.h>
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sensors/sensors.h>
  19. #include "Macros.h"
  20. #include "XUtils.h"
  21. #include "linux/LinuxMachine.h"
  22. #ifdef BUILD_STATIC
  23. #define sym_sensors_init sensors_init
  24. #define sym_sensors_cleanup sensors_cleanup
  25. #define sym_sensors_get_detected_chips sensors_get_detected_chips
  26. #define sym_sensors_get_features sensors_get_features
  27. #define sym_sensors_get_subfeature sensors_get_subfeature
  28. #define sym_sensors_get_value sensors_get_value
  29. #else
  30. static int (*sym_sensors_init)(FILE*);
  31. static void (*sym_sensors_cleanup)(void);
  32. static const sensors_chip_name* (*sym_sensors_get_detected_chips)(const sensors_chip_name*, int*);
  33. static const sensors_feature* (*sym_sensors_get_features)(const sensors_chip_name*, int*);
  34. static const sensors_subfeature* (*sym_sensors_get_subfeature)(const sensors_chip_name*, const sensors_feature*, sensors_subfeature_type);
  35. static int (*sym_sensors_get_value)(const sensors_chip_name*, int, double*);
  36. static void* dlopenHandle = NULL;
  37. #endif /* BUILD_STATIC */
  38. int LibSensors_init(void) {
  39. #ifdef BUILD_STATIC
  40. return sym_sensors_init(NULL);
  41. #else
  42. if (!dlopenHandle) {
  43. /* Find the unversioned libsensors.so (symlink) and prefer that, but Debian has .so.5 and Fedora .so.4 without
  44. matching symlinks (unless people install the -dev packages) */
  45. dlopenHandle = dlopen("libsensors.so", RTLD_LAZY);
  46. if (!dlopenHandle)
  47. dlopenHandle = dlopen("libsensors.so.5", RTLD_LAZY);
  48. if (!dlopenHandle)
  49. dlopenHandle = dlopen("libsensors.so.4", RTLD_LAZY);
  50. if (!dlopenHandle)
  51. goto dlfailure;
  52. /* Clear any errors */
  53. dlerror();
  54. #define resolve(symbolname) do { \
  55. *(void **)(&sym_##symbolname) = dlsym(dlopenHandle, #symbolname); \
  56. if (!sym_##symbolname || dlerror() != NULL) \
  57. goto dlfailure; \
  58. } while(0)
  59. resolve(sensors_init);
  60. resolve(sensors_cleanup);
  61. resolve(sensors_get_detected_chips);
  62. resolve(sensors_get_features);
  63. resolve(sensors_get_subfeature);
  64. resolve(sensors_get_value);
  65. #undef resolve
  66. }
  67. return sym_sensors_init(NULL);
  68. dlfailure:
  69. if (dlopenHandle) {
  70. dlclose(dlopenHandle);
  71. dlopenHandle = NULL;
  72. }
  73. return -1;
  74. #endif /* BUILD_STATIC */
  75. }
  76. void LibSensors_cleanup(void) {
  77. #ifdef BUILD_STATIC
  78. sym_sensors_cleanup();
  79. #else
  80. if (dlopenHandle) {
  81. sym_sensors_cleanup();
  82. dlclose(dlopenHandle);
  83. dlopenHandle = NULL;
  84. }
  85. #endif /* BUILD_STATIC */
  86. }
  87. int LibSensors_reload(void) {
  88. #ifndef BUILD_STATIC
  89. if (!dlopenHandle) {
  90. errno = ENOTSUP;
  91. return -1;
  92. }
  93. #endif /* !BUILD_STATIC */
  94. sym_sensors_cleanup();
  95. return sym_sensors_init(NULL);
  96. }
  97. static int tempDriverPriority(const sensors_chip_name* chip) {
  98. static const struct TempDriverDefs {
  99. const char* prefix;
  100. int priority;
  101. } tempDrivers[] = {
  102. { "coretemp", 0 },
  103. { "via_cputemp", 0 },
  104. { "cpu_thermal", 0 },
  105. { "k10temp", 0 },
  106. { "zenpower", 0 },
  107. /* Rockchip RK3588 */
  108. { "littlecore_thermal", 0 },
  109. { "bigcore0_thermal", 0 },
  110. { "bigcore1_thermal", 0 },
  111. { "bigcore2_thermal", 0 },
  112. /* Low priority drivers */
  113. { "acpitz", 1 },
  114. };
  115. for (size_t i = 0; i < ARRAYSIZE(tempDrivers); i++)
  116. if (String_eq(chip->prefix, tempDrivers[i].prefix))
  117. return tempDrivers[i].priority;
  118. return -1;
  119. }
  120. void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs) {
  121. assert(existingCPUs > 0 && existingCPUs < 16384);
  122. double* data = xMallocArray(existingCPUs + 1, sizeof(double));
  123. for (size_t i = 0; i < existingCPUs + 1; i++)
  124. data[i] = NAN;
  125. #ifndef BUILD_STATIC
  126. if (!dlopenHandle)
  127. goto out;
  128. #endif /* !BUILD_STATIC */
  129. unsigned int coreTempCount = 0;
  130. int topPriority = 99;
  131. int n = 0;
  132. for (const sensors_chip_name* chip = sym_sensors_get_detected_chips(NULL, &n); chip; chip = sym_sensors_get_detected_chips(NULL, &n)) {
  133. const int priority = tempDriverPriority(chip);
  134. if (priority < 0)
  135. continue;
  136. if (priority > topPriority)
  137. continue;
  138. if (priority < topPriority) {
  139. /* Clear data from lower priority sensor */
  140. for (size_t i = 0; i < existingCPUs + 1; i++)
  141. data[i] = NAN;
  142. }
  143. topPriority = priority;
  144. int m = 0;
  145. for (const sensors_feature* feature = sym_sensors_get_features(chip, &m); feature; feature = sym_sensors_get_features(chip, &m)) {
  146. if (feature->type != SENSORS_FEATURE_TEMP)
  147. continue;
  148. if (!feature->name || !String_startsWith(feature->name, "temp"))
  149. continue;
  150. unsigned long int tempID = strtoul(feature->name + strlen("temp"), NULL, 10);
  151. if (tempID == 0 || tempID == ULONG_MAX)
  152. continue;
  153. /* Feature name IDs start at 1, adjust to start at 0 to match data indices */
  154. tempID--;
  155. if (tempID > existingCPUs)
  156. continue;
  157. const sensors_subfeature* subFeature = sym_sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
  158. if (!subFeature)
  159. continue;
  160. double temp;
  161. int r = sym_sensors_get_value(chip, subFeature->number, &temp);
  162. if (r != 0)
  163. continue;
  164. /* Map temperature values to Rockchip cores
  165. *
  166. * littlecore -> cores 1..4
  167. * bigcore0 -> cores 5,6
  168. * bigcore1 -> cores 7,8
  169. */
  170. if (existingCPUs == 8) {
  171. if (String_eq(chip->prefix, "littlecore_thermal")) {
  172. data[1] = temp;
  173. data[2] = temp;
  174. data[3] = temp;
  175. data[4] = temp;
  176. coreTempCount += 4;
  177. continue;
  178. }
  179. if (String_eq(chip->prefix, "bigcore0_thermal")) {
  180. data[5] = temp;
  181. data[6] = temp;
  182. coreTempCount += 2;
  183. continue;
  184. }
  185. if (String_eq(chip->prefix, "bigcore1_thermal") || String_eq(chip->prefix, "bigcore2_thermal")) {
  186. data[7] = temp;
  187. data[8] = temp;
  188. coreTempCount += 2;
  189. continue;
  190. }
  191. }
  192. /* If already set, e.g. Ryzen reporting platform temperature for each die, use the bigger one */
  193. if (isNaN(data[tempID])) {
  194. data[tempID] = temp;
  195. if (tempID > 0)
  196. coreTempCount++;
  197. } else {
  198. data[tempID] = MAXIMUM(data[tempID], temp);
  199. }
  200. }
  201. }
  202. /* Adjust data for chips not providing a platform temperature */
  203. if (coreTempCount + 1 == activeCPUs || coreTempCount + 1 == activeCPUs / 2) {
  204. memmove(&data[1], &data[0], existingCPUs * sizeof(*data));
  205. data[0] = NAN;
  206. coreTempCount++;
  207. /* Check for further adjustments */
  208. }
  209. /* Only package temperature - copy to all cores */
  210. if (coreTempCount == 0 && !isNaN(data[0])) {
  211. for (size_t i = 1; i <= existingCPUs; i++)
  212. data[i] = data[0];
  213. /* No further adjustments */
  214. goto out;
  215. }
  216. /* No package temperature - set to max core temperature */
  217. if (coreTempCount > 0 && isNaN(data[0])) {
  218. double maxTemp = -HUGE_VAL;
  219. for (size_t i = 1; i <= existingCPUs; i++) {
  220. if (isgreater(data[i], maxTemp)) {
  221. maxTemp = data[i];
  222. data[0] = data[i];
  223. }
  224. }
  225. /* Check for further adjustments */
  226. }
  227. /* Only temperature for core 0, maybe Ryzen - copy to all other cores */
  228. if (coreTempCount == 1 && !isNaN(data[1])) {
  229. for (size_t i = 2; i <= existingCPUs; i++)
  230. data[i] = data[1];
  231. /* No further adjustments */
  232. goto out;
  233. }
  234. /* Half the temperatures, probably HT/SMT - copy to second half */
  235. const size_t delta = activeCPUs / 2;
  236. if (coreTempCount == delta) {
  237. memcpy(&data[delta + 1], &data[1], delta * sizeof(*data));
  238. /* No further adjustments */
  239. goto out;
  240. }
  241. out:
  242. for (size_t i = 0; i <= existingCPUs; i++)
  243. cpus[i].temperature = data[i];
  244. free(data);
  245. }
  246. #endif /* HAVE_SENSORS_SENSORS_H */