cgroup

簡介

CGroup 提供了一個 CGroup 虛擬文件系統,作爲進行分組管理和各子系統設置的用戶接口。要使用 CGroup,必須掛載 CGroup 文件系統。這時通過掛載選項指定使用哪個子系統

安裝
yum install libcgroup libcgroup-tools
啓動
sudo service cgconfig start/stop
start up at the boot time.
chkconfig cgroup on
/etc/cgconfig.conf對mysql限制
group foo {    
    cpu {  
            cpu.cfs_quota_us = 50000;  
            cpu.cfs_period_us = 100000;  
    }  
    cpuset {    
            cpuset.cpus = "3";    
            cpuset.mems = "0";    
    }    
    cpuacct{  
  
    }  
    memory {    
            memory.limit_in_bytes=104857600;  
            memory.swappiness=0;  
            # memory.max_usage_in_bytes=104857600;  
            # memory.oom_control=0;  
    }   
    blkio  {  
           blkio.throttle.read_bps_device="8:0 524288";  
           blkio.throttle.write_bps_device="8:0 524288";  
    }   
}   
修改cgrules.conf文件掛載服務,cgroup 的文件系統默認會掛載到 /sys/fs/cgroup/ 目錄下。
# /etc/cgrules.conf  
#The format of this file is described in cgrules.conf(5)  
#manual page.  
#  
# Example:  
#<user>         <controllers>   <destination>  
#@student       cpu,memory      usergroup/student/  
#peter          cpu             test1/  
#%              memory          test2/  
test cpu foo
臨時對cpu進行限制(重啓失效)

1.運行cpu消耗shell腳本foo

while [ True ];do  
x='a'
done;

2.使用top命令查看pid爲30791進程cpu佔用率接近95%



3.使用cgroup對pid爲30791cpu進行限制到50%

mkdir -p /sys/fs/cgroup/cpu/foo/ 
echo 50000 > /sys/fs/cgroup/cpu/foo/cpu.cfs_quota_us  
echo 100000 > /sys/fs/cgroup/cpu/foo/cpu.cfs_period_us
echo 30791 > /sys/fs/cgroup/cpu/foo/tasks 

4.使用top命令查看pid爲30791cpu佔用率接近50%


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章