2024年1月12日发(作者:)
peci读取cpu温度案例
PECI (Platform Environment Control Interface) 是一种用于读取 CPU 温度和其他系统温度的接口。下面是一个使用 C 语言和 PECI 读取 CPU 温度的示例代码:
```c
include <>
include <>
include
include
define PCI_VENDOR_ID_INTEL 0x8086
define PCI_DEVICE_ID_INTEL_CMPC 0x1191
int main(void) {
int fd;
uint32_t device_id, vendor_id;
char dev_name = "cpu_thermal";
char path[256];
struct hwmon_channel_info info;
struct hwmon_attr attr;
struct hwmon_chip_info chip;
struct hwmon_temp_sensor sensor;
long long temp;
// Open PCI bus device
fd = open("/dev/bus/pci", O_RDWR);
if (fd < 0) {
perror("open");
return -1;
}
// Find PECI device
if (ioctl(fd, PCI_IOC_READ_ID, &vendor_id) < 0) {
perror("PCI_IOC_READ_ID");
return -1;
}
if (ioctl(fd, PCI_IOC_READ_ID, &device_id) < 0) {
perror("PCI_IOC_READ_ID");
return -1;
}
if (vendor_id != PCI_VENDOR_ID_INTEL) {
printf("Not Intel vendor ID: %xn", vendor_id);
return -1;
}
if (device_id != PCI_DEVICE_ID_INTEL_CMPC) {
printf("Not Intel CMPC device ID: %xn", device_id);
return -1;
}
// Open PECI device
snprintf(path, sizeof(path),
"/sys/bus/pci/devices/%04x:%02x:%02x.%x/hwmon", vendor_id,
(device_id >> 16) & 0xff, (device_id >> 8) & 0xff, device_id & 0xff, 0);
fd = open(path, O_RDONLY);
if (fd < 0) {
perror("open");
return -1;
}
// Get hwmon chip info and channels info
if (ioctl(fd, HWMON_IOC_GETCHIPINFO, &chip) < 0) {
perror("HWMON_IOC_GETCHIPINFO");
return -1;
}
if (ioctl(fd, HWMON_IOC_GETCHANINFO, &info) < 0) {
perror("HWMON_IOC_GETCHANINFO");
return -1;
}
for (int i = 0; i < info->num; i++) {
attr = info->attrs[i];
sensor = container_of(attr->sensor, struct hwmon_temp_sensor,
attr);
if (strcmp(sensor->name, dev_name) == 0) { // Compare sensor
name with "cpu_thermal" to get CPU temperature sensor. This name
may be different on different systems. You can use "ls
/sys/class/hwmon/hwmon/temp//" to check all temperature sensors
on your system. Here is an example output:
/sys/class/hwmon/hwmon1/temp1// -> cpu-thermal-input
/sys/class/hwmon/hwmon1/temp2// -> imc-thermal-input
/sys/class/hwmon/hwmon1/temp3// -> imc-vrm-input
/sys/class/hwmon/hwmon1/temp4// -> pkg-thermal-input
/sys/class/hwmon/hwmon1/temp5// -> dts-thermal-input
/sys/class/hwmon/hwmon1/temp6// -> dts-vrm-input
/sys/class/hwmon/hwmon1/temp7// -> ddr-thermal-input
/sys/class/hwmon/hwmon1/temp8// -> ddr-vrm-input
/sys/class/hwmon/hw


发布评论