Hardware/CPU/CPULoad.cs
author moel.mich
Tue, 25 May 2010 18:57:28 +0000
changeset 127 76aaf45a01c7
parent 90 3333b29a1746
child 165 813d8bc3192f
permissions -rw-r--r--
Added a workaround for the "You must keep the stream open for the lifetime of the Image." problem of the Image.FromStream method. This also reduced the overall memory usage (private working set).
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@99
    81
    private long[] GetIdleTimes() {      
moel@26
    82
      SystemProcessorPerformanceInformation[] informations = new
moel@99
    83
        SystemProcessorPerformanceInformation[64];
moel@99
    84
moel@99
    85
      int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
moel@26
    86
moel@26
    87
      IntPtr returnLength;
moel@26
    88
      NtQuerySystemInformation(
moel@26
    89
        SystemInformationClass.SystemProcessorPerformanceInformation,
moel@99
    90
        informations, informations.Length * size, out returnLength);
moel@99
    91
moel@99
    92
      long[] result = new long[(int)returnLength / size];
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@90
   100
    public CPULoad(CPUID[][] cpuid) {
moel@90
   101
      this.cpuid = cpuid;
moel@90
   102
      this.coreLoads = new float[cpuid.Length];         
moel@26
   103
      this.systemTime = DateTime.Now.Ticks;
moel@26
   104
      this.totalLoad = 0;
moel@26
   105
      try {
moel@26
   106
        this.idleTimes = GetIdleTimes();
moel@26
   107
      } catch (Exception) {
moel@26
   108
        this.idleTimes = null;
moel@26
   109
      }
moel@26
   110
      if (idleTimes != null)
moel@26
   111
        available = true;
moel@26
   112
    }
moel@26
   113
moel@26
   114
    public bool IsAvailable {
moel@26
   115
      get { return available; }
moel@26
   116
    }
moel@26
   117
moel@26
   118
    public float GetTotalLoad() {
moel@26
   119
      return totalLoad;
moel@26
   120
    }
moel@26
   121
moel@26
   122
    public float GetCoreLoad(int core) {
moel@26
   123
      return coreLoads[core];
moel@26
   124
    }
moel@26
   125
moel@26
   126
    public void Update() {
moel@26
   127
      if (this.idleTimes == null)
moel@26
   128
        return;
moel@26
   129
moel@26
   130
      long systemTime = DateTime.Now.Ticks;
moel@26
   131
      long[] idleTimes = GetIdleTimes();
moel@26
   132
moel@26
   133
      if (systemTime - this.systemTime < 10000)
moel@26
   134
        return;
moel@26
   135
moel@26
   136
      float total = 0;
moel@90
   137
      int count = 0;
moel@90
   138
      for (int i = 0; i < cpuid.Length; i++) {
moel@26
   139
        float value = 0;
moel@90
   140
        for (int j = 0; j < cpuid[i].Length; j++) {
moel@90
   141
          long index = cpuid[i][j].Thread;
moel@99
   142
          if (index < idleTimes.Length) {
moel@99
   143
            long delta = idleTimes[index] - this.idleTimes[index];
moel@99
   144
            value += delta;
moel@99
   145
            total += delta;
moel@99
   146
            count++;
moel@99
   147
          }
moel@26
   148
        }
moel@90
   149
        value = 1.0f - value / (cpuid[i].Length * 
moel@26
   150
          (systemTime - this.systemTime));
moel@26
   151
        value = value < 0 ? 0 : value;
moel@26
   152
        coreLoads[i] = value * 100;
moel@26
   153
      }
moel@99
   154
      if (count > 0) {
moel@99
   155
        total = 1.0f - total / (count * (systemTime - this.systemTime));
moel@99
   156
        total = total < 0 ? 0 : total;
moel@99
   157
      } else {
moel@99
   158
        total = 0;
moel@99
   159
      }
moel@26
   160
      this.totalLoad = total * 100;
moel@26
   161
moel@26
   162
      this.systemTime = systemTime;
moel@26
   163
      this.idleTimes = idleTimes;
moel@26
   164
    }
moel@26
   165
moel@26
   166
  }
moel@26
   167
}