Hardware/CPU/CPULoad.cs
author moel.mich
Mon, 02 Jan 2012 20:45:18 +0000
changeset 329 756af5ee409e
parent 195 0ee888c485d5
child 344 3145aadca3d2
permissions -rw-r--r--
Small corrections for AMD family 15h CPUs (based on documentation).
     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-2011
    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.Runtime.InteropServices;
    40 
    41 namespace OpenHardwareMonitor.Hardware.CPU {
    42   internal class CPULoad {
    43 
    44     [StructLayout(LayoutKind.Sequential)]
    45     protected struct SystemProcessorPerformanceInformation {
    46       public long IdleTime;
    47       public long KernelTime;
    48       public long UserTime;
    49       public long Reserved0;
    50       public long Reserved1;
    51       public ulong Reserved2;
    52     }
    53 
    54     protected enum SystemInformationClass {
    55       SystemBasicInformation = 0,
    56       SystemCpuInformation = 1,
    57       SystemPerformanceInformation = 2,
    58       SystemTimeOfDayInformation = 3,
    59       SystemProcessInformation = 5,
    60       SystemProcessorPerformanceInformation = 8
    61     }
    62 
    63     private readonly CPUID[][] cpuid;
    64 
    65     private long[] idleTimes;
    66     private long[] totalTimes;
    67 
    68     private float totalLoad;
    69     private readonly float[] coreLoads;
    70 
    71     private readonly bool available;
    72 
    73     private static bool GetTimes(out long[] idle, out long[] total) {      
    74       SystemProcessorPerformanceInformation[] informations = new
    75         SystemProcessorPerformanceInformation[64];
    76 
    77       int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
    78 
    79       idle = null;
    80       total = null;
    81 
    82       IntPtr returnLength;
    83       if (NativeMethods.NtQuerySystemInformation(
    84         SystemInformationClass.SystemProcessorPerformanceInformation,
    85         informations, informations.Length * size, out returnLength) != 0)
    86         return false;
    87 
    88       idle = new long[(int)returnLength / size];
    89       total = new long[(int)returnLength / size];
    90 
    91       for (int i = 0; i < idle.Length; i++) {
    92         idle[i] = informations[i].IdleTime;
    93         total[i] = informations[i].KernelTime + informations[i].UserTime;
    94       }
    95 
    96       return true;
    97     }
    98 
    99     public CPULoad(CPUID[][] cpuid) {
   100       this.cpuid = cpuid;
   101       this.coreLoads = new float[cpuid.Length];         
   102       this.totalLoad = 0;
   103       try {
   104         GetTimes(out idleTimes, out totalTimes);
   105       } catch (Exception) {
   106         this.idleTimes = null;
   107         this.totalTimes = null;
   108       }
   109       if (idleTimes != null)
   110         available = true;
   111     }
   112 
   113     public bool IsAvailable {
   114       get { return available; }
   115     }
   116 
   117     public float GetTotalLoad() {
   118       return totalLoad;
   119     }
   120 
   121     public float GetCoreLoad(int core) {
   122       return coreLoads[core];
   123     }
   124 
   125     public void Update() {
   126       if (this.idleTimes == null)
   127         return;
   128 
   129       long[] newIdleTimes;
   130       long[] newTotalTimes;
   131 
   132       if (!GetTimes(out newIdleTimes, out newTotalTimes))
   133         return;
   134 
   135       for (int i = 0; i < Math.Min(newTotalTimes.Length, totalTimes.Length); i++) 
   136         if (newTotalTimes[i] - this.totalTimes[i] < 100000)
   137           return;
   138 
   139       if (newIdleTimes == null || newTotalTimes == null)
   140         return;
   141 
   142       float total = 0;
   143       int count = 0;
   144       for (int i = 0; i < cpuid.Length; i++) {
   145         float value = 0;
   146         for (int j = 0; j < cpuid[i].Length; j++) {
   147           long index = cpuid[i][j].Thread;
   148           if (index < newIdleTimes.Length && index < totalTimes.Length) {
   149             float idle = 
   150               (float)(newIdleTimes[index] - this.idleTimes[index]) /
   151               (float)(newTotalTimes[index] - this.totalTimes[index]);
   152             value += idle;
   153             total += idle;
   154             count++;
   155           }
   156         }
   157         value = 1.0f - value / cpuid[i].Length;
   158         value = value < 0 ? 0 : value;
   159         coreLoads[i] = value * 100;
   160       }
   161       if (count > 0) {
   162         total = 1.0f - total / count;
   163         total = total < 0 ? 0 : total;
   164       } else {
   165         total = 0;
   166       }
   167       this.totalLoad = total * 100;
   168 
   169       this.totalTimes = newTotalTimes;
   170       this.idleTimes = newIdleTimes;
   171     }
   172 
   173     protected static class NativeMethods {
   174 
   175       [DllImport("ntdll.dll")]
   176       public static extern int NtQuerySystemInformation(
   177         SystemInformationClass informationClass,
   178         [Out] SystemProcessorPerformanceInformation[] informations,
   179         int structSize, out IntPtr returnLength);
   180     }
   181   }
   182 }