diff --git a/vs/csharp/cgroup/Dockerfile b/vs/csharp/cgroup/Dockerfile new file mode 100644 index 0000000..29ec19d --- /dev/null +++ b/vs/csharp/cgroup/Dockerfile @@ -0,0 +1,27 @@ +# Use the official .NET Core SDK image from Docker Hub +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build + +# Set the working directory in the container +WORKDIR /app + +# Copy the project file and restore dependencies +COPY *.csproj ./ +RUN dotnet restore + +# Copy the remaining source code +COPY . . + +# Build the project +RUN dotnet build -c Release -o out + +# Run unit tests (if any) +# RUN dotnet test + +# Publish the project +RUN dotnet publish -c Release -o out + +# Build runtime image +FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime +WORKDIR /app +COPY --from=build /app/out . +ENTRYPOINT ["dotnet", "YourAppName.dll"] diff --git a/vs/csharp/cgroup/Program.cs b/vs/csharp/cgroup/Program.cs new file mode 100644 index 0000000..1f0ba8b --- /dev/null +++ b/vs/csharp/cgroup/Program.cs @@ -0,0 +1,51 @@ +using System; +using System.IO; + +namespace YourAppName +{ + class Program + { + static void Main(string[] args) + { + PrintSystemInfo(); + } + + static void PrintSystemInfo() + { + Console.WriteLine("System Information:"); + Console.WriteLine("-------------------"); + + // Querying CPU information + Console.WriteLine("CPU:"); + int coreCount = Environment.ProcessorCount; + Console.WriteLine($" Number of Cores: {coreCount}"); + + // Querying memory information + Console.WriteLine("\nMemory:"); + if (File.Exists("/proc/meminfo")) + { + string[] lines = File.ReadAllLines("/proc/meminfo"); + foreach (string line in lines) + { + string[] parts = line.Split(':'); + if (parts.Length == 2) + { + string name = parts[0].Trim(); + string value = parts[1].Trim(); + + if (name == "MemTotal" || name == "MemFree") + { + long memoryKiB = long.Parse(value.Split(' ')[0]); + double memoryMiB = memoryKiB / 1024.0; + Console.WriteLine($" {name}: {memoryMiB:F2} MiB"); + } + } + } + } + else + { + Console.WriteLine("Memory information retrieval is not supported on this platform."); + } + } + } +} diff --git a/vs/csharp/cgroup/YourAppName.csproj b/vs/csharp/cgroup/YourAppName.csproj new file mode 100644 index 0000000..120e38c --- /dev/null +++ b/vs/csharp/cgroup/YourAppName.csproj @@ -0,0 +1,8 @@ + + + + Exe + net7.0 + + + diff --git a/vs/csharp/cgroup/dew_it.sh b/vs/csharp/cgroup/dew_it.sh new file mode 100644 index 0000000..1998d82 --- /dev/null +++ b/vs/csharp/cgroup/dew_it.sh @@ -0,0 +1,2 @@ +docker build -t s50600822/cgroupcheck:csharp . +docker run --cpus=2 --memory=512m s50600822/cgroupcheck:csharp \ No newline at end of file diff --git a/vs/java/cgroup/README.md b/vs/java/cgroup/README.md index 0df0ccf..c3fc747 100644 --- a/vs/java/cgroup/README.md +++ b/vs/java/cgroup/README.md @@ -156,6 +156,64 @@ Total System Memory: 7941 MB ``` +### Csharp +```c# +using System; +using System.IO; + +namespace App { + class Program { + static void Main(string[] args) { + PrintSystemInfo(); + } + + static void PrintSystemInfo() { + Console.WriteLine("System Information:"); + Console.WriteLine("-------------------"); + + Console.WriteLine("CPU:"); + int coreCount = Environment.ProcessorCount; + Console.WriteLine($" Number of Cores: {coreCount}"); + + Console.WriteLine("\nMemory:"); + if (File.Exists("/proc/meminfo")) { + string[] lines = File.ReadAllLines("/proc/meminfo"); + foreach (string line in lines) { + string[] parts = line.Split(':'); + if (parts.Length == 2) { + string name = parts[0].Trim(); + string value = parts[1].Trim(); + + if (name == "MemTotal" || name == "MemFree") { + long memoryKiB = long.Parse(value.Split(' ')[0]); + double memoryMiB = memoryKiB / 1024.0; + Console.WriteLine($" {name}: {memoryMiB:F2} MiB"); + } + } + } + } else { + Console.WriteLine("Memory information retrieval is not supported on this platform."); + } + } + } +} + +``` + +test +```bash +docker run --cpus=2 --memory=512m s50600822/cgroupcheck:csharp + +System Information: +------------------- +CPU: + Number of Cores: 2 + +Memory: + MemTotal: 7841.18 MiB + MemFree: 5416.21 MiB +``` + ### See also https://github.com/s50600822/Notes/wiki/Cgroup