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