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

心已入冬 提交于 2020-12-29 07:17:02

问题


There are many questions similar to this but I haven't found solution there.

How can I get CPU temperature in C or C++ on Linux Ubuntu 12.10 without call to sensors? I can of course just read it from file, however I cannot find where it is stored in 12.10. And is simple reading a text file only possibility or maybe I can query the kernel using system call or signal?

Content of my folder /proc/acpi/ is just

event  wakeup

No THEMP0 there or anything like this. sensors application however can display a temperature on my machine.

no /sys/class/thermal/thermal_zone0/ directory

in /sys/class/thermal I have

cooling_device0@  cooling_device1@  cooling_device2@  cooling_device3@

I'm trying to browse lm-sensors source code in search for how it retrieves temperature, to no avail so far, however I am close. The file is

http://lm-sensors.org/browser/lm-sensors/trunk/lib/sysfs.c

in particular:

line 846:

846 int sensors_read_sysfs_attr(const sensors_chip_name *name,
847                             const sensors_subfeature *subfeature,
848                             double *value)

回答1:


According to the sysfs documentation, the sensors information is stored under /sys/class/hwmon with different directory for each chip. Which is consistent with the outputs I see on my Ubuntu 13.10.

The files used by sensors are:

/sys/class/hwmon/hwmon*/device/temp*

Depending on the number chips/virtual devices, there can be many hwmon directories.

Output on my dual core system:

$ 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

Where hwmon1 is the one for my CPUs:

$ 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

The values from temp2* and temp3* correspond to core 0 and core 1 respectively. Basically these are the files sensors read data from. Depending on your hardware devices you have, your CPU directory (hwmon1 in my case) with temperature information may be different.




回答2:


Based on lm-sensors and answer of Blue Moon I have written this code that works fine on Ubuntu 12.10 with AMD FX 4100 Quad Core processor:

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

A code (this is just example) can be found here, and here is a logging tool.



来源:https://stackoverflow.com/questions/23716135/get-cpu-temperature-on-linux-ubuntu-12-10-with-amd-fx-4100-quad-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!