LibSensors.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /* Rockchip RK3566 */
  113. { "soc_thermal", 0 },
  114. /* Low priority drivers */
  115. { "acpitz", 1 },
  116. };
  117. for (size_t i = 0; i < ARRAYSIZE(tempDrivers); i++)
  118. if (String_eq(chip->prefix, tempDrivers[i].prefix))
  119. return tempDrivers[i].priority;
  120. return -1;
  121. }
  122. void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs) {
  123. assert(existingCPUs > 0 && existingCPUs < 16384);
  124. double* data = xMallocArray(existingCPUs + 1, sizeof(double));
  125. for (size_t i = 0; i < existingCPUs + 1; i++)
  126. data[i] = NAN;
  127. #ifndef BUILD_STATIC
  128. if (!dlopenHandle)
  129. goto out;
  130. #endif /* !BUILD_STATIC */
  131. unsigned int coreTempCount = 0;
  132. int topPriority = 99;
  133. int n = 0;
  134. for (const sensors_chip_name* chip = sym_sensors_get_detected_chips(NULL, &n); chip; chip = sym_sensors_get_detected_chips(NULL, &n)) {
  135. const int priority = tempDriverPriority(chip);
  136. if (priority < 0)
  137. continue;
  138. if (priority > topPriority)
  139. continue;
  140. if (priority < topPriority) {
  141. /* Clear data from lower priority sensor */
  142. for (size_t i = 0; i < existingCPUs + 1; i++)
  143. data[i] = NAN;
  144. }
  145. topPriority = priority;
  146. int m = 0;
  147. for (const sensors_feature* feature = sym_sensors_get_features(chip, &m); feature; feature = sym_sensors_get_features(chip, &m)) {
  148. if (feature->type != SENSORS_FEATURE_TEMP)
  149. continue;
  150. if (!feature->name || !String_startsWith(feature->name, "temp"))
  151. continue;
  152. unsigned long int tempID = strtoul(feature->name + strlen("temp"), NULL, 10);
  153. if (tempID == 0 || tempID == ULONG_MAX)
  154. continue;
  155. /* Feature name IDs start at 1, adjust to start at 0 to match data indices */
  156. tempID--;
  157. if (tempID > existingCPUs)
  158. continue;
  159. const sensors_subfeature* subFeature = sym_sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
  160. if (!subFeature)
  161. continue;
  162. double temp;
  163. int r = sym_sensors_get_value(chip, subFeature->number, &temp);
  164. if (r != 0)
  165. continue;
  166. /* Map temperature values to Rockchip cores
  167. *
  168. * littlecore -> cores 1..4
  169. * bigcore0 -> cores 5,6
  170. * bigcore1 -> cores 7,8
  171. */
  172. if (existingCPUs == 8) {
  173. if (String_eq(chip->prefix, "littlecore_thermal")) {
  174. data[1] = temp;
  175. data[2] = temp;
  176. data[3] = temp;
  177. data[4] = temp;
  178. coreTempCount += 4;
  179. continue;
  180. }
  181. if (String_eq(chip->prefix, "bigcore0_thermal")) {
  182. data[5] = temp;
  183. data[6] = temp;
  184. coreTempCount += 2;
  185. continue;
  186. }
  187. if (String_eq(chip->prefix, "bigcore1_thermal") || String_eq(chip->prefix, "bigcore2_thermal")) {
  188. data[7] = temp;
  189. data[8] = temp;
  190. coreTempCount += 2;
  191. continue;
  192. }
  193. }
  194. /* Rockchip RK3566 */
  195. if (existingCPUs == 4) {
  196. if (String_eq(chip->prefix, "soc_thermal")) {
  197. data[1] = temp;
  198. data[2] = temp;
  199. data[3] = temp;
  200. data[4] = temp;
  201. coreTempCount += 4;
  202. continue;
  203. }
  204. }
  205. /* If already set, e.g. Ryzen reporting platform temperature for each die, use the bigger one */
  206. if (isNaN(data[tempID])) {
  207. data[tempID] = temp;
  208. if (tempID > 0)
  209. coreTempCount++;
  210. } else {
  211. data[tempID] = MAXIMUM(data[tempID], temp);
  212. }
  213. }
  214. }
  215. /* Adjust data for chips not providing a platform temperature */
  216. if (coreTempCount + 1 == activeCPUs || coreTempCount + 1 == activeCPUs / 2) {
  217. memmove(&data[1], &data[0], existingCPUs * sizeof(*data));
  218. data[0] = NAN;
  219. coreTempCount++;
  220. /* Check for further adjustments */
  221. }
  222. /* Only package temperature - copy to all cores */
  223. if (coreTempCount == 0 && !isNaN(data[0])) {
  224. for (size_t i = 1; i <= existingCPUs; i++)
  225. data[i] = data[0];
  226. /* No further adjustments */
  227. goto out;
  228. }
  229. /* No package temperature - set to max core temperature */
  230. if (coreTempCount > 0 && isNaN(data[0])) {
  231. double maxTemp = -HUGE_VAL;
  232. for (size_t i = 1; i <= existingCPUs; i++) {
  233. if (isgreater(data[i], maxTemp)) {
  234. maxTemp = data[i];
  235. data[0] = data[i];
  236. }
  237. }
  238. /* Check for further adjustments */
  239. }
  240. /* Only temperature for core 0, maybe Ryzen - copy to all other cores */
  241. if (coreTempCount == 1 && !isNaN(data[1])) {
  242. for (size_t i = 2; i <= existingCPUs; i++)
  243. data[i] = data[1];
  244. /* No further adjustments */
  245. goto out;
  246. }
  247. /* Half the temperatures, probably HT/SMT - copy to second half */
  248. const size_t delta = activeCPUs / 2;
  249. if (coreTempCount == delta) {
  250. memcpy(&data[delta + 1], &data[1], delta * sizeof(*data));
  251. /* No further adjustments */
  252. goto out;
  253. }
  254. out:
  255. for (size_t i = 0; i <= existingCPUs; i++)
  256. cpus[i].temperature = data[i];
  257. free(data);
  258. }
  259. #endif /* HAVE_SENSORS_SENSORS_H */