Hardware/CPU/CPULoad.cs
author moel.mich
Fri, 02 Apr 2010 16:05:07 +0000
changeset 86 b4f0f206173d
child 90 3333b29a1746
permissions -rw-r--r--
Fixed Issue 26. Added a dialog to automatically send a crash report.
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@26
    71
    private uint coreCount;
moel@26
    72
    private uint logicalProcessorsPerCore;
moel@26
    73
moel@26
    74
    private long systemTime;
moel@26
    75
    private long[] idleTimes;
moel@26
    76
moel@26
    77
    private float totalLoad;
moel@26
    78
    private float[] coreLoads;
moel@26
    79
moel@26
    80
    private bool available = false;
moel@26
    81
moel@26
    82
    private long[] GetIdleTimes() {
moel@26
    83
      long[] result = new long[coreCount * logicalProcessorsPerCore];
moel@26
    84
      SystemProcessorPerformanceInformation[] informations = new
moel@26
    85
       SystemProcessorPerformanceInformation[result.Length];
moel@26
    86
moel@26
    87
      IntPtr returnLength;
moel@26
    88
      NtQuerySystemInformation(
moel@26
    89
        SystemInformationClass.SystemProcessorPerformanceInformation,
moel@26
    90
        informations, informations.Length * 
moel@26
    91
        Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation)),
moel@26
    92
        out returnLength);
moel@26
    93
moel@26
    94
      for (int i = 0; i < result.Length; i++)
moel@26
    95
        result[i] = informations[i].IdleTime;
moel@26
    96
moel@26
    97
      return result;
moel@26
    98
    }
moel@26
    99
moel@26
   100
    public CPULoad(uint coreCount, uint logicalProcessorsPerCore) {
moel@26
   101
      this.coreCount = coreCount;
moel@26
   102
      this.logicalProcessorsPerCore = logicalProcessorsPerCore;
moel@26
   103
      this.coreLoads = new float[coreCount];         
moel@26
   104
      this.systemTime = DateTime.Now.Ticks;
moel@26
   105
      this.totalLoad = 0;
moel@26
   106
      try {
moel@26
   107
        this.idleTimes = GetIdleTimes();
moel@26
   108
      } catch (Exception) {
moel@26
   109
        this.idleTimes = null;
moel@26
   110
      }
moel@26
   111
      if (idleTimes != null)
moel@26
   112
        available = true;
moel@26
   113
    }
moel@26
   114
moel@26
   115
    public bool IsAvailable {
moel@26
   116
      get { return available; }
moel@26
   117
    }
moel@26
   118
moel@26
   119
    public float GetTotalLoad() {
moel@26
   120
      return totalLoad;
moel@26
   121
    }
moel@26
   122
moel@26
   123
    public float GetCoreLoad(int core) {
moel@26
   124
      return coreLoads[core];
moel@26
   125
    }
moel@26
   126
moel@26
   127
    public void Update() {
moel@26
   128
      if (this.idleTimes == null)
moel@26
   129
        return;
moel@26
   130
moel@26
   131
      long systemTime = DateTime.Now.Ticks;
moel@26
   132
      long[] idleTimes = GetIdleTimes();
moel@26
   133
moel@26
   134
      if (systemTime - this.systemTime < 10000)
moel@26
   135
        return;
moel@26
   136
moel@26
   137
      float total = 0;
moel@26
   138
      for (int i = 0; i < coreCount; i++) {
moel@26
   139
        float value = 0;
moel@26
   140
        for (int j = 0; j < logicalProcessorsPerCore; j++) {
moel@26
   141
          long index = i * logicalProcessorsPerCore + j;
moel@26
   142
          long delta = idleTimes[index] - this.idleTimes[index];
moel@26
   143
          value += delta;
moel@26
   144
          total += delta;
moel@26
   145
        }
moel@26
   146
        value = 1.0f - value / (logicalProcessorsPerCore * 
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@26
   151
      total = 1.0f - total / (coreCount * logicalProcessorsPerCore *
moel@26
   152
        (systemTime - this.systemTime));
moel@26
   153
      total = total < 0 ? 0 : total;
moel@26
   154
      this.totalLoad = total * 100;
moel@26
   155
moel@26
   156
      this.systemTime = systemTime;
moel@26
   157
      this.idleTimes = idleTimes;
moel@26
   158
    }
moel@26
   159
moel@26
   160
  }
moel@26
   161
}