linux资源隔离是哪些,【转载】Linux cgroup资源隔离各个击破之
Linux cgroup 有兩個子系統支持CPU隔離。
一個是cpu子系統,另一個是cpuset子系統。
cpu子系統根據進程設置的調度屬性,選擇對應的CPU資源調度方法
.1. 完全公平調度 Completely Fair Scheduler (CFS)
原理詳見
CFS用于處理以下幾種進程調度策略
SCHED_OTHER
SCHED_BATCH
SCHED_IDLE
.2. 實時調度 Real-Time scheduler (RT)
原理詳見
RT用于處理以下幾種進程調度策略
SCHED_FIFO
SCHED_RR
CFS調度方法
CFS調度針對屬性為SCHED_OTHER, SCHED_BATCH, SCHED_IDLE的進程。
限制手段分為兩方面,
.1. 限制資源組的CPU使用硬上限,
.2. 以及資源組的CPU使用權重。
CFS調度資源組內的任務在CPU空閑時超權重使用CPU資源,但是不能超過硬上限。
例子
groupA : cpu.shares=250
groupB : cpu.shares=750
CFS保證了groupA的進程能使用25%的CPU資源,groupB的進程能使用75%的CPU資源。
如果CPU較空閑,groupA的進程能使用超過25%的CPU資源。
如果又加了個groupC進來,并且配置了cpu.shares = 250,那么CPU資源將在三個GROUP之間重分配。
groupA : groupB : groupC = 25:75:25
注意 cpu.shares 務必 >= 2
cpu.shares只限制了使用下限,如果同時還需要設置CPU使用上限,可以通過以下兩個參數來設置。
cpu.cfs_period_us = 統計CPU使用時間的周期
cpu.cfs_quota_us = 周期內允許占用的CPU時間(指單核的時間, 多核則需要在設置時累加)
如果分組中的任務在周期cpu.cfs_period_us內使用的CPU時間超過了cpu.cfs_quota_us,則進入抑制狀態,并且需要等下一個周期才能繼續使用CPU。
例子,周期為1秒,允許使用4秒CPU時間。(假設CPU>=4核心,表示這個組在一個使用周期(1s)內可以跑滿4核資源)
cpu.cfs_period_us = 1000000
cpu.cfs_quota_us = 4000000
RT(real-time)調度方法
RT調度針對屬性為SCHED_FIFO, SCHED_RR的進程。
與cfs的quota和period類似,限制了CPU使用的上限。但是rt調度只限制real-time tasks的CPU使用。
The RT scheduler works in a similar way to the ceiling enforcement control of the CFS (for more information, refer to Section 3.2.1, “CFS Tunable Parameters”) but limits CPU access to real-time tasks only.
cpu.rt_period_us = 統計CPU使用時間的周期
cpu.rt_runtime_us = 周期內允許任務使用單個CPU核的時間,如果系統中有多個核,則可以使用核倍數的時間 (計算方法與cfs不一樣,需要注意)
例子
As mentioned above, the access times are multiplied by the number of logical CPUs.
For example, setting cpu.rt_runtime_us to 200000 and cpu.rt_period_us to 1000000 translates to the task being able to
access a single CPU for 0.4 seconds out of every 1 second on systems with two CPUs (0.2 x 2),
or 0.8 seconds on systems with four CPUs (0.2 x 4).
分組統計信息
既然有抑制狀態和CPU時間片的概念,那就有對應的統計信息
用來報告該分組內的CPU調度周期,抑制次數,抑制時長等信息。(注意它的統計不包括子分組的,另外有一個cpuacct的子系統統計信息包含了子分組的,另一篇文檔會講到)
cpu.stat
reports CPU time statistics using the following values:
已經過去多少個片段了
nr_periods — number of period intervals (as specified in cpu.cfs_period_us) that have elapsed.
抑制了多少次
nr_throttled — number of times tasks in a cgroup have been throttled
(that is, not allowed to run because they have exhausted all of the available time as specified by their quota).
所有任務加起來總共抑制了多長時間
throttled_time — the total time duration (in nanoseconds) for which tasks in a cgroup have been throttled.
從統計信息的抑制時間和抑制次數,可以判斷是否需要給分組增加CPU的上限。
例子
.1.
限制組cgroupA的任務最多可以使用8核資源
限制組cgroupB的任務最多可以使用16核資源
加載CPU子系統,創建子資源分區
mkdir -p /cgroup/cpu
mount -t cgroup -o cpu cpu /cgroup/cpu
cd /cgroup/cpu
mkdir cgroupA
mkdir cgroupB
配置資源配比(以100為基數,核數乘以100即得到cpu.shares)
cd cgroupA
echo 800 > cpu.shares
echo 1000000 > cpu.cfs_period_us
echo 8000000 > cpu.cfs_quota_us
cd ../cgroupB
echo 1600 > cpu.shares
echo 1000000 > cpu.cfs_period_us
echo 16000000 > cpu.cfs_quota_us
運行任務
cgexec -g cpu:cgroupA pg_ctl start -D /home/digoal/pgdata1921
cgexec -g cpu:cgroupB pg_ctl start -D /home/digoal/pgdata1922
小結
.1. 限下限
cpu.shares
.2. 限上限
cpu.cfs_period_us
cpu.cfs_quota_us
.3. 限實時任務上限
cpu.rt_period_us
cpu.rt_runtime_us
cpuacct 子系統
cpuacct 子系統是用來統計CPU使用情況的子系統,功能定位不是隔離資源,而是統計資源的使用情況。
cpuacct子系統的統計數據包含子分區的。
總結
以上是生活随笔為你收集整理的linux资源隔离是哪些,【转载】Linux cgroup资源隔离各个击破之的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 软键盘遮住按钮,Andr
- 下一篇: oracle存储返回sql查询,如何做才