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