Linux cgroups Overview

Source: Internet
Author: User

Starting with version 2.6.24, the Linux kernel provides a feature called Cgroups (control group). CGROUPS is the acronym for Control groups, which is used to limit, count, and isolate the resources that a group of processes occupy. It is also one of the foundations of LXC (Linux container), a lightweight virtualization technology at present. Each set of processes is a control group, which is a cgroup. Cgroups is divided into several subsystems, each of which represents a facility or resource controller used to dispatch a class of resources such as CPU clocks, memory, block devices, and so on. In implementation, Cgroups does not add a new system call, but instead behaves as a Cgroup file system that can mount one or more subsystems to a directory. Such as

Mount-t Cgroup-o CPU Cpu/sys/fs/cgroup/cpu

The CPU subsystem is mounted on the/sys/fs/cgroup/cpu. It is also possible to mount multiple subsystems on a single directory, or even mount them all to a directory, but I think it would be more flexible to mount each subsystem in a different directory. mount|awk ‘$5=="cgroup" {print $0}‘you can see the control groups that are currently mounted.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/76/wKioL1RR0UmBKH_NAASAtA8M1og784.jpg "title=" 4.png " alt= "Wkiol1rr0umbkh_naasata8m1og784.jpg"/>

cat /proc/cgroupsyou can see the status of all current control groups. The following script can be used to mount all subsystems to their respective directories.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/76/wKiom1RR0TuTzcZDAADznI0dCgs558.jpg "title=" 5.png "alt=" wkiom1rr0tutzczdaadzni0dcgs558.jpg "/>

#!/bin/bashcgroot= "${1:-/sys/fs/cgroup}" subsys= "${2:-blkio CPU CPUACCT cpuset devices freezer memory Net_cls Net_prio NS Perf_event} "mount-t Tmpfs cgroup_root" ${cgroot} "for SS in $subsys; Do mkdir-p "$cgroot/$ss" mount-t cgroup-o "$ss" "$ss" "$cgroot/$ss" done

Look at what's in those directories, like LS/sys/fs/cgroup/cpu.

cgroup.event_control  cpu.cfs_period_us  cpu.rt_period_us   cpu.shares   notify_on_release  taskscgroup.procs           cpu.cfs_quota_us   cpu.rt_runtime_us  cpu.stat     Release_agent 

which begins with "CPU." is something special in this subsystem. The other ones are in the corresponding directory of each subsystem. These files are used to read Resource usage information and to restrict resources. To create a control group, create a directory in the subsystem you need. such as  mkdir/sys/fs/cgroup/cpu/foo   Creates a/foo control group. The same set of files will appear in the newly created directory. In this directory, you can also continue to create cgroup by creating a directory. In other words, cgroup can be as hierarchical as the directory structure. Mount the point directory with each subsystem, which is equivalent to the root directory. Each of the different paths represents a different cgroup. In different subsystems, the same path represents the same control group. For example, in the CPU, memory has Foo/bar directory, you can use that/foo/bar to operate the CPU, memory two subsystems. For the same subsystem, each process belongs to and belongs to only one cgroup, which by default is at root cgroup. Hierarchies facilitate the organization and management of control groups, and for some configuration items, hierarchies are also related to resource allocation. Alternatively, you can modify the owner of a directory to allow non-root users to manipulate certain security groups as well.

Cgroups settings and information reads are performed by reading and writing to those files. For example

# echo 2048 >/sys/fs/cgroup/cpu/foo/cpu.shares

Set the Cpu.shares parameter of the/foo control group to 2048.

Previously, some files were shared in each directory. Those are the universal settings. Among them, tasks and cgroups.procs are used to manage the processes in the control group. To add a process to a control group, write the PID to the tasks file in the appropriate directory. Such as

# echo 5678 >/sys/fs/cgroup/cpu/foo/tasks

The 5678 process is added to the/foo control group. So what's the difference between tasks and Cgroups.procs? The above-mentioned management restrictions on "process" are actually not accurate enough. The unit that the system dispatches to the task is the thread. In this case, the thread ID is what you see in Tasks. In Cgroups.procs, the thread group ID, which is the generic process ID, is called. Write a general PID to the tasks, only the corresponding thread of the PID, and other processes generated by it, the thread will belong to this control group, the original other threads will not. Writing Cgroups.procs will add all the current threads. If the write Cgroups.procs is not a thread group ID, but a generic thread ID, it will automatically find the corresponding thread group ID to join in. Once a process has joined a control group, the control group's restrictions will take effect immediately. Want to know which control group a process belongs to, through  cat/proc/<pid>/cgroup   View.

To move the process out of the control group, write the PID to the tasks file of the root cgroup. Since each process belongs to and belongs to only one cgroup, the original relationship is lifted when the new Cgroup is added. To delete a cgroup, you can delete the corresponding directory with rmdir. However, before deleting, you must let the process all exit, the corresponding subsystem resources have been released, otherwise it cannot be deleted.

The cgroups is operated by the file system access method in front. In fact, there is also a set of command-line tools.

lssubsys -amYou can view the mount points of each subsystem,

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/76/wKiom1RR0Xzj-1kYAAFuqOGNLyc755.jpg "title=" 6.png " alt= "Wkiom1rr0xzj-1kyaafuqognlyc755.jpg"/>

There is also a set of commands that start with "CG" that can be used to manage. Where cgexec can be used to run a program directly in the specified control group in some subsystems. such as cgexec -g "cpu,blkio:/foo" bash . Other commands and specific parameters can be viewed by man.

The following is a bash version of Cgexec, which demonstrates the use of cgroups and can be used without determining whether to install command-line tools.

#!/bin/bash# usage: # ./cgexec.sh cpu:g1,memory:g2/g21 sleep 100blkio_dir= "/sys/ Fs/cgroup/blkio "memory_dir="/sys/fs/cgroup/memory "cpuset_dir="/sys/fs/cgroup/cpuset "perf_event_dir="/sys/fs/ Cgroup/perf_event "freezer_dir="/sys/fs/cgroup/freezer "net_cls_dir="/sys/fs/cgroup/net_cls "cpuacct_dir="/sys/fs /cgroup/cpuacct "cpu_dir="/sys/fs/cgroup/cpu "hugetlb_dir="/sys/fs/cgroup/hugetlb "devices_dir="/sys/fs/cgroup/ Devices "groups=" $ "shiftifs=", '  g_arr= ($groups) for g in ${g_arr[@]}; do   Ifs= ': '  g_info= ($g)   if [ ${#g_info [@]} -ne 2 ]; then     echo  "bad arg  $g"  >&2    continue  fi   g_name=${g_info[0]}  g_path=${g_info[1]}  if [  "$g _path"  ==   "${g_path#/}"  ]; then    g_path= "/$g _path"   fi  echo   $g _name  $g _path  var= "${g_name}_dir"   d=${!var}  if [ -z  "$d"  ]; then     echo  "bad cg name  $g _name"  >&2     Continue  fi  path= "${d}${g_path}"   if [ ! -d  "$path" &NBSP;] ; then    echo  "Cg not exists"  >&2     continue  fi  echo  "$$"  > "${path}/tasks" doneexec $*

Cgroups in a lot of things, originally intended to write only one, and later felt or divided into a few say more clearly. After that, I will write something specific to use.

Resources:
Cgroups docs–kernel.org
Resource Management guide–redhat.com
How I used CGroups to Manage System resources–oracle.com


This article from "Insist is Victory" blog, declined reprint!

Linux cgroups Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.