- Steps to allocate resource using cgroups and slice in Linux with examples
- Few pointers on cgroups
- Understanding slice
- How resources are allocated in the slice?
- What does these values mean?
- What if I create multiple services in system.slice?
- How to create custom slice?
- Test resource allocation using examples
- Monitor resource usage per slice
- Related Posts
- 4 thoughts on “Steps to allocate resource using cgroups and slice in Linux with examples”
- Всё, что Вы хотели знать о слайсах
- How to slice an array in Bash
- 4 Answers 4
- Slice linux ��� ���
- Automatic DependenciesВ¶
- Implicit DependenciesВ¶
- Default DependenciesВ¶
Steps to allocate resource using cgroups and slice in Linux with examples
Table of Contents
In my last article I had shared a step by step guide to change tmpfs partition size for /dev/shm, /run and others using fstab and systemd on Linux. Now to start with this article, cgroup or Control Group provides resource management and resource accounting for groups of processes. Cgroups kernel implementation is mostly in non-critical paths in terms of performance. The cgroups subsystem implements a new Virtual File System (VFS) type named “ cgroups ”. All cgroups actions are done by filesystem actions, like creating cgroups directories in a cgroup filesystem, writing or reading to entries in these directories, mounting cgroup filesystems, etc.
Few pointers on cgroups
- cgroup is now integrated with systemd in recent Linux versions since kernel 2.6.24.
- Control group place resources in controllers that represent the type of resource i.e you can define groups of available resources to make sure your application like webserver has guaranteed claim on resources
- In order to do so, cgroup works with default controller which are cpu, memory and blkio
- These controllers are divided into tree structure where different weight or limits are applied to each branch
- Each of these branches is a cgroup
- One or more processes are assigned to a cgroup
- cgroups can be applied from the command line or from systemd
- Manual creation happens through the cgconfig service and the cgred process
- In all cases, cgroup settings are written to /sys/fs/cgroups
These are the different controllers which are created by the kernel itself. Each of these controllers have their own tunables for example
Understanding slice
By default, systemd automatically creates a hierarchy of slice, scope and service units to provide a unified structure for the cgroup tree. Services, scopes, and slices are created manually by the system administrator or dynamically by programs. By default, the operating system defines a number of built-in services that are necessary to run the system. Also, there are four slices created by default:
- -.slice — the root slice;
- system.slice — the default place for all system services;
- user.slice — the default place for all user sessions;
- machine.slice — the default place for all virtual machines and Linux containers.
How resources are allocated in the slice?
Let us take an example of CPUShares .
Now assuming we assign below value of CPUShares to below slice
What does these values mean?
They actually individually mean nothing but instead these values are used as a comparison factor between all the slices. Here if we assume that if total CPU availability is 100% then user.slice will get
7%, system.slice will get 4 times the allocation of user.slice i.e.
30% and machine.slice will get twice the allocation of system.slice which will be around
60% of the available CPU resource.
What if I create multiple services in system.slice?
This is a valid question, assuming I created three service inside system.slice with CPUShares value as defined below
If we sum it up the total becomes larger than 1024 which is actually assigned to system.slice in the above example. Well again, these values are only meant of comparison and in real mean nothing. Here service1 will get the maximum amount of available resource i.e. if 100% of resource is available for system.slice then the service1 will get
56%, service2 will get
14% and service3 will get
28% of the available CPU
This is how this cgroup settings in the big level relates between different slices and between different slices relates to different services.
How to create custom slice?
- Every name of a slice unit corresponds to the path to a location in the hierarchy.
- Child slice will inherit the settings from parent slice.
- The dash («-«) character acts as a separator of the path components.
For example, if the name of a slice looks as follows:
It means that a slice called parent-name.slice is a subslice of the parent.slice . This slice can have its own subslice named parent-name-name2.slice , and so on..
Test resource allocation using examples
Now we will create two systemd unit files namely stress1.service and stress2.service. These service scripts will utilise all the CPU on my system
Using these systemd unit files I will put some CPU load using system.slice
This is my second unit file with same content to stress the CPU
Start these services
Now validate the CPU usage using top command
As you see I have two processes which are trying to utilise available CPU, now since both are in the system slice, the process equally gets the available resource. So both the process gets
50% of the CPU as expected.
Now let us try to add a new process on the user.slice using a while command in the the background
Next check the CPU usage, and as expected now the available CPU is equally divided into 3 processes. there is no distinction between user.slice and system.slice
Now let us enable the slicing by enabling below values in » /etc/systemd/system.conf «
Reboot the node to activate the changes
Once the system is back UP, next we will again start our stress1 and stress2 service and a while loop using bash shell
Now validate the CPU usage using top command
As you see now our slicing has become effective. The user slice is now able to claim 50% of the CPU while the system slice is divided at
25% for both the stress service.
Let us now further reserve the CPU using CPUShares for our systemd unit files.
Now in the above unit files I have given priority to stress2.service so it will be allowed double of the resource allocated to stress1.service.
Next restart the services
Validate the top output
So as expected, out of the available 50% CPU resources for system.slice, stress2 gets double the CPU allocated to stress1 service.
Monitor resource usage per slice
systemd-cgtop shows the top control groups of the local Linux control group hierarchy, ordered by their CPU, memory, or disk I/O load. The display is refreshed in regular intervals (by default every 1s), similar in style to top command. Resource usage is only accounted for control groups in the relevant hierarchy, i.e. CPU usage is only accounted for control groups in the » cpuacct » hierarchy, memory usage only for those in » memory » and disk I/O usage for those in » blkio «.
Lastly I hope this article on understanding cgroups and slices with examples on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
4 thoughts on “Steps to allocate resource using cgroups and slice in Linux with examples”
I am using rhel8 to test this out. And I did not have to start default accounting in /etc/systemd/system.conf.
I have two CPUs.
1st case: only started stress1 and stress2 and they were using 100% both CPUs.
2nd case: along with above, now, I started while true loop on my logged in session. This process landed in user.slice and observed that now, this take 1 100% cpu and other two processes take 50% each. i.e. system divided equally CPUs. 1 cpu for user.slice and 1 cpu for system.slice.
3rd case: I added CPUShares accordingly into stress1 and stress2, Now, I see that, user.slice at 100% utilized by while true loop and stress2 take about 66% and stress1 takes about 33% of other CPU.
Thanks Manish for sharing your observation. Were you using the same values as provided in the article?
I will also retest this using RHEL 8 and update here if I see any discrepancies or if this article needs any update
Hi
I really enjoyed this article.
I need to learn more about cgroups.
I have 32 cpu’s and I need to allow each user the use of a maximum of
of only 2 cpu’s.
Can you give me some guidance on this please?
thx
Josh
This is an interesting question, give me some time and I will find this for you and if possible I may also write an article on this topic
With RHEL 6 I know this was possible using cgconfig and creating user/group based templates but this is deprecated with RHEL 7 as we have systemd now where can can create crgoups based on slice, user or user and with RHEL 8 we have cgroupV2 which I have not analysed properly
So let me see what is the best possible option to achieve this
Источник
Всё, что Вы хотели знать о слайсах
Маленькое вступление. Уверен, что каждый, кто использовал питон некоторое время, полюбил выражения в прямоугольных скобочках. В этой статье я хочу от «а» до «я» рассказать о срезах. Для начала немного о терминологии: в английском языке их называют «slice». Я буду называть их то «слайсами», то «срезами», как в моем понимании этого слова. Будем все учиться на примерах. Для меня, такой метод был бы самым удобным, быстрым и простым.
Для начала, самое распространенное применение. Создания копии последовательности или ее части.
Рассмотрим срез как часть последовательности. Например, несколько срезов со списка:
Это ещё не все,
Далеко не все знают, но могут быть слайсы с тремя параметрами:
Все эти действия можно проворачивать со строками, кортежами и списками.
Совсем забыл, спасибо xeningem:
Но и это ещё не все, есть несколько действий со срезами, которые можно делать только со списками (ну, почти). Дело в том, что они единственные из базовых последовательностей, которые могут изменяться, и, для которых, имеет значение порядок (нельзя делать срезы из словарей и множеств/наборов). Дальше пойдет разговор о срезах, которые изменяют последовательность.
Слайсы можно удалять, например:
Ещё можно вставлять элементы:
В варианте замены:
Ну, или вариант вставки попроще:
Если мы хотим создать класс, с которого можно снимать срезы? Проще некуда, для этого есть два пути:
Неправильный:
1) Переопределить фунции __getslice__, __delslice__ и __setslice__. Но это устаревший метод (в 2.0 помечен как deprecated)
И правильный:
2) Переопределить __getitem__, __setitem__ и __delitem__.
С первого взгляда все кажется предельно простым, но если присмотреться, то __getitem__(self, key) – получает только один параметр, key, а у среза у нас может быть целых 3 числа… Все предельно просто: в случае, когда кто-то пытается срезать кусочек от нашего объекта, значением key функция получит объект типа slice:
Конечно, пример очень символический, но понять можно: у объекта класса slice есть три свойства: start, stop и step, соответствуют числам из скобок среза. Нужно быть внимательным, если число пропущена, то значение будет None, например [::] будет slice(None, None, None) а [:-3] будет slice(None, -3, None).
Замену/вставку и удаление срезов делаем по аналогии.
Как упражнение, можете попробовать перегрузить словарь, чтобы с него можно было делать срезы. В питоне3 это будет начинаться как class SliceableDict(dict):
Ну, вроде все, что есть о срезах.
Если что пропустил с удовольствием выучу и допишу.
Upd1: накопил 5 кармы, перенес в питоний блог. Спасибо.
Upd2: Спасибо за комментарии, исправился, дополнил.
Источник
How to slice an array in Bash
Looking the «Array» section in the bash(1) man page, I didn’t find a way to slice an array.
So I came up with this overly complicated function:
Is there a better way to do this?
4 Answers 4
See the Parameter Expansion section in the Bash man page. A[@] returns the contents of the array, :1:2 takes a slice of length 2, starting at index 1.
Note that the fact that a b c is one array element (and that it contains an extra space) is preserved.
There is also a convenient shortcut to get all elements of the array starting with specified index. For example «$» would be the «tail» of the array, that is the array without its first element.
Array slicing like in Python (From the rebash library):
At the risk of beating a dead horse, I was inspired by @jandob’s answer and made this version that
- Is simpler (doesn’t have so much shift logic or rewriting of variables as often).
- Respects quoted strings without dealing with IFS ( -r mode only).
- Allows the user to specify [start, end) slicing or [start, length] slicing via -l flag.
- Allows you to echo the resulting array (default behavior), or «return» it into a new array for use in the calling parent (via -r slicedArray ).
Источник
Slice linux ��� ���
A unit configuration file whose name ends in » .slice » encodes information about a slice unit. A slice unit is a concept for hierarchically managing resources of a group of processes. This management is performed by creating a node in the Linux Control Group (cgroup) tree. Units that manage processes (primarily scope and service units) may be assigned to a specific slice. For each slice, certain resource limits may be set that apply to all processes of all units contained in that slice. Slices are organized hierarchically in a tree. The name of the slice encodes the location in the tree. The name consists of a dash-separated series of names, which describes the path to the slice from the root slice. The root slice is named -.slice . Example: foo-bar.slice is a slice that is located within foo.slice , which in turn is located in the root slice -.slice .
Note that slice units cannot be templated, nor is possible to add multiple names to a slice unit by creating additional symlinks to its unit file.
By default, service and scope units are placed in system.slice , virtual machines and containers registered with systemd-machined (8) are found in machine.slice , and user sessions handled by systemd-logind (8) in user.slice . See systemd.special (7) for more information.
See systemd.unit (5) for the common options of all unit configuration files. The common configuration items are configured in the generic [Unit] and [Install] sections. The slice specific configuration options are configured in the [Slice] section. Currently, only generic resource control settings as described in systemd.resource-control (5) are allowed.
See the New Control Group Interfaces for an introduction on how to make use of slice units from programs.
Automatic DependenciesВ¶
Implicit DependenciesВ¶
The following dependencies are implicitly added:
Slice units automatically gain dependencies of type After= and Requires= on their immediate parent slice unit.
Default DependenciesВ¶
The following dependencies are added unless DefaultDependencies=no is set:
Источник