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@313:   Portions created by the Initial Developer are Copyright (C) 2009-2011
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.Runtime.InteropServices;
moel@26: 
moel@26: namespace OpenHardwareMonitor.Hardware.CPU {
moel@165:   internal class CPULoad {
moel@26: 
moel@26:     [StructLayout(LayoutKind.Sequential)]
moel@195:     protected 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@195:     protected enum SystemInformationClass {
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@195:     private readonly CPUID[][] cpuid;
moel@26: 
moel@26:     private long[] idleTimes;
moel@313:     private long[] totalTimes;
moel@26: 
moel@26:     private float totalLoad;
moel@195:     private readonly float[] coreLoads;
moel@26: 
moel@195:     private readonly bool available;
moel@26: 
moel@313:     private static bool GetTimes(out long[] idle, out long[] total) {      
moel@26:       SystemProcessorPerformanceInformation[] informations = new
moel@99:         SystemProcessorPerformanceInformation[64];
moel@99: 
moel@99:       int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
moel@26: 
moel@313:       idle = null;
moel@313:       total = null;
moel@313: 
moel@26:       IntPtr returnLength;
moel@167:       if (NativeMethods.NtQuerySystemInformation(
moel@26:         SystemInformationClass.SystemProcessorPerformanceInformation,
moel@167:         informations, informations.Length * size, out returnLength) != 0)
moel@313:         return false;
moel@99: 
moel@313:       idle = new long[(int)returnLength / size];
moel@313:       total = new long[(int)returnLength / size];
moel@26: 
moel@313:       for (int i = 0; i < idle.Length; i++) {
moel@313:         idle[i] = informations[i].IdleTime;
moel@313:         total[i] = informations[i].KernelTime + informations[i].UserTime;
moel@313:       }
moel@26: 
moel@313:       return true;
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.totalLoad = 0;
moel@26:       try {
moel@313:         GetTimes(out idleTimes, out totalTimes);
moel@26:       } catch (Exception) {
moel@26:         this.idleTimes = null;
moel@313:         this.totalTimes = 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@313:       long[] newIdleTimes;
moel@313:       long[] newTotalTimes;
moel@26: 
moel@313:       if (!GetTimes(out newIdleTimes, out newTotalTimes))
moel@167:         return;
moel@167: 
moel@313:       for (int i = 0; i < Math.Min(newTotalTimes.Length, totalTimes.Length); i++) 
moel@313:         if (newTotalTimes[i] - this.totalTimes[i] < 100000)
moel@313:           return;
moel@313: 
moel@313:       if (newIdleTimes == null || newTotalTimes == 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@313:           if (index < newIdleTimes.Length && index < totalTimes.Length) {
moel@313:             float idle = 
moel@313:               (float)(newIdleTimes[index] - this.idleTimes[index]) /
moel@313:               (float)(newTotalTimes[index] - this.totalTimes[index]);
moel@313:             value += idle;
moel@313:             total += idle;
moel@99:             count++;
moel@99:           }
moel@26:         }
moel@313:         value = 1.0f - value / cpuid[i].Length;
moel@26:         value = value < 0 ? 0 : value;
moel@26:         coreLoads[i] = value * 100;
moel@26:       }
moel@99:       if (count > 0) {
moel@313:         total = 1.0f - total / count;
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@313:       this.totalTimes = newTotalTimes;
moel@167:       this.idleTimes = newIdleTimes;
moel@26:     }
moel@26: 
moel@195:     protected 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: }