Hardware/CPU/CPULoad.cs
author moel.mich
Sun, 25 Apr 2010 14:40:19 +0000
changeset 97 1a2d7dad320e
parent 26 0e01b63e1fdc
child 99 6d8377af9fb1
permissions -rw-r--r--
Fixed Issue 43.
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@26
    44
  public 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@26
    65
    [DllImport("ntdll.dll")]
moel@26
    66
    private static extern int NtQuerySystemInformation(
moel@26
    67
      SystemInformationClass informationClass,
moel@26
    68
      [Out] SystemProcessorPerformanceInformation[] informations, 
moel@26
    69
      int structSize, out IntPtr returnLength);
moel@26
    70
moel@90
    71
    private CPUID[][] cpuid;
moel@26
    72
moel@26
    73
    private long systemTime;
moel@26
    74
    private long[] idleTimes;
moel@26
    75
moel@26
    76
    private float totalLoad;
moel@26
    77
    private float[] coreLoads;
moel@26
    78
moel@26
    79
    private bool available = false;
moel@26
    80
moel@26
    81
    private long[] GetIdleTimes() {
moel@90
    82
      long[] result = new long[cpuid.Length * cpuid[0].Length];
moel@26
    83
      SystemProcessorPerformanceInformation[] informations = new
moel@26
    84
       SystemProcessorPerformanceInformation[result.Length];
moel@26
    85
moel@26
    86
      IntPtr returnLength;
moel@26
    87
      NtQuerySystemInformation(
moel@26
    88
        SystemInformationClass.SystemProcessorPerformanceInformation,
moel@26
    89
        informations, informations.Length * 
moel@26
    90
        Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation)),
moel@26
    91
        out returnLength);
moel@26
    92
moel@26
    93
      for (int i = 0; i < result.Length; i++)
moel@26
    94
        result[i] = informations[i].IdleTime;
moel@26
    95
moel@26
    96
      return result;
moel@26
    97
    }
moel@26
    98
moel@90
    99
    public CPULoad(CPUID[][] cpuid) {
moel@90
   100
      this.cpuid = cpuid;
moel@90
   101
      this.coreLoads = new float[cpuid.Length];         
moel@26
   102
      this.systemTime = DateTime.Now.Ticks;
moel@26
   103
      this.totalLoad = 0;
moel@26
   104
      try {
moel@26
   105
        this.idleTimes = GetIdleTimes();
moel@26
   106
      } catch (Exception) {
moel@26
   107
        this.idleTimes = null;
moel@26
   108
      }
moel@26
   109
      if (idleTimes != null)
moel@26
   110
        available = true;
moel@26
   111
    }
moel@26
   112
moel@26
   113
    public bool IsAvailable {
moel@26
   114
      get { return available; }
moel@26
   115
    }
moel@26
   116
moel@26
   117
    public float GetTotalLoad() {
moel@26
   118
      return totalLoad;
moel@26
   119
    }
moel@26
   120
moel@26
   121
    public float GetCoreLoad(int core) {
moel@26
   122
      return coreLoads[core];
moel@26
   123
    }
moel@26
   124
moel@26
   125
    public void Update() {
moel@26
   126
      if (this.idleTimes == null)
moel@26
   127
        return;
moel@26
   128
moel@26
   129
      long systemTime = DateTime.Now.Ticks;
moel@26
   130
      long[] idleTimes = GetIdleTimes();
moel@26
   131
moel@26
   132
      if (systemTime - this.systemTime < 10000)
moel@26
   133
        return;
moel@26
   134
moel@26
   135
      float total = 0;
moel@90
   136
      int count = 0;
moel@90
   137
      for (int i = 0; i < cpuid.Length; i++) {
moel@26
   138
        float value = 0;
moel@90
   139
        for (int j = 0; j < cpuid[i].Length; j++) {
moel@90
   140
          long index = cpuid[i][j].Thread;
moel@26
   141
          long delta = idleTimes[index] - this.idleTimes[index];
moel@26
   142
          value += delta;
moel@26
   143
          total += delta;
moel@90
   144
          count++;
moel@26
   145
        }
moel@90
   146
        value = 1.0f - value / (cpuid[i].Length * 
moel@26
   147
          (systemTime - this.systemTime));
moel@26
   148
        value = value < 0 ? 0 : value;
moel@26
   149
        coreLoads[i] = value * 100;
moel@26
   150
      }
moel@90
   151
      total = 1.0f - total / (count * (systemTime - this.systemTime));
moel@26
   152
      total = total < 0 ? 0 : total;
moel@26
   153
      this.totalLoad = total * 100;
moel@26
   154
moel@26
   155
      this.systemTime = systemTime;
moel@26
   156
      this.idleTimes = idleTimes;
moel@26
   157
    }
moel@26
   158
moel@26
   159
  }
moel@26
   160
}