Linux内核Thermal框架详解一、总述

本文深入剖析Linux内核的Thermal框架,该框架用于设备温度控制,防止过热导致的系统不稳定。关键组件包括温度传感器驱动、冷却设备和温控策略。Thermalzone、coolingdevice和governor协同工作,根据传感器数据调整设备状态,确保系统温度保持在安全范围内。文章详细介绍了各组件的角色和相互作用,并展示了框架的代码结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文部分内容参考

万字长文 | Thermal框架源码剖析

Linux Thermal机制源码分析之框架概述_不捡风筝的玖伍贰柒的博客-CSDN博客

https://www.cnblogs.com/hellokitty2/p/15600099.html

特此致谢!

Thermal,中文意思是热的、保暖的。在Linux内核中,Thermal特指一套关于温控机制的驱动框架。也就是说,Linux Thermal框架Linux系统下温度控制相关的一套架构,主要用来解决随着设备性能不断增强而引起的日益严重的发热问题,控制系统运行过程中各个器件所产生的热量,使设备温度维持在一个安全、舒适的范围,防止SoC 等硬件芯片也会因过热而造成系统不稳定甚至缩减芯片寿命

Thermal框架是在软件层面上对自然界散热系统的抽象。以日常生活中的温控设备——空调为例,试想一下空调的工作机制及整个温控过程:

(0)假设场景是夏天,某人刚回到家,当前室内温度是35度,用户赶紧打开空调并设定目标温度为25度;

(1)对房间降温首先要知道房间里当前温度是多少,这个工作需要温度传感器来完成。温度传感器探知室内温度;

(2)温度传感器将当前温度传给空调内部的MCU(姑且认为空调的控制系统是由一颗内嵌的MCU来完成的);

(3)MCU得知当前温差是10度,比较大,因此加大功率,让房间迅速降温(我们知道空调内部的降温设备是雪种);

(4)一段时间后,室温降至28度(此时温度传感器重复步骤(1)和(2))。此时MCU开始减小功率,降温速度开始趋缓,直至稳定在 25 度。

当然,空调实际的工作方式不一定和上面描述的一模一样,但是基本原理是这样。我们试图通过空调的例子抽取温控系统的必要组件,然后过渡到Linux内核中的Thermal 框架:

(1)既然要控制房间温度,当然要能知道房间当前的温度,这个必要组件就是温度传感器。

(2)降温或者升温,需要实际的设备来支撑,这个必要组件就是雪种。

(3)温差大,加大功率以便迅速降温;温差小,降低功率省电。这个必要组件就是温控策略,或者叫温控算法。

(4)整个温控系统需要有一个类似大脑的设备来统筹管理,这个必要组件就是MCU。

类比到Thermal框架(主要考虑CPU/GPU的温控),其必要组件和上面空调的例子十分相似:

(1)Thermal sensor driver:SoC内部CPU和GPU的旁边通常会有用于获取它们温度的传感器,比如 tsadc(Temperature Sensor ADC)。

(2)Thermal cooling device:降温设备,比如风扇。这里有点特殊的是,CPU和GPU不仅是发热设备(即需要实施温控策略的设备),也可以是降温设备。当我们降低CPU/GPU的运行频率的时候,它们就在充当降温设备(降低产热量即是在降温)。

(3)Thermal governer:温控策略,Linux内核中的温控策略要比上面的空调控制精细得多,而且也提供了多种策略。

(4)Thermal core:组织并管理上面三个组件,并通过sysfs和用户空间交互。

归纳一下:核心为thermal_core;可以获取温度的设备抽象为thermal_zone_device,如Temp Sensor、NTC(板上的热敏电阻)等;控制温度的设备抽象为thermal_cooling_device,如风扇、CPU、DDR、GPU等;温控策略抽象为thermal_governor,如step_wise、bang_bang等。

铺垫了这么多内容,到了正式引出主角——Thrermal框架的时候了。

一、框架结构

Thermal框架由core、governor、zone、cooling device以及sensor driver组成。 

Thermal整体框架结构如下图所示:

各个模块之间关系如下:

thermal_cooling_device对应系统实施冷却措施的驱动,是温控的执行者。cooling device维护一个cooling等级,即state。一般state越高即系统的冷却需求越高。cooling device根据不同等级的冷却需求进行冷却行为。

cooling device只根据state进行冷却操作,是实施者;结构struct cpufreq_cooling_devicestruct devfreq_cooling_device作为对thermal_cooling_device的扩展,分别主要在cpufreq_cooling.c和devfreq_cooling.c中使用;而state的计算由thermal governor完成thermal_instance结构体描述trip point与cooling device的绑定关系,即当trip point触发后由哪个cooling device去实施冷却措施。每个trip point必须与一个cooling device绑定,才有实际意义。
一个Thermal Zone可以有多个Cooling设备;同时还提供一个核心函数thermal_zone_device_update作为 Thermal 中断处理函数和轮询函数,轮询时间会根据不同Trip Delay调节

模块之间的代码流程图如下:

 

基于系统层次的框架结构如下图所示:

 

  • thermal core

thermal主要的程序,驱动初始化程序,维系thermal zone、governor、cooling device三者的关系,并通过 sysfs 和用户空间交互。

  • thermal govnernor

温度控制算法(温控策略)。解决温控发生时(即throttle),cooling device如何选择cooling state的问题。

Linux内核中提供了多种温控策略,如下:

  • step_wise
  • power_allocator
  • user_space
  • fair_share
  • bang_bang

可见,Linux内核的温控策略是比较精细的。

  • thermal cooling device

系统温控的执行者,实施冷却措施的驱动(cpufreq_cooling、cpuidle_cooling、 devfreq_cooling等)。通俗地讲,就是降温设备,如风扇。cooling device根据governor计算出来的state,实施冷却操作,一般情况下,state越高表示系统的冷却需求越高。cooling device需要与trip point进行绑定,当 trip point 触发后,由相应的cooling device 去实施冷却措施。

这里有一点需要特别说明:CPU和GPU(当然,也包括其它类似设备)不仅是发热设备(即需要实施温控策略的设备),也可以是降温设备,当我们降低CPU/GPU的运行频率的时候,它们就在充当降温设备。降低产热量即是在降温。

  • thermal zone device

创建thermal zone结点和连接thermal sensor,在/sys/class/thermal/目录下的thermal_zone*,通过dtsi文件进行配置生成。thermal sensor是温度传感器(即热敏电阻NTC),主要是给thermal提供温度感知。

二、代码结构

Thermal框架整体代码结构如下图所示:

可以看到,还是挺复杂的。通过上图中复杂的层次就可以推断,代码量及其中包含的知识点是非常多的。不过没有关系,后续文章会将整体拆分为一个个子部分,分别对每个子部分进行讲解和分析。

### Thermal Desktop Software for Thermal Management Thermal management plays a critical role in ensuring the reliability and performance of IT systems. Specialized software tools like Thermal Desktop provide comprehensive solutions to manage heat dissipation effectively. #### Overview of Thermal Desktop Software Thermal Desktop is an advanced thermal analysis tool designed specifically for modeling complex electronic assemblies, including computers and servers[^1]. This software enables engineers to simulate various operating conditions and predict temperature distributions within components accurately. By integrating with other CAD tools, it facilitates detailed analyses that help optimize cooling strategies while maintaining optimal temperatures across all hardware elements. The application supports multiple simulation scenarios such as steady-state or transient operations under different environmental settings. Users can define custom materials properties, boundary conditions, and power profiles tailored to specific use cases. Additionally, automated mesh generation simplifies model creation without compromising accuracy. For instance, when managing ACPI-enabled devices where dynamic adjustments are made based on workload demands, Thermal Desktop allows users to incorporate these variables into simulations: ```python def acpi_thermal_simulation(workload_profile): """ Simulate thermal behavior using ACPI states. Args: workload_profile (dict): Dictionary containing CPU/GPU load percentages over time intervals Returns: dict: Temperature readings at specified component locations throughout simulation period """ pass # Placeholder function body; actual implementation would involve calling API functions provided by Thermal Desktop SDK ``` This capability ensures realistic predictions about how changes in activity levels will impact overall system thermals, aiding in designing efficient yet robust architectures capable of handling varying workloads gracefully. --related questions-- 1. How does Thermal Desktop integrate with existing CAD environments? 2. What types of material property data can be imported/exported from/to Thermal Desktop? 3. Can Thermal Desktop support real-time monitoring interfaces during live operation tests? 4. Are there any limitations regarding supported file formats for importing geometry models?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝天居士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值