前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个限制进程 CPU 使用率的解决方案

一个限制进程 CPU 使用率的解决方案

作者头像
耕耘实录
发布2019-07-04 20:56:39
3.8K0
发布2019-07-04 20:56:39
举报
文章被收录于专栏:耕耘实录耕耘实录

版权声明:本文为耕耘实录原创文章,各大自媒体平台同步更新。欢迎转载,转载请注明出处,谢谢。联系本人:ecsboy(微信),136625317(QQ) https://cloud.tencent.com/developer/article/1457434

一个限制进程 CPU 使用率的解决方案

一 背景

在最近的一个项目中,需要限制 CPU 使用率。通过查阅各种资料,发现已经有直接可以使用的软件可以使用,这个软件就是cpulimit,这个软件使用非常简单。但是,结合实际使用场景,被限制的进程不能后台运行,一旦后台运行,进程即会立刻退出,而且该进程运行一段时间后会产生子进程及相关进程。针对这种情况,经过思考,通过以下解决方案解决该问题。

cpulimit 的原理: 为进程预设一个 cpu 占用率上限,并实时监控进程是否超出此上限值,若超出则让该进程暂停运行一段时间。cpulimit 使用 SIGSTOP 和 SIGCONT 这两个信号来控制进程。它不会修改进程的 nice 值,而是通过监控进程的 CPU 占用率来做出动态调整。

cpulimit 的优势是可以控制进程的cpu使用率的上限值。但与 nice 相比也有缺点,那就是即使 cpu 是空闲的,进程也不能完全使用整个 cpu 资源。

二 解决步骤

2.1 安装cpulimit

[root@gysl-dev ~]# yum -y install epel-release
[root@gysl-dev ~]# yum -y install cpulimit

2.2 执行脚本

[root@gysl-dev ~]# sh cpulimit.sh

cpulimit.sh脚本内容:

#!/bin/bash
while true;  
    do  
        sleep 30;
        pgrep palrun>&/dev/null;  
        if [ $? -eq 0 ]; then  
            for pid in `pgrep palrun`;  
                do  
                    cpulimit -l 70 -p $pid &  
                done;  
        break;
        fi;  
        done &

将以上脚本加入到需要限制 CPU 使用率的进行启动脚本的最前面,对该脚本的解释。

由于需要限制 CPU 使用率的进程不能在后台运行,所以把限制脚本加入到启动脚本的最前面,并切换到后台运行,sleep 30秒,待需要限制的进程启动并创建子进程后对其进行限制。

三 总结

3.1 官方帮助信息

[root@gysl-dev ~]# cpulimit --help
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 100 (required) #限制 CPU 使用百分比
      -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)

3.2 cpulimit命令使用实践

[root@gysl-dev ~]# cpulimit -l 70 -v -p 6258
1 cpu detected
Process 6258 found
Priority changed to -10
Members in the process group owned by 6258: 1

%CPU    work quantum    sleep quantum   active rate
70.09%   73424 us        26575 us       73.42%
69.86%   70778 us        29221 us       70.78%
69.94%   71703 us        28296 us       71.70%
69.77%   70495 us        29504 us       70.50%
69.91%   74194 us        25805 us       74.19%
69.49%   69281 us        30718 us       69.28%
69.78%   72668 us        27331 us       72.67%
70.35%   70634 us        29365 us       70.63%
69.66%   72786 us        27213 us       72.79%
70.27%   69679 us        30320 us       69.68%
69.56%   72325 us        27674 us       72.33%
70.40%   71926 us        28073 us       71.93%
69.43%   71330 us        28669 us       71.33%
69.50%   72184 us        27815 us       72.18%
70.16%   69835 us        30164 us       69.84%
69.37%   74080 us        25919 us       74.08%
69.84%   69417 us        30582 us       69.42%
69.95%   71415 us        28584 us       71.42%
70.81%   71334 us        28665 us       71.33%

3.3 注意事项

本次实验使用了比较老旧的版本,仅支持单 CPU 的资源限制。笔者在后来的 v2.4 版本实践中发现,新版本支持多 CPU 的资源限制。

3.4 GitHub 源码

CPUlimit源码

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年03月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一个限制进程 CPU 使用率的解决方案
    • 一 背景
      • 二 解决步骤
        • 2.1 安装cpulimit
        • 2.2 执行脚本
      • 三 总结
        • 3.1 官方帮助信息
        • 3.2 cpulimit命令使用实践
        • 3.3 注意事项
        • 3.4 GitHub 源码
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档