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

get CPU temperature on linux ubuntu 12.10 with AMD FX 4100 Quad Core

本文关键字:四核 4100 获取 温度 CPU FX AMD ubuntu linux      更新时间:2023-10-16

有很多类似的问题,但我还没有找到解决方案。

如何在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传感器源代码,寻找它如何检索温度,到目前为止无济于事,但我很接近。文件是

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文档,传感器信息存储在/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 0core 1。基本上这些是sensors读取数据的文件。根据您的硬件设备,您的CPU目录(在我的情况下是hwmon1)的温度信息可能不同。

基于lm传感器和Blue Moon的答案,我编写了这段代码,在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( "%lfn", value);
    }
    return 0;
}

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