目标
控制进程对CPU的使用,以降低进程内DNN模型的运行时间(paper要求这个场景)
方案一:降低CPU频率(尝试后无效)
参考资料:cpufreq的简介
步骤一:使用内核的cpupower命令,把cpufreq govenor的模式从performance切换到powersave。
理论介绍
我们可以使用cpufreq切换cpu的模式,但是如果你的cpu采用的是Active mode with hardware-managed P-states (HWP),那么CPU实际的频率还是由CPU内部的逻辑决定的
实验室的渣渣服务器使用的是Intel P-state driver且无法使用命令intel_pstate=disable,将driver切换到ACPI CPUfreq driver.
The Intel P-state driver can operate in three different modes:
-
Active mode with hardware-managed P-states (HWP)
-
Active mode without hardware-managed P-states (HWP)
-
Passive mode
By default, the Intel P-state driver operates in active mode with or without(需要进一步判定我们的设备是否有这个功能) HWP depending on whether the CPU supports HWP.
Active mode with hardware-managed P-states.
When active mode with HWP is used, the Intel P-state driver instructs the CPU to perform the P-state selection. The driver can provide frequency hints. However, the final selection depends on CPU internal logic.(我们指定了模式,但是最终实际运行的state是由CPU内部的逻辑决定的)
In active mode with HWP, the Intel P-state driver provides two P-state selection algorithms:
-
Performance
-
Powersave
With the Performance governor, the driver instructs internal CPU logic to be performance-oriented. The range of allowed P-states is restricted to the upper boundary of the range that the driver is allowed to use.With the Powersave governor, the driver instructs internal CPU logic to be powersave-oriented.
具体方法
1)查看所有CPU的频率信息
cpupower -c all frequency-info
2)查看当前CPU的实时频率
watch -n1 "cat /proc/cpuinfo | grep \"^[c]pu MHz\""
3)修改CPU的模式
cpupower frequency-set --governor performance|powersave
4) 设置频率上下限
cpupower -c all frequency-set --min X
X的单位khz,X的值应该限定在CPU物理频率的最小和最大值之间
实验结果
我实验机器的driver采用的是intel P_state 的active mode with HWP,我将其设置为powersave模式,最低值设置的是1.2GHZ,但是跑起来的时候最高频率是2.9GHZ。经过查询后,我的机器不能切换到without HWP。
方案二、限制进行对CPU的使用(亲测有效)
步骤一:使用sudo权限安装cpulimit
cpulimit可以根据进程id或可执行文件的名称和路径,限制进程或可执行程序的CPU利用率,本人采用的是pid的方式。在具体实现中有如下几个要点:
1)获取进程的pid
在python中可以调用os.getpid()获得主进程的进程号,然后进行如下调用。注意:这里一定在cpulimit后添加&,以令该命令在后台运行,否则其会阻塞当前进程,导致程序异常。
os.system("taskset -cp 0 " + str(pid)) #将进程pid绑定到0号CPU
os.system("sudo cpulimit -l 50 -p " + str(pid) +" &") #限制pid的CPU使用率
2)根据进程名称,查询进程pid。
ps x | grep 进程名称 | grep -v grep | awk '{print $1}'
上述查询返回的可能是个数组,该数组可以使用如下方式进行遍历
PID = $ps x | grep pid_name | grep -v grep | awk '{print $1}'
# for 循环
for item in ${PID[*]}
do
echo $item
done
#while循环
model_num=1 #pid中元素的个数
i=1
while((i<=$model_num))
do
echo $PID[i]
((i++))
done
3)修改进程的名称
python里没有直接修改进程名称的方法,但是C语言可以。不过,python封装了C语言的这个方法。具体操作如下:
pip install setproctitle #安装包
setproctitle.setproctitle("pid_new_name") # 修改当前进程的名称
4)最终的脚本
#!/bin/bash
model_num=3 #test$i系列进程的进程数
i=1
while((i<=$model_num))
do
echo $i
PID=$(ps x | grep test$i | grep -v grep | awk '{print $1}') #查询名称为test$i的进程对应的pid
echo =====================$PID
sudo cpulimit -l 30 --pid $PID &
((i++))
done
#sudo cpulimit -l 30 python predict_inception.py 1 ==> 这个语句会报错
#ps x | grep test1 | grep -v grep | awk '{print $1}'
方案三、使用Intel P-state Driver(亲测有效)
https://www.kernel.org/doc/html/v4.12/admin-guide/pm/intel_pstate.html
max_perf_pct
Maximum P-state the driver is allowed to set in percent of the maximum supported performance level (the highest supported turbo P-state).
This attribute will not be exposed if the intel_pstate=per_cpu_perf_limits argument is present in the kernel command line.
min_perf_pct
Minimum P-state the driver is allowed to set in percent of the maximum supported performance level (the highest supported turbo P-state).
This attribute will not be exposed if the intel_pstate=per_cpu_perf_limits argument is present in the kernel command line.
echo 20 | sudo tee /sys/devices/system/cpu/intel_pstate/max_perf_pct #将max_perf_pct从100调整为20
echo 20 | sudo tee /sys/devices/system/cpu/intel_pstate/min_perf_pct #将min_perf_pct设置为20
只是使用cat 查看 /sys/devices/system/cpu/intel_pstate/min_perf_pct 或 /sys/devices/system/cpu/intel_pstate/max_perf_pct 时,他们的值并没有变成20,但是实际在运行时模型的时间已经增长,即调整生效。
方案四、powercap(设备不允许)
https://github.com/powercap/powercap
https://github.com/powercap/raplcap
我下载并编译了上边两个项目,但是我不确实是否不用安装上述项目,直接使用kernel的命令行就能设置(我倾向不安装上边项目就能直接用命令行控制)。
1. 确定设备是否支持powercap
dmesg | grep powercap
2. 根据powercap-set -h的内容进行设置,
比如powercap-set -p intel-rapl -z 0 -c 0 -s 7801
本人的设备不支持,所有该方法没有测试成功
设定好时间期限后,不要轻易放弃尝试,加油,你是最胖哒!