Hardware/CPU/GenericCPU.cs
changeset 201 958e9fe8afdf
parent 195 0ee888c485d5
child 203 ca487ba88c24
     1.1 --- a/Hardware/CPU/GenericCPU.cs	Mon Sep 27 23:48:41 2010 +0000
     1.2 +++ b/Hardware/CPU/GenericCPU.cs	Thu Sep 30 16:51:09 2010 +0000
     1.3 @@ -55,13 +55,13 @@
     1.4      protected readonly int coreCount;
     1.5      protected readonly string name;
     1.6  
     1.7 -    protected readonly bool hasTSC;
     1.8 -    protected readonly bool invariantTSC;
     1.9 -    private readonly double estimatedMaxClock;
    1.10 +    private readonly bool hasTimeStampCounter;
    1.11 +    private readonly bool isInvariantTimeStampCounter;
    1.12 +    private readonly double estimatedTimeStampCounterFrequency;
    1.13  
    1.14      private ulong lastTimeStampCount;
    1.15      private long lastTime;
    1.16 -    private double maxClock;    
    1.17 +    private double timeStampCounterFrequency;    
    1.18  
    1.19      private readonly Vendor vendor;
    1.20  
    1.21 @@ -89,19 +89,19 @@
    1.22        this.coreCount = cpuid.Length;
    1.23        this.name = cpuid[0][0].Name;      
    1.24  
    1.25 -      // check if processor has TSC
    1.26 +      // check if processor has a TSC
    1.27        if (cpuid[0][0].Data.GetLength(0) > 1
    1.28          && (cpuid[0][0].Data[1, 3] & 0x10) != 0)
    1.29 -        hasTSC = true;
    1.30 +        hasTimeStampCounter = true;
    1.31        else
    1.32 -        hasTSC = false;
    1.33 +        hasTimeStampCounter = false;
    1.34  
    1.35 -      // check if processor supports invariant TSC 
    1.36 +      // check if processor supports an invariant TSC 
    1.37        if (cpuid[0][0].ExtData.GetLength(0) > 7
    1.38          && (cpuid[0][0].ExtData[7, 3] & 0x100) != 0)
    1.39 -        invariantTSC = true;
    1.40 +        isInvariantTimeStampCounter = true;
    1.41        else
    1.42 -        invariantTSC = false;
    1.43 +        isInvariantTimeStampCounter = false;
    1.44  
    1.45        if (coreCount > 1)
    1.46          totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this, settings);
    1.47 @@ -119,30 +119,30 @@
    1.48            ActivateSensor(totalLoad);
    1.49        }
    1.50  
    1.51 -      if (hasTSC)
    1.52 -        estimatedMaxClock = EstimateMaxClock();
    1.53 +      if (hasTimeStampCounter)
    1.54 +        estimatedTimeStampCounterFrequency = EstimateTimeStampCounterFrequency();
    1.55        else
    1.56 -        estimatedMaxClock = 0;
    1.57 -      maxClock = estimatedMaxClock;
    1.58 +        estimatedTimeStampCounterFrequency = 0;
    1.59 +      timeStampCounterFrequency = estimatedTimeStampCounterFrequency;
    1.60  
    1.61        lastTimeStampCount = 0;
    1.62        lastTime = 0;
    1.63      }
    1.64  
    1.65 -    private static double EstimateMaxClock() {
    1.66 +    private static double EstimateTimeStampCounterFrequency() {
    1.67        // preload the function
    1.68 -      EstimateMaxClock(0);
    1.69 -      EstimateMaxClock(0);
    1.70 +      EstimateTimeStampCounterFrequency(0);
    1.71 +      EstimateTimeStampCounterFrequency(0);
    1.72  
    1.73 -      // estimate the max clock in MHz      
    1.74 -      List<double> estimatedMaxClocks = new List<double>(3);
    1.75 +      // estimate the frequency in MHz      
    1.76 +      List<double> estimatedFrequency = new List<double>(3);
    1.77        for (int i = 0; i < 3; i++)
    1.78 -        estimatedMaxClocks.Add(1e-6 * EstimateMaxClock(0.025));
    1.79 -      estimatedMaxClocks.Sort();
    1.80 -      return estimatedMaxClocks[1];
    1.81 +        estimatedFrequency.Add(1e-6 * EstimateTimeStampCounterFrequency(0.025));
    1.82 +      estimatedFrequency.Sort();
    1.83 +      return estimatedFrequency[1];
    1.84      }
    1.85  
    1.86 -    private static double EstimateMaxClock(double timeWindow) {
    1.87 +    private static double EstimateTimeStampCounterFrequency(double timeWindow) {
    1.88        long ticks = (long)(timeWindow * Stopwatch.Frequency);
    1.89        uint lsbBegin, msbBegin, lsbEnd, msbEnd;
    1.90  
    1.91 @@ -195,12 +195,13 @@
    1.92          Environment.NewLine);
    1.93        r.AppendFormat("Threads per Core: {0}{1}", cpuid[0].Length,
    1.94          Environment.NewLine);
    1.95 -      r.AppendLine("TSC: " +
    1.96 -        (hasTSC ? (invariantTSC ? "Invariant" : "Not Invariant") : "None"));
    1.97        r.AppendLine(string.Format(CultureInfo.InvariantCulture,
    1.98          "Timer Frequency: {0} MHz", Stopwatch.Frequency * 1e-6));
    1.99 +      r.AppendLine("Time Stamp Counter: " + (hasTimeStampCounter ? (
   1.100 +        isInvariantTimeStampCounter ? "Invariant" : "Not Invariant") : "None"));
   1.101        r.AppendLine(string.Format(CultureInfo.InvariantCulture,
   1.102 -        "Max Clock: {0} MHz", Math.Round(maxClock * 100) * 0.01));
   1.103 +        "Time Stamp Counter Frequency: {0} MHz",
   1.104 +        Math.Round(timeStampCounterFrequency * 100) * 0.01));   
   1.105        r.AppendLine();
   1.106  
   1.107        uint[] msrArray = GetMSRs();
   1.108 @@ -239,22 +240,27 @@
   1.109        get { return HardwareType.CPU; }
   1.110      }
   1.111  
   1.112 -    protected double MaxClock {
   1.113 -      get { return maxClock; }
   1.114 +    public bool HasTimeStampCounter {
   1.115 +      get { return hasTimeStampCounter; }
   1.116 +    }
   1.117 +
   1.118 +    public double TimeStampCounterFrequency {
   1.119 +      get { return timeStampCounterFrequency; }
   1.120      }
   1.121  
   1.122      public override void Update() {
   1.123 -      if (hasTSC) {
   1.124 +      if (hasTimeStampCounter) {
   1.125          uint lsb, msb;
   1.126          WinRing0.RdtscTx(out lsb, out msb, (UIntPtr)1);
   1.127          long time = Stopwatch.GetTimestamp();
   1.128          ulong timeStampCount = ((ulong)msb << 32) | lsb;
   1.129          double delta = ((double)(time - lastTime)) / Stopwatch.Frequency;
   1.130          if (delta > 0.5) {
   1.131 -          if (invariantTSC)
   1.132 -            maxClock = (timeStampCount - lastTimeStampCount) / (1e6 * delta);
   1.133 +          if (isInvariantTimeStampCounter)
   1.134 +            timeStampCounterFrequency = 
   1.135 +              (timeStampCount - lastTimeStampCount) / (1e6 * delta);
   1.136            else
   1.137 -            maxClock = estimatedMaxClock;
   1.138 +            timeStampCounterFrequency = estimatedTimeStampCounterFrequency;
   1.139  
   1.140            lastTimeStampCount = timeStampCount;
   1.141            lastTime = time;