Introduction to Cgroups, the Linux Control Group

2023-12-02 00:48

本文主要是介绍Introduction to Cgroups, the Linux Control Group,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://linuxaria.com/article/introduction-to-cgroups-the-linux-conrol-group?lang=en

Cgroups is present in the official Linux kernel 2.6.24 (late 2007), still he’s not much know or used (at least for what i know).
In this article I’ll give you an overview of this powerful Linux tool to control how much CPU, memory, disk I/O or network I/O each process or user can use in your server.

So in short cgroups it’s a feature to limit, account and isolate resource usage (CPU, memory, disk I/O, etc.) of process groups.

Let’s see how.

Theory

A control group is a collection of processes that are bound by the same criteria. These groups can be hierarchical, where each group inherits limits from its parent group. The kernel provides access to multiple controllers (subsystems) through the cgroups interface.

The infrastructure of Cgroups provides only the features of grouping the tasks, while the various Cgroups subsystem implements the specific control policies for each resource. This framework it’s really powerful and allow you to set rules on resources not only based on users and groups.

For example you could:

To keep a Web server from using all the memory on a system that’s also running a data base.
To allocate system resources among user groups of different priority (e.g. guests 10% of the CPU, users 40% of the CPU, powerusers 50% of the CPU )

But Cgroups can be used also to isolate and give special command to groups of processes, so we can say that there are 2 kind of subsystems

  1. The Isolation and Special Control Subsystems
  2. The Resource Control Subsystems

I’ll focus on the control of resources so this is a small overview of the Isolation and Special Control Subsystems:

  • The CPUset : assigns individual CPUs and memory nodes to cgroups.
  • The Namespace : provides a private view of the system to the processes in a cgroup, and is used primarily for OS-level virtualization.
  • The Freezer : stops all the processes in a cgroup from executing by removing them from the kernel task scheduler.
  • The Device : allows or denies access to devices to processes in a cgroup.
  • The Checkpoint / Restart : Save the state of all processes in a cgroup to a dump file. Restart it later (or just save the state and continue).

The administration of Cgroups is performed by the use of a special virtual file system (sort of procfs or sysfs), trivially called cgroupfs.

Cgropus in practice

Unless you have a server release of some years ago (red hat and centos 5.X for example), you should have a kernel able to use Cgroups, in general check if your kernel is >= of 2.6.24 with the command uname -a this is the requisite to use cgroups.

Than you need the tools in user-space, i’m doing the tests on my Ubuntu, on this idistribution the files to be installed are cgroup-bin and libcgroup1 :

sudo aptitude install cgroup-bin libcgroup1

Once installed you’ll have a new filesystem mounted:

# ls /sys/fs/cgroup
cpu  cpuacct  devices  memory

On other Linux distributions you could need to mount it manually with a command like :

#sudo mount -t cgroup none /mnt
You can also mount only certain Cgroups subsystem by adding the -o cgroup_name option to the mount command.

Back on Ubuntu (and Debian) you have a file that can be used to choose which cgroups subsystem mount, it’s /etc/cgconfig.conf , inside you’ll find the standard configuration:

注:cgconfig.conf 是cgconfig的配置文件,cgconfig在/etc/init.d/下面,是一个服务,可以通过service cgconfig start 来启动,ubuntu下可以使用sysv-rc-conf来配置服务,fedora下面可以用chkconf命令来配置。

mount {cpu = /sys/fs/cgroup/cpu;cpuacct = /sys/fs/cgroup/cpuacct;devices = /sys/fs/cgroup/devices;memory = /sys/fs/cgroup/memory;
}

This basically tell which subsystem use and where to mount them.
We have already saw the devices subsystem the others in the example are:

cpu : this subsystem uses the scheduler to provide cgroup processes access to the CPU.
cpuacct : this subsystem generates automatic reports on CPU resources used by processes in a cgroup.
memory : this subsystem sets limits on memory use by processes in a cgroup, and generates automatic reports on memory resources used by those processes.

To list which types of Cgroups are known use the following command:

#cat /proc/cgroups #subsys_name  hierarchy       num_cgroups     enabled
cpuset  0  1  1
ns      0  1  1
cpu     1  1  1
cpuacct 2  1  1
memory  4  1  1
devices 3  1  1
freezer 0  1  1
net_cls 0  1  1
blkio   0  1  1

Example : Create 2 cgroups

So far we have saw a lot of information, it’s time to see a practical example.
We’ll create 2 control groups called Browsers and Multimedia and we’ll set cgropus so that Browsers can use at max half the shares of CPU used by Multimedia. This is done because we want to ensure that always the multimedia respond within a certain time, to do not miss a frame or hear the annoying “jumps” even when the rest of the system is overloaded.

To do it we have 2 ways, working on the filesystem and using some special commands from the command line.

First method it’s to use the meta-filesystem of cgroups.

The first thing to do it’s create the 2 cgroups, that are nothing more than a directory on our filesystem.
We want to work with Cpu subsystem, so we’ll create the 2 directory in that path:

># cd sys/fs/cgroup/cpu
># mkdir Browsers
># mkdir Multimedia

At this point we have to tell the system that the Cgroups Multimedia should have twice the weight than the Browsers, we do this by creatings within the group a file named cpu.shares . that contains only a value, and giving a value to Multimedia that is twice that of Browsers:

$ echo 2048 >  /sys/fs/cgroup/cpu/Multimedia/cpu.shares
$ echo 1024 >  /sys/fs/cgroup/cpu/Browsers/cpu.shares

We are done, well almost.
Now we must say which processes belong to the cgroup Multimedia and which one to the cgroup Browsers.
To move a processes into a cgroup his PID must be listed in the file /path_to_cgroup/tasks, so we could simply do something like this:

$ firefox &
$ echo $! >  /sys/fs/cgroup/cpu/Browsers/tasks$ mplayer music.mp3 &
$ echo $! >  /sys/fs/cgroup/cpu/Multimedia/tasks

And now we are really done.

Second method it’s to use a set of commands

Creations of the 2 cgroups is done with the command cgcreate
Syntax is

cgcreate -t uid:gid -a uid:gid -g subsystems:path

So to create the same 2 cgroups we have to give the commands:

cgcreate -g cpu:/Browsers
cgcreate -g cpu:/Multimedia

Now we must give a weight to MultiMedia that is the double of the value of Browsers, to do it we use the command cgget
The syntax for cgset is: cgset -r parameter=value path_to_cgroup, so in our example we’ll give:

cgset -r cpu.shares=1024 Browsers
cgset -r cpu.shares=2048 Multimedia

To move a process into a cgroup you can use the command cgclassify
The syntax for cgclassify is: cgclassify -g subsystems:path_to_cgroup pidlist

So if our browser has pid 1234 and our multimedia application has pid 9876 we should use these commands:

cgclassify -g cpu:Browsers 1234
cgclassify -g cpu:Multimedia 9876

And that’s all, we have successfully created 2 cgroups where, for the cpu, one cgroups use the double of shares of the other.
We could use some similar rules also for memory (so the web server don’t eat all the memory of our database), network or disk I/O.

Naturally you can also mix the various cgroup rules so a process has limitations on Cpu and Memory.

The cgred Daemon

Cgred is a daemon that moves tasks into cgroups according to parameters set in the /etc/cgrules.conf file. Entries in the /etc/cgrules.conf file can take one of the two forms:

  • user hierarchies control_group
  • user:command hierarchies control_group
For example:
maria                      devices         /usergroup/staff
This entry specifies that any processes that belong to the user named maria access the devices subsystem according to the parameters specified in the /usergroup/staff cgroup. To associate particular commands with particular cgroups, add the command parameter, as follows:
maria:ftp          devices         /usergroup/staff/ftp
The entry now specifies that when the user named maria uses the ftp command, the process is automatically moved to the /usergroup/staff/ftp cgroup in the hierarchy that contains the devices subsystem.

 

Conclusions

This was just a small example of the many uses that you can have for cgroups.
It’s easiest and most powerful way to limit resources on linux systems.

References

Effective Linux Resource Management, cgroups on Suse
Resource Management on Red Hta Enterprise 6
Cgroups in Debian Squeeze
Linux kernel hacking: contenitori di processi/1

这篇关于Introduction to Cgroups, the Linux Control Group的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/443339

相关文章

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用

Linux Mint Xia 22.1重磅发布: 重要更新一览

《LinuxMintXia22.1重磅发布:重要更新一览》Beta版LinuxMint“Xia”22.1发布,新版本基于Ubuntu24.04,内核版本为Linux6.8,这... linux Mint 22.1「Xia」正式发布啦!这次更新带来了诸多优化和改进,进一步巩固了 Mint 在 Linux 桌面

LinuxMint怎么安装? Linux Mint22下载安装图文教程

《LinuxMint怎么安装?LinuxMint22下载安装图文教程》LinuxMint22发布以后,有很多新功能,很多朋友想要下载并安装,该怎么操作呢?下面我们就来看看详细安装指南... linux Mint 是一款基于 Ubuntu 的流行发行版,凭借其现代、精致、易于使用的特性,深受小伙伴们所喜爱。对

什么是 Linux Mint? 适合初学者体验的桌面操作系统

《什么是LinuxMint?适合初学者体验的桌面操作系统》今天带你全面了解LinuxMint,包括它的历史、功能、版本以及独特亮点,话不多说,马上开始吧... linux Mint 是一款基于 Ubuntu 和 Debian 的知名发行版,它的用户体验非常友好,深受广大 Linux 爱好者和日常用户的青睐,

Linux(Centos7)安装Mysql/Redis/MinIO方式

《Linux(Centos7)安装Mysql/Redis/MinIO方式》文章总结:介绍了如何安装MySQL和Redis,以及如何配置它们为开机自启,还详细讲解了如何安装MinIO,包括配置Syste... 目录安装mysql安装Redis安装MinIO总结安装Mysql安装Redis搜索Red

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

Linux磁盘分区、格式化和挂载方式

《Linux磁盘分区、格式化和挂载方式》本文详细介绍了Linux系统中磁盘分区、格式化和挂载的基本操作步骤和命令,包括MBR和GPT分区表的区别、fdisk和gdisk命令的使用、常见的文件系统格式以... 目录一、磁盘分区表分类二、fdisk命令创建分区1、交互式的命令2、分区主分区3、创建扩展分区,然后

Linux中chmod权限设置方式

《Linux中chmod权限设置方式》本文介绍了Linux系统中文件和目录权限的设置方法,包括chmod、chown和chgrp命令的使用,以及权限模式和符号模式的详细说明,通过这些命令,用户可以灵活... 目录设置基本权限命令:chmod1、权限介绍2、chmod命令常见用法和示例3、文件权限详解4、ch