Linux thermal子系統和lm_sensors用戶態工具

Linux thermal子系統和lm_sensors用戶態工具

-v0.1 2020.2.18 Sherlock init

簡介:本文分析Linux thermal子系統的現狀,以及可能與之配套使用的lm_sensors用戶態
工具的軟件構架。提供給寫thermal驅動的同學可以參考。

  1. Linux thermal驅動

Linux thermal是一個內核和溫度檢測控制有關的驅動子系統,他的位置在drivers/thermal/*.
相關的內核頭文件在include/linux/thermal.h。具體的設備驅動需要向thermal框架註冊
thermal_zone_device, thermal框架會在thermal_zone_device裏封裝一個device向系統
註冊,通過這個device向用戶態暴露一組sysfs屬性文件。用戶態可以通過這組文件設置相關
參數、獲取相關信息。

在我的筆記本上,這組屬性文件大概是這樣的:

wangzhou@kllp05:/sys/class$ tree thermal/thermal_zone0
thermal/thermal_zone0
├── available_policies
├── device -> ../../../LNXSYSTM:00/LNXSYBUS:01/LNXTHERM:00
├── emul_temp
├── integral_cutoff
├── k_d
├── k_i
├── k_po
├── k_pu
├── mode
├── offset
├── passive
├── policy
├── power
│   ├── async
│   ├── autosuspend_delay_ms
│   ├── control
│   ├── runtime_active_kids
│   ├── runtime_active_time
│   ├── runtime_enabled
│   ├── runtime_status
│   ├── runtime_suspended_time
│   └── runtime_usage
├── slope
├── subsystem -> ../../../../class/thermal
├── sustainable_power
├── temp
├── trip_point_0_temp
├── trip_point_0_type
├── type
└── uevent

這裏只是展示了thermal_zone0, 當然一個系統裏可以多個這樣的設備。

實際上一個具體的驅動和thermal子系統的關係可以通過三個對象去描述,一個就是
這裏的thermal_zone_thermal,它表示測量溫度的sensor;還可以註冊這個sensor所在
溫度管理域的降溫設備;有了降溫設備,還可以註冊相應的溫度調節策略。(to do: …)

實際上,你要是隻想讀溫度出來,只註冊一個thermal_zone_thermal並提供其中一個
獲取溫度的回調函數的實現就可以了:thermal_zone_device_ops->.get_temp。這個函數
會被/sys/class/thermal//temp的show函數調用,從而顯示測量的溫度。

thermal子系統還會根據註冊thermal_zone_device時的參數,把設備的信息通過
/sys/class/hwmon子系統暴露出來。如果你不帶註冊參數,thermal子系統默認會通過
/sys/class/hwmon暴露信息, 比如這樣:

wangzhou@kllp05:/sys/class$ tree hwmon/hwmon0
hwmon/hwmon0
├── name
├── power
│   ├── async
│   ├── autosuspend_delay_ms
│   ├── control
│   ├── runtime_active_kids
│   ├── runtime_active_time
│   ├── runtime_enabled
│   ├── runtime_status
│   ├── runtime_suspended_time
│   └── runtime_usage
├── subsystem -> ../../../../class/hwmon
├── temp1_crit
├── temp1_input
└── uevent

temp1_input的show函數會最終調用到驅動裏的.get_temp。

  1. lm_sensors用戶態工具的使用

lm_sensors可以讀取系統上sensor的信息。它的源代碼可以在這裏下載到:
https://github.com/lm-sensors/lm-sensors.git

粗略從代碼上看,它使用的是hwmon接口提供的信息。

在ubuntu系統上你可以使用如下命令簡單嘗試下lm_sensors:

sudo apt-get install lm-sensors

/* this is a Perl script in /usr/sbin/ */
sudo sensors-detect
輸入這個命令後,一路YES。

wangzhou@kllp05:~/notes$ sensors
iwlwifi-virtual-0
Adapter: Virtual device
temp1:        +37.0°C  

thinkpad-isa-0000
Adapter: ISA adapter
fan1:           0 RPM

acpitz-virtual-0
Adapter: Virtual device
temp1:        +30.0°C  (crit = +128.0°C)

coretemp-isa-0000
Adapter: ISA adapter
Package id 0:  +33.0°C  (high = +100.0°C, crit = +100.0°C)
Core 0:        +31.0°C  (high = +100.0°C, crit = +100.0°C)
Core 1:        +33.0°C  (high = +100.0°C, crit = +100.0°C)

pch_skylake-virtual-0
Adapter: Virtual device
temp1:        +30.0°C  
  1. lm_sensors分析

(to do: sensors-detect, sensors, sensord, config…)

  1. Linux thermal和lm_sensors的關係

如上,現在的lm_sensors使用的是hwmon接口獲取信息。
(to do: …)

發佈了154 篇原創文章 · 獲贊 18 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章