20230107-限制CPU使用率之cpulimit

大量的服務(wù)器從CentOS 7遷移到Alma Linux 8后,部署服務(wù)器上部署的ClamAV需要限制其CPU使用率。CentOS6和CentOS7一直通過cgroup進(jìn)行限制,但是在Alma Linux 8上試用cgroup后發(fā)現(xiàn)其工具libcgroup-tools已經(jīng)沒有了cgred服務(wù)。通過源碼包也確認(rèn)確實(shí)Linux 8上的libcgroup套件不提供該服務(wù)(方法:訪問https://git.centos.org/rpms/libcgroup/releases直接查看libcgroup-0.41-19.el8 源碼包文件)。

在尋求Alma Linux 8上cgroup更合適應(yīng)用方法時(shí),也調(diào)查其他限制進(jìn)程CPU使用率的方法。主要有以下三種方法:

  1. nice 命令

  2. cpulimit 命令

  3. cgroup(Control group,控制組)

nice命令通過手動(dòng)降低任務(wù)的優(yōu)先級(jí),限制該任務(wù)CPU使用率。在運(yùn)行cpu密集型的后臺(tái)進(jìn)程或批處理作業(yè)時(shí)非常適合,系統(tǒng)自帶最常用命令集coreutils的命令之一,使用也非常簡(jiǎn)單。

一、cpulimit簡(jiǎn)介

cpulimit 命令的工作原理是為進(jìn)程預(yù)設(shè)一個(gè) cpu 占用率門限,并實(shí)時(shí)監(jiān)控進(jìn)程是否超出此門限,若超出則讓該進(jìn)程暫停運(yùn)行一段時(shí)間。cpulimit 使用 SIGSTOP 和 SIGCONT 這兩個(gè)信號(hào)來控制進(jìn)程。cpulimit 的優(yōu)勢(shì)是可以控制進(jìn)程的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)|進(jìn)程允許占用cpu的百分比,從0~200,及0%~200%;補(bǔ)充說明:范圍為0%~100%*核心數(shù),此處為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|加這個(gè)參數(shù)同時(shí)限制子進(jìn)程
      -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服務(wù)

# yum install httpd
# systemctl start httpd

利用ab命令發(fā)起大量服務(wù)請(qǐng)求

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

top命令查看所有httpd進(jìn)程的cpu占用率

# top

限制httpd服務(wù)的cpu占用率

# cpulimit -i -e httpd -l 10
-i:同時(shí)限制子進(jìn)程
-e httpd:限制httpd進(jìn)程
-l 10:限制httpd進(jìn)程最高cpu占用率10%

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

# top
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容