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中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变

Linux系统中的firewall-offline-cmd详解(收藏版)

《Linux系统中的firewall-offline-cmd详解(收藏版)》firewall-offline-cmd是firewalld的一个命令行工具,专门设计用于在没有运行firewalld服务的... 目录主要用途基本语法选项1. 状态管理2. 区域管理3. 服务管理4. 端口管理5. ICMP 阻断

Linux实现线程同步的多种方式汇总

《Linux实现线程同步的多种方式汇总》本文详细介绍了Linux下线程同步的多种方法,包括互斥锁、自旋锁、信号量以及它们的使用示例,通过这些同步机制,可以解决线程安全问题,防止资源竞争导致的错误,示例... 目录什么是线程同步?一、互斥锁(单人洗手间规则)适用场景:特点:二、条件变量(咖啡厅取餐系统)工作流