source_code: https://github.com/emagii/cpufrequtils
cpufreq-set - A small tool which allows to modify cpufreq settings.(修改內存頻率的工具)
cpufreq-set allows you to modify cpufreq settings without having to type e.g. “/sys/devices/system/cpu/cpu0/cpufreq/scaling_set_speed” all the time.(不需要直接操作sysfs相關節點即可調整cpufreq).
OPTIONS
-c --cpu
number of CPU where cpufreq settings shall be modified.(cpu核心號)
-d --min <FREQ>new minimum CPU frequency the governor may select.(調控器可以選擇新的最低 CPU 頻率。)-u --max <FREQ>new maximum CPU frequency the governor may select.-g --governor <GOV>new cpufreq governor.(設定調控器類型。)-f --freq <FREQ>specific frequency to be set. Requires userspace governor to be available and loaded.(要設置的特定頻率。要求用戶空間調控器可用并已加載。)-r --relatedmodify all hardware-related CPUs at the same time(同時修改所有與硬件相關的 CPU。)-h --helpPrints out the help screen.
REMARKS(備注/特別說明)
Omitting the -c or --cpu argument is equivalent to setting it to zero.(省略 -c 或 --cpu 參數等同于將其設置為零。)
The -f FREQ, --freq FREQ parameter cannot be combined with any other parameter except the -c CPU, --cpu CPU parameter.(-f 只能用在存在-c參數的場景。)FREQuencies can be passed in Hz, kHz (default), MHz, GHz, or THz by postfixing the value with the wanted unit name, without any space (frequency inkHz =^ Hz * 0.001 =^ MHz * 1000 =^ GHz * 1000000).(FREQuencies 可以以 Hz、kHz(默認)、MHz、GHz 或 THz 為單位傳遞,方法是在值后加上所需的單位名稱,沒有任何空格(頻率 kHz =^ Hz * 0.001 =^ MHz * 1000 =^ GHz * 1000000)。)
FILES(依賴文件)
/sys/devices/system/cpu/cpu*/cpufreq/
/proc/cpufreq (deprecated - 廢棄)
/proc/sys/cpu/ (deprecated - 廢棄)
// cpufreq-info -g
|- cpufreq_get_available_governors|- sysfs_get_available_governors(cpu)|- sysfs_read_file(cpu, "scaling_available_governors", linebuf, sizeof(linebuf))
由上述源碼來看,cpuutils
的本質是修改cpu sysfs
導出的相關節點來調整cpu的頻率。
可是cpu支持哪些governors
是在哪里做限定的,scaling_available_governors
看到僅支持performance
模式。
因為sysfs是內核導出的節點,那就找一找內核源碼??看了下v6.6
的內核源碼,發現僅支持兩種模式。
// drivers/cpufreq/cpufreq.c
static unsigned int cpufreq_parse_policy(char *str_governor)
{if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))return CPUFREQ_POLICY_PERFORMANCE;if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))return CPUFREQ_POLICY_POWERSAVE;return CPUFREQ_POLICY_UNKNOWN;
}
除了performance
和powersave
兩種模式,可是為什么其他文章說支持以下所有模式??
命令:sudo cpufreq-set -g 《模式》
powersave,是無論如何都只會保持最低頻率的所謂”省電”模式;
userspace,是自定義頻率時的模式,這個是當你設定特定頻率時自動轉變的;
ondemand,默認模式。一有cpu計算量的任務,就會立即達到最大頻率運行,等執行完 畢就立即回到最低頻率;
conservative,翻譯成保守(中庸)模 式,會自動在頻率上下限調整,ondemand的區別在于它會按需分配頻率,而不是一味追求最高頻率;
performance,顧名思義只注重效率,無論如何一直保持以最大頻率運行。
// cpufreq-set -g
|- cpufreq_modify_policy_governor()|- verify_gov(new_gov, governor) // 對輸入的governor字段做合法性判斷|- sysfs_write_one_value|- sysfs_write_file // 將值寫入到cpu WRITE_SCALING_GOVERNOR節點。
/* write access */enum {WRITE_SCALING_MIN_FREQ,WRITE_SCALING_MAX_FREQ,WRITE_SCALING_GOVERNOR,WRITE_SCALING_SET_SPEED,MAX_WRITE_FILES
};static const char *write_files[MAX_VALUE_FILES] = {[WRITE_SCALING_MIN_FREQ] = "scaling_min_freq",[WRITE_SCALING_MAX_FREQ] = "scaling_max_freq",[WRITE_SCALING_GOVERNOR] = "scaling_governor",[WRITE_SCALING_SET_SPEED] = "scaling_setspeed",
};
從代碼來看,cpufreq-set
僅僅是將輸入的governor
簡單校驗之后寫入對應節點,真正的governor
合法性判斷由內核來做。
// cpufreq_conservative.c
// cpufreq_ondemand.c
// cpufreq_powersave.c
// cpufreq_userspace.c
// cpufreq_performance.c
這寫代碼都被編譯成了單獨的模塊,當向scaling_governor
節點寫governor
名時,對應的驅動會被加載,同時scaling_available_frequencies
也會添加對應的governer
。
$ lsmod | grep cpu
cpufreq_ondemand 28672 0
cpufreq_conservative 20480 1
cpufreq_powersave 16384 0
acpi_cpufreq 45056 0