提问者:小点点

在linux ubuntu 12.10和AMD FX 4100四核上获取CPU温度


有许多类似的问题,但我还没有找到解决办法。

如何在Linux Ubuntu 12.10上用C或C++获取CPU温度而不调用sensors? 我当然可以从文件中读取它,但是我找不到它在12.10中的存储位置。 简单地读取一个文本文件是唯一的可能性,还是我可以使用系统调用或信号查询内核?

我的文件夹/proc/acpi/的内容只是

event  wakeup

没有那里或类似的东西。 但是sensors应用程序可以在我的机器上显示温度。

没有/sys/class/thermal/thermal_zone0/目录

/sys/class/therm中,我有

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)

共2个答案

匿名用户

根据sysfs文档,传感器信息存储在/sys/class/hwmon下,每个芯片的目录不同。 这与我在Ubuntu13.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*中的值分别对应于核心0核心1。 基本上,这些文件是sensors从中读取数据的文件。 根据您所拥有的硬件设备,包含温度信息的CPU目录(hwmon1)可能不同。

匿名用户

基于LM传感器和蓝月亮的答案,我编写了这段代码,可以在Ubuntu12.10和AMD FX4100四核处理器上很好地工作:

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;
}

这里可以找到一段代码(这只是示例),这里是一个日志记录工具。