有很多类似的问题,但我还没有找到解决方案。
如何在 Linux Ubuntu 12.10上获取 C 或 C ++ 中的 CPU 温度,而无需调用sensors
?我当然可以从文件中读取它,但是我找不到它存储在 12.10 中的位置。
我的文件夹 / proc / acpi / 的内容只是
event wakeup
没有 THEMP0 或类似的东西。sensors
但是应用程序可以在我的机器上显示温度。
no/sys/class/thermal/thermal_zone0/
目录
在/sys/class/thermal
中,我有
cooling_device0@ cooling_device1@ cooling_device2@ cooling_device3@
我试图浏览lm-sensors源代码,以搜索它如何检索温度,到目前为止无济于事,但是我很接近。该文件是
http://lm-sensors.org/browser/lm-sensors/trunk/lib/sysfs.c特别是:
第 846 行:
846 int sensors_read_sysfs_attr(const sensors_chip_name *name,
847 const sensors_subfeature *subfeature,
848 double *value)
根据sysfs documentation,传感器信息存储在/sys/class/hwmon
下,每个芯片的目录不同。这与我在 Ubuntu 13.10 上看到的输出一致。
传感器使用的文件有:
/sys/class/hwmon/hwmon*/device/temp*
根据芯片 / 虚拟设备的数量,可以有许多hwmon
目录。
我的双核系统上的输出:
$ pwd
/sys/class/hwmon
$ ls -l
total 0
lrwxrwxrwx 1 root root 0 May 17 14:29 hwmon0 -> ../../devices/virtual/hwmon/hwmon0
lrwxrwxrwx 1 root root 0 May 17 14:29 hwmon1 -> ../../devices/platform/coretemp.0/hwmon/hwmon1
lrwxrwxrwx 1 root root 0 May 17 14:29 hwmon2 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/hwmon/hwmon2
其中hwmon1
是我的 CPU:
$ pwd
/sys/class/hwmon/hwmon1/device
$ ls -l
total 0
lrwxrwxrwx 1 root root 0 May 17 14:29 driver -> ../../../bus/platform/drivers/coretemp
drwxr-xr-x 3 root root 0 May 17 14:29 hwmon
-r--r--r-- 1 root root 4096 May 17 23:21 modalias
-r--r--r-- 1 root root 4096 May 17 14:29 name
drwxr-xr-x 2 root root 0 May 17 23:21 power
lrwxrwxrwx 1 root root 0 May 17 14:29 subsystem -> ../../../bus/platform
-r--r--r-- 1 root root 4096 May 17 14:29 temp2_crit
-r--r--r-- 1 root root 4096 May 17 14:29 temp2_crit_alarm
-r--r--r-- 1 root root 4096 May 17 14:29 temp2_input
-r--r--r-- 1 root root 4096 May 17 23:11 temp2_label
-r--r--r-- 1 root root 4096 May 17 14:29 temp2_max
-r--r--r-- 1 root root 4096 May 17 14:29 temp3_crit
-r--r--r-- 1 root root 4096 May 17 14:29 temp3_crit_alarm
-r--r--r-- 1 root root 4096 May 17 14:29 temp3_input
-r--r--r-- 1 root root 4096 May 17 23:11 temp3_label
-r--r--r-- 1 root root 4096 May 17 14:29 temp3_max
-rw-r--r-- 1 root root 4096 May 17 14:29 uevent
temp2*
和temp3*
中的值分别对应于core 0
和core 1
。基本上,这些是sensors
从中读取数据的文件。根据您的硬件设备,您的 CPU 目录(在我的例子中是hwmon1
)与温度信息可能不同。
基于 lm 传感器和蓝月亮的答案我写了这个代码,在 Ubuntu 12.10 与 AMD FX 4100 四核处理器上工作正常:
int main(void) {
double value;
int TEMP_IDX_MAX = 3;
FILE *f;
const char* n[] = {"/sys/class/hwmon/hwmon0/device/temp1_input",
"/sys/class/hwmon/hwmon0/device/temp2_input",
"/sys/class/hwmon/hwmon0/device/temp3_input"};
for ( int i = 0; i < TEMP_IDX_MAX; ++i) {
if ( ( f = fopen( n[i], "r"))) {
int res, err = 0;
errno = 0;
res = fscanf( f, "%lf", &value);
if ( res == EOF && errno == EIO)
err = -SENSORS_ERR_IO;
else if ( res != 1)
err = -SENSORS_ERR_ACCESS_R;
res = fclose( f);
if ( err)
return err;
if ( res == EOF) {
if ( errno == EIO)
return -SENSORS_ERR_IO;
else
return -SENSORS_ERR_ACCESS_R;
}
value /= get_type_scaling( SENSORS_SUBFEATURE_TEMP_INPUT);
} else
return -SENSORS_ERR_KERNEL;
printf( "%lf\n", value);
}
return 0;
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(22条)