Hardware/CPU/CPULoad.cs
author moel.mich
Tue, 21 Sep 2010 20:32:36 +0000
changeset 195 0ee888c485d5
parent 182 4801e9eaf979
child 313 596df85bb4e1
permissions -rw-r--r--
Refactored some of the hardware monitoring code and fixed a few code inspection warnings.
moel@26
     1
/*
moel@26
     2
  
moel@26
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@26
     4
moel@26
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@26
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@26
     7
  the License. You may obtain a copy of the License at
moel@26
     8
 
moel@26
     9
  http://www.mozilla.org/MPL/
moel@26
    10
moel@26
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@26
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@26
    13
  for the specific language governing rights and limitations under the License.
moel@26
    14
moel@26
    15
  The Original Code is the Open Hardware Monitor code.
moel@26
    16
moel@26
    17
  The Initial Developer of the Original Code is 
moel@26
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@26
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@26
    20
  the Initial Developer. All Rights Reserved.
moel@26
    21
moel@26
    22
  Contributor(s):
moel@26
    23
moel@26
    24
  Alternatively, the contents of this file may be used under the terms of
moel@26
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@26
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@26
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@26
    28
  of those above. If you wish to allow use of your version of this file only
moel@26
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@26
    30
  use your version of this file under the terms of the MPL, indicate your
moel@26
    31
  decision by deleting the provisions above and replace them with the notice
moel@26
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@26
    33
  the provisions above, a recipient may use your version of this file under
moel@26
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@26
    35
 
moel@26
    36
*/
moel@26
    37
moel@26
    38
using System;
moel@26
    39
using System.Runtime.InteropServices;
moel@26
    40
moel@26
    41
namespace OpenHardwareMonitor.Hardware.CPU {
moel@165
    42
  internal class CPULoad {
moel@26
    43
moel@26
    44
    [StructLayout(LayoutKind.Sequential)]
moel@195
    45
    protected struct SystemProcessorPerformanceInformation {
moel@26
    46
      public long IdleTime;
moel@26
    47
      public long KernelTime;
moel@26
    48
      public long UserTime;
moel@26
    49
      public long Reserved0;
moel@26
    50
      public long Reserved1;
moel@26
    51
      public ulong Reserved2;
moel@26
    52
    }
moel@26
    53
moel@195
    54
    protected enum SystemInformationClass {
moel@26
    55
      SystemBasicInformation = 0,
moel@26
    56
      SystemCpuInformation = 1,
moel@26
    57
      SystemPerformanceInformation = 2,
moel@26
    58
      SystemTimeOfDayInformation = 3,
moel@26
    59
      SystemProcessInformation = 5,
moel@26
    60
      SystemProcessorPerformanceInformation = 8
moel@26
    61
    }
moel@26
    62
moel@195
    63
    private readonly CPUID[][] cpuid;
moel@26
    64
moel@26
    65
    private long systemTime;
moel@26
    66
    private long[] idleTimes;
moel@26
    67
moel@26
    68
    private float totalLoad;
moel@195
    69
    private readonly float[] coreLoads;
moel@26
    70
moel@195
    71
    private readonly bool available;
moel@26
    72
moel@167
    73
    private static long[] GetIdleTimes() {      
moel@26
    74
      SystemProcessorPerformanceInformation[] informations = new
moel@99
    75
        SystemProcessorPerformanceInformation[64];
moel@99
    76
moel@99
    77
      int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
moel@26
    78
moel@26
    79
      IntPtr returnLength;
moel@167
    80
      if (NativeMethods.NtQuerySystemInformation(
moel@26
    81
        SystemInformationClass.SystemProcessorPerformanceInformation,
moel@167
    82
        informations, informations.Length * size, out returnLength) != 0)
moel@167
    83
        return null;
moel@99
    84
moel@99
    85
      long[] result = new long[(int)returnLength / size];
moel@26
    86
moel@26
    87
      for (int i = 0; i < result.Length; i++)
moel@26
    88
        result[i] = informations[i].IdleTime;
moel@26
    89
moel@26
    90
      return result;
moel@26
    91
    }
moel@26
    92
moel@90
    93
    public CPULoad(CPUID[][] cpuid) {
moel@90
    94
      this.cpuid = cpuid;
moel@90
    95
      this.coreLoads = new float[cpuid.Length];         
moel@26
    96
      this.systemTime = DateTime.Now.Ticks;
moel@26
    97
      this.totalLoad = 0;
moel@26
    98
      try {
moel@26
    99
        this.idleTimes = GetIdleTimes();
moel@26
   100
      } catch (Exception) {
moel@26
   101
        this.idleTimes = null;
moel@26
   102
      }
moel@26
   103
      if (idleTimes != null)
moel@26
   104
        available = true;
moel@26
   105
    }
moel@26
   106
moel@26
   107
    public bool IsAvailable {
moel@26
   108
      get { return available; }
moel@26
   109
    }
moel@26
   110
moel@26
   111
    public float GetTotalLoad() {
moel@26
   112
      return totalLoad;
moel@26
   113
    }
moel@26
   114
moel@26
   115
    public float GetCoreLoad(int core) {
moel@26
   116
      return coreLoads[core];
moel@26
   117
    }
moel@26
   118
moel@26
   119
    public void Update() {
moel@26
   120
      if (this.idleTimes == null)
moel@26
   121
        return;
moel@26
   122
moel@167
   123
      long newSystemTime = DateTime.Now.Ticks;
moel@167
   124
      long[] newIdleTimes = GetIdleTimes();
moel@26
   125
moel@167
   126
      if (newSystemTime - this.systemTime < 10000)
moel@167
   127
        return;
moel@167
   128
moel@167
   129
      if (newIdleTimes == null)
moel@26
   130
        return;
moel@26
   131
moel@26
   132
      float total = 0;
moel@90
   133
      int count = 0;
moel@90
   134
      for (int i = 0; i < cpuid.Length; i++) {
moel@26
   135
        float value = 0;
moel@90
   136
        for (int j = 0; j < cpuid[i].Length; j++) {
moel@90
   137
          long index = cpuid[i][j].Thread;
moel@167
   138
          if (index < newIdleTimes.Length) {
moel@167
   139
            long delta = newIdleTimes[index] - this.idleTimes[index];
moel@99
   140
            value += delta;
moel@99
   141
            total += delta;
moel@99
   142
            count++;
moel@99
   143
          }
moel@26
   144
        }
moel@90
   145
        value = 1.0f - value / (cpuid[i].Length * 
moel@167
   146
          (newSystemTime - this.systemTime));
moel@26
   147
        value = value < 0 ? 0 : value;
moel@26
   148
        coreLoads[i] = value * 100;
moel@26
   149
      }
moel@99
   150
      if (count > 0) {
moel@167
   151
        total = 1.0f - total / (count * (newSystemTime - this.systemTime));
moel@99
   152
        total = total < 0 ? 0 : total;
moel@99
   153
      } else {
moel@99
   154
        total = 0;
moel@99
   155
      }
moel@26
   156
      this.totalLoad = total * 100;
moel@26
   157
moel@167
   158
      this.systemTime = newSystemTime;
moel@167
   159
      this.idleTimes = newIdleTimes;
moel@26
   160
    }
moel@26
   161
moel@195
   162
    protected static class NativeMethods {
moel@167
   163
moel@167
   164
      [DllImport("ntdll.dll")]
moel@167
   165
      public static extern int NtQuerySystemInformation(
moel@167
   166
        SystemInformationClass informationClass,
moel@167
   167
        [Out] SystemProcessorPerformanceInformation[] informations,
moel@167
   168
        int structSize, out IntPtr returnLength);
moel@167
   169
    }
moel@26
   170
  }
moel@26
   171
}