# Linux资源管理

## 什么是Cgroups

`control groups`，控制组，也称为`cgroups`，是Linux内核通过进程的层次化组织用于控制资源分配的功能 - 可以管控CPU时间片、系统内存、网路带宽，或者这些资源的组合。

通过使用cgroups，系统管理员可以非常好的得到资源分配，权重，拒绝和管理以及监控系统资源。硬件资源也可以分配和应用程序和用户，有效增加利用率。

控制组提供了层次化组织和标记进程，并且将资源分配给进程的方式。传统上，所有的进程能够获得系统相似的资源，系统管理员可以调整进程的`niceness`值（优先值）。使用这种接近的方式，应用程序启动大量的进程就能够获得比较少进程的应用程序更多的资源，而不管较少进程的应用程序的重要程度。

通过绑定系统的cgroup层次化结构和systemd单元树，Red Hat Enterprise Linux 7将资源管理设置从`进程级别`改到`应用程序级别`。这样就可以通过`systemctl`命令来管理系统资源，或者通过更改systemd单元文件。

在Red Hat Enterprise Linux的早期版本（RHEL 5/6），系统管理员使用`libcgroup`软件包中的`cgconfig`命令来构建自定的cgroup层次结构。当前(RHEL 7)已不建议使用`libcgroup`软件包，因为这个工具很容易导致和默认的cgroup层次冲突的配置。然而，这个`libcgroup`软件包依然为特定环境所保留，例如某些情况下不安装`systemd`，尤其是是使用`net-prio`子系统的环境。

> 本章节文档将同时兼顾Red Hat Enterpirse 7 和 6 系列，并阐述两者操作方法的区别。

**`注意：RHEL 6 和 RHEL 7 的cgroup概念及使用方法有较大差异，请注意区别！`**

* [RHEL 6 Cgroups介绍](/cloud-atlas-draft/os/linux/kernel/cgroups/rhel6.md)
* [RHEL 7 Cgroups介绍](/cloud-atlas-draft/os/linux/kernel/cgroups/rhel7.md)

> 2013年8月的[All About the Linux Kernel: Cgroup’s Redesign](https://www.linux.com/news/featured-blogs/200-libby-clark/733595-all-about-the-linux-kernel-cgroups-redesign)一文介绍了结合Systemd的全新设计开发的[内核cgroup子系统设计cgroup-v2](https://www.kernel.org/doc/Documentation/cgroup-v2.txt)
>
> [cgroup-v1文档](https://www.kernel.org/doc/Documentation/cgroup-v1/)

## 参考

* [Resource Management Guide: Managing system resources on Red Hat Enterprise Linux 7](https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Resource_Management_Guide/index.html)
* [All About the Linux Kernel: Cgroup’s Redesign](https://www.linux.com/news/featured-blogs/200-libby-clark/733595-all-about-the-linux-kernel-cgroups-redesign)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/os/linux/kernel/cgroups.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
