Skip to content

Commit

Permalink
Revert changes from SonarLint
Browse files Browse the repository at this point in the history
  • Loading branch information
DarylTodosichuk committed Sep 23, 2024
1 parent f423cd3 commit 7f0cdcf
Showing 1 changed file with 70 additions and 74 deletions.
144 changes: 70 additions & 74 deletions applications/dotnetapp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,92 @@
// Variant of https://github.com/dotnet/core/tree/main/samples/dotnet-runtimeinfo
// Ascii text: https://ascii.co.uk/text (Univers font)

internal class Program
{
private static void Main(string[] args)
{
global::System.Object value1 = WriteLine("""DotNet Console Application...""");
WriteLine("""
DotNet Console Application...
""");

const double Mebi = 1024 * 1024;
const double Gibi = Mebi * 1024;
GCMemoryInfo gcInfo = GC.GetGCMemoryInfo();
long totalMemoryBytes = gcInfo.TotalAvailableMemoryBytes;
const double Mebi = 1024 * 1024;
const double Gibi = Mebi * 1024;
GCMemoryInfo gcInfo = GC.GetGCMemoryInfo();
long 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();
// 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();
// 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)})");
// Hardware information
WriteLine($"{nameof(Environment.ProcessorCount)}: {Environment.ProcessorCount}");
WriteLine($"{nameof(GCMemoryInfo.TotalAvailableMemoryBytes)}: {totalMemoryBytes} ({GetInBestUnit(totalMemoryBytes)})");

string[] memoryLimitPaths = new string[]
{
string[] memoryLimitPaths = new string[]
{
"/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 = new string[]
{
string[] currentMemoryPaths = new string[]
{
"/sys/fs/cgroup/memory.current",
"/sys/fs/cgroup/memory/memory.usage_in_bytes",
};
};

// cgroup information
if (OperatingSystem.IsLinux() &&
GetBestValue(memoryLimitPaths, out long memoryLimit, out string bestMemoryLimitPath) &&
memoryLimit > 0)
{
// get memory cgroup information
GetBestValue(currentMemoryPaths, out long currentMemory, out string memoryPath);
// cgroup information
if (OperatingSystem.IsLinux() &&
GetBestValue(memoryLimitPaths, out long memoryLimit, out string? bestMemoryLimitPath) &&
memoryLimit > 0)
{
// get memory cgroup information
GetBestValue(currentMemoryPaths, out long currentMemory, out string? memoryPath);

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}");
}
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}");
}

string GetInBestUnit(long size)
{
if (size < Mebi)
{
return $"{size} bytes";
}
else if (size < Gibi)
{
double mebibytes = size / Mebi;
return $"{mebibytes:F} MiB";
}
else
{
double gibibytes = size / Gibi;
return $"{gibibytes:F} GiB";
}
}
string GetInBestUnit(long size)
{
if (size < Mebi)
{
return $"{size} bytes";
}
else if (size < Gibi)
{
double mebibytes = size / Mebi;
return $"{mebibytes:F} MiB";
}
else
{
double gibibytes = size / Gibi;
return $"{gibibytes:F} GiB";
}
}

bool GetBestValue(string[] paths, out long limit, [NotNullWhen(true)] out string bestPath)
bool GetBestValue(string[] paths, out long limit, [NotNullWhen(true)] out string? bestPath)
{
foreach (string path in paths)
{
if (Path.Exists(path) &&
long.TryParse(File.ReadAllText(path), out limit))
{
foreach (string path in paths)
{
if (Path.Exists(path) &&
long.TryParse(File.ReadAllText(path), out limit))
{
bestPath = path;
return true;
}
}

bestPath = null;
limit = 0;
return false;
bestPath = path;
return true;
}

global::System.Object value = Console.WriteLine("""Run....""");
while (true) ;
}
}

bestPath = null;
limit = 0;
return false;
}

Console.WriteLine("Run...");
while (true);

0 comments on commit 7f0cdcf

Please sign in to comment.