use cgroup limit cpu core usage

简介:
本文将测试一下使用cgroup的cpuset组来控制进程对CPU的使用 : 
测试环境CentOS 7.x x64
创建一个继承组
[root@150 rg1]# cd /sys/fs/cgroup/cpuset/
[root@150 cpuset]# mkdir cg1

继承组自动创建对应的限制文件
[root@localhost cpuset]# cd cg1
[root@localhost cg1]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  8 00:46 cgroup.clone_children
-rw-r--r-- 1 root root 0 Jan  8 00:46 cgroup.procs
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.cpu_exclusive
-rw-r--r-- 1 root root 0 Jan  8 03:06 cpuset.cpus
-r--r--r-- 1 root root 0 Jan  8 00:46 cpuset.effective_cpus
-r--r--r-- 1 root root 0 Jan  8 00:46 cpuset.effective_mems
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.mem_exclusive
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.mem_hardwall
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.memory_migrate
-r--r--r-- 1 root root 0 Jan  8 00:46 cpuset.memory_pressure
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.memory_spread_page
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.memory_spread_slab
-rw-r--r-- 1 root root 0 Jan  8 03:01 cpuset.mems
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.sched_load_balance
-rw-r--r-- 1 root root 0 Jan  8 00:46 cpuset.sched_relax_domain_level
-rw-r--r-- 1 root root 0 Jan  8 00:46 notify_on_release
-rw-r--r-- 1 root root 0 Jan  8 03:01 tasks

继承组的tasks为空.

查看CPU资源
# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    1
Core(s) per socket:    4
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 26
Model name:            Intel(R) Xeon(R) CPU           E5504  @ 2.00GHz
Stepping:              5
CPU MHz:               1995.053
BogoMIPS:              3989.39
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              4096K
NUMA node0 CPU(s):     0-7


设置cpuset.mems
[root@localhost cg1]# echo 0 > ./cpuset.mems

否则在将PID添加到这个组的时候, 会报错如下 : 
[root@localhost cg1]# echo 48413 >./tasks 
-bash: echo: write error: No space left on device


设置cpuset.cpus
[root@localhost cg1]# echo 7 > ./cpuset.cpus

将数据库的主进程加入到这个组
[root@localhost cg1]# ps -ewf|grep postgres
postgres   48413       1  0 Jan07 ?        00:00:00 /opt/pgsql9.3.5/bin/postgres
[root@localhost cg1]# echo 48413 >./tasks

现在数据库被限制了只能使用7号CPU 核.
postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -c 16 -j 4 -h 127.0.0.1 -T 30
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 560871
tps = 18695.122321 (including connections establishing)
tps = 18746.566935 (excluding connections establishing)
statement latencies in milliseconds:
        0.851678        select 1;


使用top的P选项查看 : 
在top界面按 
选择
* P       = Last Used Cpu (SMP) 

查看到所有postgres进程新fork的backend process都用的7号cpu
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                     P 
  54349 postgres  20   0  307264  10076   8308 R   7.3  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14172) idle           7 
  54351 postgres  20   0  307264  10076   8308 S   7.3  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14174) idle           7 
  54353 postgres  20   0  307264  10140   8372 R   7.3  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14176) idle           7 
  54354 postgres  20   0  307264  10076   8308 R   7.3  0.0   0:00.80 postgres: postgres postgres 127.0.0.1(14177) idle           7 
  54355 postgres  20   0  307264   9900   8136 S   7.3  0.0   0:00.80 postgres: postgres postgres 127.0.0.1(14178) idle           7 
  54356 postgres  20   0  307264   9836   8068 R   7.3  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14179) idle           7 
  54357 postgres  20   0  307268  10080   8308 R   7.3  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14180) idle           7 
  54358 postgres  20   0  307268  10016   8244 R   7.3  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14181) idle           7 
  54360 postgres  20   0  307268   9956   8184 R   7.3  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14183) idle           7 
  54361 postgres  20   0  307268  10020   8248 R   7.3  0.0   0:00.78 postgres: postgres postgres 127.0.0.1(14184) idle           7 
  54350 postgres  20   0  307264  10076   8308 R   3.7  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14173) idle           7 
  54352 postgres  20   0  307264  10076   8308 R   3.7  0.0   0:00.79 postgres: postgres postgres 127.0.0.1(14175) idle           7 
  54359 postgres  20   0  307268  10020   8248 R   3.7  0.0   0:00.78 postgres: postgres postgres 127.0.0.1(14182) idle           7 
  54362 postgres  20   0  307268   9956   8184 R   3.7  0.0   0:00.78 postgres: postgres postgres 127.0.0.1(14185) idle           7 
  54363 postgres  20   0  307268   9960   8188 R   3.7  0.0   0:00.78 postgres: postgres postgres 127.0.0.1(14186) idle           7 
  54364 postgres  20   0  307268   9956   8184 R   3.7  0.0   0:00.77 postgres: postgres postgres 127.0.0.1(14187) idle           7 
  48413 postgres  20   0  306188  15896  15036 S   0.0  0.0   0:00.28 /opt/pgsql9.3.5/bin/postgres                                7 

改为0-7可以使用8个CPU核
[root@localhost cg1]# echo 0-7 > ./cpuset.cpus

再次测试 : 
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                     
  54424 postgres  20   0  307264  10004   8236 S  35.6  0.0   0:04.28 postgres: postgres postgres 127.0.0.1(14191) BIND           4 
  54423 postgres  20   0  307264  10076   8308 S  35.1  0.0   0:04.23 postgres: postgres postgres 127.0.0.1(14190) idle           4 
  54425 postgres  20   0  307264  10076   8308 R  34.6  0.0   0:04.24 postgres: postgres postgres 127.0.0.1(14192) idle           3 
  54433 postgres  20   0  307268   9336   7580 S  34.0  0.0   0:03.97 postgres: postgres postgres 127.0.0.1(14200) idle           6 
  54426 postgres  20   0  307264  10116   8348 S  33.5  0.0   0:04.20 postgres: postgres postgres 127.0.0.1(14193) idle           3 
  54427 postgres  20   0  307264  10076   8308 S  33.5  0.0   0:03.99 postgres: postgres postgres 127.0.0.1(14194) idle           6 
  54428 postgres  20   0  307268   9652   7892 R  33.0  0.0   0:04.05 postgres: postgres postgres 127.0.0.1(14195) idle           6 
  54429 postgres  20   0  307268   9652   7892 R  32.5  0.0   0:04.06 postgres: postgres postgres 127.0.0.1(14196) idle           1 
  54434 postgres  20   0  307268  10020   8248 S  31.9  0.0   0:03.92 postgres: postgres postgres 127.0.0.1(14201) idle           5 
  54437 postgres  20   0  307268   9896   8124 R  31.9  0.0   0:03.91 postgres: postgres postgres 127.0.0.1(14204) idle           2 
  54438 postgres  20   0  307268   9956   8184 R  31.9  0.0   0:03.85 postgres: postgres postgres 127.0.0.1(14205) BIND           0 
  54430 postgres  20   0  307268   9648   7888 R  31.4  0.0   0:03.99 postgres: postgres postgres 127.0.0.1(14197) idle           3 
  54431 postgres  20   0  307268   9940   8168 S  31.4  0.0   0:03.91 postgres: postgres postgres 127.0.0.1(14198) idle           3 
  54432 postgres  20   0  307268   9340   7588 S  31.4  0.0   0:03.92 postgres: postgres postgres 127.0.0.1(14199) idle           4 
  54435 postgres  20   0  307268   9956   8184 S  30.9  0.0   0:03.80 postgres: postgres postgres 127.0.0.1(14202) idle           7 
  54436 postgres  20   0  307268   9956   8184 R  30.9  0.0   0:03.85 postgres: postgres postgres 127.0.0.1(14203) BIND           7 

当然性能也有提升
postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -c 16 -j 4 -h 127.0.0.1 -T 30
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 2924931
tps = 97490.141264 (including connections establishing)
tps = 97533.454952 (excluding connections establishing)
statement latencies in milliseconds:
        0.162975        select 1;



[参考]
目录
相关文章
|
Oracle 关系型数据库 Linux
国产化之 Arm64 CPU + 银河麒麟系统 安装 .NetCore
国产化之 Arm64 CPU + 银河麒麟系统 安装 .NetCore
1241 0
|
关系型数据库
InnoDB redo log thread cpu usage
InnoDB 在8.0 里面把写redo log 角色的各个线程都独立出来, 每一个thread 都处于wait 状态, 同样用户thread 调用log_write_up_to 以后, 也会进入wait 状态.这里的wait 等待最后都是通过调用 os_event_wait_for 来实现, 而 os_event_wait_for 是先spin + wait 的方式实现.所以这里有两个参数会影响os_event_wait_for 函数:spins_limit,timeout.
159 0
|
Ubuntu Linux Docker
docker 没有 /sys/fs/cgroup/cpu/docker这个目录
docker 没有 /sys/fs/cgroup/cpu/docker这个目录
1374 0
|
SQL XML 数据格式
Q&A – High CPU Usage on Alibaba Cloud SQL Server
A primary issue with SQL Server is its sensitivity to latency, often resulting in performance issues.
1777 0
Q&A – High CPU Usage on Alibaba Cloud SQL Server
|
SQL 关系型数据库 RDS
Troubleshooting High CPU Usage on Alibaba Cloud SQL Server
A primary issue with SQL Server is its sensitivity to latency, often resulting in performance issues.
1361 0
Troubleshooting High CPU Usage on Alibaba Cloud SQL Server
|
调度 数据中心
Linux cgroup资源隔离各个击破之 - cpu隔离1
Linux cgroup 有两个子系统支持CPU隔离。一个是cpu子系统,另一个是cpuset子系统。 cpu子系统包括两种CPU资源隔离方法 .1. 完全公平调度 Completely Fair Scheduler (CFS) .2. 实时调度 Real-Time scheduler (
16953 0
|
Linux
Linux上获取CPU Core个数的实现
可以通过多种手段取得CPU Core的个数,如: 1) 调用系统提供的函数get_nprocs(),可以在头文件sys/sysinfo.h中发现它 2) 借助系统提供的sysconf()函数:sysconf(_SC_NPROCESSORS_CONF)、s...
1352 0