cgroups

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書

cgroups
原作者Paul Menage 與 Rohit Seth
開發者kernel.org (Tejun Heo與其他成員)以及freedesktop.org
首次發佈2007年,​17年前​(2007
程式語言C語言
作業系統Linux
類型行程群組的資源管理功能
許可協定GPLLGPL
網站www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txtwww.freedesktop.org/wiki/Software/systemd/ControlGroupInterface/
Unified hierarchy cgroups 且 systemd.

cgroups,其名稱源自控制群組(英語:control groups)的簡寫,是Linux內核的一個功能,用來限制、控制與分離一個行程群組資源(如CPU、記憶體、磁碟輸入輸出等)[1]

這個專案最早是由Google的工程師(主要是Paul Menage和Rohit Seth)在2006年發起,最早的名稱為行程容器(process containers)[2]。在2007年時,因為在Linux內核中,容器(container)這個名詞有許多不同的意義,為避免混亂,被重新命名為cgroup,並且被合併到2.6.24版的內核中去(該版本於2008年1月發佈)[3]。自那以後,開發人員陸續添加了許多新功能和控制器,例如2014年對kernfs的支援[4]防火牆功能[5]和統一階層[6]。cgroup v2在Linux內核4.5中被合併[7],介面和內部功能發生了重大變化[8]

功能[編輯]

cgroups的一個設計目標是為不同的用例提供統一的介面,從控制單一行程(像nice)到作業系統層虛擬化(像OpenVZLinux-VServerLXC)。cgroups提供:

使用[編輯]

控制組(cgroup)是一組受相同條件約束的行程,並與一組參數或限制相關聯。這些組可以是階層的,意味着每個組都從其父組繼承限制。

內核通過cgroup介面提供對多個控制器(也稱為子系統)的訪問[18];例如,「memory」控制器限制記憶體使用,「cpuacct」記錄CPU使用情況等。

控制組可以通過多種方式使用:

  • 手動讀寫 cgroup 的虛擬檔案系統。
  • 使用 cgcreatecgexeccgclassify (來自 libcgroup )等工具,即時建立和管理群組。
  • 通過「規則引擎守護程式」,可以根據組態自動將特定用戶、組或命令的行程移動到cgroups中。
  • 間接通過使用 cgroups 的其他軟件,如DockerFirejail、LXC[19]libvirtsystemdOpen Grid Scheduler/Grid Engine[20],以及Google已停止開發的lmctfy

Linux 內核文件包含了控制組版本1(cgroup v1)[21] 和版本2(cgroup v2)[22]的一些設置和使用的技術細節。

也可以使用 systemd-cgtop 命令[23]按資源使用情況顯示頂級控制組。

重新設計[編輯]

cgroups的重新設計始於2013年[24],隨後在Linux內核的3.15和3.16版本中引入了額外的改變。[25][26][27]

命名空間隔離[編輯]

雖然不是cgroups工作的技術部分,但Linux內核的一個相關特性是命名空間隔離,它將一組行程分離開來,使它們無法「看到」其他組中的資源。例如,PID命名空間在每個命名空間中提供了一個獨立的行程識別碼列舉。還有可用的掛載、用戶、UTS、網絡和SysV IPC命名空間。

  • PID namespace (PID命名空間) 提供了行程識別碼(PID)、行程列表及其詳細資訊的分離。雖然新的命名空間與其他同級命名空間隔離,但其「父」命名空間中的行程仍然可以看到子命名空間中的所有行程,儘管PID號碼不同。[28]
  • Network namespace (網絡命名空間) 隔離了(物理或虛擬的)網絡介面控制器、iptables防火牆規則、路由表等。網絡命名空間可以使用"veth"虛擬乙太網路裝置連接在一起。
  • "UTS" namespace 允許更改主機名
  • Mount namespace 允許建立不同的檔案系統佈局,或將某些掛載點設置為唯讀。[29]
  • IPC namespace 隔離了命名空間之間的System V行程間通訊
  • User namespace 在命名空間之間隔離用戶ID。[30]
  • Cgroup namespace [31]

命名空間是通過"unshare"命令或系統呼叫建立的,或者作為"clone"系統呼叫中的"new"標誌。[32]

在 cgroups 開發早期,添加了"ns"子系統,以整合命名空間和控制組。如果掛載了"ns"控制組,每個命名空間也會在控制組階層中建立一個新的組。這是一個實驗,後來被認為與cgroups API不太匹配,並從內核中移除。

Linux的命名空間受到了貝爾實驗室九號專案中廣泛使用的更通用的命名空間功能的啟發。[33]

統一階層(Unified hierarchy)[編輯]

Kernfs於2014年3月引入Linux內核,版本為3.14,主要作者是Tejun Heo。[34]一個獨立的kernfs的主要動機之一是cgroups檔案系統。Kernfs基本上是通過將一些sysfs邏輯拆分為一個獨立實體來建立的,從而為其他內核子系統實現自己的虛擬檔案系統提供了便利,包括處理裝置連接和斷開連接、動態建立和刪除以及其他屬性。重設計繼續到Linux內核的3.15版本。

內核記憶體控制組(kmemcg)[編輯]

內核記憶體控制組(kmemcg)已合併到Linux內核主線版本3.8(2013年2月18日,​11年前​(2013-02-18)。[35][36][37]kmemcg控制器可以限制內核用於管理其自身內部行程所使用的記憶體量。

cgroup對OOM killer的感知[編輯]

Linux內核4.19(2018年10月)引入了對OOM killer英語OOM killer實現的cgroup感知,這增加了以cgroup為單位殺死行程的能力,從而保證了工作負載的完整性。[38]

應用[編輯]

各種專案都使用cgroups作為基礎,包括CoreOSDocker(2013年)、HadoopJelasticKubernetes[39]lmctfy(Let Me Contain That For You)、LXC(LinuX Containers)、systemdMesos和Mesosphere[40],以及HTCondor。主要的Linux發行版也採用了它,例如Red Hat Enterprise Linux(RHEL)6.0在2010年11月採用[41],比主線Linux內核晚三年。

2019年10月29日,Fedora專案將Fedora 31預設修改為使用CgroupsV2。[42]

參見[編輯]

參照[編輯]

  1. ^ Control Group v2 — The Linux Kernel documentation. www.kernel.org. [2024-02-29]. 
  2. ^ Jonathan Corbet. Process containers. LWN.net. 2007-05-29 [2012-06-16]. (原始內容存檔於2017-06-12). 
  3. ^ Jonathan Corbet. Notes from a container. LWN.net. 2007-10-29 [2012-06-16]. (原始內容存檔於2012-06-22). 
  4. ^ LKML: Tejun Heo: [PATCHSET cgroup/for-3.15] cgroup: convert to kernfs. lkml.org. [2024-02-29]. 
  5. ^ [PATCH 12/12] netfilter: x_tables: lightweight process control group matching — Linux Network Development. web.archive.org. 2014-04-24 [2024-02-29]. 
  6. ^ LKML: Tejun Heo: [PATCHSET cgroup/for-3.15] cgroup: prepare for the default unified hierarchy. lkml.org. [2024-02-29]. 
  7. ^ kernel/git/torvalds/linux.git - Linux kernel source tree. git.kernel.org. [2024-02-29]. 
  8. ^ Control Group v2 — The Linux Kernel documentation. www.kernel.org. [2024-02-29]. 
  9. ^ Jonathan Corbet. Controlling memory use in containers. LWN. 2007-07-31 [2012-06-16]. (原始內容存檔於2018-01-29). 
  10. ^ Balbir Singh, Vaidynathan Srinivasan. Containers: Challenges with the memory resource controller and its performance (PDF). Ottawa Linux Symposium. July 2007 [2012-06-16]. (原始內容存檔 (PDF)於2012-05-23). 
  11. ^ Using cgroups to limit I/O · André Carvalho. andrestc.com. [2024-02-29]. 
  12. ^ The container throttling problem. danluu.com. [2024-02-29]. 
  13. ^ Derr, Simon (2004). "CPUSETS". 
  14. ^ setrlimit(2) — Arch manual pages. man.archlinux.org. [2024-02-29]. 
  15. ^ Jonathan Corbet. Kernel space: Fair user scheduling for Linux. Network World. 2007-10-23 [2012-06-16]. (原始內容存檔於2009-05-24). 
  16. ^ Kamkamezawa Hiroyu. Cgroup and Memory Resource Controller (PDF). Japan Linux Symposium. 2008-11-19 [2012-06-16]. (原始內容 (PDF presentation slides)存檔於2011-07-22). 
  17. ^ 17.0 17.1 Dave Hansen. Resource Management (PDF). Linux Foundation. [2012-06-16]. (原始內容 (PDF presentation slides)存檔於2011-10-09). 
  18. ^ Notes from a container [LWN.net]. lwn.net. [2024-02-29]. 
  19. ^ IBM Developer. developer.ibm.com. [2024-02-29]. 
  20. ^ Rayson. Grid Engine cgroups Integration. [2024-02-29] (英語). 
  21. ^ v1. 
  22. ^ linux/Documentation/admin-guide/cgroup-v2.rst at master · torvalds/linux. GitHub. [2024-02-29] (英語). 
  23. ^ systemd-cgtop. www.freedesktop.org. [2024-02-29]. 
  24. ^ All About the Linux Kernel: Cgroup's Redesign. Linux.com. 2013-08-15 [2014-05-19]. 
  25. ^ The unified control group hierarchy in 3.16. LWN.net. 2014-06-11. 
  26. ^ Pull cgroup updates for 3.15 from Tejun Heo. kernel.org. 2014-04-03. 
  27. ^ Pull cgroup updates for 3.16 from Tejun Heo. kernel.org. 2014-06-09. 
  28. ^ Pavel Emelyanov, Kir Kolyshkin. PID namespaces in the 2.6.24 kernel. LWN.net. 19 November 2007. 
  29. ^ Serge E. Hallyn, Ram Pai. Applying mount namespaces. IBM developerWorks. 17 September 2007. 
  30. ^ Michael Kerrisk. Namespaces in operation, part 5: User namespaces. lwn.net Linux Info from the Source. 27 February 2013. 
  31. ^ LKML: Linus Torvalds: Linux 4.6-rc1. 
  32. ^ Janak Desai. Linux kernel documentation on unshare. 11 January 2006. 
  33. ^ The Use of Name Spaces in Plan 9. 1992 [15 February 2015]. (原始內容存檔於6 September 2014). 
  34. ^ kernfs, sysfs, driver-core: implement synchronous self-removal. LWN.net. 2014-02-03 [2014-04-07]. 
  35. ^ memcg: kmem controller infrastructure. kernel.org source code. 2012-12-18. 
  36. ^ memcg: kmem accounting basic infrastructure. kernel.org source code. 2012-12-18. 
  37. ^ memcg: add documentation about the kmem controller. kernel.org. 2012-12-18. 
  38. ^ Linux_4.19 - Linux Kernel Newbies. 
  39. ^ Mesosphere to Bring Google’s Kubernetes to Mesos - Mesosphere. web.archive.org. 2015-09-06 [2024-02-29]. 
  40. ^ Mesosphere to Bring Google’s Kubernetes to Mesos - Mesosphere. web.archive.org. 2015-09-06 [2024-02-29]. 
  41. ^ "6.0 Release Notes" (PDF). redhat.com. 
  42. ^ 1732114 – Modify Fedora 31 to use CgroupsV2 by default. bugzilla.redhat.com. [2024-02-29]. 

外部連結[編輯]