RAM Code Refactoring (#108)

RAM Code Refactoring
Added Virtual Memory

Thanks to @aquacomputer
This commit is contained in:
Aqua Computer
2019-07-25 19:00:05 +02:00
committed by Phyxion
parent 95d1b8d766
commit e22a7cba64
4 changed files with 77 additions and 68 deletions
+38 -55
View File
@@ -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 <mmoeller@openhardwaremonitor.org>
*/
// 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<Kernel32.MemoryStatusEx>();
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;
}
}
}
}
+5 -12
View File
@@ -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 <mmoeller@openhardwaremonitor.org>
*/
// 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();
}
}
}
}
+32
View File
@@ -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);
}
}
+2 -1
View File
@@ -21,7 +21,7 @@
</PropertyGroup>
<ItemGroup>
<None Include="LICENSE" Pack="true" Visible="false" PackagePath=""/>
<None Include="LICENSE" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
@@ -75,6 +75,7 @@
<Compile Include="Hardware\Nvidia\NvmlReturn.cs" />
<Compile Include="Hardware\Opcode.cs" />
<Compile Include="Hardware\RAM\GenericRAM.cs" />
<Compile Include="Interop\Kernel32.cs" />
<Compile Include="Hardware\RAM\RAMGroup.cs" />
<Compile Include="Hardware\Ring0.cs" />
<Compile Include="Hardware\KernelDriver.cs" />