-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathProgram.cs
More file actions
92 lines (77 loc) · 3.08 KB
/
Program.cs
File metadata and controls
92 lines (77 loc) · 3.08 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// See https://aka.ms/new-console-template for more information
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.InteropServices;
using static System.Console;
// Variant of https://github.com/dotnet/core/tree/main/samples/dotnet-runtimeinfo
// Ascii text: https://ascii.co.uk/text (Univers font)
WriteLine("""
42
42 ,d ,d
42 42 42
,adPPYb,42 ,adPPYba, MM42MMM 8b,dPPYba, ,adPPYba, MM42MMM
a8" `Y42 a8" "8a 42 42P' `"8a a8P_____42 42
8b 42 8b d8 42 42 42 8PP!!!!!!! 42
"8a, ,d42 "8a, ,a8" 42, 42 42 "8b, ,aa 42,
`"8bbdP"Y8 `"YbbdP"' "Y428 42 42 `"Ybbd8"' "Y428
""");
const double Mebi = 1024 * 1024;
const double Gibi = Mebi * 1024;
var gcInfo = GC.GetGCMemoryInfo();
var totalMemoryBytes = gcInfo.TotalAvailableMemoryBytes;
// OS and .NET information
WriteLine($"{nameof(RuntimeInformation.OSArchitecture)}: {RuntimeInformation.OSArchitecture}");
WriteLine($"{nameof(RuntimeInformation.OSDescription)}: {RuntimeInformation.OSDescription}");
WriteLine($"{nameof(RuntimeInformation.FrameworkDescription)}: {RuntimeInformation.FrameworkDescription}");
WriteLine();
// Environment information
WriteLine($"{nameof(Environment.UserName)}: {Environment.UserName}");
WriteLine($"HostName : {Dns.GetHostName()}");
WriteLine();
// Hardware information
WriteLine($"{nameof(Environment.ProcessorCount)}: {Environment.ProcessorCount}");
WriteLine($"{nameof(GCMemoryInfo.TotalAvailableMemoryBytes)}: {totalMemoryBytes} ({GetInBestUnit(totalMemoryBytes)})");
string[] memoryLimitPaths =
[
"/sys/fs/cgroup/memory.max",
"/sys/fs/cgroup/memory.high",
"/sys/fs/cgroup/memory.low",
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
];
string[] currentMemoryPaths =
[
"/sys/fs/cgroup/memory.current",
"/sys/fs/cgroup/memory/memory.usage_in_bytes",
];
// cgroup information
if (OperatingSystem.IsLinux()
&& TryReadFirstLongFromPaths(memoryLimitPaths, out long memoryLimit, out string? bestMemoryLimitPath)
&& memoryLimit > 0)
{
// get memory cgroup information
TryReadFirstLongFromPaths(currentMemoryPaths, out long currentMemory, out _);
WriteLine($"cgroup memory constraint: {bestMemoryLimitPath}");
WriteLine($"cgroup memory limit: {memoryLimit} ({GetInBestUnit(memoryLimit)})");
WriteLine($"cgroup memory usage: {currentMemory} ({GetInBestUnit(currentMemory)})");
WriteLine($"GC Hard limit %: {(double)totalMemoryBytes / memoryLimit * 100:N0}");
}
static string GetInBestUnit(long size) => size switch
{
< (long)Mebi => $"{size} bytes",
< (long)Gibi => $"{size / Mebi:F} MiB",
_ => $"{size / Gibi:F} GiB"
};
static bool TryReadFirstLongFromPaths(string[] paths, out long limit, [NotNullWhen(true)] out string? bestPath)
{
foreach (string path in paths)
{
if (File.Exists(path) && long.TryParse(File.ReadAllText(path), out limit))
{
bestPath = path;
return true;
}
}
bestPath = null;
limit = 0;
return false;
}