Hardware/CPU/CPULoad.cs
author moel.mich
Sat, 24 Apr 2010 19:59:52 +0000
changeset 90 3333b29a1746
parent 26 0e01b63e1fdc
child 99 6d8377af9fb1
permissions -rw-r--r--
Implemented APIC based CPU enumeration (Issue 41).
     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   public 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     [DllImport("ntdll.dll")]
    66     private static extern int NtQuerySystemInformation(
    67       SystemInformationClass informationClass,
    68       [Out] SystemProcessorPerformanceInformation[] informations, 
    69       int structSize, out IntPtr returnLength);
    70 
    71     private CPUID[][] cpuid;
    72 
    73     private long systemTime;
    74     private long[] idleTimes;
    75 
    76     private float totalLoad;
    77     private float[] coreLoads;
    78 
    79     private bool available = false;
    80 
    81     private long[] GetIdleTimes() {
    82       long[] result = new long[cpuid.Length * cpuid[0].Length];
    83       SystemProcessorPerformanceInformation[] informations = new
    84        SystemProcessorPerformanceInformation[result.Length];
    85 
    86       IntPtr returnLength;
    87       NtQuerySystemInformation(
    88         SystemInformationClass.SystemProcessorPerformanceInformation,
    89         informations, informations.Length * 
    90         Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation)),
    91         out returnLength);
    92 
    93       for (int i = 0; i < result.Length; i++)
    94         result[i] = informations[i].IdleTime;
    95 
    96       return result;
    97     }
    98 
    99     public CPULoad(CPUID[][] cpuid) {
   100       this.cpuid = cpuid;
   101       this.coreLoads = new float[cpuid.Length];         
   102       this.systemTime = DateTime.Now.Ticks;
   103       this.totalLoad = 0;
   104       try {
   105         this.idleTimes = GetIdleTimes();
   106       } catch (Exception) {
   107         this.idleTimes = 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 systemTime = DateTime.Now.Ticks;
   130       long[] idleTimes = GetIdleTimes();
   131 
   132       if (systemTime - this.systemTime < 10000)
   133         return;
   134 
   135       float total = 0;
   136       int count = 0;
   137       for (int i = 0; i < cpuid.Length; i++) {
   138         float value = 0;
   139         for (int j = 0; j < cpuid[i].Length; j++) {
   140           long index = cpuid[i][j].Thread;
   141           long delta = idleTimes[index] - this.idleTimes[index];
   142           value += delta;
   143           total += delta;
   144           count++;
   145         }
   146         value = 1.0f - value / (cpuid[i].Length * 
   147           (systemTime - this.systemTime));
   148         value = value < 0 ? 0 : value;
   149         coreLoads[i] = value * 100;
   150       }
   151       total = 1.0f - total / (count * (systemTime - this.systemTime));
   152       total = total < 0 ? 0 : total;
   153       this.totalLoad = total * 100;
   154 
   155       this.systemTime = systemTime;
   156       this.idleTimes = idleTimes;
   157     }
   158 
   159   }
   160 }