Hardware/CPU/CPULoad.cs
author moel.mich
Tue, 24 Aug 2010 20:14:54 +0000
changeset 174 51d62ed28ff5
parent 166 fa9dfbfc4145
child 182 4801e9eaf979
permissions -rw-r--r--
Fixed Issue 105 and Issue 106.
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.Collections.Generic;
moel@26
    40
using System.Runtime.InteropServices;
moel@26
    41
using System.Text;
moel@26
    42
moel@26
    43
namespace OpenHardwareMonitor.Hardware.CPU {
moel@165
    44
  internal class CPULoad {
moel@26
    45
moel@26
    46
    [StructLayout(LayoutKind.Sequential)]
moel@26
    47
    private struct SystemProcessorPerformanceInformation {
moel@26
    48
      public long IdleTime;
moel@26
    49
      public long KernelTime;
moel@26
    50
      public long UserTime;
moel@26
    51
      public long Reserved0;
moel@26
    52
      public long Reserved1;
moel@26
    53
      public ulong Reserved2;
moel@26
    54
    }
moel@26
    55
moel@26
    56
    private enum SystemInformationClass : int {
moel@26
    57
      SystemBasicInformation = 0,
moel@26
    58
      SystemCpuInformation = 1,
moel@26
    59
      SystemPerformanceInformation = 2,
moel@26
    60
      SystemTimeOfDayInformation = 3,
moel@26
    61
      SystemProcessInformation = 5,
moel@26
    62
      SystemProcessorPerformanceInformation = 8
moel@26
    63
    }
moel@26
    64
moel@90
    65
    private CPUID[][] cpuid;
moel@26
    66
moel@26
    67
    private long systemTime;
moel@26
    68
    private long[] idleTimes;
moel@26
    69
moel@26
    70
    private float totalLoad;
moel@26
    71
    private float[] coreLoads;
moel@26
    72
moel@26
    73
    private bool available = false;
moel@26
    74
moel@167
    75
    private static long[] GetIdleTimes() {      
moel@26
    76
      SystemProcessorPerformanceInformation[] informations = new
moel@99
    77
        SystemProcessorPerformanceInformation[64];
moel@99
    78
moel@99
    79
      int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
moel@26
    80
moel@26
    81
      IntPtr returnLength;
moel@167
    82
      if (NativeMethods.NtQuerySystemInformation(
moel@26
    83
        SystemInformationClass.SystemProcessorPerformanceInformation,
moel@167
    84
        informations, informations.Length * size, out returnLength) != 0)
moel@167
    85
        return null;
moel@99
    86
moel@99
    87
      long[] result = new long[(int)returnLength / size];
moel@26
    88
moel@26
    89
      for (int i = 0; i < result.Length; i++)
moel@26
    90
        result[i] = informations[i].IdleTime;
moel@26
    91
moel@26
    92
      return result;
moel@26
    93
    }
moel@26
    94
moel@90
    95
    public CPULoad(CPUID[][] cpuid) {
moel@90
    96
      this.cpuid = cpuid;
moel@90
    97
      this.coreLoads = new float[cpuid.Length];         
moel@26
    98
      this.systemTime = DateTime.Now.Ticks;
moel@26
    99
      this.totalLoad = 0;
moel@26
   100
      try {
moel@26
   101
        this.idleTimes = GetIdleTimes();
moel@26
   102
      } catch (Exception) {
moel@26
   103
        this.idleTimes = null;
moel@26
   104
      }
moel@26
   105
      if (idleTimes != null)
moel@26
   106
        available = true;
moel@26
   107
    }
moel@26
   108
moel@26
   109
    public bool IsAvailable {
moel@26
   110
      get { return available; }
moel@26
   111
    }
moel@26
   112
moel@26
   113
    public float GetTotalLoad() {
moel@26
   114
      return totalLoad;
moel@26
   115
    }
moel@26
   116
moel@26
   117
    public float GetCoreLoad(int core) {
moel@26
   118
      return coreLoads[core];
moel@26
   119
    }
moel@26
   120
moel@26
   121
    public void Update() {
moel@26
   122
      if (this.idleTimes == null)
moel@26
   123
        return;
moel@26
   124
moel@167
   125
      long newSystemTime = DateTime.Now.Ticks;
moel@167
   126
      long[] newIdleTimes = GetIdleTimes();
moel@26
   127
moel@167
   128
      if (newSystemTime - this.systemTime < 10000)
moel@167
   129
        return;
moel@167
   130
moel@167
   131
      if (newIdleTimes == null)
moel@26
   132
        return;
moel@26
   133
moel@26
   134
      float total = 0;
moel@90
   135
      int count = 0;
moel@90
   136
      for (int i = 0; i < cpuid.Length; i++) {
moel@26
   137
        float value = 0;
moel@90
   138
        for (int j = 0; j < cpuid[i].Length; j++) {
moel@90
   139
          long index = cpuid[i][j].Thread;
moel@167
   140
          if (index < newIdleTimes.Length) {
moel@167
   141
            long delta = newIdleTimes[index] - this.idleTimes[index];
moel@99
   142
            value += delta;
moel@99
   143
            total += delta;
moel@99
   144
            count++;
moel@99
   145
          }
moel@26
   146
        }
moel@90
   147
        value = 1.0f - value / (cpuid[i].Length * 
moel@167
   148
          (newSystemTime - this.systemTime));
moel@26
   149
        value = value < 0 ? 0 : value;
moel@26
   150
        coreLoads[i] = value * 100;
moel@26
   151
      }
moel@99
   152
      if (count > 0) {
moel@167
   153
        total = 1.0f - total / (count * (newSystemTime - this.systemTime));
moel@99
   154
        total = total < 0 ? 0 : total;
moel@99
   155
      } else {
moel@99
   156
        total = 0;
moel@99
   157
      }
moel@26
   158
      this.totalLoad = total * 100;
moel@26
   159
moel@167
   160
      this.systemTime = newSystemTime;
moel@167
   161
      this.idleTimes = newIdleTimes;
moel@26
   162
    }
moel@26
   163
moel@167
   164
    private static class NativeMethods {
moel@167
   165
moel@167
   166
      [DllImport("ntdll.dll")]
moel@167
   167
      public static extern int NtQuerySystemInformation(
moel@167
   168
        SystemInformationClass informationClass,
moel@167
   169
        [Out] SystemProcessorPerformanceInformation[] informations,
moel@167
   170
        int structSize, out IntPtr returnLength);
moel@167
   171
    }
moel@26
   172
  }
moel@26
   173
}