kill_cpu.c
#include <stdlib.h> int main() { while(1); return 0; }
$ gcc -o out kill_cpu.c $ ./out
$ top top - 15:44:08 up 207 days, 21:29, 2 users, load average: 0.08, 0.13, 0.10 Tasks: 231 total, 2 running, 229 sleeping, 0 stopped, 0 zombie Cpu0 : 1.0%us, 1.0%sy, 0.0%ni, 98.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu1 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 1.0%us, 1.0%sy, 0.0%ni, 98.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
看到某一個核的cpu已經達到了100%。這是因爲這個進程被調度到了一個核上,沒有使用多線程,因此天然就是一個核的cpu是100%。那麼如何讓全部的cpu都是100%呢。linux
$ for i in `seq 1 $(cat /proc/cpuinfo | grep "physical id" | wc -l)`; do ./out & done [1] 5992 [2] 5993 [3] 5994 [4] 5995 $ top top - 15:56:54 up 207 days, 21:42, 2 users, load average: 1.03, 0.35, 0.17 Tasks: 234 total, 5 running, 229 sleeping, 0 stopped, 0 zombie Cpu0 : 98.0%us, 1.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 1.0%si, 0.0%st Cpu1 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 : 99.0%us, 1.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
咱們看到全部的核都是100%,cat /proc/cpuinfo | grep "physical id" | wc -l
是獲取到CPU的核數,邏輯核數。這樣每個cpu上都會調度到一個死循環的進程。shell
$ ps aux | grep [.]/out | awk '{print $2}' | xargs sudo kill -9
$ top top - 16:11:03 up 207 days, 21:56, 2 users, load average: 0.81, 0.47, 0.27 Tasks: 232 total, 2 running, 230 sleeping, 0 stopped, 0 zombie Cpu0 : 1.0%us, 2.0%sy, 0.0%ni, 94.1%id, 0.0%wa, 2.0%hi, 1.0%si, 0.0%st Cpu1 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
咱們看到當前的進程在cpu2上,能夠經過taskset 命令綁定cpu多線程
# taskset -cp 0 7368 pid 7368's current affinity list: 0-3 pid 7368's new affinity list: 0
進程實際上四個核都是能夠使用的,當時因爲沒有使用多線程,因此只調度到了一個核上。如今通過taskset設置後,如今咱們再來看看top的輸出:運維
$ top top - 16:14:09 up 207 days, 21:59, 2 users, load average: 0.99, 0.71, 0.39 Tasks: 230 total, 3 running, 227 sleeping, 0 stopped, 0 zombie Cpu0 : 98.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 1.0%hi, 1.0%si, 0.0%st Cpu1 : 0.0%us, 1.0%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 : 1.0%us, 1.0%sy, 0.0%ni, 98.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
如今跑到了cpu0上了。linux運維
$ lookbusy -c 30
讓全部的cpu使用率都是30%ui
$ lookbusy -c 75 -n 2
讓兩個cpu的使用率爲30%線程
$ lookbusy --cpu-mode curve --cpu-curve-peak 14h -c 20-80
讓cpu在20%-%80之間,週期爲24H,在14點,即下午2點達到峯值code
$ lookbusy -c 20-30 --cpu-mode curve --cpu-curve-period 60m --cpu-curve-peak 30m
讓cpu以60分鐘爲週期,30分鐘是峯值,使用率在20% - 30%之間。進程