【问题标题】:get CPU temperature on linux ubuntu 12.10 with AMD FX 4100 Quad Core使用 AMD FX 4100 四核在 linux ubuntu 12.10 上获取 CPU 温度
【发布时间】:2020-10-07 09:48:50
【问题描述】:

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

如何在 Linux Ubuntu 12.10 不调用 sensors 的情况下获取 C 或 C++ 中的 CPU 温度?我当然可以从文件中读取它,但是我找不到它在 12.10 中的存储位置。是否只有读取文本文件的可能性,或者我可以使用系统调用或信号查询内核?

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

event  wakeup

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

没有/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)

【问题讨论】:

  • strace -f /usr/bin/sensors 2>&1 |grep open 看看sensors 打开了什么,可能会给你一个线索。

标签: c++ c linux cpu temperature


【解决方案1】:

基于 lm-sensors 和 Blue Moon 的回答,我编写了这段代码,该代码在带有 AMD FX 4100 四核处理器的 Ubuntu 12.10 上运行良好:

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

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

【讨论】:

  • /sys/class/hwmon/hwmonX/device/name 可用于检查您是否感兴趣的硬件(coretemp 用于 CPU)以提高可移植性,因为 CPU 目录(hwmon2)可能在所有机器上都不相同使用相同的操作系统。
【解决方案2】:

根据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 0core 1。基本上这些是sensors 从中读取数据的文件。根据您拥有的硬件设备,带有温度信息的 CPU 目录(在我的情况下为 hwmon1)可能会有所不同。

【讨论】:

  • 谢谢,/sys/class/hwmon/hwmon2/device/temp1_input 是我要找的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
相关资源
最近更新 更多