In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use cgroups in linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Starting with version 2.6.24, the linux kernel provides a feature called cgroups (Control Group). Cgroups is the abbreviation of control groups, which is used to limit, count and isolate the resources occupied by a group of processes. It is also one of the foundations of the current lightweight virtualization technology lxc (linux container). Each set of processes is a control group, that is, a cgroup. Cgroups is divided into several subsystems, each subsystem represents a facility or resource controller, which is used to schedule the use of a certain type of resources, such as cpu clock, memory, block devices and so on. In implementation, cgroups does not add new system calls, but behaves as a cgroup file system that can mount one or more subsystems to a directory. Such as
The code is as follows:
Mount-t cgroup-o cpu cpu / sys/fs/cgroup/cpu
The cpu subsystem is mounted on / sys/fs/cgroup/cpu. It is also possible to mount multiple subsystems in one directory, or even all to one directory, but I think it is more flexible to mount each subsystem in a different directory. You can see the currently mounted control group with mount | awk'$5groups = "cgroup" {print $0}'. You can see the status of all current control groups with cat / proc/cgroups. The following script allows you to mount all subsystems to their respective directories.
The code is as follows:
#! / bin/bash
Cgroot= "${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
See what's in those directories, such as ls / sys/fs/cgroup/cpu.
The code is as follows:
Cgroup.event_control cpu.cfs_period_us cpu.rt_period_us cpu.shares notify_on_release tasks
Cgroup.procs cpu.cfs_quota_us cpu.rt_runtime_us cpu.stat release_agent
Among them "cpu." It starts with something unique to this subsystem. The other ones are in the corresponding directory of each subsystem. These files are used to read resource usage information and impose resource restrictions. To create a control group, create a directory in the desired subsystem. For example, mkdir / sys/fs/cgroup/cpu/foo creates a control group for / foo. The same set of files will appear in the new directory. In this directory, you can also continue to create a cgroup by creating a directory. In other words, cgroup can be as hierarchical as the directory structure. Mounting a dot directory with each subsystem is equivalent to the root directory. Each different path represents a different cgroup. In different subsystems, the same path represents the same control group. For example, if there is a foo/bar directory in both cpu and memory, you can use that / foo/bar to operate the cpu and memory subsystems. For the same subsystem, each process belongs to and only belongs to one cgroup, and the default is in the root cgroup. The hierarchy facilitates the organization and management of control groups, and for some configuration items, it is also related to resource allocation. Alternatively, you can modify the owner of a directory so that non-root users can also manipulate certain security groups.
The setting and information reading of cgroups is done by reading and writing to those files. For example
The code is as follows:
# echo 2048 > / sys/fs/cgroup/cpu/foo/cpu.shares
Set the cpu.shares parameter of the control group / foo to 2048.
As mentioned earlier, some files are shared in every directory. Those are the general settings. Where tasks and cgroups.procs are used to manage the processes in the control group. To add a process to a control group, write pid to the tasks file in the corresponding directory. Such as
The code is as follows:
# 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 administrative restrictions on "processes" mentioned above are actually not accurate enough. The task scheduling unit of the system is the thread. Here, what you see in tasks is the thread id. In cgroups.procs is the thread group id, which is commonly known as process id. If a general pid is written to the tasks, only the corresponding thread of this pid and other processes and threads generated by it will belong to this control group, but the original other threads will not. Writing to cgroups.procs adds all current threads to it. If a thread group id is not written to the cgroups.procs, but a normal thread id is written, the corresponding thread group id will be automatically found to join in. After the process joins a control group, the restrictions corresponding to the control group will take effect immediately. If you want to know which control groups a process belongs to, you can check it through cat / proc//cgroup.
To move the process out of the control group, write pid to the tasks file of the root cgroup. Because each process belongs to and belongs to only one cgroup, the original relationship is broken after adding to the new cgroup. To delete a cgroup, delete the corresponding directory with rmdir. However, before deleting, you must let all the processes exit, and the resources of the corresponding subsystem have been released, otherwise it cannot be deleted.
All the previous operations on cgroups are done through file system access. In fact, there is also a set of command line tools.
Lssubsys-am can view the mount points of each subsystem, and a set of commands starting with "cg" can be used to manage. Cgexec can be used to run a program directly in a specified control group in some subsystems. Such as cgexec-g "cpu,blkio:/foo" bash. Other commands and specific parameters can be viewed through man.
The following is a bash version of cgexec that demonstrates the use of cgroups and can also be used when you are not sure whether to install command-line tools.
The code is as follows:
#! / bin/bash
# usage:
#. / cgexec.sh cpu:g1,memory:g2/g21 sleep 100
Blkio_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= "$1"
Shift
IFS=',' gaffarr = ($groups)
For g in ${g_arr [@]}; do
IFS=':' gambiinfo = ($g)
If [${# g_info [@]}-ne 2]; then
Echo "bad arg $g" > & 2
Continue
Fi
Groupname ${g_info [0]}
Grouppathways ${g_info [1]}
If ["$g_path" = = "${g_path#/}"]; then
Gaming path = "/ $g_path"
Fi
Echo $g_name $g_path
Var= "${g_name} _ dir"
Dumped ${! var}
If [- z "$d"]; then
Echo "bad cg name $g_name" > & 2
Continue
Fi
Path= "${d} ${g_path}"
If [!-d "$path"]; then
Echo "cg not exists" > & 2
Continue
Fi
Echo "$$" > "${path} / tasks"
Done
Exec $*
Thank you for reading this article carefully. I hope the article "how to use cgroups in linux" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.