moel@26: /*
moel@26:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@26:  
moel@344:   Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
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: }