Fixed the CPULoad implementation for multi CPU systems.
authormoel.mich
Mon, 26 Apr 2010 18:38:31 +0000
changeset 996d8377af9fb1
parent 98 4775bffe6173
child 100 25d18bbaa9cf
Fixed the CPULoad implementation for multi CPU systems.
Hardware/CPU/CPULoad.cs
     1.1 --- a/Hardware/CPU/CPULoad.cs	Sun Apr 25 19:05:15 2010 +0000
     1.2 +++ b/Hardware/CPU/CPULoad.cs	Mon Apr 26 18:38:31 2010 +0000
     1.3 @@ -78,17 +78,18 @@
     1.4  
     1.5      private bool available = false;
     1.6  
     1.7 -    private long[] GetIdleTimes() {
     1.8 -      long[] result = new long[cpuid.Length * cpuid[0].Length];
     1.9 +    private long[] GetIdleTimes() {      
    1.10        SystemProcessorPerformanceInformation[] informations = new
    1.11 -       SystemProcessorPerformanceInformation[result.Length];
    1.12 +        SystemProcessorPerformanceInformation[64];
    1.13 +
    1.14 +      int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
    1.15  
    1.16        IntPtr returnLength;
    1.17        NtQuerySystemInformation(
    1.18          SystemInformationClass.SystemProcessorPerformanceInformation,
    1.19 -        informations, informations.Length * 
    1.20 -        Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation)),
    1.21 -        out returnLength);
    1.22 +        informations, informations.Length * size, out returnLength);
    1.23 +
    1.24 +      long[] result = new long[(int)returnLength / size];
    1.25  
    1.26        for (int i = 0; i < result.Length; i++)
    1.27          result[i] = informations[i].IdleTime;
    1.28 @@ -138,18 +139,24 @@
    1.29          float value = 0;
    1.30          for (int j = 0; j < cpuid[i].Length; j++) {
    1.31            long index = cpuid[i][j].Thread;
    1.32 -          long delta = idleTimes[index] - this.idleTimes[index];
    1.33 -          value += delta;
    1.34 -          total += delta;
    1.35 -          count++;
    1.36 +          if (index < idleTimes.Length) {
    1.37 +            long delta = idleTimes[index] - this.idleTimes[index];
    1.38 +            value += delta;
    1.39 +            total += delta;
    1.40 +            count++;
    1.41 +          }
    1.42          }
    1.43          value = 1.0f - value / (cpuid[i].Length * 
    1.44            (systemTime - this.systemTime));
    1.45          value = value < 0 ? 0 : value;
    1.46          coreLoads[i] = value * 100;
    1.47        }
    1.48 -      total = 1.0f - total / (count * (systemTime - this.systemTime));
    1.49 -      total = total < 0 ? 0 : total;
    1.50 +      if (count > 0) {
    1.51 +        total = 1.0f - total / (count * (systemTime - this.systemTime));
    1.52 +        total = total < 0 ? 0 : total;
    1.53 +      } else {
    1.54 +        total = 0;
    1.55 +      }
    1.56        this.totalLoad = total * 100;
    1.57  
    1.58        this.systemTime = systemTime;