temps: try this

This commit is contained in:
Alastair Poole 2020-10-08 12:54:33 +01:00
parent 6c39ea11e8
commit 64050fc9c0
3 changed files with 88 additions and 0 deletions

View File

@ -66,6 +66,13 @@ _test(int all)
printf("Starting testing\n");
inf = system_info_all_get();
int cpu_count = system_cpu_count_get();
for (int i = 0; i < cpu_count; i++)
{
int temp = system_cpu_n_temperature_get(i);
if (temp != -1) printf(" cpu %d temp %d C\n", i, temp);
}
system_info_all_free(inf);
if (!all) goto out;

View File

@ -104,6 +104,9 @@ system_cpu_frequency_get(void);
int
system_cpu_n_frequency_get(int n);
int
system_cpu_n_temperature_get(int n);
int
system_cpu_frequency_min_max_get(int *min, int *max);

View File

@ -279,6 +279,84 @@ system_cpu_usage_get(int *ncpu)
return system_cpu_usage_delayed_get(ncpu, 1000000);
}
static char _core_temp[128][512];
static char _hwmon_path[256];
int
_cpu_n_temperature_read(int n)
{
int temp = -1;
char *b = file_contents(_core_temp[n]);
if (b)
{
temp = atoi(b) / 1000;
free(b);
}
return temp;
}
int
system_cpu_n_temperature_get(int n)
{
#if defined(__linux__)
static int init = 0;
int cpu_count, i;
if (!init)
{
char buf[4096];
struct dirent *dh;
DIR *dir;
memset(&_core_temp, 0, sizeof(_core_temp));
memset(&_hwmon_path, 0, sizeof(_hwmon_path));
dir = opendir("/sys/class/hwmon");
if (!dir)
{
init = 1;
return -1;
}
while ((dh = readdir(dir)) != NULL)
{
if (dh->d_name[0] == '.') continue;
snprintf(buf, sizeof(buf), "/sys/class/hwmon/%s", dh->d_name);
char *link = realpath(buf, NULL);
if (!link) continue;
snprintf(buf, sizeof(buf), "%s/name", link);
char *b = file_contents(buf);
if (b)
{
if (!strncmp(b, "coretemp", 8))
snprintf(_hwmon_path, sizeof(_hwmon_path), "%s", link);
free(b);
}
free(link);
}
closedir(dir);
init = 1;
}
if (!_hwmon_path[0]) return -1;
if (_core_temp[n][0])
return _cpu_n_temperature_read(n);
cpu_count = system_cpu_count_get();
i = 1 + ((cpu_count + n) / 2) - (cpu_count / 2);
snprintf(_core_temp[n], sizeof(_core_temp[n]), "%s/temp%d_input", _hwmon_path, i);
return _cpu_n_temperature_read(n);
#endif
return -1;
}
int
system_cpu_n_frequency_get(int n)
{