CGroup2 Class

  • Supported on linux

Represents a cgroup v2 control group for managing and limiting resource usage of processes.

[SupportedOSPlatform("linux")]
public sealed class CGroup2

Remarks

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

NameDescription
NameGets the name of the cgroup.
ParentGets the parent cgroup, or null if this is the root cgroup.
RootGets the root cgroup.

Gets the name of the cgroup.

public string Name { get; }

Property value

string

Gets the parent cgroup, or null if this is the root cgroup.

public CGroup2? Parent { get; }

Property value

CGroup2?

Gets the root cgroup.

public static CGroup2 Root { get; }

Property value

CGroup2

Methods

NameDescription
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.

AssociateProcess overloads

AssociateProcess(Process)

CGroup2.cs

Adds a process to this cgroup.

public void AssociateProcess(Process process)
Parameters
process Process
The process to add.

AssociateProcess(int)

CGroup2.cs

Adds a process to this cgroup by its PID.

public void AssociateProcess(int pid)
Parameters
pid int
The process ID.

AssociateThread(int)

CGroup2.cs

Adds a thread to this cgroup by its TID.

public void AssociateThread(int tid)

Parameters

tid int
The thread ID.

CreateOrGetChild(string)

CGroup2.cs

Creates or gets a child cgroup.

public CGroup2 CreateOrGetChild(string name)

Parameters

name string
The name of the child cgroup.

Returns

CGroup2 The child cgroup.

Delete()

CGroup2.cs

Deletes 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.cs

Checks if this cgroup exists.

public bool Exists()

Returns

bool

Freeze()

CGroup2.cs

Freezes all processes in this cgroup.

public void Freeze()

GetAvailableControllers()

CGroup2.cs

Gets the list of available controllers.

public IEnumerable<string> GetAvailableControllers()

Returns

IEnumerable<string>

Exceptions

DirectoryNotFoundException
The cgroup does not exist.

GetChild(string)

CGroup2.cs

Gets an existing child cgroup without creating it.

public CGroup2? GetChild(string name)

Parameters

name string
The name of the child cgroup.

Returns

CGroup2? The child cgroup.

GetCpuMax()

CGroup2.cs

Gets 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.cs

Gets 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.cs

Gets 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.cs

Gets 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.cs

Gets the effective CPUs (actually granted by parent).

public CGroupValue<int[]> GetCpusetCpusEffective()

Returns

CGroupValue<int[]> The CPU numbers.

GetCpusetMems()

CGroup2.Cpuset.cs

Gets 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.cs

Gets the effective memory nodes (actually granted by parent).

public CGroupValue<int[]> GetCpusetMemsEffective()

Returns

CGroupValue<int[]> The memory node numbers.

GetCpusetPartition()

CGroup2.Cpuset.cs

Gets 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.cs

Gets 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.cs

Gets the current HugeTLB usage for a specific page size.

public CGroupValue<long> GetHugeTlbCurrent(string pageSize)

Parameters

pageSize string
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.cs

Gets the number of times the HugeTLB limit was hit.

public CGroupValue<long> GetHugeTlbEventsMax(string pageSize)

Parameters

pageSize string
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.cs

Gets the HugeTLB usage limit for a specific page size.

public CGroupValue<long> GetHugeTlbMax(string pageSize)

Parameters

pageSize string
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.cs

Gets 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.cs

Gets 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.cs

Gets 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.cs

Gets the memory maximum limit in bytes.

public CGroupValue<long> GetMemoryMax()

Returns

CGroupValue<long> The limit, or CGroupValueState.NotConfigured when memory is unlimited.

GetMemoryMin()

CGroup2.cs

Gets 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.cs

Gets 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.cs

Gets 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.cs

Gets 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.cs

Gets all process IDs in this cgroup.

public IEnumerable<int> GetProcesses()

Returns

IEnumerable<int>

Exceptions

DirectoryNotFoundException
The cgroup does not exist.

GetSwapMax()

CGroup2.cs

Gets 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.cs

Gets all thread IDs in this cgroup.

public IEnumerable<int> GetThreads()

Returns

IEnumerable<int>

Exceptions

FileNotFoundException
The running kernel does not support thread mode (Linux 4.14+).
DirectoryNotFoundException
The cgroup does not exist.

IsFrozen()

CGroup2.cs

Gets whether the cgroup is frozen.

public bool IsFrozen()

Returns

bool

Remarks

This getter keeps a plain Boolean: the freezer needs Linux 5.2+, and a kernel without a freezer cannot have frozen the cgroup, so its absence is a correct false rather than a missing value.

Kills all processes in this cgroup and its descendants.

public void Kill()

RemoveCpuMax()

CGroup2.cs

Removes the CPU maximum bandwidth limit. The period is left unchanged.

public void RemoveCpuMax()

RemoveIoMax(int, int)

CGroup2.cs

Removes IO limits for a device.

public void RemoveIoMax(int major, int minor)

Parameters

major int
Device major number.
minor int
Device minor number.

SetControllers(ReadOnlySpan<string>)

CGroup2.cs

Set multiple controllers in the subtree.

public void SetControllers(params scoped ReadOnlySpan<string> controllers)

Parameters

controllers ReadOnlySpan<string>
The controller names.

SetCpuMax(long?, long)

CGroup2.cs

Sets the CPU maximum bandwidth limit.

public void SetCpuMax(long? maxMicroseconds, long periodMicroseconds = 100000L)

Parameters

maxMicroseconds long?
Maximum time in microseconds that the cgroup can run during one period.
periodMicroseconds long
Period in microseconds (default is 100000 = 100ms).

SetCpuWeight(int)

CGroup2.cs

Sets the CPU weight (relative share of CPU time).

public void SetCpuWeight(int weight)

Parameters

weight int
Weight value between 1 and 10000 (default is 100).

SetCpusetCpus(ReadOnlySpan<int>)

CGroup2.Cpuset.cs

Sets the CPUs that tasks in this cgroup can use.

public void SetCpusetCpus(params scoped ReadOnlySpan<int> cpus)

Parameters

cpus ReadOnlySpan<int>
Array of CPU numbers (e.g., [0, 1, 2] for CPUs 0-2).

SetCpusetCpusRaw(string)

CGroup2.Cpuset.cs

Sets the CPUs using a raw format string (e.g., "0-3,6,8-10").

public void SetCpusetCpusRaw(string cpuList)

Parameters

cpuList string
CPU list in cgroup format.

SetCpusetMems(ReadOnlySpan<int>)

CGroup2.Cpuset.cs

Sets the memory nodes that tasks in this cgroup can use.

public void SetCpusetMems(params scoped ReadOnlySpan<int> nodes)

Parameters

nodes ReadOnlySpan<int>
Array of memory node numbers.

SetCpusetMemsRaw(string)

CGroup2.Cpuset.cs

Sets the memory nodes using a raw format string.

public void SetCpusetMemsRaw(string nodeList)

Parameters

nodeList string
Memory node list in cgroup format.

SetCpusetPartition(string)

CGroup2.Cpuset.cs

Sets the cpuset partition type.

public void SetCpusetPartition(string partitionType)

Parameters

partitionType string
The partition type ("member", "root", or "isolated").

SetDefaultIoWeight(int)

CGroup2.cs

Sets the default IO weight.

public void SetDefaultIoWeight(int weight)

Parameters

weight int
Weight value between 1 and 10000 (default is 100).

SetHugeTlbMax(string, long?)

CGroup2.HugeTlb.cs

Sets the HugeTLB usage limit for a specific page size.

public void SetHugeTlbMax(string pageSize, long? bytes)

Parameters

pageSize string
The huge page size (e.g., "2MB", "1GB").
bytes long?
Maximum usage in bytes, or null for no limit.

SetIoMax(int, int, long?, long?, long?, long?)

CGroup2.cs

Sets 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

major int
Device major number.
minor int
Device minor number.
readBytesPerSecond long?
Read bandwidth limit in bytes per second, or null for no limit.
writeBytesPerSecond long?
Write bandwidth limit in bytes per second, or null for no limit.
readIopsPerSecond long?
Read IOPS limit, or null for no limit.
writeIopsPerSecond long?
Write IOPS limit, or null for no limit.

SetIoWeight(int, int, int)

CGroup2.cs

Sets the IO weight for a device.

public void SetIoWeight(int major, int minor, int weight)

Parameters

major int
Device major number.
minor int
Device minor number.
weight int
Weight value between 1 and 10000 (default is 100).

SetMemoryHigh(long?)

CGroup2.cs

Sets the memory high limit (soft limit with throttling).

public void SetMemoryHigh(long? bytes)

Parameters

bytes long?
High memory limit in bytes, or null for no limit.

SetMemoryLow(long?)

CGroup2.cs

Sets the memory low limit (best-effort protection).

public void SetMemoryLow(long? bytes)

Parameters

bytes long?
Low memory limit in bytes, or null for no protection.

SetMemoryMax(long?)

CGroup2.cs

Sets the memory maximum limit in bytes.

public void SetMemoryMax(long? bytes)

Parameters

bytes long?
Maximum memory in bytes, or null for no limit.

SetMemoryMin(long?)

CGroup2.cs

Sets the memory min limit (hard protection).

public void SetMemoryMin(long? bytes)

Parameters

bytes long?
Min memory limit in bytes, or null for no protection.

SetPidsMax(long?)

CGroup2.cs

Sets the maximum number of processes (PIDs) allowed.

public void SetPidsMax(long? max)

Parameters

max long?
Maximum number of processes, or null for no limit.

SetSwapMax(long?)

CGroup2.cs

Sets the swap maximum limit in bytes.

public void SetSwapMax(long? bytes)

Parameters

bytes long?
Maximum swap in bytes, or null for no limit.

ToString()

CGroup2.cs

Returns a string representation of this cgroup.

public override string ToString()

Returns

string

Unfreeze()

CGroup2.cs

Unfreezes all processes in this cgroup.

public void Unfreeze()