1.1 --- a/Hardware/CPU/GenericCPU.cs Mon Nov 01 20:44:21 2010 +0000
1.2 +++ b/Hardware/CPU/GenericCPU.cs Wed Nov 03 22:07:46 2010 +0000
1.3 @@ -130,8 +130,12 @@
1.4 }
1.5
1.6 if (hasTimeStampCounter) {
1.7 + ulong mask = ThreadAffinity.Set(1UL << cpuid[0][0].Thread);
1.8 +
1.9 estimatedTimeStampCounterFrequency =
1.10 - EstimateTimeStampCounterFrequency();
1.11 + EstimateTimeStampCounterFrequency();
1.12 +
1.13 + ThreadAffinity.Set(mask);
1.14 } else {
1.15 estimatedTimeStampCounterFrequency = 0;
1.16 }
1.17 @@ -139,7 +143,7 @@
1.18 timeStampCounterFrequency = estimatedTimeStampCounterFrequency;
1.19 }
1.20
1.21 - private static double EstimateTimeStampCounterFrequency() {
1.22 + private static double EstimateTimeStampCounterFrequency() {
1.23 // preload the function
1.24 EstimateTimeStampCounterFrequency(0);
1.25 EstimateTimeStampCounterFrequency(0);
1.26 @@ -148,6 +152,7 @@
1.27 List<double> estimatedFrequency = new List<double>(3);
1.28 for (int i = 0; i < 3; i++)
1.29 estimatedFrequency.Add(1e-6 * EstimateTimeStampCounterFrequency(0.025));
1.30 +
1.31 estimatedFrequency.Sort();
1.32 return estimatedFrequency[1];
1.33 }
1.34 @@ -156,7 +161,6 @@
1.35 long ticks = (long)(timeWindow * Stopwatch.Frequency);
1.36 ulong countBegin, countEnd;
1.37
1.38 - Thread.BeginThreadAffinity();
1.39 long timeBegin = Stopwatch.GetTimestamp() +
1.40 (long)Math.Ceiling(0.001 * ticks);
1.41 long timeEnd = timeBegin + ticks;
1.42 @@ -164,7 +168,6 @@
1.43 countBegin = Opcode.Rdtsc();
1.44 while (Stopwatch.GetTimestamp() < timeEnd) { }
1.45 countEnd = Opcode.Rdtsc();
1.46 - Thread.EndThreadAffinity();
1.47
1.48 return (((double)(countEnd - countBegin)) * Stopwatch.Frequency) /
1.49 (timeEnd - timeBegin);
1.50 @@ -173,7 +176,7 @@
1.51
1.52 private static void AppendMSRData(StringBuilder r, uint msr, int thread) {
1.53 uint eax, edx;
1.54 - if (Ring0.RdmsrTx(msr, out eax, out edx, (UIntPtr)(1L << thread))) {
1.55 + if (Ring0.RdmsrTx(msr, out eax, out edx, 1UL << thread)) {
1.56 r.Append(" ");
1.57 r.Append((msr).ToString("X8", CultureInfo.InvariantCulture));
1.58 r.Append(" ");
1.59 @@ -264,9 +267,7 @@
1.60 if (hasTimeStampCounter && isInvariantTimeStampCounter) {
1.61
1.62 // make sure always the same thread is used
1.63 - IntPtr thread = NativeMethods.GetCurrentThread();
1.64 - UIntPtr mask = NativeMethods.SetThreadAffinityMask(thread,
1.65 - (UIntPtr)(1L << cpuid[0][0].Thread));
1.66 + ulong mask = ThreadAffinity.Set(1UL << cpuid[0][0].Thread);
1.67
1.68 // read time before and after getting the TSC to estimate the error
1.69 long firstTime = Stopwatch.GetTimestamp();
1.70 @@ -274,7 +275,7 @@
1.71 long time = Stopwatch.GetTimestamp();
1.72
1.73 // restore the thread affinity mask
1.74 - NativeMethods.SetThreadAffinityMask(thread, mask);
1.75 + ThreadAffinity.Set(mask);
1.76
1.77 double delta = ((double)(time - lastTime)) / Stopwatch.Frequency;
1.78 double error = ((double)(time - firstTime)) / Stopwatch.Frequency;
1.79 @@ -304,16 +305,5 @@
1.80 totalLoad.Value = cpuLoad.GetTotalLoad();
1.81 }
1.82 }
1.83 -
1.84 - private static class NativeMethods {
1.85 - private const string KERNEL = "kernel32.dll";
1.86 -
1.87 - [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
1.88 - public static extern UIntPtr
1.89 - SetThreadAffinityMask(IntPtr handle, UIntPtr mask);
1.90 -
1.91 - [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
1.92 - public static extern IntPtr GetCurrentThread();
1.93 - }
1.94 }
1.95 }