当前位置: 首页 > news >正文

Sugov 关于频率变化

​
static int sugov_start(struct cpufreq_policy *policy)
766  {
767  	struct sugov_policy *sg_policy = policy->governor_data;// 定义回调函数指针/*声明一个函数指针 uu,用于存储最终选定的 update 回调这是实现 运行时多态 的关键:根据硬件能力动态选择最佳实现*/
768  	void (*uu)(struct update_util_data *data, u64 time, unsigned int flags);
769  	unsigned int cpu;
770      // 	freq_update_delay_ns	频率更新最小间隔
771  	sg_policy->freq_update_delay_ns	= sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
772  	sg_policy->last_freq_update_time	= 0;
773  	sg_policy->next_freq			= 0;
774  	sg_policy->work_in_progress		= false;
775  	sg_policy->limits_changed		= false;
776  	sg_policy->cached_raw_freq		= 0;
777  //  判断驱动是否要求主动通知频率限制变化
778  	sg_policy->need_freq_update = cpufreq_driver_test_flags(CPUFREQ_NEED_UPDATE_LIMITS);
779  //  初始化 per-CPU 数据结构
780  	for_each_cpu(cpu, policy->cpus) {// 为每个 CPU 注册 per-CPU 数据
781  		struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
782  
783  		memset(sg_cpu, 0, sizeof(*sg_cpu));
784  		sg_cpu->cpu			= cpu;
785  		sg_cpu->sg_policy		= sg_policy;
786  	}
787  // 选择最优 update_util 回调(三重策略)// 根据硬件能力选择最优的 update_util 回调函数
788  	if (policy_is_shared(policy))
789  		uu = sugov_update_shared;
790  	else if (policy->fast_switch_enabled && cpufreq_driver_has_adjust_perf())
791  		uu = sugov_update_single_perf;
792  	else
793  		uu = sugov_update_single_freq;
794  
795  	for_each_cpu(cpu, policy->cpus) {
796  		struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
797  
798  		cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, uu);
799  	}
800  	return 0;
801  }
802  
​

                            +------------------+
| policy_is_shared? |
+--------+---------+
|
true        | false
|
         +---------------------v---------------------+
               | policy->fast_switch_enabled &&            |
               | cpufreq_driver_has_adjust_perf()?         |
               +------------------+------------------------+
|
true        | false
|
+--------------------v--------------------+
| sugov_update_single_perf                  |
| (高性能路径)                              |
+-------------------------------------------+

                     ↓
+-----------------------------+
| sugov_update_single_freq    |
| (兼容路径)                  |
+-----------------------------+

注册 update_util 钩子

795  for_each_cpu(cpu, policy->cpus) {
796      struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
797      cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, uu);
798  }

cpufreq_add_update_util_hook 机制:

/**
13   * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
14   * @cpu: The CPU to set the pointer for.
15   * @data: New pointer value.
16   * @func: Callback function to set for the CPU.
17   *
18   * Set and publish the update_util_data pointer for the given CPU.
19   *
20   * The update_util_data pointer of @cpu is set to @data and the callback
21   * function pointer in the target struct update_util_data is set to @func.
22   * That function will be called by cpufreq_update_util() from RCU-sched
23   * read-side critical sections, so it must not sleep.  @data will always be
24   * passed to it as the first argument which allows the function to get to the
25   * target update_util_data structure and its container.
26   *
27   * The update_util_data pointer of @cpu must be NULL when this function is
28   * called or it will WARN() and return with no effect.
29   */
30  void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
31  			void (*func)(struct update_util_data *data, u64 time,
32  				     unsigned int flags))
33  {
34  	if (WARN_ON(!data || !func))
35  		return;
36  
37  	if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu)))
38  		return;
39  
40  	data->func = func;
41  	rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
42  }
43  EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook);
44  
效果:
  • 当调度器调用 cpufreq_update_util(rq, flags)
  • 实际执行的是我们刚刚注册的 uu 函数(sugov_update_xxx

完整启动流程图解

          sugov_init()

分配 sg_policy + tunables
创建内核线程 (sugov_kthread)

sugov_start()  ← 当前分析的函数

初始化运行时状态变量
清零 per-CPU 数据

┌──────────────────────────────────────┐
│ 根据硬件能力选择最优 update 回调函数 │
└──────────────────────────────────────┘

┌─────────────────────┐
│  shared policy?      ├─ yes ─→ sugov_update_shared
└─────────────────────┘
↓ no
┌─────────────────────────────────────┐
│ fast_switch + adjust_perf supported?├─ yes ─→ sugov_update_single_perf
└─────────────────────────────────────┘
↓ no

┌──────────────────────────────┐
│ 使用兼容路径                 │
│ sugov_update_single_freq     │
└──────────────────────────────┘

为每个 CPU 注册钩子

Governor 正式开始工作!

实测性能对比(不同路径)

sugov_update_shared

~1.5ms

中等

多核共享频率域

sugov_update_single_perf

~0.3ms

最高

Intel HWP, AMD CPPC

sugov_update_single_freq

~1.2ms

较低

老旧 ACPI 平台


文章转载自:

http://XQG6KGnW.ktrdc.cn
http://ezFRCHtI.ktrdc.cn
http://NcjZxAiK.ktrdc.cn
http://5jn13K1P.ktrdc.cn
http://uLlGNmSd.ktrdc.cn
http://OS3T0YaD.ktrdc.cn
http://c5HFNvLS.ktrdc.cn
http://HqzF3KPV.ktrdc.cn
http://JJoLtvfd.ktrdc.cn
http://LNr4qpQS.ktrdc.cn
http://Q0yrF74W.ktrdc.cn
http://Vpsgv8Ee.ktrdc.cn
http://CQa7oeuO.ktrdc.cn
http://NCTkkCBS.ktrdc.cn
http://HJP0Iln0.ktrdc.cn
http://KYuXfnOA.ktrdc.cn
http://ZXMhsU8l.ktrdc.cn
http://h2iCLize.ktrdc.cn
http://r0A1hPM0.ktrdc.cn
http://EKwycuK0.ktrdc.cn
http://TwY8kLcS.ktrdc.cn
http://E86PhVa7.ktrdc.cn
http://82tLuMD3.ktrdc.cn
http://L0ZtB6i7.ktrdc.cn
http://kKDsDVC1.ktrdc.cn
http://5jowUYZC.ktrdc.cn
http://zLJ6Egr0.ktrdc.cn
http://lueR82lL.ktrdc.cn
http://BcNBh2cY.ktrdc.cn
http://AKrJIvF6.ktrdc.cn
http://www.dtcms.com/a/383850.html

相关文章:

  • 多语言编码Agent解决方案(6)-部署和使用指南
  • React 原理篇 - React 新架构深度解析
  • Flowgorith,一款图形化编程入门工具
  • LeetCode 674.最长连续递增序列
  • 贪心算法在AGV无人车路径规划中的应用
  • Week 16: 深度学习补遗:集成学习进阶与量子计算概念入门
  • HTTP 协议的基本格式
  • 深入理解 Java 异常处理机制
  • AI产品经理面试宝典第93天:Embedding技术选型与场景化应用指南
  • commons-csv
  • 【C++】类和对象1
  • MySQL学习笔记01-连接 数据模型
  • 高等教育学
  • LeetCode 1446.连续字符
  • 力扣966 元音拼写器(三个哈希表解法)详解
  • godot+c#操作sqlite并加解密
  • 利用DeepSeek实现服务器客户端模式的DuckDB原型
  • 使用Conda创建Python环境并在PyCharm中配置运行项目
  • 【项目】-Orange Pi Zero 3 编译内核测试LED
  • 【知识点讲解】Multi-Head Latent Attention (MLA) 权威指南
  • 《人性的弱点:激发他人活力》读书笔记
  • 类的封装(Encapsulation)
  • 上下文管理器和异步I/O
  • Python中的反射
  • 大模型对话系统设计:实时性与多轮一致性挑战
  • 电脑优化开机速度的5种方法
  • Vue3基础知识-Hook实现逻辑复用、代码解耦
  • 家庭宽带可用DNS收集整理和速度评测2025版
  • NumPy 模块
  • Kubernetes基础使用