Changed the CPU clock calculation. If no invariant TSC is available, then the max CPU clock is estimated at startup under load, otherwise an average over one second is used.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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.
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.
39 using System.Collections.Generic;
40 using System.Runtime.InteropServices;
43 namespace OpenHardwareMonitor.Hardware.CPU {
44 public class CPULoad {
46 [StructLayout(LayoutKind.Sequential)]
47 private struct SystemProcessorPerformanceInformation {
49 public long KernelTime;
51 public long Reserved0;
52 public long Reserved1;
53 public ulong Reserved2;
56 private enum SystemInformationClass : int {
57 SystemBasicInformation = 0,
58 SystemCpuInformation = 1,
59 SystemPerformanceInformation = 2,
60 SystemTimeOfDayInformation = 3,
61 SystemProcessInformation = 5,
62 SystemProcessorPerformanceInformation = 8
65 [DllImport("ntdll.dll")]
66 private static extern int NtQuerySystemInformation(
67 SystemInformationClass informationClass,
68 [Out] SystemProcessorPerformanceInformation[] informations,
69 int structSize, out IntPtr returnLength);
71 private uint coreCount;
72 private uint logicalProcessorsPerCore;
74 private long systemTime;
75 private long[] idleTimes;
77 private float totalLoad;
78 private float[] coreLoads;
80 private bool available = false;
82 private long[] GetIdleTimes() {
83 long[] result = new long[coreCount * logicalProcessorsPerCore];
84 SystemProcessorPerformanceInformation[] informations = new
85 SystemProcessorPerformanceInformation[result.Length];
88 NtQuerySystemInformation(
89 SystemInformationClass.SystemProcessorPerformanceInformation,
90 informations, informations.Length *
91 Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation)),
94 for (int i = 0; i < result.Length; i++)
95 result[i] = informations[i].IdleTime;
100 public CPULoad(uint coreCount, uint logicalProcessorsPerCore) {
101 this.coreCount = coreCount;
102 this.logicalProcessorsPerCore = logicalProcessorsPerCore;
103 this.coreLoads = new float[coreCount];
104 this.systemTime = DateTime.Now.Ticks;
107 this.idleTimes = GetIdleTimes();
108 } catch (Exception) {
109 this.idleTimes = null;
111 if (idleTimes != null)
115 public bool IsAvailable {
116 get { return available; }
119 public float GetTotalLoad() {
123 public float GetCoreLoad(int core) {
124 return coreLoads[core];
127 public void Update() {
128 if (this.idleTimes == null)
131 long systemTime = DateTime.Now.Ticks;
132 long[] idleTimes = GetIdleTimes();
134 if (systemTime - this.systemTime < 10000)
138 for (int i = 0; i < coreCount; i++) {
140 for (int j = 0; j < logicalProcessorsPerCore; j++) {
141 long index = i * logicalProcessorsPerCore + j;
142 long delta = idleTimes[index] - this.idleTimes[index];
146 value = 1.0f - value / (logicalProcessorsPerCore *
147 (systemTime - this.systemTime));
148 value = value < 0 ? 0 : value;
149 coreLoads[i] = value * 100;
151 total = 1.0f - total / (coreCount * logicalProcessorsPerCore *
152 (systemTime - this.systemTime));
153 total = total < 0 ? 0 : total;
154 this.totalLoad = total * 100;
156 this.systemTime = systemTime;
157 this.idleTimes = idleTimes;