fix ram count error

This commit is contained in:
Vector Von 2022-06-14 15:23:36 +08:00
parent c528c7910f
commit 1d21654404
2 changed files with 66 additions and 4 deletions

View File

@ -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<string, string> statusInfo = new Dictionary<string, string>();
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());

59
PerformanceInfo.cs Normal file
View File

@ -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;
}
}
}
}