forked from dotnet/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceMonitoringLinuxCgroupVersion.cs
More file actions
57 lines (49 loc) · 2.04 KB
/
ResourceMonitoringLinuxCgroupVersion.cs
File metadata and controls
57 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.IO;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Detects cgroup version on Linux OS.
/// </summary>
internal static class ResourceMonitoringLinuxCgroupVersion
{
/// <summary>
/// Get drive metadata for each drive in the system and detects the cgroup version.
/// </summary>
/// <returns>True/False.</returns>
[ExcludeFromCodeCoverage]
public static bool GetCgroupType()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
var injectParserV2 = false;
const string CgroupVersion =
#if NET10_0_OR_GREATER
"cgroup2";
#else
"cgroup2fs";
#endif
const string UnifiedCgroupPath = "/sys/fs/cgroup/unified";
// We check which cgroup version is mounted in the system and based on that we inject the parser.
foreach (DriveInfo d in allDrives)
{
// Currently there are some OS'es which mount both cgroup v1 and v2. And v2 is mounted under /sys/fs/cgroup/unified
// So, we are checking for the unified cgroup and fallback to cgroup v1, because the path for cgroup v2 is different.
// This is mostly to support WSL/WSL2, where both cgroup v1 and v2 are mounted and make debugging for Linux easier in VS.
// https://systemd.io/CGROUP_DELEGATION/#three-different-tree-setups
if (d.DriveType == DriveType.Ram && d.DriveFormat == CgroupVersion && d.VolumeLabel == UnifiedCgroupPath)
{
injectParserV2 = false;
break;
}
if (d.DriveType == DriveType.Ram && d.DriveFormat == CgroupVersion)
{
injectParserV2 = true;
break;
}
}
return injectParserV2;
}
}