linux thermal framework(5)_thermal core
原文:蜗窝科技linux thermal framework(5)_thermal core
1. 介绍
本文从thermal framework core对内部实现做一个简单的分析
2. 提供给用户空间的接口
thermal framework通过sysfs和debugfs向上提供接口,接口分为两类,一类是thermal zone,一类是cooling device,sysfs的接口如下:
2.1 "thermal"目录
由于发热的设备以及降温的设备都是抽象出来的,并不是一个具体的物理设备,也没有compatible的driver和设备probe,因此linux使用class来描述这一类thermal设备,thermal class中包含了所有向thermal_core注册的thermal_zone和cooling_device的子目录,并按照id区分
2.2 "thermal_zone"目录
每个thermal zone目录都包含以下节点
文件/目录 | 说明 |
available_policies | 可选温控策略列表(如step_wise、fair_share等),需内核配置支持(如CONFIG_THERMAL_GOVERNORS) |
temp | 当前温度值(单位:毫摄氏度)。 |
type | 温度传感器类型(如cpu-thermal)。 |
power/ | 电源管理相关属性(如设备休眠状态)。 |
policy | 当前生效的温控策略(需available_policies存在时才可见)。 |
subsystem | 符号链接,指向Thermal子系统的根目录。 |
sustainable_power | 可持续功耗阈值(单位:毫瓦)。 |
k_po/k_pu/k_i/k_d | PID控制算法的参数(用于动态调整温控策略)。 |
integral_cutoff | PID积分项的截断值。 |
slope/offset | 温度校准参数(线性公式:实际温度 = slope × 原始值 + offset)。 |
3.1 "cooling device"目录
每个cooling device目录都包含以下节点
/sys/class/thermal/cooling_device0
|-- cur_state
|-- max_state
|-- power
|-- autosuspend_delay_ms
|-- control
|-- runtime_active_time
|-- runtime_status
`-- runtime_suspended_time
|-- stats
|-- reset
|-- time_in_state_ms
|-- total_trans
`-- trans_table
|-- subsystem -> ../../../../class/thermal
|-- type
1)cur_state,这个cooling device当前state
2)max_state,这个cooling device支持的最大state
4)stats-time_in_state_ms,在每个state持续的时间
5)total_trans,状态切换的总次数
6)trans_table,二位表格,表示从某一个状态转换到另一个状态的次数
7)type,这个cooling device的类型
3. thermal core的初始化
kernel-6.6/drivers/thermal/thermal_core.c
static int __init thermal_init(void)
{
int result;
/* 1. 初始化Thermal Netlink通信机制(用户态与内核态通信) */
result = thermal_netlink_init();
if (result)
goto error; // 失败时跳转到错误处理
/* 2. 注册所有内置的温控策略(Governor) */
result = thermal_register_governors();
if (result)
goto unregister_netlink; // 失败时回滚Netlink初始化
/* 3. 分配并初始化thermal_class结构体 */
thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
if (!thermal_class) {
result = -ENOMEM; // 内存分配失败
goto unregister_governors; // 回滚Governor注册
}
/* 配置thermal_class属性 */
thermal_class->name = "thermal"; // 对应/sys/class/thermal目录
thermal_class->dev_release = thermal_release; // 设备释放回调函数
/* 4. 向内核注册thermal类(创建sysfs接口) */
result = class_register(thermal_class);
if (result) {
kfree(thermal_class); // 注册失败则释放内存
thermal_class = NULL;
goto unregister_governors; // 回滚Governor注册
}
/* 5. 注册电源管理事件通知器(处理系统休眠唤醒事件) */
result = register_pm_notifier(&thermal_pm_nb);
if (result)
pr_warn("Thermal: Can not register suspend notifier, return %d\n",
result); // 非致命错误,仅打印警告
return 0; // 初始化成功
/* 错误处理路径(按初始化逆序回滚) */
unregister_governors:
thermal_unregister_governors(); // 注销已注册的Governor
unregister_netlink:
thermal_netlink_exit(); // 关闭Netlink通信
error:
/* 销毁互斥锁(防止资源泄漏) */
mutex_destroy(&thermal_list_lock); // Thermal Zone列表锁
mutex_destroy(&thermal_governor_lock); // Governor操作锁
return result;
}
1)thermal_debug_init来初始化debugfs
2)thermal_netlink_init,初始化thermal netlink,有thermal事件可以通知用户态进程
3)thermal_register_governors 会遍历所有governor_thermal_table的所有entry,即遍历所有静态定义的governors,调用thermal_register_governor函数将这个governor注册进thermal core中,具体流程是,将governor添加进governor list中,然后遍历所有的thermal zone,如果该governor和这个thermal zone的governor名字一致的话就将thermal zone的governor设置为这个governor
4)thermal_class linux通过class来作为一类设备的集合,thermal_class就是所有thermal zone和cooling device这类设备的集合,thermal_init会初始化thermal_class并将其注册进系统中
5)register_pm_notifier 将thermal的通知链加进pm_chain中