Added a few checks and delays to the driver loading code to increase the chance of loading the driver.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Runtime.InteropServices;
14 namespace OpenHardwareMonitor.Hardware.CPU {
15 internal class CPULoad {
17 [StructLayout(LayoutKind.Sequential)]
18 protected struct SystemProcessorPerformanceInformation {
20 public long KernelTime;
22 public long Reserved0;
23 public long Reserved1;
24 public ulong Reserved2;
27 protected enum SystemInformationClass {
28 SystemBasicInformation = 0,
29 SystemCpuInformation = 1,
30 SystemPerformanceInformation = 2,
31 SystemTimeOfDayInformation = 3,
32 SystemProcessInformation = 5,
33 SystemProcessorPerformanceInformation = 8
36 private readonly CPUID[][] cpuid;
38 private long[] idleTimes;
39 private long[] totalTimes;
41 private float totalLoad;
42 private readonly float[] coreLoads;
44 private readonly bool available;
46 private static bool GetTimes(out long[] idle, out long[] total) {
47 SystemProcessorPerformanceInformation[] informations = new
48 SystemProcessorPerformanceInformation[64];
50 int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
56 if (NativeMethods.NtQuerySystemInformation(
57 SystemInformationClass.SystemProcessorPerformanceInformation,
58 informations, informations.Length * size, out returnLength) != 0)
61 idle = new long[(int)returnLength / size];
62 total = new long[(int)returnLength / size];
64 for (int i = 0; i < idle.Length; i++) {
65 idle[i] = informations[i].IdleTime;
66 total[i] = informations[i].KernelTime + informations[i].UserTime;
72 public CPULoad(CPUID[][] cpuid) {
74 this.coreLoads = new float[cpuid.Length];
77 GetTimes(out idleTimes, out totalTimes);
79 this.idleTimes = null;
80 this.totalTimes = null;
82 if (idleTimes != null)
86 public bool IsAvailable {
87 get { return available; }
90 public float GetTotalLoad() {
94 public float GetCoreLoad(int core) {
95 return coreLoads[core];
98 public void Update() {
99 if (this.idleTimes == null)
103 long[] newTotalTimes;
105 if (!GetTimes(out newIdleTimes, out newTotalTimes))
108 for (int i = 0; i < Math.Min(newTotalTimes.Length, totalTimes.Length); i++)
109 if (newTotalTimes[i] - this.totalTimes[i] < 100000)
112 if (newIdleTimes == null || newTotalTimes == null)
117 for (int i = 0; i < cpuid.Length; i++) {
119 for (int j = 0; j < cpuid[i].Length; j++) {
120 long index = cpuid[i][j].Thread;
121 if (index < newIdleTimes.Length && index < totalTimes.Length) {
123 (float)(newIdleTimes[index] - this.idleTimes[index]) /
124 (float)(newTotalTimes[index] - this.totalTimes[index]);
130 value = 1.0f - value / cpuid[i].Length;
131 value = value < 0 ? 0 : value;
132 coreLoads[i] = value * 100;
135 total = 1.0f - total / count;
136 total = total < 0 ? 0 : total;
140 this.totalLoad = total * 100;
142 this.totalTimes = newTotalTimes;
143 this.idleTimes = newIdleTimes;
146 protected static class NativeMethods {
148 [DllImport("ntdll.dll")]
149 public static extern int NtQuerySystemInformation(
150 SystemInformationClass informationClass,
151 [Out] SystemProcessorPerformanceInformation[] informations,
152 int structSize, out IntPtr returnLength);