熟话说,不懂温控的性能工程师不是好司机。性能与温控一直都是相爱相杀,即有要求第三方整改时的同仇敌忾,又有因一方修改参数导致另一方指标劣化时的拍案而起。知己知彼方能百战不败,了解温控框架,是性能工程师的必备技能。
MTK温控框架是基于Linux的Thermal机制。Linux的Thermal机制是基于Zone为单位的热管理机制,核心包括三个部分:获取区域温度的设备thermal_zone_device,对区域降温的设备thermal_cooling_device,温控策略thermal_governor。thermal_governor从thermal_zone_device获取区域温度,然后根据当前温度,决定调用哪个降温设备来为该区域降温。总体上说,哪个区域发热,就调用影响该区域温度的降温设备来为该区域降温,也就是头痛医头,脚痛医脚。
MTK温控框架如下图所示。service thermal声明为main类型,init进程在启动main类型的service时会启动thermal进程。thermal进程解析thermal.conf配置文件,根据文件配置的参数设置Linux Thermal Core,设置参数的节点位于/proc/driver/thermal/目录下。其中温控将SOC目标功耗会发送给PPM模块,PPM将目标功耗转换为拔核或限频。
一、Linux Thermal Core
内核将采集区域温度的设备抽象为结构体struct thermal_zone_device,主要成员包括:char type[]设备名称;int temperature当前温度;int last_temperature上次采集温度;struct thermal_governor *governor对应governor;int polling_delay温度采集时间间隔等等。其中struct thermal_zone_device_ops *ops是采集区域温度设备的操作抽象,包括绑定降温设备、获取设备温度等。
struct thermal_zone_device {
int id; //每个thermal_zone_device有独立的id
char type[THERMAL_NAME_LENGTH]; //名称
.................................
int polling_delay; //采集时间间隔
int temperature; //当前温度
int last_temperature; //上次采集温度
.................................
struct thermal_zone_device_ops *ops; //对操作集合
struct thermal_governor *governor; //对应温控策略
struct list_head thermal_instances; //链接对应cooling设备
................................
};
struct thermal_zone_device_ops {
//绑定降温设备
int (*bind) (struct thermal_zone_device *,
struct thermal_cooling_device *);
//解绑降温设备
int (*unbind) (struct thermal_zone_device *,
struct thermal_cooling_device *);
//获取当前温度
int (*get_temp) (struct thermal_zone_device *, int *);
//获取触发等级对应的温度
int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
..................................................................
};
内核将温控策略抽象为结构体struct thermal_governor,主要成员包括:char name[THERMAL_NAME_LENGTH]策略名称;int (*throttle)()温控决策等等。
struct thermal_governor {
char name[THERMAL_NAME_LENGTH];
int (*bind_to_tz)(struct thermal_zone_device *tz);
void (*unbind_from_tz)(struct thermal_zone_device *tz);
int (*throttle)(struct thermal_zone_device *tz, int trip);
struct list_head governor_list;
};
执行温控策略的设备成为区域降温设备,内核抽象为结构体struct thermal_cooling_device,struct thermal_cooling_device_ops是区域降温设备的操作集合。
struct thermal_cooling_device {
int id; //每个thermal_cooling_device有独立的id
char type[THERMAL_NAME_LENGTH]; // 名称
struct device device;
struct device_node *np;
void *devdata;
const struct thermal_cooling_device_ops *ops;
bool updated; /* true if the cooling device does not need update */
struct mutex lock; /* protect thermal_instances list */
struct list_head thermal_instances;
struct list_head node;
};
struct thermal_cooling_device_ops {
int (*get_max_state) (struct thermal_cooling_device *, unsigned long *); //获取总的状态数,相当于降温等级
int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);