diff --git a/Hardware/RAM/GenericRAM.cs b/Hardware/RAM/GenericRAM.cs index b3be40d..1f93968 100644 --- a/Hardware/RAM/GenericRAM.cs +++ b/Hardware/RAM/GenericRAM.cs @@ -1,35 +1,41 @@ -/* - - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. - - Copyright (C) 2012 Michael Möller - -*/ +// Mozilla Public License 2.0 +// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +// Copyright (C) LibreHardwareMonitor and Contributors +// All Rights Reserved using System.Runtime.InteropServices; +using OpenHardwareMonitor.Interop; namespace OpenHardwareMonitor.Hardware.RAM { internal class GenericRAM : Hardware { - private Sensor loadSensor; - private Sensor usedMemory; - private Sensor availableMemory; + private Sensor physicalMemoryUsed; + private Sensor physicalMemoryAvailable; + private Sensor physicalMemoryLoad; + private Sensor virtualMemoryUsed; + private Sensor virtualMemoryAvailable; + private Sensor virtualMemoryLoad; public GenericRAM(string name, ISettings settings) - : base(name, new Identifier("ram"), settings) - { - loadSensor = new Sensor("Memory", 0, SensorType.Load, this, settings); - ActivateSensor(loadSensor); + : base(name, new Identifier("ram"), settings) { - usedMemory = new Sensor("Used Memory", 0, SensorType.Data, this, - settings); - ActivateSensor(usedMemory); + physicalMemoryUsed = new Sensor("Memory Used", 0, SensorType.Data, this, settings); + ActivateSensor(physicalMemoryUsed); - availableMemory = new Sensor("Available Memory", 1, SensorType.Data, this, - settings); - ActivateSensor(availableMemory); + physicalMemoryAvailable = new Sensor("Memory Available", 1, SensorType.Data, this, settings); + ActivateSensor(physicalMemoryAvailable); + + physicalMemoryLoad = new Sensor("Memory", 0, SensorType.Load, this, settings); + ActivateSensor(physicalMemoryLoad); + + virtualMemoryUsed = new Sensor("Virtual Memory Used", 2, SensorType.Data, this, settings); + ActivateSensor(virtualMemoryUsed); + + virtualMemoryAvailable = new Sensor("Virtual Memory Available", 3, SensorType.Data, this, settings); + ActivateSensor(virtualMemoryAvailable); + + virtualMemoryLoad = new Sensor("Virtual Memory", 1, SensorType.Load, this, settings); + ActivateSensor(virtualMemoryLoad); } public override HardwareType HardwareType { @@ -39,42 +45,19 @@ namespace OpenHardwareMonitor.Hardware.RAM { } public override void Update() { - NativeMethods.MemoryStatusEx status = new NativeMethods.MemoryStatusEx(); - status.Length = checked((uint)Marshal.SizeOf( - typeof(NativeMethods.MemoryStatusEx))); + Kernel32.MemoryStatusEx status = new Kernel32.MemoryStatusEx(); + status.Length = (uint)Marshal.SizeOf(); - if (!NativeMethods.GlobalMemoryStatusEx(ref status)) + if (!Kernel32.GlobalMemoryStatusEx(ref status)) return; - loadSensor.Value = 100.0f - - (100.0f * status.AvailablePhysicalMemory) / - status.TotalPhysicalMemory; + physicalMemoryUsed.Value = (float)(status.TotalPhysicalMemory - status.AvailablePhysicalMemory) / (1024 * 1024 * 1024); + physicalMemoryAvailable.Value = (float)status.AvailablePhysicalMemory / (1024 * 1024 * 1024); + physicalMemoryLoad.Value = 100.0f - (100.0f * status.AvailablePhysicalMemory) / status.TotalPhysicalMemory; - usedMemory.Value = (float)(status.TotalPhysicalMemory - - status.AvailablePhysicalMemory) / (1024 * 1024 * 1024); - - availableMemory.Value = (float)status.AvailablePhysicalMemory / - (1024 * 1024 * 1024); - } - - private class NativeMethods { - [StructLayout(LayoutKind.Sequential)] - public struct MemoryStatusEx { - public uint Length; - public uint MemoryLoad; - public ulong TotalPhysicalMemory; - public ulong AvailablePhysicalMemory; - public ulong TotalPageFile; - public ulong AvailPageFile; - public ulong TotalVirtual; - public ulong AvailVirtual; - public ulong AvailExtendedVirtual; - } - - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool GlobalMemoryStatusEx( - ref NativeMethods.MemoryStatusEx buffer); + virtualMemoryUsed.Value = (float)(status.TotalPageFile - status.AvailPageFile) / (1024 * 1024 * 1024); + virtualMemoryAvailable.Value = (float)status.AvailPageFile / (1024 * 1024 * 1024); + virtualMemoryLoad.Value = 100.0f - (100.0f * status.AvailPageFile) / status.TotalPageFile; } } -} +} \ No newline at end of file diff --git a/Hardware/RAM/RAMGroup.cs b/Hardware/RAM/RAMGroup.cs index a3706fe..7179b4f 100644 --- a/Hardware/RAM/RAMGroup.cs +++ b/Hardware/RAM/RAMGroup.cs @@ -1,12 +1,7 @@ -/* - - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. - - Copyright (C) 2012-2013 Michael Möller - -*/ +// Mozilla Public License 2.0 +// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +// Copyright (C) LibreHardwareMonitor and Contributors +// All Rights Reserved using System; using System.Collections.Generic; @@ -19,12 +14,10 @@ namespace OpenHardwareMonitor.Hardware.RAM { public RAMGroup(SMBIOS smbios, ISettings settings) { // No implementation for RAM on Unix systems - if (Software.OperatingSystem.IsLinux) { hardware = new Hardware[0]; return; } - hardware = new Hardware[] { new GenericRAM("Generic Memory", settings) }; } @@ -39,4 +32,4 @@ namespace OpenHardwareMonitor.Hardware.RAM { ram.Close(); } } -} +} \ No newline at end of file diff --git a/Interop/Kernel32.cs b/Interop/Kernel32.cs new file mode 100644 index 0000000..24d5211 --- /dev/null +++ b/Interop/Kernel32.cs @@ -0,0 +1,32 @@ +// Mozilla Public License 2.0 +// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +// Copyright (C) LibreHardwareMonitor and Contributors +// All Rights Reserved + +using System; +using System.IO; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace OpenHardwareMonitor.Interop { + internal class Kernel32 { + private const string KERNEL = "kernel32.dll"; + + [StructLayout(LayoutKind.Sequential)] + public struct MemoryStatusEx { + public uint Length; + public uint MemoryLoad; + public ulong TotalPhysicalMemory; + public ulong AvailablePhysicalMemory; + public ulong TotalPageFile; + public ulong AvailPageFile; + public ulong TotalVirtual; + public ulong AvailVirtual; + public ulong AvailExtendedVirtual; + } + + [DllImport(KERNEL, CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool GlobalMemoryStatusEx(ref MemoryStatusEx buffer); + } +} \ No newline at end of file diff --git a/OpenHardwareMonitorLib.csproj b/OpenHardwareMonitorLib.csproj index f3a3b0b..13f36c5 100644 --- a/OpenHardwareMonitorLib.csproj +++ b/OpenHardwareMonitorLib.csproj @@ -21,7 +21,7 @@ - + @@ -75,6 +75,7 @@ +