Skip to content

Commit

Permalink
cgroup - csharp
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Mar 2, 2024
1 parent d4e4a46 commit 50f3f07
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
26 changes: 26 additions & 0 deletions vs/csharp/cgroup/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM mcr.microsoft.com/dotnet/sdk:8.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:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "YourAppName.dll"]
51 changes: 51 additions & 0 deletions vs/csharp/cgroup/Program.cs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
}
8 changes: 8 additions & 0 deletions vs/csharp/cgroup/YourAppName.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

</Project>
2 changes: 2 additions & 0 deletions vs/csharp/cgroup/dew_it.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker build -t s50600822/cgroupcheck:csharp .
docker run --cpus=2 --memory=512m s50600822/cgroupcheck:csharp
58 changes: 58 additions & 0 deletions vs/java/cgroup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50f3f07

Please sign in to comment.