# Linux 核心設計:CPUFreq - CPU frequency and voltage scaling 此份文檔會持續更新,源於筆者最近在做 Linux kernel 電源管理的專案,故將相關筆記整理於此。 ## 簡介 CPUFreq 是 Linux 核心的子系統之一,其他的子系統還有排程、記憶體管理、鎖…等等。CPU 時脈的縮放允許我們即時改變 CPU 時脈的速度,這是一個好方法來節省電池電力。因為更低的時脈速度,CPU 就會有更少的電力消耗。 CPUFreq 的核心程式碼位於 Linux 核心的 drivers/cpufreq/cpufreq.c。這個程式碼為 CPUFreq 架構的驅動程式(做真正的頻率遷移)、notifiers 提供標準化介面。這些是裝置驅動程式或其他 Linux 核心的模組需要被通知政策改變,例如溫度模組如 ACPI 或所有頻率改變,如 timing code。另外,kernel “constant” loops_per_jiffy 在這裡被更新當頻率改變 [1]。 ![image](https://hackmd.io/_uploads/rkOqu1yfee.png) <center>圖一:CPUFreq 框架的架構圖 [2]。</center> ## CPUFreq notifiers 有兩種:policy notifiers 和 transition notifiers。 1. CPUFreq policy notifiers 當新政策創立或移除會通知。 2. CPUFreq transition notifiers These are notified twice for each online CPU in the policy, when the CPUfreq driver switches the CPU core frequency and this change has no any external implications. ## 參考資料 [1] CPUFreq 官方文件:https://www.kernel.org/doc/html/latest/cpu-freq/index.html [2] 筆者認為,全網目前關於 CPUFreq 概念、原理寫的最簡單易懂的文章:https://blog.csdn.net/feelabclihu/article/details/118617876 [3] Intel Open Source Technology Center 的相關演講:https://www.youtube.com/watch?v=55Zlt5efcJQ [4] 一篇 DFVS 相關的碩士論文: https://www.airitilibrary.com/Article/Detail/U0006-0908201116045000 [5] 谁动了我的 CPU 频率:https://blog.mygraphql.com/zh/notes/low-tec/kernel/cpu-frequency/ [6] 初探Linux CPU动态调频与实测: https://zhuanlan.zhihu.com/p/33753019 [7] 电源管理入门-6 CPUFreq:https://juejin.cn/post/7289709491465076772