CGroup2 Class
- Supported on linux
Represents a cgroup v2 control group for managing and limiting resource usage of processes.
[SupportedOSPlatform("linux")]
public sealed class CGroup2Remarks
Getters that read a single interface file return CGroupValue<T>, which distinguishes a value from the three ways a value can be absent: the file does not exist (the controller is not enabled on the parent cgroup, or the kernel does not support the feature), the file holds no limit (max), or the file holds content this library does not understand. Reading a cgroup that no longer exists throws DirectoryNotFoundException, so a stale instance does not look like a cgroup without controllers.
Properties
| Name | Description |
|---|---|
| Name | Gets the name of the cgroup. |
| Parent | Gets the parent cgroup, or null if this is the root cgroup. |
| Root | Gets the root cgroup. |
Name
CGroup2.csParent
CGroup2.csRoot
CGroup2.csMethods
| Name | Description |
|---|---|
| AssociateProcess(Process) | Adds a process to this cgroup. |
| AssociateProcess(int) | Adds a process to this cgroup by its PID. |
| AssociateThread(int) | Adds a thread to this cgroup by its TID. |
| CreateOrGetChild(string) | Creates or gets a child cgroup. |
| Delete() | Deletes this cgroup. The cgroup must be empty (no processes and no child cgroups). |
| Exists() | Checks if this cgroup exists. |
| Freeze() | Freezes all processes in this cgroup. |
| GetAvailableControllers() | Gets the list of available controllers. |
| GetChild(string) | Gets an existing child cgroup without creating it. |
| GetCpuMax() | Gets the CPU maximum bandwidth limit. |
| GetCpuStat() | Gets CPU statistics for this cgroup. |
| GetCpuWeight() | Gets the CPU weight. |
| GetCpusetCpus() | Gets the CPUs that tasks in this cgroup can use. |
| GetCpusetCpusEffective() | Gets the effective CPUs (actually granted by parent). |
| GetCpusetMems() | Gets the memory nodes that tasks in this cgroup can use. |
| GetCpusetMemsEffective() | Gets the effective memory nodes (actually granted by parent). |
| GetCpusetPartition() | Gets the cpuset partition type. |
| GetEnabledControllers() | Gets the list of enabled controllers in the subtree. |
| GetHugeTlbCurrent(string) | Gets the current HugeTLB usage for a specific page size. |
| GetHugeTlbEventsMax(string) | Gets the number of times the HugeTLB limit was hit. |
| GetHugeTlbMax(string) | Gets the HugeTLB usage limit for a specific page size. |
| GetMemoryCurrent() | Gets the current memory usage in bytes. |
| GetMemoryHigh() | Gets the memory high limit in bytes. |
| GetMemoryLow() | Gets the memory low limit in bytes. |
| GetMemoryMax() | Gets the memory maximum limit in bytes. |
| GetMemoryMin() | Gets the memory min limit in bytes. |
| GetMemoryStat() | Gets memory statistics for this cgroup. |
| GetPidsCurrent() | Gets the current number of processes. |
| GetPidsMax() | Gets the maximum number of processes allowed. |
| GetProcesses() | Gets all process IDs in this cgroup. |
| GetSwapMax() | Gets the swap maximum limit in bytes. |
| GetThreads() | Gets all thread IDs in this cgroup. |
| IsFrozen() | Gets whether the cgroup is frozen. |
| Kill() | Kills all processes in this cgroup and its descendants. |
| RemoveCpuMax() | Removes the CPU maximum bandwidth limit. The period is left unchanged. |
| RemoveIoMax(int, int) | Removes IO limits for a device. |
| SetControllers(ReadOnlySpan<string>) | Set multiple controllers in the subtree. |
| SetCpuMax(long?, long) | Sets the CPU maximum bandwidth limit. |
| SetCpuWeight(int) | Sets the CPU weight (relative share of CPU time). |
| SetCpusetCpus(ReadOnlySpan<int>) | Sets the CPUs that tasks in this cgroup can use. |
| SetCpusetCpusRaw(string) | Sets the CPUs using a raw format string (e.g., "0-3,6,8-10"). |
| SetCpusetMems(ReadOnlySpan<int>) | Sets the memory nodes that tasks in this cgroup can use. |
| SetCpusetMemsRaw(string) | Sets the memory nodes using a raw format string. |
| SetCpusetPartition(string) | Sets the cpuset partition type. |
| SetDefaultIoWeight(int) | Sets the default IO weight. |
| SetHugeTlbMax(string, long?) | Sets the HugeTLB usage limit for a specific page size. |
| SetIoMax(int, int, long?, long?, long?, long?) | Sets IO bandwidth limits for a device. When every limit is null, the limits of the device are removed. |
| SetIoWeight(int, int, int) | Sets the IO weight for a device. |
| SetMemoryHigh(long?) | Sets the memory high limit (soft limit with throttling). |
| SetMemoryLow(long?) | Sets the memory low limit (best-effort protection). |
| SetMemoryMax(long?) | Sets the memory maximum limit in bytes. |
| SetMemoryMin(long?) | Sets the memory min limit (hard protection). |
| SetPidsMax(long?) | Sets the maximum number of processes (PIDs) allowed. |
| SetSwapMax(long?) | Sets the swap maximum limit in bytes. |
| ToString() | Returns a string representation of this cgroup. |
| Unfreeze() | Unfreezes all processes in this cgroup. |
AssociateThread(int)
CGroup2.csCreateOrGetChild(string)
CGroup2.csDelete()
CGroup2.csDeletes this cgroup. The cgroup must be empty (no processes and no child cgroups).
public void Delete()Exceptions
IOException- If the cgroup cannot be deleted.
Exists()
CGroup2.csFreeze()
CGroup2.csFreezes all processes in this cgroup.
public void Freeze()GetAvailableControllers()
CGroup2.csGets the list of available controllers.
public IEnumerable<string> GetAvailableControllers()GetChild(string)
CGroup2.csGetCpuMax()
CGroup2.csGets the CPU maximum bandwidth limit.
public CGroupValue<CpuMax> GetCpuMax()Returns
CGroupValue<CpuMax> The quota and period. The value is never CGroupValueState.NotConfigured: the period is meaningful even when the quota is unlimited, so an unlimited quota is reported as CpuMax.MaxMicroseconds being null rather than by the state.
GetCpuStat()
CGroup2.csGets CPU statistics for this cgroup.
public CGroupValue<CpuStat> GetCpuStat()Returns
CGroupValue<CpuStat> The statistics. The value is never CGroupValueState.NotConfigured nor CGroupValueState.Invalid: keys this library does not know are ignored.
GetCpuWeight()
CGroup2.csGets the CPU weight.
public CGroupValue<int> GetCpuWeight()Returns
CGroupValue<int> The weight. The value is never CGroupValueState.NotConfigured: a cgroup with the CPU controller enabled always has a weight.
GetCpusetCpus()
CGroup2.Cpuset.csGets the CPUs that tasks in this cgroup can use.
public CGroupValue<int[]> GetCpusetCpus()Returns
CGroupValue<int[]> The CPU numbers, or an empty array when the cgroup inherits the CPUs of its parent.
GetCpusetCpusEffective()
CGroup2.Cpuset.csGets the effective CPUs (actually granted by parent).
public CGroupValue<int[]> GetCpusetCpusEffective()Returns
CGroupValue<int[]> The CPU numbers.
GetCpusetMems()
CGroup2.Cpuset.csGets the memory nodes that tasks in this cgroup can use.
public CGroupValue<int[]> GetCpusetMems()Returns
CGroupValue<int[]> The memory node numbers, or an empty array when the cgroup inherits the memory nodes of its parent.
GetCpusetMemsEffective()
CGroup2.Cpuset.csGets the effective memory nodes (actually granted by parent).
public CGroupValue<int[]> GetCpusetMemsEffective()Returns
CGroupValue<int[]> The memory node numbers.
GetCpusetPartition()
CGroup2.Cpuset.csGets the cpuset partition type.
public CGroupValue<string> GetCpusetPartition()Returns
CGroupValue<string> The partition type ("member", "root", "isolated", or a value reporting an invalid partition).
GetEnabledControllers()
CGroup2.csGets the list of enabled controllers in the subtree.
public IEnumerable<string> GetEnabledControllers()Returns
IEnumerable<string> The enabled controllers, or an empty sequence when the subtree enables none.
Exceptions
DirectoryNotFoundException- The cgroup does not exist.
GetHugeTlbCurrent(string)
CGroup2.HugeTlb.csGets the current HugeTLB usage for a specific page size.
public CGroupValue<long> GetHugeTlbCurrent(string pageSize)Parameters
pageSizestring- The huge page size (e.g., "2MB", "1GB").
Returns
CGroupValue<long> Current usage in bytes, or CGroupValueState.Unavailable when the running kernel does not provide this page size.
GetHugeTlbEventsMax(string)
CGroup2.HugeTlb.csGets the number of times the HugeTLB limit was hit.
public CGroupValue<long> GetHugeTlbEventsMax(string pageSize)Parameters
pageSizestring- The huge page size (e.g., "2MB", "1GB").
Returns
CGroupValue<long> Number of limit hits, or CGroupValueState.Unavailable when the running kernel does not provide this page size.
GetHugeTlbMax(string)
CGroup2.HugeTlb.csGets the HugeTLB usage limit for a specific page size.
public CGroupValue<long> GetHugeTlbMax(string pageSize)Parameters
pageSizestring- The huge page size (e.g., "2MB", "1GB").
Returns
CGroupValue<long> The limit in bytes, CGroupValueState.NotConfigured when there is no limit, or CGroupValueState.Unavailable when the running kernel does not provide this page size.
GetMemoryCurrent()
CGroup2.csGets the current memory usage in bytes.
public CGroupValue<long> GetMemoryCurrent()Returns
CGroupValue<long> The usage. The value is never CGroupValueState.NotConfigured: a cgroup with the memory controller enabled always reports a usage.
GetMemoryHigh()
CGroup2.csGets the memory high limit in bytes.
public CGroupValue<long> GetMemoryHigh()Returns
CGroupValue<long> The limit, or CGroupValueState.NotConfigured when there is no limit.
GetMemoryLow()
CGroup2.csGets the memory low limit in bytes.
public CGroupValue<long> GetMemoryLow()Returns
CGroupValue<long> The protection level. memory.low uses 0 rather than max for no protection, so the value is never CGroupValueState.NotConfigured.
GetMemoryMax()
CGroup2.csGets the memory maximum limit in bytes.
public CGroupValue<long> GetMemoryMax()Returns
CGroupValue<long> The limit, or CGroupValueState.NotConfigured when memory is unlimited.
GetMemoryMin()
CGroup2.csGets the memory min limit in bytes.
public CGroupValue<long> GetMemoryMin()Returns
CGroupValue<long> The protection level. memory.min uses 0 rather than max for no protection, so the value is never CGroupValueState.NotConfigured.
GetMemoryStat()
CGroup2.csGets memory statistics for this cgroup.
public CGroupValue<MemoryStat> GetMemoryStat()Returns
CGroupValue<MemoryStat> The statistics. The value is never CGroupValueState.NotConfigured nor CGroupValueState.Invalid: keys this library does not know are ignored.
GetPidsCurrent()
CGroup2.csGets the current number of processes.
public CGroupValue<long> GetPidsCurrent()Returns
CGroupValue<long> The number of processes. The value is never CGroupValueState.NotConfigured: a cgroup with the PID controller enabled always reports a count.
GetPidsMax()
CGroup2.csGets the maximum number of processes allowed.
public CGroupValue<long> GetPidsMax()Returns
CGroupValue<long> The limit, or CGroupValueState.NotConfigured when there is no limit.
GetProcesses()
CGroup2.csGets all process IDs in this cgroup.
public IEnumerable<int> GetProcesses()GetSwapMax()
CGroup2.csGets the swap maximum limit in bytes.
public CGroupValue<long> GetSwapMax()Returns
CGroupValue<long> The limit, or CGroupValueState.NotConfigured when there is no limit.
GetThreads()
CGroup2.csGets all thread IDs in this cgroup.
public IEnumerable<int> GetThreads()Returns
Exceptions
FileNotFoundException- The running kernel does not support thread mode (Linux 4.14+).
DirectoryNotFoundException- The cgroup does not exist.
IsFrozen()
CGroup2.csKill()
CGroup2.csKills all processes in this cgroup and its descendants.
public void Kill()RemoveCpuMax()
CGroup2.csRemoves the CPU maximum bandwidth limit. The period is left unchanged.
public void RemoveCpuMax()RemoveIoMax(int, int)
CGroup2.csSetControllers(ReadOnlySpan<string>)
CGroup2.csSet multiple controllers in the subtree.
public void SetControllers(params scoped ReadOnlySpan<string> controllers)Parameters
controllersReadOnlySpan<string>- The controller names.
SetCpuMax(long?, long)
CGroup2.csSetCpuWeight(int)
CGroup2.csSetCpusetCpus(ReadOnlySpan<int>)
CGroup2.Cpuset.csSets the CPUs that tasks in this cgroup can use.
public void SetCpusetCpus(params scoped ReadOnlySpan<int> cpus)Parameters
cpusReadOnlySpan<int>- Array of CPU numbers (e.g., [0, 1, 2] for CPUs 0-2).
SetCpusetCpusRaw(string)
CGroup2.Cpuset.csSetCpusetMems(ReadOnlySpan<int>)
CGroup2.Cpuset.csSets the memory nodes that tasks in this cgroup can use.
public void SetCpusetMems(params scoped ReadOnlySpan<int> nodes)Parameters
nodesReadOnlySpan<int>- Array of memory node numbers.
SetCpusetMemsRaw(string)
CGroup2.Cpuset.csSetCpusetPartition(string)
CGroup2.Cpuset.csSetDefaultIoWeight(int)
CGroup2.csSetHugeTlbMax(string, long?)
CGroup2.HugeTlb.csSetIoMax(int, int, long?, long?, long?, long?)
CGroup2.csSets IO bandwidth limits for a device. When every limit is null, the limits of the device are removed.
public void SetIoMax(int major, int minor, long? readBytesPerSecond = null, long? writeBytesPerSecond = null, long? readIopsPerSecond = null, long? writeIopsPerSecond = null)Parameters
majorint- Device major number.
minorint- Device minor number.
readBytesPerSecondlong?- Read bandwidth limit in bytes per second, or null for no limit.
writeBytesPerSecondlong?- Write bandwidth limit in bytes per second, or null for no limit.
readIopsPerSecondlong?- Read IOPS limit, or null for no limit.
writeIopsPerSecondlong?- Write IOPS limit, or null for no limit.
SetIoWeight(int, int, int)
CGroup2.csSetMemoryHigh(long?)
CGroup2.csSetMemoryLow(long?)
CGroup2.csSetMemoryMax(long?)
CGroup2.csSetMemoryMin(long?)
CGroup2.csSetPidsMax(long?)
CGroup2.csSetSwapMax(long?)
CGroup2.csToString()
CGroup2.csUnfreeze()
CGroup2.csUnfreezes all processes in this cgroup.
public void Unfreeze()