zoukankan      html  css  js  c++  java
  • 利用cgroup控制进程使用的资源(cpu、内存等)

    实验环境:centos 6.10

    1.安装libcgroup

    yum install -y libcgroup

    2.进入资源控制器默认挂载目录/cgroup

    [root@hadoop1 cgroup]# cd /cgroup/
    [root@hadoop1 cgroup]# ll
    total 8
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 blkio
    drwxr-xr-x. 3 root root  0 Nov 12 16:22 cpu
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 cpuacct
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 cpuset
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 devices
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 freezer
    drwxr-xr-x. 3 root root  0 Nov 12 15:22 memory
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 net_cls

    3.创建两个测试脚本,use_cpu.sh模拟使用cpu的进程,use_mem.sh模拟使用内存的进程

    [root@hadoop1 cgroup]# ll
    total 8
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 blkio
    drwxr-xr-x. 3 root root  0 Nov 12 16:22 cpu
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 cpuacct
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 cpuset
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 devices
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 freezer
    drwxr-xr-x. 3 root root  0 Nov 12 15:22 memory
    drwxr-xr-x. 2 root root  0 Nov 12 15:22 net_cls
    -rwxr-xr-x. 1 root root 49 Nov 12 15:28 use_cpu.sh
    -rwxr-xr-x. 1 root root 53 Nov 12 16:41 use_mem.sh
    
    [root@hadoop1 cgroup]# cat use_cpu.sh 
    #!/bin/bash
    x=0
    while [ true ]; do
            x=$x+1
    done;
    [root@hadoop1 cgroup]# cat use_mem.sh 
    #!/bin/bash
    x="a"
    while [ true ];do
        x=$x$x
    done;

    4.直接运行脚本,分别查看cpu和内存的使用情况

    [root@hadoop1 cgroup]# nohup ./use_cpu.sh &
    [3] 7817
    [root@hadoop1 cgroup]# nohup: ignoring input and appending output to `nohup.out'
    [root@hadoop1 cgroup]# top
     PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
     7817 root      20   0  103m 1724 1148 R 100.0  0.1   0:52.89 use_cpu.sh 
    [root@hadoop1 cgroup]# kill -9 7817

     [root@hadoop1 cgroup]# nohup ./use_mem.sh &
      [3] 7822
      [root@hadoop1 cgroup]# nohup: ignoring input and appending output to `nohup.out'

      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

     7822 root      20   0 2151m 1.3g 1132 R 100.0 73.3   1:04.15 use_mem.sh

    可以看到,cpu使用量几乎达到100%,内存使用量在不断增长,截图为止显示为73.3M。

    5.创建一个cpu控制组,然后在其中运行任务,使用top观察结果

    [root@hadoop1 cpu]# cgcreate -g cpu:foo
    [root@hadoop1 cpu]# echo 50000 > foo/cpu.cfs_quota_us # cpu.cfs_period_us中的值为100000,配置50000即使用50%
    [root@hadoop1 cgroup]# cgexec -g cpu:foo nohup ./use_cpu.sh &
    [3] 7832
    [root@hadoop1 cgroup]# nohup: ignoring input and appending output to `nohup.out'
    PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
     7832 root      20   0  103m 1500 1148 R 49.9  0.1   0:16.01 use_cpu.sh                                                           
       462 root      20   0     0    0    0 S  0.3  0.0   0:01.96 jbd2/dm-0-8                                                          
         1 root      20   0 19232  752  752 S  0.0  0.0   0:02.84 init                                                                 
         2 root      20   0     0    0    0 S  0.0  0.0   0:00.01 kthreadd                                                             
         3 root      RT   0     0    0    0 S  0.0  0.0   0:00.03 migration/0  

    可以看到,测试进程使用了约50%的cpu。同理测试控制内存,分配10M给进程组

    [root@hadoop1 cgroup]# cgcreate -g memory:foo
    [root@hadoop1 cgroup]# echo 10485760 > memory/foo/memory.limit_in_bytes
    [root@hadoop1 cgroup]# cgexec -g memory:foo nohup ./use_mem.sh &
    [3] 7836
     PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND   
    7836 root      20   0  871m  11m 1156 R 87.1  0.6   1:07.82 use_mem.sh

    计算可知,测试进程大约使用了10M内存

  • 相关阅读:
    智能指针unique_ptr记录
    ubuntu系统火狐无法播放网页视频
    javascript中json对象json数组json字符串互转及取值
    C#压缩文件
    C#异步编程
    C# POST请求 json格式
    C# Http方式下载文件到本地类
    C#中NPOI操作excel之读取和写入excel数据
    浅析C#中抽象类和接口的区别
    C#自动实现Dll(OCX)控件注册的两种方法
  • 原文地址:https://www.cnblogs.com/cqdxwjd/p/9948019.html
Copyright © 2011-2022 走看看