1) 显示当前的cpu子系统的设定
[root@srv1 ~]# cgget -g cpu /
/:
cpu.rt_period_us: 1000000
cpu.rt_runtime_us: 950000
cpu.stat: nr_periods 0
nr_throttled 0
throttled_time 0
cpu.cfs_period_us: 100000
cpu.cfs_quota_us: -1
cpu.shares: 1024
2) 创建一个cgroup并确认
[root@srv1 ~]# cgcreate -g cpu:/cpu_quota-50
[root@srv1 ~]# cgget -g cpu /cpu_quota-50
/cpu_quota-50:
cpu.rt_period_us: 1000000
cpu.rt_runtime_us: 0
cpu.stat: nr_periods 0
nr_throttled 0
throttled_time 0
cpu.cfs_period_us: 100000
cpu.cfs_quota_us: -1
cpu.shares: 1024
3) 设置cgroups
每[100000μs]可访问CPU[50000μs]。即50%的μs
[root@srv1 ~]# cgset -r cpu.cfs_quota_us=50000 cpu_quota-50
[root@srv1 ~]# cgget -g cpu /cpu_quota-50
/cpu_quota-50:
cpu.rt_period_us: 1000000
cpu.rt_runtime_us: 0
cpu.stat: nr_periods 0
nr_throttled 0
throttled_time 0
cpu.cfs_period_us: 100000
cpu.cfs_quota_us: 50000
cpu.shares: 1024
4) 确认在子系统下创建了cpu_quota-50及相关控制
[root@srv1 ~]# ll /sys/fs/cgroup/cpu/cpu_quota-50/
total 0
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cgroup.clone_children
--w--w---- 1 root root 0 Feb 26 02:41 cgroup.event_control
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cgroup.procs
-r--r--r-- 1 root root 0 Feb 26 02:41 cpuacct.stat
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cpuacct.usage
-r--r--r-- 1 root root 0 Feb 26 02:41 cpuacct.usage_percpu
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cpu.cfs_period_us
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cpu.cfs_quota_us
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cpu.rt_period_us
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cpu.rt_runtime_us
-rw-rw-r-- 1 root root 0 Feb 26 02:41 cpu.shares
-r--r--r-- 1 root root 0 Feb 26 02:41 cpu.stat
-rw-rw-r-- 1 root root 0 Feb 26 02:41 notify_on_release
-rw-rw-r-- 1 root root 0 Feb 26 02:41 tasks
5) 测试
[root@srv1 ~]# yes > /dev/null &
[1] 29184
(1) 确认cpu飙升
[root@srv1 ~]# top -b -n 1 -p 29184
top - 02:47:18 up 1:35, 1 user, load average: 0.34, 0.09, 0.07
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 48.4 us, 3.2 sy, 0.0 ni, 48.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 787252 total, 347176 free, 120332 used, 319744 buff/cache
KiB Swap: 782332 total, 782332 free, 0 used. 532380 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
29184 root 20 0 107960 356 280 R 100.0 0.0 0:33.19 yes
(2) 确认每个cpu核心使用情况
[root@srv1 ~]# mpstat -P ALL 1 1 | grep ^Average
Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 49.75 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 50.25
Average: 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
Average: 1 99.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
(3) 限制进程对cpu的使用
[root@srv1 ~]# cgclassify -g cpu:cpu_quota-50 29184
[root@srv1 ~]# cat /sys/fs/cgroup/cpu/cpu_quota-50/tasks
29184
(4) 确认结果
[root@srv1 ~]# mpstat -P ALL 1 1 | grep ^Average
Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 24.50 0.00 0.50 0.00 0.00 0.00 0.00 0.00 0.00 75.00
Average: 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
Average: 1 49.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 50.00
[root@srv1 ~]# kill 29184
|