接前一篇文章Linux内核Thermal框架详解十二、Thermal Governor(2)

二、具体温控策略

上一篇文章介绍并详细分析了bang_bang governor的源码。本文介绍第2种温控策略:fair_share。

2. fair_share

fair_share governor总的策略是频率档位⽐较多的cooling device优先降频。

fair_share governor的代码在drivers/thermal/gov_fair_share.c中,也很简短,一共才124行,有效代码不到100行。如下所示:

#include <linux/thermal.h>
#include <trace/events/thermal.h>#include "thermal_core.h"/*** get_trip_level: - obtains the current trip level for a zone* @tz:       thermal zone device*/
static int get_trip_level(struct thermal_zone_device *tz)
{int count = 0;int trip_temp;enum thermal_trip_type trip_type;if (tz->trips == 0 || !tz->ops->get_trip_temp)return 0;for (count = 0; count < tz->trips; count++) {tz->ops->get_trip_temp(tz, count, &trip_temp);if (tz->temperature < trip_temp)break;}/** count > 0 only if temperature is greater than first trip* point, in which case, trip_point = count - 1*/if (count > 0) {tz->ops->get_trip_type(tz, count - 1, &trip_type);trace_thermal_zone_trip(tz, count - 1, trip_type);}return count;
}static long get_target_state(struct thermal_zone_device *tz,struct thermal_cooling_device *cdev, int percentage, int level)
{unsigned long max_state;cdev->ops->get_max_state(cdev, &max_state);return (long)(percentage * level * max_state) / (100 * tz->trips);
}/*** fair_share_throttle - throttles devices associated with the given zone* @tz: thermal_zone_device* @trip: trip point index** Throttling Logic: This uses three parameters to calculate the new* throttle state of the cooling devices associated with the given zone.** Parameters used for Throttling:* P1. max_state: Maximum throttle state exposed by the cooling device.* P2. percentage[i]/100:*   How 'effective' the 'i'th device is, in cooling the given zone.* P3. cur_trip_level/max_no_of_trips:*   This describes the extent to which the devices should be throttled.*    We do not want to throttle too much when we trip a lower temperature,*  whereas the throttling is at full swing if we trip critical levels.*    (Heavily assumes the trip points are in ascending order)* new_state of cooling device = P3 * P2 * P1*/
static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
{struct thermal_instance *instance;int total_weight = 0;int total_instance = 0;int cur_trip_level = get_trip_level(tz);mutex_lock(&tz->lock);list_for_each_entry(instance, &tz->thermal_instances, tz_node) {if (instance->trip != trip)continue;total_weight += instance->weight;total_instance++;}list_for_each_entry(instance, &tz->thermal_instances, tz_node) {int percentage;struct thermal_cooling_device *cdev = instance->cdev;if (instance->trip != trip)continue;if (!total_weight)percentage = 100 / total_instance;elsepercentage = (instance->weight * 100) / total_weight;instance->target = get_target_state(tz, cdev, percentage,cur_trip_level);mutex_lock(&cdev->lock);__thermal_cdev_update(cdev);mutex_unlock(&cdev->lock);}mutex_unlock(&tz->lock);return 0;
}static struct thermal_governor thermal_gov_fair_share = {.name        = "fair_share",.throttle = fair_share_throttle,
};
THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);

同样是麻雀虽小,五脏俱全。别看代码行数比较少,但是背后的机制却并不简单。一段一段来进行分析。

(1)THERMAL_GOVERNOR_DECLARE相关代码

先来看THERMAL_GOVERNOR_DECLARE。它是一个宏定义,在drivers/thermal/thermal_core.h中,代码如下:

/* Init section thermal table */
extern struct thermal_governor *__governor_thermal_table[];
extern struct thermal_governor *__governor_thermal_table_end[];#define THERMAL_TABLE_ENTRY(table, name)         \static typeof(name) *__thermal_table_entry_##name  \__used __section("__" #table "_thermal_table") = &name#define THERMAL_GOVERNOR_DECLARE(name)  THERMAL_TABLE_ENTRY(governor, name)

实际上这段代码在前文Linux内核Thermal框架详解四、Thermal Core(3)中已经进行了详细分析,这里就不再赘述了。不过为了便于理解和加深印象,将fair_share governor展开后的代码再次列出:

static struct thermal_governor thermal_gov_fair_share = {.name      = "fair_share",.throttle = fair_share_throttle,
};static struct thermal_governor *__thermal_table_entry_thermal_gov_fair_share    \__used __section("__governor_thermal_table") = &thermal_gov_fair_share

Thermal Governor都是通过THERMAL_GOVERNOR_DECLARE定义到了__governor_thermal_table这段空间内。然后在thermal core初始化时通过调用thermal_register_governors来注册到thermal_governor_list链表中。再之后通过经由“thermal_init->thermal_register_governors-> thermal_set_governor”路径和thermal zone device关联上。

(2)handle_non_critical_trips

struct thermal_governor中有一个成员throttle,其是一个函数指针:

int (*throttle)(struct thermal_zone_device *tz, int trip);

对于对象thermal_gov_fair_share来说,指向了fair_share_throttle函数。在解析fair_share_throttle函数之前,有一个问题必须弄清楚:这个函数是何时被调用的?

是在drivers/thermal/thermal_core.c的handle_non_critical_trips函数中,代码如下:

static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
{tz->governor ? tz->governor->throttle(tz, trip) :def_governor->throttle(tz, trip);
}

那么又是哪里调用的handle_non_critical_trips?是在drivers/thermal/thermal_core.c的handle_thermal_trip函数中,代码如下:

static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
{enum thermal_trip_type type;int trip_temp, hyst = 0;/* Ignore disabled trip points */if (test_bit(trip, &tz->trips_disabled))return;tz->ops->get_trip_temp(tz, trip, &trip_temp);tz->ops->get_trip_type(tz, trip, &type);if (tz->ops->get_trip_hyst)tz->ops->get_trip_hyst(tz, trip, &hyst);if (tz->last_temperature != THERMAL_TEMP_INVALID) {if (tz->last_temperature < trip_temp &&tz->temperature >= trip_temp)thermal_notify_tz_trip_up(tz->id, trip,tz->temperature);if (tz->last_temperature >= trip_temp &&tz->temperature < (trip_temp - hyst))thermal_notify_tz_trip_down(tz->id, trip,tz->temperature);}if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)handle_critical_trips(tz, trip, type);elsehandle_non_critical_trips(tz, trip);/** Alright, we handled this trip successfully.* So, start monitoring again.*/monitor_thermal_zone(tz);
}

对于handle_thermal_trip函数的详细分析有专门的文章章节,由于本篇文章专注于fair_share governor,故在此不深入展开。

(3)fair_share_throttle

再贴一下此函数代码:

/*** fair_share_throttle - throttles devices associated with the given zone* @tz: thermal_zone_device* @trip: trip point index** Throttling Logic: This uses three parameters to calculate the new* throttle state of the cooling devices associated with the given zone.** Parameters used for Throttling:* P1. max_state: Maximum throttle state exposed by the cooling device.* P2. percentage[i]/100:* How 'effective' the 'i'th device is, in cooling the given zone.* P3. cur_trip_level/max_no_of_trips:*   This describes the extent to which the devices should be throttled.*    We do not want to throttle too much when we trip a lower temperature,*  whereas the throttling is at full swing if we trip critical levels.*    (Heavily assumes the trip points are in ascending order)* new_state of cooling device = P3 * P2 * P1*/
static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
{struct thermal_instance *instance;int total_weight = 0;int total_instance = 0;int cur_trip_level = get_trip_level(tz);mutex_lock(&tz->lock);list_for_each_entry(instance, &tz->thermal_instances, tz_node) {if (instance->trip != trip)continue;total_weight += instance->weight;total_instance++;}list_for_each_entry(instance, &tz->thermal_instances, tz_node) {int percentage;struct thermal_cooling_device *cdev = instance->cdev;if (instance->trip != trip)continue;if (!total_weight)percentage = 100 / total_instance;elsepercentage = (instance->weight * 100) / total_weight;instance->target = get_target_state(tz, cdev, percentage,cur_trip_level);mutex_lock(&cdev->lock);__thermal_cdev_update(cdev);mutex_unlock(&cdev->lock);}mutex_unlock(&tz->lock);return 0;
}

函数注释已经将函数功能说得很清楚了:对与给定thermal zone关联的设备进行节流。调节逻辑如下:

使用3个参数计算与给定thermal zone相关联的冷却设备的最新throttle state。

用于节流的3个参数(注意不是函数的参数):

  • 参数1. max_state

冷却设备暴露的最大throttle state。

  • 参数2. percentage[i]/100

第i个设备在冷却给定区域方面的”有效性”。

  • 参数3. cur_trip_level/max_no_of_trips

这个参数描述设备应被节流的限度。当到达较低的温度时,不需要节流太多;反之如果在临界水平,节流就处于全开状态。在很大程度上假设跳闸点按升序排列。

new_state of cooling device = P3 * P2 * P1

代码的大致流程如下:

1)得到指定thermal zone的trip level

通过get_trip_level(tz)得到指定thermal zone的trip level。

get_trip_level函数在同文件(drivers/thermal/gov_fair_share.c)中实现,代码如下:

/*** get_trip_level: - obtains the current trip level for a zone* @tz:      thermal zone device*/
static int get_trip_level(struct thermal_zone_device *tz)
{int count = 0;int trip_temp;enum thermal_trip_type trip_type;if (tz->trips == 0 || !tz->ops->get_trip_temp)return 0;for (count = 0; count < tz->trips; count++) {tz->ops->get_trip_temp(tz, count, &trip_temp);if (tz->temperature < trip_temp)break;}/** count > 0 only if temperature is greater than first trip* point, in which case, trip_point = count - 1*/if (count > 0) {tz->ops->get_trip_type(tz, count - 1, &trip_type);trace_thermal_zone_trip(tz, count - 1, trip_type);}return count;
}

依次遍历各个触发点(trips),并得到相应触发点的温度。如果给定thermal zone的温度小于某一触发点的温度,则跳出循环。

未完待续……

Linux内核Thermal框架详解十三、Thermal Governor(3)相关推荐

  1. Linux内核Thermal框架详解十四、Thermal Governor(4)

    本文部分内容参考 万字长文 | Thermal框架源码剖析, Linux Thermal机制源码分析之框架概述_不捡风筝的玖伍贰柒的博客-CSDN博客, "热散由心静,凉生为室空" ...

  2. Linux内核Thermal框架详解十二、Thermal Governor(2)

    本文部分内容参考 万字长文 | Thermal框架源码剖析, Linux Thermal机制源码分析之框架概述_不捡风筝的玖伍贰柒的博客-CSDN博客, "热散由心静,凉生为室空" ...

  3. 【正点原子Linux连载】第三十五章 Linux内核顶层Makefile详解 -摘自【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.0

    1)实验平台:正点原子阿尔法Linux开发板 2)平台购买地址:https://item.taobao.com/item.htm?id=603672744434 2)全套实验源码+手册+视频下载地址: ...

  4. Linux内核Thermal框架详解十一、Thermal Governor(1)

    本文部分内容参考 万字长文 | Thermal框架源码剖析, Linux Thermal机制源码分析之框架概述_不捡风筝的玖伍贰柒的博客-CSDN博客, 特此致谢! 一.概述 Thermal Gove ...

  5. cgroup使用举例和linux内核源码详解

    cgroup的原理其实并不复杂,用法也比较简单.但是涉及的内核数据结构真的复杂,错综复杂的数据结构感觉才是cgroup真正的难点.本文结合个人学习cgroup源码的心得,尽可能以举例的形式,总结cgr ...

  6. linux内核I2C子系统详解

    1.I2C通信协议 参考博客:<I2C通信协议详解和通信流程分析>:https://csdnimg.cn/release/blogv2/dist/pc/themesSkin/skin3-t ...

  7. linux内核顶层Makefile详解

    文章目录 一.linux内核获取 二.linux内核初次编译 三.linux工程目录分析 1.获取源码 2.目录介绍 1.总体浏览 2.arch 目录 3.block 目录 4.crypto 目录 5 ...

  8. Linux 内核顶层 Makefile 详解

    Linux 内核获取 Linux 由 Linux 基金会管理与发布, Linux 官网为 https://www.kernel.org,所以你想获取最新的 Linux 版本就可以在这个网站上下载 最新 ...

  9. linux内核rcu,linux内核rcu机制详解

    RCU(Read-Copy Update)是数据同步的一种方式,在当前的Linux内核中发挥着重要的作用.RCU主要针对的数据对象是链表,目的是提高遍历读取数据的效率,为了达到目的使用RCU机制读取数 ...

最新文章

  1. java包名命名规范[【转】
  2. wxWidgets:创建应用程序的 DLL
  3. 各种Exit退出函数用法
  4. 服务器自动删文件,服务器定时删除文件工具
  5. 乐高创意机器人moc_乐高MOC作品欣赏:变形金刚及其他
  6. Bootstrap 滚动监听插件Scrollspy 的方法
  7. 磁盘不足 导致内存 linux,为什么 Linux 需要 Swapping,仅仅是内存不够用?
  8. vs2017html乱码,vs2017引用vue组件中文乱码
  9. 微信小程序连接无法跳转/ can not navigate to tabBar page错误
  10. mysql汽车品牌系列_爬取汽车之家汽车品牌型号系列数据
  11. 最新web/java/jsp实现发送手机短信验证码和邮箱验证码的注册登录功能(详细)
  12. 《都挺好》苏大强,锦鲤杨超越,表情包为何会刷屏?
  13. html借助JS简单实现图片闪烁功能
  14. ApeCoin计划推出自己的区块链,Messari分析师们怎么看?
  15. 浅谈大型互联网的企业入侵检测及防护策略
  16. 高手路过--菜鸟版系统安装==(图文安装教程)+(最新win7+win8系统)+系统工具
  17. PHP7安装redis扩展 Star.Hou
  18. “第三方支付”盈利模式
  19. opencv python书籍_OpenCV算法精解:基于Python与C++
  20. sfc 修复 xp rpc服务器,SFC—系统文件检查程序命令 System File Checker

热门文章

  1. firebase推送
  2. linux路由器静态无法转发,静态路由之路由器两种转发模式
  3. android x86 4636,最新可用.apk 3% 的殺毒軟件(1/32) 報告發現病毒 - VirSCAN.org-線上防毒引擎掃描網站 v1.02 目前支援 47 款防毒引擎...
  4. 无线电池监测系统的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  5. java狗具有特别的接飞盘的方法_训练狗接飞盘的5个技巧
  6. boke | 妇女节礼物
  7. 联合办公空间,平衡办公与社交
  8. conda list 与 pip list 命令的区别
  9. JAVA swing之用户登陆界面
  10. 字符 字符集 编码 以及乱码