From 160b4365b0f470a01d0726884c7c541544cc1f90 Mon Sep 17 00:00:00 2001 From: Urban Date: Mon, 17 May 2021 14:47:52 +0200 Subject: [PATCH] Version 1.0.1.1 --- Hardware.Info/Hardware.Info.csproj | 2 +- Hardware.Info/Linux/HardwareInfo.cs | 45 +++++++++++++++-------------- README.md | 5 +++- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/Hardware.Info/Hardware.Info.csproj b/Hardware.Info/Hardware.Info.csproj index 96d0826..590c501 100644 --- a/Hardware.Info/Hardware.Info.csproj +++ b/Hardware.Info/Hardware.Info.csproj @@ -9,7 +9,7 @@ Hardware.Info Hardware.Info - 1.0.1.0 + 1.0.1.1 Jinjinov 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. Computer;Device;Hardware;Info;Information;NET Standard;Windows;Linux;macOS diff --git a/Hardware.Info/Linux/HardwareInfo.cs b/Hardware.Info/Linux/HardwareInfo.cs index fbeced9..9af52ee 100644 --- a/Hardware.Info/Linux/HardwareInfo.cs +++ b/Hardware.Info/Linux/HardwareInfo.cs @@ -126,7 +126,7 @@ public List GetCpuList() List cpuList = new List(); 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+(.+)"); @@ -193,17 +193,17 @@ public List 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 @@ -211,22 +211,23 @@ private static void GetCpuUsage(CPU cpu) // 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); } } @@ -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 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 cpuLastSumLine = cpuStatLast.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList(); cpuLastSumLine.RemoveAt(0); ulong cpuSum = 0; @@ -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; } diff --git a/README.md b/README.md index f7c357f..b9439c6 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ How to use: 1. Include NuGet package from https://www.nuget.org/packages/Hardware.Info - + 2. Call `RefreshAll()` or one of the other `Refresh*()` methods: @@ -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 )