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;



[参考]
相关文章
|
9月前
|
Oracle 关系型数据库 Linux
国产化之 Arm64 CPU + 银河麒麟系统 安装 .NetCore
国产化之 Arm64 CPU + 银河麒麟系统 安装 .NetCore
695 0
|
Ubuntu Linux Docker
docker 没有 /sys/fs/cgroup/cpu/docker这个目录
docker 没有 /sys/fs/cgroup/cpu/docker这个目录
1190 0
|
调度 数据中心
Linux cgroup资源隔离各个击破之 - cpu隔离1
Linux cgroup 有两个子系统支持CPU隔离。一个是cpu子系统,另一个是cpuset子系统。 cpu子系统包括两种CPU资源隔离方法 .1. 完全公平调度 Completely Fair Scheduler (CFS) .2. 实时调度 Real-Time scheduler (
16730 0
|
Linux
Linux上获取CPU Core个数的实现
可以通过多种手段取得CPU Core的个数,如: 1) 调用系统提供的函数get_nprocs(),可以在头文件sys/sysinfo.h中发现它 2) 借助系统提供的sysconf()函数:sysconf(_SC_NPROCESSORS_CONF)、s...
1307 0
|
29天前
|
JSON Java Serverless
nacos常见问题之cpu和内存占用高如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
149 0
|
2月前
|
弹性计算
2024阿里云幻兽帕鲁/Palworld服务器价格表(CPU/内存/带宽/磁盘收费标准)
2024年阿里云幻兽帕鲁专用服务器的价格根据不同的配置有所不同。 • 4核16G配置的价格为32元/月,如果选择购买3个月,则价格为96元。 • 8核32G配置的价格为90元/月,如果选择购买3个月,则价格为271元。 另外,还有配置为4核16G10M带宽的服务器,其价格为26元/月起。而8核32G10M带宽的价格也是90元/月。
95 1
|
2天前
|
机器学习/深度学习 缓存 监控
linux查看CPU、内存、网络、磁盘IO命令
`Linux`系统中,使用`top`命令查看CPU状态,要查看CPU详细信息,可利用`cat /proc/cpuinfo`相关命令。`free`命令用于查看内存使用情况。网络相关命令包括`ifconfig`(查看网卡状态)、`ifdown/ifup`(禁用/启用网卡)、`netstat`(列出网络连接,如`-tuln`组合)以及`nslookup`、`ping`、`telnet`、`traceroute`等。磁盘IO方面,`iostat`(如`-k -p ALL`)显示磁盘IO统计,`iotop`(如`-o -d 1`)则用于查看磁盘IO瓶颈。
|
22天前
|
存储 缓存 PHP
阿里云服务器实例、CPU内存、带宽、操作系统选择参考
对于使用阿里云服务器的用户来说,云服务器的选择和使用非常重要,如果实例、内存、CPU、带宽等配置选择错误,可能会影响到自己业务在云服务器上的计算性能及后期运营状况,本文为大家介绍一下阿里云服务器实例、CPU内存、带宽、操作系统的选择注意事项,以供参考。
阿里云服务器实例、CPU内存、带宽、操作系统选择参考
|
1月前
|
弹性计算 固态存储 Linux
2024年阿里云服务器租用详细价格表(CPU/内存/带宽/系统盘)
2024阿里云服务器租用优惠价格表,轻量服务器2核2G3M带宽轻量服务器一年61元,2核4G4M带宽轻量服务器一年165元12个月,ECS云服务器e系列2核2G配置、3M固定带宽、40G ESSD Entry云盘,99元一年、2核4G服务器30元3个月、2核4G配置365元一年、2核8G配置522元一年,云服务器u1、云服务器c7、g7和r7优惠价格表,CPU内存带宽系统盘配置详细报价:
740 3

热门文章

最新文章