在linux ubuntu 12.10和AMD FX 4100四核上获取CPU温度
get CPU temperature on linux ubuntu 12.10 with AMD FX 4100 Quad Core
有很多类似的问题,但我还没有找到解决方案。
如何在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 0
和core 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;
}
可以在这里找到一个代码(这只是一个示例),这里是一个日志工具
相关文章:
- 对于四轴飞行器,PID中I控制器的理想值应该是多少
- 单元测试欧拉到四元数实现失败
- 找到四个 10 的所有表达式
- 在我的四叉树实现中遇到问题
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 如何在 c++ 中存储具有值的四个参数的元组
- C++ 函数,用于查找数组中四个最小最大元素的总和不起作用
- 如何有效地修剪和合并四叉树中的节点?
- 为 n 维系统实现模块化龙格-库塔四阶方法
- 将四元数旋转的游戏对象旋转另一个四元数时出现问题
- 在OpenGL(GLM/C++)中使用四元数旋转时出现问题
- 有效地计算像素到其对应核线的距离
- 如何将输入"三四+五六="转换为"34+56"?
- 如何实现四个 i8 元素组的高效_mm256_madd_epi8点积
- 想要将 CGAL 与四元数相结合是否合乎逻辑
- 使用OpenMP在四核系统上使用4个线程来加速问题
- c++ pthread多线程2倍Intel Xeon X5570,四核cpu在亚马逊EC2 HPC ubuntu实例
- 四核c++线程
- 在linux ubuntu 12.10和AMD FX 4100四核上获取CPU温度
- 在四核处理器上运行 MPICH 时出错