6

Ubuntu 16.04 下 CPU 频率与功耗关系

 3 years ago
source link: https://blog.chih.me/power-with-different-cpufreq.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Ubuntu 16.04 下 CPU 频率与功耗关系

0 Comments

实验服务器搭载两颗 Intel E5645 处理器,24块 SAS+SATA 机械硬盘。E5645 为 6 核处理器,并支持超线程技术(Intel® Hyper-Threading Technology),服务器共 24 个逻辑核,基本时钟频率为 2.40 GHz 。

Ubuntu Server 16.04.1 LTS

CPU 频率驱动程序与工具

操作系统通过CPU调频可以根据负载动态调节CPU工作频率,达到省电的目的。这个过程可以自动进行,也可以由用户空间(userspace)程序手动调节。Linux 内核实现了CPU 调频,该驱动被称为 CPUfreq, 从 3.4 内核开始,内核会自动载入相应的内核模块,默认的频率调频器 ondemand 会被启动( Intel  Sandy Bridge 架构后 CPU 的功率驱动程序为 P-State,默认调频器为 powersave )。用户调频工具有 cpufrequtils、 cpupower 等,这里使用 cpufrequtils 。

安装 cpufrequtils 包。

# apt install cpufrequtils

cpufrequtils 包包含以下三个工具,其中 cpufreq-info 用于查看cpu的相关信息,cpufreq-set 用于修改CPU的频率配置,cpufreq-aperf 用于计算一段时间内的平均频率。

/usr/bin/cpufreq-info
/usr/bin/cpufreq-set
/usr/bin/cpufreq-aperf

cpufreq-info 获得的信息包括:

  • dirver: 实用的cpufreq内核驱动
  • hardware limits: 设定的cpu频率范围
  • available frequency steps: 可选的cpu频率
  • available cpufreq governors: 可选的cpu频率调速器:

调速器 描述

ondemand 按需快速动态调整CPU频率, 一有cpu计算量的任务,就会立即达到最大频率运行,等执行完毕就立即回到最低频率(阙值为 95%)

performance 运行于最大频率

conservative 按需快速动态调整CPU频率, 一有cpu计算量的任务,就会立即达到最大频率运行,等执行完毕就立即回到最低频率(阙值为 75%)

powersave 运行于最小频率

userspace 运行于用户指定的频率

  • current CPU frequency: 当前的CPU频率
  • cpufreq stats: 统计cpu在每个频率下的工作时间

这些信息也可以手动在sysfs文件中查看,位置为 /sys/devices/system/cpu/cpuX/cpufreq/

chih@archlinux:/sys/devices/system/cpu/cpu0/cpufreq$ ls  
affected_cpus               related_cpus                   scaling_max_freq  
bios_limit                  scaling_available_frequencies  scaling_min_freq  
cpuinfo_cur_freq            scaling_available_governors    scaling_setspeed  
cpuinfo_max_freq            scaling_cur_freq               stats  
cpuinfo_min_freq            scaling_driver  
cpuinfo_transition_latency  scaling_governor 

以上为CPU0的一些内核文件,前缀 cpuinfo 代表的是 cpu 硬件上支持的频率,而 scaling 前缀代表的是可以通过 CPUFreq 系统用软件进行调节时所支持的频率。cpuinfo_cur_freq 代表通过硬件实际上读到的频率值,而 scaling_cur_freq 则是软件当前的设置值,scaling_available_frequencies 表示当前软件支持的频率值,scaling_available_governors 表示当前软件支持的调频器,scaling_governor 表示当前使用的调频器,scaling_min_freq 与 scaling_max_freq 表示软件设置的最低频率与最高频率。scaling_set_speed 表示手动设置的特定频率。

cpufreq-set 允许用户手动设置CPU运行频率,相应的选项如下所示:

  • -c --cpu  number of CPU where cpufreq settings shall be modified.

  • -d --min  new minimum CPU frequency the governor may select.

  • -u --max  new maximum CPU frequency the governor may select.

  • -g --governor  new cpufreq governor.

  • -f --freq  specific frequency to be set. Requires userspace governor to be available and loaded.

  • -r --related modify all hardware-related CPUs at the same time

  • -h --help Prints out the help screen.

因为服务器上核数比较多,可通过以下名为cpufreq-set-all的shell脚本方便的设置全部CPU核。

#!/bin/bash

for i in {0..23}; do
    cpufreq-set -c $i $@ 
done

-f 选项要求使用 userspace 调频器,使用下面的命令把所有CPU核的调频器都设置为 userspace 。

# cpufreq-set-all -g userspace

手动设定CPU的运行频率,注意,频率值必须是 scaling_available_frequencies中列出的。

# cpufreq-set-all -f 2.0Ghz

由于服务器无图形界面,使用了stress作为负载,stress 可以进行综合(内存占用、磁盘 IO)压力测试,可以反映典型用途下的服务器负载情况。

安装 stress

# aptitude install stress

产生24个cpu进程,对应服务器的总核数,可以确保100%跑满服务器CPU全部负载。

# stress -c 24

如视频 1600Mhz.mp4、2000Mhz.mp4、2400Mhz.mp4 所示,实验选取了 1.6Ghz、2.0Ghz、2.4Ghz 三组CPU工作频率,对应的服务器整机功率如下表所示:

CPU频率 CPU满载整机功率 CPU空载整机功率

1.6Ghz 575W 507W

2.0Ghz 612W 519W

2.4Ghz 652W 507W

这里的功耗是整机功耗,受磁盘、主板等的功耗影响,其实可以通过 RAPL 单独读取 CPU 的功耗。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK