From 1d2165440490134a14ef838e4bf3a692b72977dc Mon Sep 17 00:00:00 2001 From: Vector Von Date: Tue, 14 Jun 2022 15:23:36 +0800 Subject: [PATCH] fix ram count error --- MainWindow.xaml.cs | 11 +++++---- PerformanceInfo.cs | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 PerformanceInfo.cs diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index ba8ccbd..67ab850 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -105,7 +105,10 @@ namespace Topuino_Client_Windows private void Run() { PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); - PerformanceCounter ramCounter = new PerformanceCounter("Memory", "% Committed Bytes In Use"); + long ramAvailable = PerformanceInfo.GetPhysicalAvailableMemoryInMiB(); + long ramTotal = PerformanceInfo.GetTotalMemoryInMiB(); + long ramPercentFree = ramAvailable * 100 / ramTotal; + long ramPercentUsed = 100 - ramPercentFree; PerformanceCounter diskReadCounter = new PerformanceCounter("PhysicalDisk", "Disk Read Bytes/sec", "_Total"); PerformanceCounter diskWriteCounter = new PerformanceCounter("PhysicalDisk", "Disk Write Bytes/sec", "_Total"); NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); @@ -138,9 +141,9 @@ namespace Topuino_Client_Windows Dictionary statusInfo = new Dictionary(); statusInfo.Add("SN", sn); statusInfo.Add("CPU_PERCENT", ((int)cpuCounter.NextValue()).ToString()); - statusInfo.Add("MEM_PERCENT", ((int)ramCounter.NextValue()).ToString()); - statusInfo.Add("DISK_PERCENT", ((int)((double)drive0.AvailableFreeSpace / (double)drive0.TotalSize * 100)).ToString()); - statusInfo.Add("DISK1_PERCENT", ((int)((double)drive1.AvailableFreeSpace / (double)drive1.TotalSize * 100)).ToString()); + statusInfo.Add("MEM_PERCENT", ((int)ramPercentUsed).ToString()); + statusInfo.Add("DISK_PERCENT", ((int)((double)(drive0.TotalSize - drive0.AvailableFreeSpace) / drive0.TotalSize * 100)).ToString()); + statusInfo.Add("DISK1_PERCENT", ((int)((double)(drive1.TotalSize - drive1.AvailableFreeSpace) / drive1.TotalSize * 100)).ToString()); statusInfo.Add("DISK_READ_RATE", ((int)diskReadCounter.NextValue()).ToString()); statusInfo.Add("DISK_WRITE_RATE", ((int)diskWriteCounter.NextValue()).ToString()); statusInfo.Add("NET_SENT_RATE", ((int)(netBytesSentAfter - netBytesSentBefore)).ToString()); diff --git a/PerformanceInfo.cs b/PerformanceInfo.cs new file mode 100644 index 0000000..1af71a6 --- /dev/null +++ b/PerformanceInfo.cs @@ -0,0 +1,59 @@ +using System; +using System.Runtime.InteropServices; + +namespace Topuino_Client_Windows +{ + public static class PerformanceInfo + { + [DllImport("psapi.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size); + + [StructLayout(LayoutKind.Sequential)] + public struct PerformanceInformation + { + public int Size; + public IntPtr CommitTotal; + public IntPtr CommitLimit; + public IntPtr CommitPeak; + public IntPtr PhysicalTotal; + public IntPtr PhysicalAvailable; + public IntPtr SystemCache; + public IntPtr KernelTotal; + public IntPtr KernelPaged; + public IntPtr KernelNonPaged; + public IntPtr PageSize; + public int HandlesCount; + public int ProcessCount; + public int ThreadCount; + } + + public static Int64 GetPhysicalAvailableMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalAvailable.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + + public static Int64 GetTotalMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + } +}