Use cgroup limit cpu and memory utilization

  Linux kernel version 4.4 is used in our environment, has supported cgroup, cgroup as long as the selected kernel configuration, and check what you want to configure (general setup-> control group support->)

There are a variety of online information on cgroup, I will not detail here, direct mount cgoup

mkdir cgroup
mkdir cgroup/cpu
mkdir cgroup/memory
mount -t cgroup -ocpu cpu cgroup/cpu
mount -t cgroup -omemory memory cgroup/memory > /dev/util 2>&1

mkdir cgroup/cpu/cgroup0

1, limit cpu utilization

  By cfs_period_us and cpu.cfs_quota_us with the realization, cfs_period_us default value is 100ms, on behalf of the cycle time is 100ms, cpu.cfs_quota_us is the maximum time period that can process within 100ms occupied, can be set to 95ms, ie cpu utilization rate is 95%, Note that this single cpu utilization; for multi-core system, if you want to set the 95% utilization, cpu.cfs_quota_us should be N * cfs_period_us * 95%, N is the number of cpu core by cat proc / cpuinfo view (CpuNum = `cat / proc / cpuinfo | grep name | cut -f2 -d: | uniq -c | awk -F '' '{print $ 1}' `)

#set cpu ratio 98%
cfs_quota_us=`expr $CpuNum \* $cfs_period_us \* 98 / 100`
echo $cfs_quota_us > /cgroup/cpu/cgroup0/cpu.cfs_quota_us
echo $rcSPid > /cgroup/cpu/cgroup0/cgroup.procs

  For real-time environment to run the process, the need to cpu.rt_period_us set cpu.rt_runtime_us, cpu.rt_period_us is the cycle time of real-time process, default 1s; cpu.rt_runtime_us is the maximum time within a period of time, real-time process takes, the default is 0; the beginning did not notice this, cgroup a configuration on our environment, it will cause abnormal restart, later found to be rt_runtime_us 0; kernel can be set to the default value of 950ms (cat / proc / sys / kernel / sched_rt_runtime_us) or in the kernel configuration cgroup then removed sched_rt items. RT this process will not be limited cgroup, ie no cpu.rt_runtime_us this one under cgroup0 directory

  After the cpu utilization set before it can be necessary to limit the process pid join cgroup.procs; because of our environment in the process starting from rcS, so just want to start just to rcS of pid join cgroup.procs, subsequently created child processes are automatically added to the cgroup.procs in (get pid rcSPid = `ps -o pid, comm | grep -o". [0-9] * rcS "| grep -o [0-9] * `)

2, limit memory utilization

  To avoid the bug of some processes cause a memory leak, you can use memory.limit_in_bytes limit the maximum memory cgroup0 group all the processes that can be used, in bytes; because of our environment in the process of starting from rcS, and therefore only need to rcS only need to start the pid join cgroup.procs, subsequently created child processes are automatically added to the cgroup.procs in. My environment is set to memory.limit_in_bytes MemFree-200M (200M left to the kernel use is sufficient)

MemFree=`cat /proc/meminfo | grep 'MemFree' | awk -F ' ' '{print $2}'`
MemRatioKB=`expr $MemFree - 204800`
MemRatio=`expr $MemRatioKB \* 1024`#config memory ratio

cgroup mkdir / Memory / cgroup0> / dev / util 2> & 1
echo $ MemRatio>
/cgroup/memory/cgroup0/memory.limit_in_bytes
  process memory exceeds a set threshold, what will happen? It will be kill! ! If you do not want to kill, they would have to set memory.oom_control to 1, so that when memory-progress exceed the threshold, the process will be added to the waiting queue until the memory allocated to him;
#disable the kill
echo 1>
cgroup / Memory / cgroup0 / memory.oom_control

Other memory directory entry:

cgroup.event_control # interface for eventfd
memory.usage_in_bytes # display memory currently used
memory.limit_in_bytes # Set / Display the current limit of the amount of memory
memory.failcnt # display memory usage reaches the limit value of the number of
memory.max_usage_in_bytes # historical memory the maximum amount of
memory.soft_limit_in_bytes # set / display the current soft limit the amount of memory
memory.stat # display memory usage of the current cgroup
memory.use_hierarchy # set / display whether the memory usage statistics to the current sub-cgroup cgroup inside
memory.force_empty # trigger the system immediately recovered as much as possible the current cgroup can be recovered memory
notification event memory.pressure_level # set memory pressure, use with cgroup.event_control with
memory.swappiness # settings and displays the current swappiness
memory.move_charge_at_immigrate # set when the process moving to the other a cgroup, it takes up memory whether as moving past
memory.oom_control # settings / display oom controls related configuration
memory.numa_stat # display numa associated memory

Consumer attach cpu utilization and memory consumption program, also refer others.

. 1, cpu consumption, the number of process parameters are Transcend, (a nuclear process consumes a cpu)

int main(int argc, char *argv[]){
pid_t pid;
int i, pn=4;
if(argc==2){
pn=atoi(argv[1]);
}
for(i=0;i<pn;i++){
pid=fork();
if(pid<=0)break;
}
if(pid<0){
printf("fork() error\n");
return -1;
}else if(pid==0){
//sub process works
for(;;);
}else{
//wait for the sub process's termination
wait(NULL);
}
return 0;
}

2, memory consumption

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MB (1024 * 1024 * 1024)

int main(int argc, char *argv[])
{
char *p;
int i = 0;
system("cat /cgroup/cpu/cgroup0/cgroup.procs");
while(1) {
p = (char *)malloc(MB);
memset(p, 0, MB);
printf("%dG memory allocated\n", ++i);
sleep(1);
}

return 0;
}

Guess you like

Origin www.cnblogs.com/lichenyangyixin/p/10992936.html