moel@26: /*
moel@26:   
moel@26:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@26: 
moel@26:   The contents of this file are subject to the Mozilla Public License Version
moel@26:   1.1 (the "License"); you may not use this file except in compliance with
moel@26:   the License. You may obtain a copy of the License at
moel@26:  
moel@26:   http://www.mozilla.org/MPL/
moel@26: 
moel@26:   Software distributed under the License is distributed on an "AS IS" basis,
moel@26:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@26:   for the specific language governing rights and limitations under the License.
moel@26: 
moel@26:   The Original Code is the Open Hardware Monitor code.
moel@26: 
moel@26:   The Initial Developer of the Original Code is 
moel@26:   Michael Möller <m.moeller@gmx.ch>.
moel@26:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@26:   the Initial Developer. All Rights Reserved.
moel@26: 
moel@26:   Contributor(s):
moel@26: 
moel@26:   Alternatively, the contents of this file may be used under the terms of
moel@26:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@26:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@26:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@26:   of those above. If you wish to allow use of your version of this file only
moel@26:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@26:   use your version of this file under the terms of the MPL, indicate your
moel@26:   decision by deleting the provisions above and replace them with the notice
moel@26:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@26:   the provisions above, a recipient may use your version of this file under
moel@26:   the terms of any one of the MPL, the GPL or the LGPL.
moel@26:  
moel@26: */
moel@26: 
moel@26: using System;
moel@26: using System.Collections.Generic;
moel@26: using System.Runtime.InteropServices;
moel@26: using System.Text;
moel@26: 
moel@26: namespace OpenHardwareMonitor.Hardware.CPU {
moel@165:   internal class CPULoad {
moel@26: 
moel@26:     [StructLayout(LayoutKind.Sequential)]
moel@26:     private struct SystemProcessorPerformanceInformation {
moel@26:       public long IdleTime;
moel@26:       public long KernelTime;
moel@26:       public long UserTime;
moel@26:       public long Reserved0;
moel@26:       public long Reserved1;
moel@26:       public ulong Reserved2;
moel@26:     }
moel@26: 
moel@26:     private enum SystemInformationClass : int {
moel@26:       SystemBasicInformation = 0,
moel@26:       SystemCpuInformation = 1,
moel@26:       SystemPerformanceInformation = 2,
moel@26:       SystemTimeOfDayInformation = 3,
moel@26:       SystemProcessInformation = 5,
moel@26:       SystemProcessorPerformanceInformation = 8
moel@26:     }
moel@26: 
moel@90:     private CPUID[][] cpuid;
moel@26: 
moel@26:     private long systemTime;
moel@26:     private long[] idleTimes;
moel@26: 
moel@26:     private float totalLoad;
moel@26:     private float[] coreLoads;
moel@26: 
moel@26:     private bool available = false;
moel@26: 
moel@167:     private static long[] GetIdleTimes() {      
moel@26:       SystemProcessorPerformanceInformation[] informations = new
moel@99:         SystemProcessorPerformanceInformation[64];
moel@99: 
moel@99:       int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
moel@26: 
moel@26:       IntPtr returnLength;
moel@167:       if (NativeMethods.NtQuerySystemInformation(
moel@26:         SystemInformationClass.SystemProcessorPerformanceInformation,
moel@167:         informations, informations.Length * size, out returnLength) != 0)
moel@167:         return null;
moel@99: 
moel@99:       long[] result = new long[(int)returnLength / size];
moel@26: 
moel@26:       for (int i = 0; i < result.Length; i++)
moel@26:         result[i] = informations[i].IdleTime;
moel@26: 
moel@26:       return result;
moel@26:     }
moel@26: 
moel@90:     public CPULoad(CPUID[][] cpuid) {
moel@90:       this.cpuid = cpuid;
moel@90:       this.coreLoads = new float[cpuid.Length];         
moel@26:       this.systemTime = DateTime.Now.Ticks;
moel@26:       this.totalLoad = 0;
moel@26:       try {
moel@26:         this.idleTimes = GetIdleTimes();
moel@26:       } catch (Exception) {
moel@26:         this.idleTimes = null;
moel@26:       }
moel@26:       if (idleTimes != null)
moel@26:         available = true;
moel@26:     }
moel@26: 
moel@26:     public bool IsAvailable {
moel@26:       get { return available; }
moel@26:     }
moel@26: 
moel@26:     public float GetTotalLoad() {
moel@26:       return totalLoad;
moel@26:     }
moel@26: 
moel@26:     public float GetCoreLoad(int core) {
moel@26:       return coreLoads[core];
moel@26:     }
moel@26: 
moel@26:     public void Update() {
moel@26:       if (this.idleTimes == null)
moel@26:         return;
moel@26: 
moel@167:       long newSystemTime = DateTime.Now.Ticks;
moel@167:       long[] newIdleTimes = GetIdleTimes();
moel@26: 
moel@167:       if (newSystemTime - this.systemTime < 10000)
moel@167:         return;
moel@167: 
moel@167:       if (newIdleTimes == null)
moel@26:         return;
moel@26: 
moel@26:       float total = 0;
moel@90:       int count = 0;
moel@90:       for (int i = 0; i < cpuid.Length; i++) {
moel@26:         float value = 0;
moel@90:         for (int j = 0; j < cpuid[i].Length; j++) {
moel@90:           long index = cpuid[i][j].Thread;
moel@167:           if (index < newIdleTimes.Length) {
moel@167:             long delta = newIdleTimes[index] - this.idleTimes[index];
moel@99:             value += delta;
moel@99:             total += delta;
moel@99:             count++;
moel@99:           }
moel@26:         }
moel@90:         value = 1.0f - value / (cpuid[i].Length * 
moel@167:           (newSystemTime - this.systemTime));
moel@26:         value = value < 0 ? 0 : value;
moel@26:         coreLoads[i] = value * 100;
moel@26:       }
moel@99:       if (count > 0) {
moel@167:         total = 1.0f - total / (count * (newSystemTime - this.systemTime));
moel@99:         total = total < 0 ? 0 : total;
moel@99:       } else {
moel@99:         total = 0;
moel@99:       }
moel@26:       this.totalLoad = total * 100;
moel@26: 
moel@167:       this.systemTime = newSystemTime;
moel@167:       this.idleTimes = newIdleTimes;
moel@26:     }
moel@26: 
moel@167:     private static class NativeMethods {
moel@167: 
moel@167:       [DllImport("ntdll.dll")]
moel@167:       public static extern int NtQuerySystemInformation(
moel@167:         SystemInformationClass informationClass,
moel@167:         [Out] SystemProcessorPerformanceInformation[] informations,
moel@167:         int structSize, out IntPtr returnLength);
moel@167:     }
moel@26:   }
moel@26: }