Skip to content

Commit

Permalink
Version 1.0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjinov committed May 17, 2021
1 parent 155310a commit 160b436
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Hardware.Info/Hardware.Info.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<PackageId>Hardware.Info</PackageId>
<Product>Hardware.Info</Product>
<Version>1.0.1.0</Version>
<Version>1.0.1.1</Version>
<Authors>Jinjinov</Authors>
<Description>Battery, BIOS, CPU - processor, storage drive, keyboard, RAM - memory, monitor, motherboard, mouse, NIC - network adapter, printer, sound card - audio card, graphics card - video card. Hardware.Info is a .NET Standard 2.0 library and uses WMI on Windows, /dev, /proc, /sys on Linux and sysctl, system_profiler on macOS.</Description>
<PackageTags>Computer;Device;Hardware;Info;Information;NET Standard;Windows;Linux;macOS</PackageTags>
Expand Down
45 changes: 24 additions & 21 deletions Hardware.Info/Linux/HardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public List<CPU> GetCpuList()
List<CPU> cpuList = new List<CPU>();

string[] lines = TryReadFileLines("/proc/cpuinfo");

Regex vendorIdRegex = new Regex(@"^vendor_id\s+:\s+(.+)");
Regex modelNameRegex = new Regex(@"^model name\s+:\s+(.+)");
Regex cpuSpeedRegex = new Regex(@"^cpu MHz\s+:\s+(.+)");
Expand Down Expand Up @@ -193,40 +193,41 @@ public List<CPU> GetCpuList()
}

private static void GetCpuUsage(CPU cpu)
{
// Column Name Description
// 1 user Time spent with normal processing in user mode.
// 2 nice Time spent with niced processes in user mode.
// 3 system Time spent running in kernel mode.
// 4 idle Time spent in vacations twiddling thumbs.
// 5 iowait Time spent waiting for I / O to completed.This is considered idle time too.
// 6 irq Time spent serving hardware interrupts.See the description of the intr line for more details.
// 7 softirq Time spent serving software interrupts.
// 8 steal Time stolen by other operating systems running in a virtual environment.
// 9 guest Time spent for running a virtual CPU or guest OS under the control of the kernel.
{
// Column Name Description
// 1 user Time spent with normal processing in user mode.
// 2 nice Time spent with niced processes in user mode.
// 3 system Time spent running in kernel mode.
// 4 idle Time spent in vacations twiddling thumbs.
// 5 iowait Time spent waiting for I / O to completed.This is considered idle time too.
// 6 irq Time spent serving hardware interrupts.See the description of the intr line for more details.
// 7 softirq Time spent serving software interrupts.
// 8 steal Time stolen by other operating systems running in a virtual environment.
// 9 guest Time spent for running a virtual CPU or guest OS under the control of the kernel.

// > cat /proc/stat
// cpu 1279636934 73759586 192327563 12184330186 543227057 56603 68503253 0 0
// cpu0 297522664 8968710 49227610 418508635 72446546 56602 24904144 0 0
// cpu1 227756034 9239849 30760881 424439349 196694821 0 7517172 0 0
// cpu2 86902920 6411506 12412331 769921453 17877927 0 4809331 0 0
// ...
var cpuUsageLineLast = TryReadFileLines("/proc/stat");

string[] cpuUsageLineLast = TryReadFileLines("/proc/stat");
Task.Delay(500).Wait();
var cpuUsageLineNow = TryReadFileLines("/proc/stat");
string[] cpuUsageLineNow = TryReadFileLines("/proc/stat");

if (cpuUsageLineLast.Length > 0 && cpuUsageLineNow.Length > 0)
{
cpu.PercentProcessorTime = GetCpuPercentage(cpuUsageLineLast.First(), cpuUsageLineNow.First());

for (var i = 0; i < cpu.NumberOfLogicalProcessors; i++)
for (int i = 0; i < cpu.NumberOfLogicalProcessors; i++)
{
CpuCore core = new CpuCore
{
Name = i.ToString(),
PercentProcessorTime = GetCpuPercentage(cpuUsageLineLast.First(s => s.StartsWith($"cpu{i}")), cpuUsageLineNow.First(s => s.StartsWith($"cpu{i}")))
};

cpu.CpuCoreList.Add(core);
}
}
Expand All @@ -235,12 +236,13 @@ private static void GetCpuUsage(CPU cpu)
private static UInt64 GetCpuPercentage(string cpuStatLast, string cpuStatNow)
{
char[] charSeparators = new char[] { ' ' };

// Get all columns but skip the first (which is the "cpu" string)
var cpuSumLine = cpuStatNow.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string> cpuSumLine = cpuStatNow.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
cpuSumLine.RemoveAt(0);

// Get all columns but skip the first (which is the "cpu" string)
var cpuLastSumLine = cpuStatLast.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string> cpuLastSumLine = cpuStatLast.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
cpuLastSumLine.RemoveAt(0);

ulong cpuSum = 0;
Expand All @@ -250,11 +252,12 @@ private static UInt64 GetCpuPercentage(string cpuStatLast, string cpuStatNow)
cpuLastSumLine.ForEach(s => cpuLastSum += Convert.ToUInt64(s));

// Get the delta between two reads
var cpuDelta = cpuSum - cpuLastSum;
ulong cpuDelta = cpuSum - cpuLastSum;
// Get the idle time Delta
var cpuIdle = Convert.ToUInt64(cpuSumLine[3]) - Convert.ToUInt64(cpuLastSumLine[3]);
ulong cpuIdle = Convert.ToUInt64(cpuSumLine[3]) - Convert.ToUInt64(cpuLastSumLine[3]);
// Calc percentage
var cpuUsed = cpuDelta - cpuIdle;
ulong cpuUsed = cpuDelta - cpuIdle;

return 100 * cpuUsed / cpuDelta;
}

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ How to use:

1. Include NuGet package from https://www.nuget.org/packages/Hardware.Info

<PackageReference Include="Hardware.Info" Version="1.0.1.0" />
<PackageReference Include="Hardware.Info" Version="1.0.1.1" />

2. Call `RefreshAll()` or one of the other `Refresh*()` methods:

Expand Down Expand Up @@ -117,6 +117,9 @@ How to use:

Version history:

- 1.0.1.1:
- Added CpuCore info in Linux - by [@isenmann]( https://github.com/isenmann )
- Added CPU.PercentProcessorTime, CPU.CpuCoreList in Linux - by [@isenmann]( https://github.com/isenmann )
- 1.0.1.0:
- Added CpuCore info in Windows - by [@isenmann]( https://github.com/isenmann )
- Added CPU.PercentProcessorTime, CPU.CpuCoreList in Windows - by [@isenmann]( https://github.com/isenmann )
Expand Down

0 comments on commit 160b436

Please sign in to comment.