[Linux operations] common scenario simulation-CPU usage simulation
From http://www.cnblogs.com/zk47/p/4771105.html1 Single Core 100%:
#include <stdlib.h>int main(){ while(1); return 0;}
out kill_cpu.c$ ./out
- Look at the results of top:
$ toptop-15:44:08 up207 days,21st:29,2 users, load average:0.08,0.13,0.10Tasks:231 Total,2 Running,229 sleeping,0 stopped,0 ZOMBIECPU0:1.0%us,1.0%sy,0.0%ni,98.0%id,0.0%wa,0.0%hi,0.0%si,0.0%STCPU1:100.0%us,0.0%sy,0.0%ni,0.0%id,0.0%wa,0.0%hi,0.0%si,0.0%STCPU2:0.0%us,0.0%sy, 0. 0%ni,100.0%id, 0. 0%wa, 0. 0%hi, 0. 0%si, 0. 0%STCPU3: 1.0%us, 1.0%sy, 0.0%ni, 98.0%id, 0. 0%wa, 0. 0%hi, 0. 0%si, 0. 0%st
See the CPU of a certain nucleus has reached 100%. This is because the process is dispatched to a kernel and does not use multi-threading, so naturally a core CPU is 100%. So how do you get all the CPUs to be 100%?
2 Let all the nuclei be 100%.
$For I in' SEQ 1 $ (cat/proc/cpuinfo | grep "Physical ID" | wc-l) ';Do./Out & done[1]5992[2]5993[3]5994[4]5995$ Toptop-15:56:Up207 days,21st:42,2 users, load average:1.03,0.35,0.17Tasks:234 Total,5 Running,229 sleeping,0 stopped,0 ZOMBIECPU0:98.0%us,1.0%sy,0.0%ni,0.0%id,0.0%wa,0.0%hi,1.0%si,0.0%STCPU1:100.0%us,0.0%sy,0.0%ni,0.0%id,0.0%wa,0.0%hi,0.0%si,0.0%STCPU2:99.0%us,1.0%sy,0.0%ni, 0. 0%id, 0. 0%wa, 0. 0%hi, 0. 0%si, 0. 0%STCPU3: 100.0%us, 0.0%sy, 0. 0%ni, 0. 0%id, 0. 0%wa, 0. 0%hi, 0. 0%si, 0. 0%st
We see that all the cores are 100%, which is the number of cat /proc/cpuinfo | grep "physical id" | wc -l
cores that get to the CPU, the number of logical cores. This will dispatch each CPU to a dead loop process.
grep [.]/out | awk ‘{print $2}‘ | xargs sudo kill -9
3 Let a nuclear 100%
$ toptop-16:11:Up207 days,21st:56,2 users, load average:0.81,0.47,0.27Tasks:232 Total,2 Running,Sleeping,0 stopped,0 ZOMBIECPU0:1.0%us,2.0%sy,0.0%ni,94.1%id,0.0%wa,2.0%hi,1.0%si,0.0%STCPU1:0.0%us,0.0%sy,0.0%ni,100.0%id,0.0%wa,0.0%hi,0.0%si,0.0%STCPU2:100.0%us,0.0%sy,0.0%ni, 0. 0%id, 0. 0%wa, 0. 0%hi, 0. 0%si, 0. 0%STCPU3: 0.0%us, 0.0%sy, 0. 0%ni,100.0%id, 0. 0%wa, 0. 0%hi, 0. 0%si, 0. 0%st
We see that the current process is on CPU2, and the CPU can be bound by the Taskset command
# taskset -cp 0 7368pid 7368‘s current affinity list: 0-3pid 7368‘s new affinity list: 0
The process actually has four cores that can be used, which was dispatched to a single core because it was not multi-threaded. Now, after Taskset setup, let's look at the output of top:
$ toptop-16:14:09 up207 days,21st:59,2 users, load average:0.99,0.71,0.39Tasks:Total,3 Running,227 Sleeping,0 stopped,0 ZOMBIECPU2 x98.0%us,0.0%sy,0.0%ni,0.0%id,0.0%wa,1.0%hi,1.0%si,0.0%STCPU1:0.0%us,1.0%sy,0.0%ni,99.0%id,0.0%wa,0.0%hi,0.0%si,0.0%STCPU2:1.0%us,1.0%sy,0.0%ni, 98.0%id, 0.0%wa, 0. 0%hi, 0. 0%si, 0. 0%STCPU3: 0.0%us, 0.0%sy, 0. 0%ni,100.0%id, 0. 0%wa, 0. 0%hi, 0. 0%si, 0. 0%st
And now he's running to the cpu0.
4 Lookbusy
$ lookbusy -c 30
Let all CPU usage be 30%
$ lookbusy -c 75 -n 2
Get two CPU usage at 30%
$ lookbusy --cpu-mode curve --cpu-curve-peak 14h -c 20-80
Let the CPU between 20%-%80, the cycle is 24H, at 14 points, that is 2 o'clock in the afternoon peak
$ lookbusy -c 20-30 --cpu-mode curve --cpu-curve-period 60m --cpu-curve-peak 30m
Let the CPU cycle in 60 minutes, 30 minutes is the peak, the usage rate is between 20%-30%.
[Linux operations] common scenario simulation-CPU usage simulation