Skip to content

Commit 5c79bd0

Browse files
committed
add new memory sensors
1 parent 2576eb2 commit 5c79bd0

2 files changed

Lines changed: 68 additions & 28 deletions

File tree

OpenHardwareMonitorLib/Hardware/Memory/GenericWindowsMemory.cs

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,68 @@ namespace OpenHardwareMonitor.Hardware.Memory;
55

66
internal sealed class GenericWindowsMemory : Hardware
77
{
8+
private readonly Sensor _totalPhysicalMemory;
89
private readonly Sensor _physicalMemoryAvailable;
910
private readonly Sensor _physicalMemoryLoad;
1011
private readonly Sensor _physicalMemoryUsed;
1112
private readonly Sensor _virtualMemoryAvailable;
1213
private readonly Sensor _virtualMemoryLoad;
1314
private readonly Sensor _virtualMemoryUsed;
1415

16+
private readonly Sensor _commitLimit;
17+
private readonly Sensor _currentCommit;
18+
private readonly Sensor _kernelSize;
19+
private readonly Sensor _processCount;
20+
private readonly Sensor _threadCount;
21+
private readonly Sensor _handleCount;
22+
1523
public GenericWindowsMemory(string name, ISettings settings) : base(name, new Identifier("ram"), settings)
1624
{
17-
_physicalMemoryUsed = new Sensor("Memory Used", 0, SensorType.Data, this, settings);
18-
ActivateSensor(_physicalMemoryUsed);
19-
20-
_physicalMemoryAvailable = new Sensor("Memory Available", 1, SensorType.Data, this, settings);
21-
ActivateSensor(_physicalMemoryAvailable);
22-
23-
_physicalMemoryLoad = new Sensor("Memory", 0, SensorType.Load, this, settings);
24-
ActivateSensor(_physicalMemoryLoad);
25+
ActivateSensor(_physicalMemoryLoad = new Sensor("Memory", 0, SensorType.Load, this, settings));
26+
ActivateSensor(_virtualMemoryLoad = new Sensor("Virtual Memory", 1, SensorType.Load, this, settings));
2527

26-
_virtualMemoryUsed = new Sensor("Virtual Memory Used", 2, SensorType.Data, this, settings);
27-
ActivateSensor(_virtualMemoryUsed);
28+
ActivateSensor(_totalPhysicalMemory = new Sensor("Total Physical Memory", 0, SensorType.Data, this, settings));
29+
ActivateSensor(_physicalMemoryUsed = new Sensor("Memory Used", 1, SensorType.Data, this, settings));
30+
ActivateSensor(_physicalMemoryAvailable = new Sensor("Memory Available", 2, SensorType.Data, this, settings));
31+
ActivateSensor(_virtualMemoryUsed = new Sensor("Virtual Memory Used", 3, SensorType.Data, this, settings));
32+
ActivateSensor(_virtualMemoryAvailable = new Sensor("Virtual Memory Available", 4, SensorType.Data, this, settings));
33+
ActivateSensor(_commitLimit = new Sensor("Virtual memory commit limit", 5, SensorType.Data, this, settings));
34+
ActivateSensor(_currentCommit = new Sensor("Virtual memory in use", 6, SensorType.Data, this, settings));
35+
ActivateSensor(_kernelSize = new Sensor("Kernel memory usage", 7, SensorType.Data, this, settings));
2836

29-
_virtualMemoryAvailable = new Sensor("Virtual Memory Available", 3, SensorType.Data, this, settings);
30-
ActivateSensor(_virtualMemoryAvailable);
31-
32-
_virtualMemoryLoad = new Sensor("Virtual Memory", 1, SensorType.Load, this, settings);
33-
ActivateSensor(_virtualMemoryLoad);
37+
ActivateSensor(_processCount = new Sensor("Processes", 0, SensorType.IntFactor, this, settings));
38+
ActivateSensor(_threadCount = new Sensor("Threads", 1, SensorType.IntFactor, this, settings));
39+
ActivateSensor(_handleCount = new Sensor("Handles", 2, SensorType.IntFactor, this, settings));
3440
}
3541

36-
public override HardwareType HardwareType
37-
{
38-
get { return HardwareType.Memory; }
39-
}
42+
public override HardwareType HardwareType => HardwareType.Memory;
4043

4144
public override void Update()
4245
{
4346
Kernel32.MEMORYSTATUSEX status = new() { dwLength = (uint)Marshal.SizeOf<Kernel32.MEMORYSTATUSEX>() };
47+
if (Kernel32.GlobalMemoryStatusEx(ref status))
48+
{
49+
_totalPhysicalMemory.Value = (float)status.ullTotalPhys / (1024 * 1024 * 1024);
50+
_physicalMemoryUsed.Value = (float)(status.ullTotalPhys - status.ullAvailPhys) / (1024 * 1024 * 1024);
51+
_physicalMemoryAvailable.Value = (float)status.ullAvailPhys / (1024 * 1024 * 1024);
52+
_physicalMemoryLoad.Value = 100.0f - 100.0f * status.ullAvailPhys / status.ullTotalPhys;
4453

45-
if (!Kernel32.GlobalMemoryStatusEx(ref status))
46-
return;
54+
_virtualMemoryUsed.Value =
55+
(float)(status.ullTotalPageFile - status.ullAvailPageFile) / (1024 * 1024 * 1024);
56+
_virtualMemoryAvailable.Value = (float)status.ullAvailPageFile / (1024 * 1024 * 1024);
57+
_virtualMemoryLoad.Value = 100.0f - 100.0f * status.ullAvailPageFile / status.ullTotalPageFile;
58+
}
4759

48-
_physicalMemoryUsed.Value = (float)(status.ullTotalPhys - status.ullAvailPhys) / (1024 * 1024 * 1024);
49-
_physicalMemoryAvailable.Value = (float)status.ullAvailPhys / (1024 * 1024 * 1024);
50-
_physicalMemoryLoad.Value = 100.0f - ((100.0f * status.ullAvailPhys) / status.ullTotalPhys);
60+
Kernel32.PERFORMANCE_INFORMATION performanceInfo = new() { cb = (uint)Marshal.SizeOf<Kernel32.PERFORMANCE_INFORMATION>() };
61+
if (Kernel32.GetPerformanceInfo(ref performanceInfo, performanceInfo.cb))
62+
{
63+
_commitLimit.Value = (float)performanceInfo.CommitLimit * performanceInfo.PageSize / (1024 * 1024 * 1024);
64+
_currentCommit.Value = (float)performanceInfo.CommitTotal * performanceInfo.PageSize / (1024 * 1024 * 1024);
65+
_kernelSize.Value = (float)performanceInfo.KernelNonpaged * performanceInfo.PageSize / (1024 * 1024 * 1024);
5166

52-
_virtualMemoryUsed.Value = (float)(status.ullTotalPageFile - status.ullAvailPageFile) / (1024 * 1024 * 1024);
53-
_virtualMemoryAvailable.Value = (float)status.ullAvailPageFile / (1024 * 1024 * 1024);
54-
_virtualMemoryLoad.Value = 100.0f - ((100.0f * status.ullAvailPageFile) / status.ullTotalPageFile);
67+
_processCount.Value = performanceInfo.ProcessCount;
68+
_threadCount.Value = performanceInfo.ThreadCount;
69+
_handleCount.Value = performanceInfo.HandleCount;
70+
}
5571
}
5672
}

OpenHardwareMonitorLib/Interop/Kernel32.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ internal static SafeFileHandle OpenDevice(string devicePath)
9393
[return: MarshalAs(UnmanagedType.Bool)]
9494
internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
9595

96+
[DllImport(DllName, CharSet = CharSet.Auto, SetLastError = true, EntryPoint = "K32GetPerformanceInfo")]
97+
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
98+
[return: MarshalAs(UnmanagedType.Bool)]
99+
internal static extern bool GetPerformanceInfo(ref PERFORMANCE_INFORMATION pPerformanceInformation, uint cb);
100+
96101
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
97102
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
98103
internal static extern SafeFileHandle CreateFile
@@ -697,6 +702,25 @@ internal struct MEMORYSTATUSEX
697702
public ulong ullAvailExtendedVirtual;
698703
}
699704

705+
[StructLayout(LayoutKind.Sequential)]
706+
public struct PERFORMANCE_INFORMATION
707+
{
708+
public uint cb;
709+
public nint CommitTotal;
710+
public nint CommitLimit;
711+
public nint CommitPeak;
712+
public nint PhysicalTotal;
713+
public nint PhysicalAvailable;
714+
public nint SystemCache;
715+
public nint KernelTotal;
716+
public nint KernelPaged;
717+
public nint KernelNonpaged;
718+
public nint PageSize;
719+
public uint HandleCount;
720+
public uint ProcessCount;
721+
public uint ThreadCount;
722+
}
723+
700724
[StructLayout(LayoutKind.Sequential, Pack = 1)]
701725
public struct SMART_ATTRIBUTE
702726
{
@@ -895,7 +919,7 @@ internal struct STORAGE_DEVICE_DESCRIPTOR
895919
public STORAGE_BUS_TYPE BusType;
896920
public uint RawPropertiesLength;
897921
}
898-
922+
899923
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
900924
public struct DISK_PERFORMANCE
901925
{

0 commit comments

Comments
 (0)