2014-05-17 78 views
5

有很多類似的問題,但我還沒有找到解決方案。在linux ubuntu 12.10上使用AMD FX 4100四核處理器獲得CPU溫度

如何在Linux或Linux上使用C或C++獲得CPU溫度Ubuntu Ubuntu 12.10 未調用sensors?我當然可以從文件中讀取它,但是我無法在12.10中找到它的存儲位置。只是簡單地閱讀文本文件的可能性,或者我可以使用系統調用或信號查詢內核?我的文件夾中的/ proc/ACPI的

內容/只是

event wakeup 

沒有THEMP0或有這樣的事。然而,sensors應用程序可以在我的機器上顯示溫度。

沒有/sys/class/thermal/thermal_zone0/目錄

/sys/class/thermal

[email protected] [email protected] [email protected] [email protected] 

我想瀏覽搜索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) 
+0

'strace -f/usr/bin/sensors 2>&1 | grep open'來查看'sensors'打開了什麼,可能會給你一個線索。 – nos

回答

3

根據該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)與溫度信息可能會有所不同。

+0

感謝,/ sys/class/hwmon/hwmon2/device/temp1_input是我在尋找的 – 4pie0

1

基於LM-傳感器和藍月亮的答案我寫了這個代碼,在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("%lf\n", value); 
    } 

    return 0; 
} 

A碼(這只是舉例)發現herehere是一個日誌記錄工具。

+0

'/ sys/class/hwmon/hwmonX/device/name'可以用來檢查它是否是你感興趣的硬件(' coretemp'用於CPU),因爲即使使用相同的OS,所有機器上的CPU目錄('hwmon2')可能都不相同。 –