moel@370: /*
moel@370:  
moel@370:   This Source Code Form is subject to the terms of the Mozilla Public
moel@370:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@370:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@370:  
moel@370:   Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@370: 	
moel@370: */
moel@370: 
moel@372: using System.Runtime.InteropServices;
moel@370: 
moel@370: namespace OpenHardwareMonitor.Hardware.RAM {
moel@370:   internal class GenericRAM : Hardware {
moel@370: 
moel@370:     private Sensor loadSensor;
moel@377:     private Sensor usedMemory;
moel@370:     private Sensor availableMemory;
moel@370: 
moel@370:     public GenericRAM(string name, ISettings settings)
moel@370:       : base(name, new Identifier("ram"), settings)
moel@370:     {   
moel@370:       loadSensor = new Sensor("Memory", 0, SensorType.Load, this, settings);
moel@370:       ActivateSensor(loadSensor);
moel@370: 
moel@377:       usedMemory = new Sensor("Used Memory", 0, SensorType.Data, this,
moel@377:         settings);
moel@377:       ActivateSensor(usedMemory);
moel@377: 
moel@377:       availableMemory = new Sensor("Available Memory", 1, SensorType.Data, this, 
moel@372:         settings);
moel@370:       ActivateSensor(availableMemory);
moel@370:     }
moel@370: 
moel@370:     public override HardwareType HardwareType {
moel@370:       get {
moel@370:         return HardwareType.RAM;
moel@370:       }
moel@370:     }
moel@370: 
moel@370:     public override void Update() {
moel@372:       NativeMethods.MemoryStatusEx status = new NativeMethods.MemoryStatusEx();
moel@372:       status.Length = checked((uint)Marshal.SizeOf(
moel@372:           typeof(NativeMethods.MemoryStatusEx)));
moel@370: 
moel@372:       if (!NativeMethods.GlobalMemoryStatusEx(ref status))
moel@372:         return;
moel@372: 
moel@372:       loadSensor.Value = 100.0f -
moel@372:         (100.0f * status.AvailablePhysicalMemory) /
moel@372:         status.TotalPhysicalMemory;
moel@372: 
moel@377:       usedMemory.Value = (float)(status.TotalPhysicalMemory 
moel@377:         - status.AvailablePhysicalMemory) / (1024 * 1024 * 1024);
moel@377: 
moel@372:       availableMemory.Value = (float)status.AvailablePhysicalMemory /
moel@370:         (1024 * 1024 * 1024);
moel@370:     }
moel@372: 
moel@372:     private class NativeMethods {
moel@372:       [StructLayout(LayoutKind.Sequential)]
moel@372:       public struct MemoryStatusEx {
moel@372:         public uint Length;
moel@372:         public uint MemoryLoad;
moel@372:         public ulong TotalPhysicalMemory;
moel@372:         public ulong AvailablePhysicalMemory;
moel@372:         public ulong TotalPageFile;
moel@372:         public ulong AvailPageFile;
moel@372:         public ulong TotalVirtual;
moel@372:         public ulong AvailVirtual;
moel@372:         public ulong AvailExtendedVirtual;
moel@372:       }
moel@372: 
moel@372:       [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
moel@372:       [return: MarshalAs(UnmanagedType.Bool)]
moel@372:       internal static extern bool GlobalMemoryStatusEx(
moel@372:         ref NativeMethods.MemoryStatusEx buffer);
moel@372:     }
moel@370:   }
moel@370: }