Linux内核Thermal框架详解五、Thermal Core(4)
创始人
2024-06-01 10:21:39
0

本文部分内容参考Linux Thermal 学习笔记 - 爱码网。特此致谢!

接前一篇文章Linux内核Thermal框架详解四、Thermal Core(3)

三、相关源码及分析

2. thermal_register_governors

上一回说到这一段代码:

for (__governor = __governor_thermal_table;	\__governor < __governor_thermal_table_end;	\__governor++) {ret = thermal_register_governor(*governor);if (ret) {pr_err("Failed to register governor: '%s'",(*governor)->name);break;}pr_info("Registered thermal governor '%s'",(*governor)->name);
}

__governor_thermal_table上一回仔细分析了其来龙去脉,现在该关注__governor这个变量了。

__governor这个变量在之前代码中出现过,如下:

struct thermal_governor **governor;

这是一个二重指针,即指向指针的指针。在上述代码中,它一开始指向了__governor_thermal_table,还记得__governor_thermal_table是怎样定义的吗?

在drivers/thermal/thermal_core.h中:

extern struct thermal_governor *__governor_thermal_table[];
extern struct thermal_governor *__governor_thermal_table_end[];

虽然这里是extern,但是这比include/asm-generic/vmlinux.lds.h中真正定义__governor_thermal_table的地方好理解。__governor_thermal_table实际上是一个指针数组。所以使用二重指针governor指向它也就合情合理了。

再来回顾一下各种governor策略:

  • step_wise

drivers/thermal/gov_step_wise.c中:

static struct thermal_governor thermal_gov_step_wise = {.name		= "step_wise",.throttle	= step_wise_throttle,
};static struct thermal_governor *__thermal_table_entry_thermal_gov_step_wise    \__used __section("__governor_thermal_table") = &thermal_gov_step_wise
  • power_allocator

drivers/thermal/gov_power_allocator.c中:

static struct thermal_governor thermal_gov_power_allocator = {.name		= "power_allocator",.bind_to_tz	= power_allocator_bind,.unbind_from_tz	= power_allocator_unbind,.throttle	= power_allocator_throttle,
};static struct thermal_governor *__thermal_table_entry_thermal_gov_power_allocator    \__used __section("__governor_thermal_table") = &thermal_gov_power_allocator
  • fair_share

drivers/thermal/gov_fair_share.c中:

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
  • user_space

drivers/thermal/gov_user_space.c中:

static struct thermal_governor thermal_gov_user_space = {.name		= "user_space",.throttle	= notify_user_space,.bind_to_tz	= user_space_bind,
};static struct thermal_governor *__thermal_table_entry_thermal_gov_user_space    \__used __section("__governor_thermal_table") = &thermal_gov_user_space
  • bang_bang

drivers/thermal/gov_bang_bang.c中:

static struct thermal_governor thermal_gov_bang_bang = {.name		= "bang_bang",.throttle	= bang_bang_control,
};static struct thermal_governor *__thermal_table_entry_thermal_gov_bang_bang    \__used __section("__governor_thermal_table") = &thermal_gov_bang_bang

弄清楚了以上细节后就能知道,本文开头的代码的意义是:遍历所有的governor策略并进行注册。注册具体都完成了哪些工作?下边接着来看。

(2)thermal_register_governor

thermal_register_governor函数同样在drivers/thermal/thermal_core.c中实现,代码如下:

int thermal_register_governor(struct thermal_governor *governor)
{int err;const char *name;struct thermal_zone_device *pos;if (!governor)return -EINVAL;mutex_lock(&thermal_governor_lock);err = -EBUSY;if (!__find_governor(governor->name)) {bool match_default;err = 0;list_add(&governor->governor_list, &thermal_governor_list);match_default = !strncmp(governor->name,DEFAULT_THERMAL_GOVERNOR,THERMAL_NAME_LENGTH);if (!def_governor && match_default)def_governor = governor;}mutex_lock(&thermal_list_lock);list_for_each_entry(pos, &thermal_tz_list, node) {/** only thermal zones with specified tz->tzp->governor_name* may run with tz->govenor unset*/if (pos->governor)continue;name = pos->tzp->governor_name;if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {int ret;ret = thermal_set_governor(pos, governor);if (ret)dev_err(&pos->device,"Failed to set governor %s for thermal zone %s: %d\n",governor->name, pos->type, ret);}}mutex_unlock(&thermal_list_lock);mutex_unlock(&thermal_governor_lock);return err;
}

函数虽然不算太长,但也不算太短,还是有一些内容的。逐段来看:

一开始是判断并确保入参governor不为空。

接下来加锁mutex_lock(&thermal_governor_lock)。thermal_governor_lock在同文件(drivers/thermal/thermal_core.c)中定义并初始化,代码如下:

static DEFINE_MUTEX(thermal_governor_lock);

接下来是一个判断if (!__find_governor(governor->name))。__find_governor函数同样在drivers/thermal/thermal_core.c中,代码如下:

/** Governor section: set of functions to handle thermal governors** Functions to help in the life cycle of thermal governors within* the thermal core and by the thermal governor code.*/static struct thermal_governor *__find_governor(const char *name)
{struct thermal_governor *pos;if (!name || !name[0])return def_governor;list_for_each_entry(pos, &thermal_governor_list, governor_list)if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))return pos;return NULL;
}

要弄清楚这个函数的功能,就必须弄清楚list_for_each_entry的含义。list_for_each_entry是一个宏,在include/linux/list.h中,代码如下:

/*** list_for_each_entry	-	iterate over list of given type* @pos:	the type * to use as a loop cursor.* @head:	the head for your list.* @member:	the name of the list_head within the struct.*/
#define list_for_each_entry(pos, head, member)				\for (pos = list_first_entry(head, typeof(*pos), member);	\!list_entry_is_head(pos, head, member);			\pos = list_next_entry(pos, member))

list_first_entry当然也在include/linux/list.h中,代码如下:

/*** list_first_entry - get the first element from a list* @ptr:	the list head to take the element from.* @type:	the type of the struct this is embedded in.* @member:	the name of the list_head within the struct.** Note, that list is expected to be not empty.*/
#define list_first_entry(ptr, type, member) \list_entry((ptr)->next, type, member)

list_entry也在include/linux/linux.h中,就在list_first_entry宏定义的上边,代码如下:

/*** list_entry - get the struct for this entry* @ptr:	the &struct list_head pointer.* @type:	the type of the struct this is embedded in.* @member:	the name of the list_head within the struct.*/
#define list_entry(ptr, type, member) \container_of(ptr, type, member)

由上,list_first_entry展开为:

#define list_first_entry    container_of((ptr)->next, type, member)

list_entry_is_head同样在include/linux/linux.h中,代码如下:

/*** list_entry_is_head - test if the entry points to the head of the list* @pos:	the type * to cursor* @head:	the head for your list.* @member:	the name of the list_head within the struct.*/
#define list_entry_is_head(pos, head, member)				\(&pos->member == (head))

list_next_entry同样在include/linux/linux.h中,代码如下:

/*** list_next_entry - get the next element in list* @pos:	the type * to cursor* @member:	the name of the list_head within the struct.*/
#define list_next_entry(pos, member) \list_entry((pos)->member.next, typeof(*(pos)), member)

相关内容

热门资讯

追梦少年作文850字(精简3... 追梦少年作文850字 篇一追梦少年追梦少年是指那些积极向上、勇敢追求梦想的年轻人。他们充满激情和动力...
二年级作文我喜欢狮子(优秀6... 二年级作文我喜欢狮子 篇一我喜欢狮子我喜欢狮子,因为它们是世界上最强壮的动物之一。狮子有一头浓密的金...
看电影作文200字二年级【实... 看电影作文200字二年级 篇一我喜欢看电影我喜欢看电影,因为电影可以带给我快乐和惊喜。每次我看电影,...
小宁捡到钱二年级作文【经典6... 小宁捡到钱二年级作文 篇一今天放学的时候,我在回家的路上捡到了一张钱。当时我正在走路,突然看到地上闪...
二年级游乐园冲浪作文(优秀6... 二年级游乐园冲浪作文 篇一我最喜欢的游乐园冲浪今天,我和爸爸妈妈去了一个很大很有趣的游乐园。这个游乐...
二年级有趣的游戏作文200字... 二年级有趣的游戏作文200字 篇一:捉迷藏捉迷藏是我们二年级最喜欢的游戏之一。这个游戏的规则很简单:...
我的好朋友二年级作文【实用6... 我的好朋友二年级作文 篇一我的好朋友我有一个非常好的朋友,她叫小芳。她和我在同一个班级,我们从小学一...
我的家【优质6篇】 我的家 篇一我的家是一个温馨而快乐的地方。无论是平日的热闹还是周末的宁静,家里总是充满着欢声笑语和爱...
二年级作文不少于200个字【... 二年级作文不少于600个字 篇一我的暑假生活暑假终于来了,我迫不及待地迎接了这个长假。在这个暑假里,...
二年级暑假趣事作文捉老鼠(推... 二年级暑假趣事作文捉老鼠 篇一暑假快到了,我和弟弟决定在家里玩捉老鼠的游戏。我们找来了一些小道具,准...
小学二年级避暑山庄旅游作文(... 小学二年级避暑山庄旅游作文 篇一我和家人去了一个非常有趣的地方——避暑山庄。这个地方真的很美,有很多...
二年级作文游庐山【精简6篇】 二年级作文游庐山 篇一我和爸爸妈妈一起去了庐山。庐山是中国著名的山岳风景区,被誉为“江南第一山”。我...
二年级下册看图写话春天来了作... 二年级下册看图写话春天来了作文 篇一春天来了春天来了,大地变得生机勃勃。图中的小朋友们正在户外玩耍,...
二年级打雪仗作文指导【通用6... 二年级打雪仗作文指导 篇一打雪仗是冬天最有趣的活动之一,对于二年级的小朋友来说,更是一种享受。下面是...
二年级美丽的早晨作文(实用6... 二年级美丽的早晨作文 篇一美丽的早晨早晨的阳光透过窗户洒进来,房间里弥漫着一股清新的味道。我慢慢睁开...
小学二年级海边旅游作文200... 小学二年级海边旅游作文200字作文 篇一我和家人去海边旅游了,真是一个美好的经历!早上,我们一大早就...
二年级【优秀6篇】 二年级 篇一:我的暑假生活暑假终于来了,我迫不及待地开始了我的暑假生活。在这个悠长的假期里,我过得非...
赏荷花二年级作文【通用6篇】 赏荷花二年级作文 篇一欣赏荷花的美丽今天,我和爸爸妈妈一起去公园赏荷花。公园里有一个大大的荷花池,里...
舞蹈汇演作文二年级【精彩6篇... 舞蹈汇演作文二年级 篇一舞蹈汇演是一场精彩绝伦的表演,让我感受到了舞蹈的魅力和美妙。我在二年级的时候...
二年级作文我家的厨师(优质6... 二年级作文我家的厨师 篇一我家的厨师是我妈妈。她是一个非常厉害的厨师,每天都能给我们做出美味可口的饭...