AlmaLinuxJAVA

20230107-限制CPU使用率之cpulimit

2023-01-07  本文已影响0人  負笈在线

大量的服务器从CentOS 7迁移到Alma Linux 8后,部署服务器上部署的ClamAV需要限制其CPU使用率。CentOS6和CentOS7一直通过cgroup进行限制,但是在Alma Linux 8上试用cgroup后发现其工具libcgroup-tools已经没有了cgred服务。通过源码包也确认确实Linux 8上的libcgroup套件不提供该服务(方法:访问https://git.centos.org/rpms/libcgroup/releases直接查看libcgroup-0.41-19.el8 源码包文件)。

在寻求Alma Linux 8上cgroup更合适应用方法时,也调查其他限制进程CPU使用率的方法。主要有以下三种方法:

  1. nice 命令

  2. cpulimit 命令

  3. cgroup(Control group,控制组)

nice命令通过手动降低任务的优先级,限制该任务CPU使用率。在运行cpu密集型的后台进程或批处理作业时非常适合,系统自带最常用命令集coreutils的命令之一,使用也非常简单。

一、cpulimit简介

cpulimit 命令的工作原理是为进程预设一个 cpu 占用率门限,并实时监控进程是否超出此门限,若超出则让该进程暂停运行一段时间。cpulimit 使用 SIGSTOP 和 SIGCONT 这两个信号来控制进程。cpulimit 的优势是可以控制进程的cpu使用率的上限值。

二、cpulimit使用

cpulimit安装

# yum install epel-release
# yum install cpulimit
# cpulimit -h
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 200 (required)|进程允许占用cpu的百分比,从0~200,及0%~200%;补充说明:范围为0%~100%*核心数,此处为2核
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes|加这个参数同时限制子进程
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

cpulimit使用

安装并启用httpd服务

# yum install httpd
# systemctl start httpd

利用ab命令发起大量服务请求

# ab -c 100 -n 500000  http://172.26.37.144/

top命令查看所有httpd进程的cpu占用率

# top

限制httpd服务的cpu占用率

# cpulimit -i -e httpd -l 10
-i:同时限制子进程
-e httpd:限制httpd进程
-l 10:限制httpd进程最高cpu占用率10%

再次top命令查看是否限制有效

# top
上一篇 下一篇

猜你喜欢

热点阅读