Hardware/RAM/GenericRAM.cs
author sl
Sun, 03 Feb 2013 19:06:01 +0100
changeset 392 8af90fa0940f
parent 372 2de3a3e5f0b5
permissions -rw-r--r--
Now fetching some of our Sound Graph DLL function pointers.
moel@370
     1
/*
moel@370
     2
 
moel@370
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@370
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@370
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@370
     6
 
moel@370
     7
  Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@370
     8
	
moel@370
     9
*/
moel@370
    10
moel@372
    11
using System.Runtime.InteropServices;
moel@370
    12
moel@370
    13
namespace OpenHardwareMonitor.Hardware.RAM {
moel@370
    14
  internal class GenericRAM : Hardware {
moel@370
    15
moel@370
    16
    private Sensor loadSensor;
moel@377
    17
    private Sensor usedMemory;
moel@370
    18
    private Sensor availableMemory;
moel@370
    19
moel@370
    20
    public GenericRAM(string name, ISettings settings)
moel@370
    21
      : base(name, new Identifier("ram"), settings)
moel@370
    22
    {   
moel@370
    23
      loadSensor = new Sensor("Memory", 0, SensorType.Load, this, settings);
moel@370
    24
      ActivateSensor(loadSensor);
moel@370
    25
moel@377
    26
      usedMemory = new Sensor("Used Memory", 0, SensorType.Data, this,
moel@377
    27
        settings);
moel@377
    28
      ActivateSensor(usedMemory);
moel@377
    29
moel@377
    30
      availableMemory = new Sensor("Available Memory", 1, SensorType.Data, this, 
moel@372
    31
        settings);
moel@370
    32
      ActivateSensor(availableMemory);
moel@370
    33
    }
moel@370
    34
moel@370
    35
    public override HardwareType HardwareType {
moel@370
    36
      get {
moel@370
    37
        return HardwareType.RAM;
moel@370
    38
      }
moel@370
    39
    }
moel@370
    40
moel@370
    41
    public override void Update() {
moel@372
    42
      NativeMethods.MemoryStatusEx status = new NativeMethods.MemoryStatusEx();
moel@372
    43
      status.Length = checked((uint)Marshal.SizeOf(
moel@372
    44
          typeof(NativeMethods.MemoryStatusEx)));
moel@370
    45
moel@372
    46
      if (!NativeMethods.GlobalMemoryStatusEx(ref status))
moel@372
    47
        return;
moel@372
    48
moel@372
    49
      loadSensor.Value = 100.0f -
moel@372
    50
        (100.0f * status.AvailablePhysicalMemory) /
moel@372
    51
        status.TotalPhysicalMemory;
moel@372
    52
moel@377
    53
      usedMemory.Value = (float)(status.TotalPhysicalMemory 
moel@377
    54
        - status.AvailablePhysicalMemory) / (1024 * 1024 * 1024);
moel@377
    55
moel@372
    56
      availableMemory.Value = (float)status.AvailablePhysicalMemory /
moel@370
    57
        (1024 * 1024 * 1024);
moel@370
    58
    }
moel@372
    59
moel@372
    60
    private class NativeMethods {
moel@372
    61
      [StructLayout(LayoutKind.Sequential)]
moel@372
    62
      public struct MemoryStatusEx {
moel@372
    63
        public uint Length;
moel@372
    64
        public uint MemoryLoad;
moel@372
    65
        public ulong TotalPhysicalMemory;
moel@372
    66
        public ulong AvailablePhysicalMemory;
moel@372
    67
        public ulong TotalPageFile;
moel@372
    68
        public ulong AvailPageFile;
moel@372
    69
        public ulong TotalVirtual;
moel@372
    70
        public ulong AvailVirtual;
moel@372
    71
        public ulong AvailExtendedVirtual;
moel@372
    72
      }
moel@372
    73
moel@372
    74
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
moel@372
    75
      [return: MarshalAs(UnmanagedType.Bool)]
moel@372
    76
      internal static extern bool GlobalMemoryStatusEx(
moel@372
    77
        ref NativeMethods.MemoryStatusEx buffer);
moel@372
    78
    }
moel@370
    79
  }
moel@370
    80
}