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

c, c++, cpu, linux, temperature

Solution

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.

Problem

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) ```

Original source