Added a new sensor type "Factor" for dimensionless values (and similar) that are not to be shown as percent ("Level" type). Changed the write amplification sensor to use the new "Factor" sensor type. Added the temperature SMART attribute for Sandforce SSDs as hidden sensor (as it may show fake results on some hardware).
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-2011
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.Runtime.InteropServices;
41 namespace OpenHardwareMonitor.Hardware.CPU {
42 internal class CPULoad {
44 [StructLayout(LayoutKind.Sequential)]
45 protected struct SystemProcessorPerformanceInformation {
47 public long KernelTime;
49 public long Reserved0;
50 public long Reserved1;
51 public ulong Reserved2;
54 protected enum SystemInformationClass {
55 SystemBasicInformation = 0,
56 SystemCpuInformation = 1,
57 SystemPerformanceInformation = 2,
58 SystemTimeOfDayInformation = 3,
59 SystemProcessInformation = 5,
60 SystemProcessorPerformanceInformation = 8
63 private readonly CPUID[][] cpuid;
65 private long[] idleTimes;
66 private long[] totalTimes;
68 private float totalLoad;
69 private readonly float[] coreLoads;
71 private readonly bool available;
73 private static bool GetTimes(out long[] idle, out long[] total) {
74 SystemProcessorPerformanceInformation[] informations = new
75 SystemProcessorPerformanceInformation[64];
77 int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
83 if (NativeMethods.NtQuerySystemInformation(
84 SystemInformationClass.SystemProcessorPerformanceInformation,
85 informations, informations.Length * size, out returnLength) != 0)
88 idle = new long[(int)returnLength / size];
89 total = new long[(int)returnLength / size];
91 for (int i = 0; i < idle.Length; i++) {
92 idle[i] = informations[i].IdleTime;
93 total[i] = informations[i].KernelTime + informations[i].UserTime;
99 public CPULoad(CPUID[][] cpuid) {
101 this.coreLoads = new float[cpuid.Length];
104 GetTimes(out idleTimes, out totalTimes);
105 } catch (Exception) {
106 this.idleTimes = null;
107 this.totalTimes = null;
109 if (idleTimes != null)
113 public bool IsAvailable {
114 get { return available; }
117 public float GetTotalLoad() {
121 public float GetCoreLoad(int core) {
122 return coreLoads[core];
125 public void Update() {
126 if (this.idleTimes == null)
130 long[] newTotalTimes;
132 if (!GetTimes(out newIdleTimes, out newTotalTimes))
135 for (int i = 0; i < Math.Min(newTotalTimes.Length, totalTimes.Length); i++)
136 if (newTotalTimes[i] - this.totalTimes[i] < 100000)
139 if (newIdleTimes == null || newTotalTimes == null)
144 for (int i = 0; i < cpuid.Length; i++) {
146 for (int j = 0; j < cpuid[i].Length; j++) {
147 long index = cpuid[i][j].Thread;
148 if (index < newIdleTimes.Length && index < totalTimes.Length) {
150 (float)(newIdleTimes[index] - this.idleTimes[index]) /
151 (float)(newTotalTimes[index] - this.totalTimes[index]);
157 value = 1.0f - value / cpuid[i].Length;
158 value = value < 0 ? 0 : value;
159 coreLoads[i] = value * 100;
162 total = 1.0f - total / count;
163 total = total < 0 ? 0 : total;
167 this.totalLoad = total * 100;
169 this.totalTimes = newTotalTimes;
170 this.idleTimes = newIdleTimes;
173 protected static class NativeMethods {
175 [DllImport("ntdll.dll")]
176 public static extern int NtQuerySystemInformation(
177 SystemInformationClass informationClass,
178 [Out] SystemProcessorPerformanceInformation[] informations,
179 int structSize, out IntPtr returnLength);