Linux 中如何查詢 CPU 溫度?

2024-09-01

Linux 中的設備溫度資料,記錄在 /sys/class/thermal/thermal_zone[0-*]/temp 檔案中

例如:

# cat /sys/class/thermal/thermal_zone0/temp
2800  此數字除以1000 就是攝氏溫度

# cat /sys/class/thermal/thermal_zone1/temp
34000  此數字除以1000 就是攝氏溫度


要比較美觀的話:
# echo "Temperature: $[$(cat /sys/class/thermal/thermal_zone0/temp)/1000] °C"
Temperature: 28 °C


至於 thermal_zone0 / thermal_zone1 各代表哪個設備? 可這樣查詢:
# cat /sys/devices/virtual/thermal/thermal_zone0/type
acpitz      <--ACPI(Advanced Configuration and Power Interface)電源管理和設定的介面

# cat /sys/devices/virtual/thermal/thermal_zone1/type
x86_pkg_temp   <--這個指的是 x86 cpu (arm CPU 則是 CPU-therm)


特別注意:
虛擬機無 /sys/devices/virtual/thermal/thermal_zone[0-*] 目錄
必需是實體主機才行。

/sys/class/thermal/thermal_zone[0-*] 資料,這裡有詳細文件: https://www.kernel.org/doc/Documentation/thermal/sysfs-api.txt



也有不錯的工具 lm-sensors 可直接查詢:

安裝 lm-sensors
# apt install lm-sensors
# sensors-detect   執行後 會有一堆設定,都按 Enter 即可
# sensors-detect version 3.6.0
# System: innotek GmbH VirtualBox [1.2]
# Board: Oracle Corporation VirtualBox
# Kernel: 6.8.4-2-pve x86_64
# Processor: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz (6/60/3)

This program will help you determine which kernel modules you need
to load to use lm_sensors most effectively. It is generally safe
and recommended to accept the default answers to all questions,
unless you know what you're doing.

Some south bridges, CPUs or memory controllers contain embedded sensors.
Do you want to scan for them? This is totally safe. (YES/no): YES
Module cpuid loaded successfully.
Silicon Integrated Systems SIS5595...                       No
VIA VT82C686 Integrated Sensors...                          No
VIA VT8231 Integrated Sensors...                            No
AMD K8 thermal sensors...                                   No
AMD Family 10h thermal sensors...                           No
AMD Family 11h thermal sensors...                           No
::
::


查詢設備溫度的方式:

# sensors
acpitz-acpi-0
Adapter: ACPI interface
temp1:        +27.8°C

coretemp-isa-0000
Adapter: ISA adapter
Package id 0:  +35.0°C  (high = +80.0°C, crit = +100.0°C)
Core 0:        +33.0°C  (high = +80.0°C, crit = +100.0°C)
Core 1:        +35.0°C  (high = +80.0°C, crit = +100.0°C)
Core 2:        +31.0°C  (high = +80.0°C, crit = +100.0°C)
Core 3:        +30.0°C  (high = +80.0°C, crit = +100.0°C)
Core 4:        +32.0°C  (high = +80.0°C, crit = +100.0°C)
Core 5:        +32.0°C  (high = +80.0°C, crit = +100.0°C)

另一個軟體 glances 可以看到整個 Linux 系統更多的監控資訊:

apt install glances



cpuinfo

/proc/cpuinfo 檔案即時會記錄 cpu 每個核心(core)的型號、規格..基本資料以及各核心目前的頻率

例如

# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 151
model name      : 12th Gen Intel(R) Core(TM) i5-12400
stepping        : 5
microcode       : 0x35
cpu MHz         : 800.000
cache size      : 18432 KB
physical id     : 0
siblings        : 12
core id         : 0
cpu cores       : 6
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 32
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts....略過 
vmx flags       : vnmi preemption_timer posted_intr invvpid ept_x_only ept_ad ept_1gb flexpriority apicv tsc_offset vtpr mtf vapic ept vpid unrestricted_guest vapic_reg vid ple shadow_vmcs pml ept_mode_based_exec tsc_scaling usr_wait_pause
bugs            : spectre_v1 spectre_v2 spec_store_bypass swapgs eibrs_pbrsb
bogomips        : 4992.00
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 151
model name      : 12th Gen Intel(R) Core(TM) i5-12400
stepping        : 5
microcode       : 0x35
cpu MHz         : 1240.801
cache size      : 18432 KB
physical id     : 0
siblings        : 12
core id         : 0
cpu cores       : 6
apicid          : 1
initial apicid  : 1
fpu             : yes
fpu_exception   : yes
cpuid level     : 32
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts....略過 
vmx flags       : vnmi preemption_timer posted_intr invvpid ept_x_only ept_ad ept_1gb flexpriority apicv tsc_offset vtpr mtf vapic ept vpid unrestricted_guest vapic_reg vid ple shadow_vmcs pml ept_mode_based_exec tsc_scaling usr_wait_pause
bugs            : spectre_v1 spectre_v2 spec_store_bypass swapgs eibrs_pbrsb
bogomips        : 4992.00
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:
:::
:::

cpuinfo 中的一些數字 也可以在這個目錄下找到 /sys/devices/system/cpu/cpufreq/policy[0-*]

# ls -1 /sys/devices/system/cpu/cpufreq/policy0
affected_cpus
base_frequency
cpuinfo_max_freq  CPU最高的執行頻率
cpuinfo_min_freq  CPU最低的執行頻率
cpuinfo_transition_latency
energy_performance_available_preferences
energy_performance_preference
related_cpus
scaling_available_governors  可以選用的運作模式,如performance powersave
scaling_cur_freq  
scaling_driver
scaling_governor  目前此 CPU 的運作模式(通常都是 performance)
scaling_max_freq  
scaling_min_freq
scaling_setspeed  CPU目前的頻率(數據會一直變動)
# cat /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
performance     目前CPU的運作模式是「效能模式」   

參考 https://docs.redhat.com/zh_hans/documentation/red_hat_enterprise_linux/6/html/power_management_guide/tuning_cpufreq_policy_and_speed

分類:Linux      138
Tag linux , cpu , Temperature ,
留言

留言
top