【问题标题】:Is there a way to call the RDTSC assembly instruction from C#?有没有办法从 C# 调用 RDTSC 汇编指令?
【发布时间】:2015-04-15 06:15:30
【问题描述】:

我想要一个非常高分辨率的计时器用于我的 C# 应用程序。我想访问 RDTSC 汇编指令。有没有办法做到这一点?

编辑:我正在移植一些 C++ 代码并尝试保留与原始代码相同的功能。我可能会切换到更多的 .NET,但想评估 RDTSC 指令,以便将结果与原始结果进行比较。

【问题讨论】:

  • 您是否第一次尝试使用QueryPerformanceCounter 的P/Invoke?直接使用RDTSC 会充满potential problems,除非您实际上从未计划将周期数转换为时间。
  • 这应该可以工作stackoverflow.com/a/959144/57428
  • 请注意,链接的代码在 VirtualAlloc 中有错误。第二个参数必须是 IntPtr 或 UIntPtr
  • 只需 pinvoke 使用 RDTSC 的 operating system function。
  • 不是我的强项,但有什么理由这样做而不是仅仅使用StopWatch?

标签: c#


【解决方案1】:

你可以这样做:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;

public static class Rdtsc
{
    [StructLayout(LayoutKind.Sequential)]
    private struct SystemInfo
    {
        public ushort wProcessorArchitecture;
        public ushort wReserved;
        public uint dwPageSize;
        public IntPtr lpMinimumApplicationAddress;
        public IntPtr lpMaximumApplicationAddress;
        public IntPtr dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public ushort wProcessorLevel;
        public ushort wProcessorRevision;
    }

    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern void GetNativeSystemInfo(out SystemInfo lpSystemInfo);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    private static extern IntPtr VirtualAlloc(IntPtr lpAddress, IntPtr dwSize, uint flAllocationType, uint flProtect);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool VirtualProtect(IntPtr lpAddress, IntPtr dwSize, uint flAllocationType, out uint lpflOldProtect);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, uint dwFreeType);

    private const uint PAGE_READWRITE = 0x04;
    private const uint PAGE_EXECUTE = 0x10;
    private const uint MEM_COMMIT = 0x1000;
    private const uint MEM_RELEASE = 0x8000;

    [SuppressUnmanagedCodeSecurity]
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate ulong TimestampDelegate();

    public static readonly TimestampDelegate Timestamp;

    static Rdtsc()
    {
        SystemInfo systemInfo;
        GetNativeSystemInfo(out systemInfo);

        if (systemInfo.wProcessorArchitecture != 0 /* PROCESSOR_ARCHITECTURE_INTEL */ && 
            systemInfo.wProcessorArchitecture != 9 /* PROCESSOR_ARCHITECTURE_AMD64 */)
        {
            // Fallback for ARM/IA64/...
            Timestamp = StopwatchGetTimestamp;
            return;
        }

        byte[] body;

        if (Environment.Is64BitProcess)
        {
            body = new byte[] 
            {
                0x0f, 0x31, // rdtsc
                0x48, 0xc1, 0xe2, 0x20, // shl rdx,20h 
                0x48, 0x0b, 0xc2, // or rax,rdx 
                0xc3, // ret
            };
        }
        else
        {
            body = new byte[] 
            {
                0x0f, 0x31, // rdtsc
                0xc3, // ret
            };
        }

        IntPtr buf = IntPtr.Zero;

        try
        {
            // We VirtualAlloc body.Length bytes, with R/W access
            // Note that from what I've read, MEM_RESERVE is useless
            // if the first parameter is IntPtr.Zero
            buf = VirtualAlloc(IntPtr.Zero, (IntPtr)body.Length, MEM_COMMIT, PAGE_READWRITE);

            if (buf == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            // Copy our instructions in the buf
            Marshal.Copy(body, 0, buf, body.Length);

            // Change the access of the allocated memory from R/W to Execute
            uint oldProtection;
            bool result = VirtualProtect(buf, (IntPtr)body.Length, PAGE_EXECUTE, out oldProtection);

            if (!result)
            {
                throw new Win32Exception();
            }

            // Create a delegate to the "function"
            Timestamp = (TimestampDelegate)Marshal.GetDelegateForFunctionPointer(buf, typeof(TimestampDelegate));

            buf = IntPtr.Zero;
        }
        finally
        {
            // There was an error!
            if (buf != IntPtr.Zero)
            {
                // Free the allocated memory
                bool result = VirtualFree(buf, IntPtr.Zero, MEM_RELEASE);

                if (!result)
                {
                    throw new Win32Exception();
                }
            }
        }
    }

    // Fallback if rdtsc isn't available
    private static ulong StopwatchGetTimestamp()
    {
        return unchecked((ulong)Stopwatch.GetTimestamp());
    }
}

一些注意事项:

  • 我已经包含了 ARM 处理器的后备 (Stopwatch.GetTimestamp())。
  • 我不使用 CPUID 来阻止 RDTSC 指令移动 ("cpuid" before "rdtsc")。我不擅长组装,所以我不知道该怎么做。如果您想修改代码,请随意修改并在“正确”的操作码上添加注释以使用
  • 我正在使用 RDTSC 而不是 RDTSCP(同样的问题,汇编不是我的语言)
  • 它非常慢...假设等效的 Visual C++ 代码在 RDTSC 的 18-24 个滴答中被调用而没有内联,而 C# 版本在初始预热之后被调用27-100 次 RDTSC。

Visual C++ 对比代码:

__declspec(noinline) uint64_t __stdcall Rdtsc(void)
{
    return __rdtsc();
}

Aaaa 和现在...使用 rdtscp 完全实现

public static class Rdtsc
{
    [StructLayout(LayoutKind.Sequential)]
    private struct SystemInfo
    {
        public ushort wProcessorArchitecture;
        public ushort wReserved;
        public uint dwPageSize;
        public IntPtr lpMinimumApplicationAddress;
        public IntPtr lpMaximumApplicationAddress;
        public IntPtr dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public ushort wProcessorLevel;
        public ushort wProcessorRevision;
    }

    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern void GetNativeSystemInfo(out SystemInfo lpSystemInfo);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    private static extern IntPtr VirtualAlloc(IntPtr lpAddress, IntPtr dwSize, uint flAllocationType, uint flProtect);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool VirtualProtect(IntPtr lpAddress, IntPtr dwSize, uint flAllocationType, out uint lpflOldProtect);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, uint dwFreeType);

    private const uint PAGE_READWRITE = 0x04;
    private const uint PAGE_EXECUTE = 0x10;
    private const uint PAGE_EXECUTE_READWRITE = 0x40;
    private const uint MEM_COMMIT = 0x1000;
    private const uint MEM_RELEASE = 0x8000;

    [SuppressUnmanagedCodeSecurity]
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate ulong FuncUInt64();

    /// <summary>
    /// Uses rdtsc. On non-Intel uses Stopwatch.GetTimestamp.
    /// </summary>
    public static readonly FuncUInt64 Timestamp;

    /// <summary>
    /// Uses rdtscp if present. Otherwise uses cpuid + rdtsc. On 
    /// non-Intel uses Stopwatch.GetTimestamp.
    /// </summary>
    public static readonly FuncUInt64 TimestampP;

    public static readonly bool IsRdtscSupported;
    public static readonly bool IsRdtscPSupported;

    static Rdtsc()
    {
        SystemInfo systemInfo;
        GetNativeSystemInfo(out systemInfo);

        if (systemInfo.wProcessorArchitecture != 0 /* PROCESSOR_ARCHITECTURE_INTEL */ && 
            systemInfo.wProcessorArchitecture != 9 /* PROCESSOR_ARCHITECTURE_AMD64 */)
        {
            // Fallback for ARM/IA64/...
            Timestamp = StopwatchGetTimestamp;
            TimestampP = StopwatchGetTimestamp;
            IsRdtscSupported = false;
            IsRdtscPSupported = false;
            return;
        }

        byte[] cpuid, rdtsc, rdtscp, rdtsccpuid;

        IsRdtscSupported = true;

        // Assembly generated with https://defuse.ca/online-x86-assembler.htm

        if (Environment.Is64BitProcess)
        {
            /* CPUID x64:
                    push rbx;
                    mov eax, 0x80000000;
                    cpuid;
                    mov ebx, 0x80000001;
                    cmp eax, ebx;
                    jb Error;
                    mov eax, ebx;
                    cpuid;
                    mov eax, ecx;
                    shl rax, 0x20;
                    or rax, rdx
                    jmp End;
                Error:
                    xor rax, rax;
                End:
                    pop rbx;
                    ret;

                0:  53                      push   rbx
                1:  b8 00 00 00 80          mov    eax,0x80000000
                6:  0f a2                   cpuid
                8:  bb 01 00 00 80          mov    ebx,0x80000001
                d:  39 d8                   cmp    eax,ebx
                f:  72 0f                   jb     20 <Error>
                11: 89 d8                   mov    eax,ebx
                13: 0f a2                   cpuid
                15: 89 c8                   mov    eax,ecx
                17: 48 c1 e0 20             shl    rax,0x20
                1b: 48 09 d0                or     rax,rdx
                1e: eb 03                   jmp    23 <End>
                0000000000000020 <Error>:
                20: 48 31 c0                xor    rax,rax
                0000000000000023 <End>:
                23: 5b                      pop    rbx
                24: c3                      ret
             */
            cpuid = new byte[] { 0x53, 0xB8, 0x00, 0x00, 0x00, 0x80, 0x0F, 0xA2, 0xBB, 0x01, 0x00, 0x00, 0x80, 0x39, 0xD8, 0x72, 0x16, 0x89, 0xD8, 0x48, 0xC7, 0xC2, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xA2, 0x89, 0xC8, 0x48, 0xC1, 0xE0, 0x20, 0x48, 0x09, 0xD0, 0xEB, 0x03, 0x48, 0x31, 0xC0, 0x5B, 0xC3 };

            /* RDTSC x64:
                rdtsc;
                shl rdx, 0x20;
                or rax,rdx;
                ret;

                0:  0f 31                   rdtsc
                2:  48 c1 e2 20             shl    rdx,0x20
                6:  48 09 d0                or     rax,rdx
                9:  c3                      ret
             */
            rdtsc = new byte[] { 0x0F, 0x31, 0x48, 0xC1, 0xE2, 0x20, 0x48, 0x09, 0xD0, 0xC3 };

            /* RDTSCP x64
                rdtscp;
                shl rdx, 0x20;
                or rax, rdx;
                ret;

                0:  0f 01 f9                rdtscp
                3:  48 c1 e2 20             shl    rdx,0x20
                7:  48 09 d0                or     rax,rdx
                a:  c3                      ret
             */
            rdtscp = new byte[] { 0x0F, 0x01, 0xF9, 0x48, 0xC1, 0xE2, 0x20, 0x48, 0x09, 0xD0, 0xC3 };

            /* RDTSC + CPUID x64
                push rbx;
                xor eax, eax;
                cpuid;
                rdtsc;
                shl rdx, 0x20;
                or rax, rdx;
                pop rbx;
                ret;

                0:  53                      push   rbx
                1:  31 c0                   xor    eax,eax
                3:  0f a2                   cpuid
                5:  0f 31                   rdtsc
                7:  48 c1 e2 20             shl    rdx,0x20
                b:  48 09 d0                or     rax,rdx
                e:  5b                      pop    rbx
                f:  c3                      ret
             */
            rdtsccpuid = new byte[] { 0x53, 0x31, 0xC0, 0x0F, 0xA2, 0x0F, 0x31, 0x48, 0xC1, 0xE2, 0x20, 0x48, 0x09, 0xD0, 0x5B, 0xC3 };
        }
        else
        {
            /* CPUID x86:
                    push ebx;
                    mov eax, 0x80000000;
                    cpuid;
                    mov ebx, 0x80000001;
                    cmp eax, ebx;
                    jb Error;
                    mov eax, ebx;
                    cpuid;
                    mov eax, edx;
                    mov edx, ecx;
                    jmp End;
                Error:
                    xor eax, eax;
                    xor edx, edx;
                End:
                    pop ebx;
                    ret;

                0:  53                      push   ebx
                1:  b8 00 00 00 80          mov    eax,0x80000000
                6:  0f a2                   cpuid
                8:  bb 01 00 00 80          mov    ebx,0x80000001
                d:  39 d8                   cmp    eax,ebx
                f:  72 0a                   jb     1b <Error>
                11: 89 d8                   mov    eax,ebx
                13: 0f a2                   cpuid
                15: 89 d0                   mov    eax,edx
                17: 89 ca                   mov    edx,ecx
                19: eb 04                   jmp    1f <End>
                0000001b <Error>:
                1b: 31 c0                   xor    eax,eax
                1d: 31 d2                   xor    edx,edx
                0000001f <End>:
                1f: 5b                      pop    ebx
                20: c3                      ret
            */
            cpuid = new byte[] { 0x53, 0xB8, 0x00, 0x00, 0x00, 0x80, 0x0F, 0xA2, 0xBB, 0x01, 0x00, 0x00, 0x80, 0x39, 0xD8, 0x72, 0x0A, 0x89, 0xD8, 0x0F, 0xA2, 0x89, 0xD0, 0x89, 0xCA, 0xEB, 0x04, 0x31, 0xC0, 0x31, 0xD2, 0x5B, 0xC3 };

            /* RDTSC x86:
                rdtsc;
                ret;

                0:  0f 31                   rdtsc
                2:  c3                      ret
             */
            rdtsc = new byte[] { 0x0F, 0x31, 0xC3 };

            /* RDTSCP x86
                rdtscp;
                ret;

                0:  0f 01 f9                rdtscp
                3:  c3                      ret
             */
            rdtscp = new byte[] { 0x0F, 0x01, 0xF9, 0xC3 };

            /* RDTSC + CPUID x86
                push ebx;
                xor eax,eax;
                cpuid;
                rdtsc;
                pop ebx;
                ret;

                0:  53                      push   ebx
                1:  31 c0                   xor    eax,eax
                3:  0f a2                   cpuid
                5:  0f 31                   rdtsc
                7:  5b                      pop    ebx
                8:  c3                      ret
             */
            rdtsccpuid = new byte[] { 0x53, 0x31, 0xC0, 0x0F, 0xA2, 0x0F, 0x31, 0x5B, 0xC3 };
        }

        IntPtr buf = IntPtr.Zero;

        try
        {
            // We pad the functions to 64 bytes (the length of a cache
            // line on the Intel processors)
            int cpuidLength = (cpuid.Length & 63) != 0 ? (cpuid.Length | 63) + 1 : cpuid.Length;
            int rdtscLength = (rdtsc.Length & 63) != 0 ? (rdtsc.Length | 63) + 1 : rdtsc.Length;
            int rdtscpLength = (rdtscp.Length & 63) != 0 ? (rdtscp.Length | 63) + 1 : rdtscp.Length;
            int rdtsccpuidLength = (rdtsccpuid.Length & 63) != 0 ? (rdtsccpuid.Length | 63) + 1 : rdtsccpuid.Length;

            // We don't know which one of rdtscp or rdtsccpuid we will
            // use, so we calculate space for the biggest one.
            // Note that it is very unlikely that we will go over 4096
            // bytes (the minimum size of memory allocated by 
            // VirtualAlloc)
            int totalLength = cpuidLength + rdtscLength + Math.Max(rdtscpLength, rdtsccpuidLength);

            // We VirtualAlloc totalLength bytes, with R/W access
            // Note that from what I've read, MEM_RESERVE is useless
            // if the first parameter is IntPtr.Zero
            buf = VirtualAlloc(IntPtr.Zero, (IntPtr)totalLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

            if (buf == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            // Copy cpuid instructions in the buf
            Marshal.Copy(cpuid, 0, buf, cpuid.Length);

            for (int i = cpuid.Length; i < cpuidLength; i++)
            {
                Marshal.WriteByte(buf, i, 0x90); // nop
            }

            // Copy rdtsc instructions in the buf
            Marshal.Copy(rdtsc, 0, buf + cpuidLength, rdtsc.Length);

            for (int i = rdtsc.Length; i < rdtscLength; i++)
            {
                Marshal.WriteByte(buf, cpuidLength + i, 0x90); // nop
            }

            var cpuidFunc = (FuncUInt64)Marshal.GetDelegateForFunctionPointer(buf, typeof(FuncUInt64));

            // We use cpuid, EAX=0x80000001 to check for the rdtscp
            ulong supportedFeatures = cpuidFunc();

            byte[] rdtscpSelected;
            int rdtscpSelectedLength;

            // Check the rdtscp flag
            if ((supportedFeatures & (1L << 27)) != 0)
            {
                // rdtscp supported
                rdtscpSelected = rdtscp;
                rdtscpSelectedLength = rdtscpLength;
                IsRdtscPSupported = true;
            }
            else
            {
                // rdtscp not supported. We use cpuid + rdtsc
                rdtscpSelected = rdtsccpuid;
                rdtscpSelectedLength = rdtsccpuidLength;
                IsRdtscPSupported = false;
            }

            // Copy rdtscp/rdtsccpuid instructions in the buf
            Marshal.Copy(rdtscpSelected, 0, buf + cpuidLength + rdtscLength, rdtscpSelected.Length);

            for (int i = rdtscpSelected.Length; i < rdtscpSelectedLength; i++)
            {
                Marshal.WriteByte(buf, cpuidLength + rdtscLength + i, 0x90); // nop
            }

            // Change the access of the allocated memory from R/W to Execute
            uint oldProtection;
            bool result = VirtualProtect(buf, (IntPtr)totalLength, PAGE_EXECUTE, out oldProtection);

            if (!result)
            {
                throw new Win32Exception();
            }

            // Create a delegate to the "function"
            Timestamp = (FuncUInt64)Marshal.GetDelegateForFunctionPointer(buf + cpuidLength, typeof(FuncUInt64));
            TimestampP = (FuncUInt64)Marshal.GetDelegateForFunctionPointer(buf + cpuidLength + rdtscLength, typeof(FuncUInt64));

            buf = IntPtr.Zero;
        }
        finally
        {
            // There was an error!
            if (buf != IntPtr.Zero)
            {
                // Free the allocated memory
                bool result = VirtualFree(buf, IntPtr.Zero, MEM_RELEASE);

                if (!result)
                {
                    throw new Win32Exception();
                }
            }
        }
    }

    // Fallback if rdtsc isn't available. We can't use directly
    // Stopwatch.GetTimestamp() because the return type is different.
    private static ulong StopwatchGetTimestamp()
    {
        return unchecked((ulong)Stopwatch.GetTimestamp());
    }
}

要长得多……有两种方法,

ulong ts1 = Rdtsc.Timestamp();
ulong ts2 = Rdtsc.TimestampP();

第一个使用rdtsc,而第二个使用rdtscp。 rdtscp 比 rdtsc 好,因为它没有在管道中重新排序。 TimestampP 方法一使用cpuid + rdtsc 为旧处理器提供了回退,但回退速度相当慢。对于这两种情况,使用Stopwatch.GetTimestamp() 的非 Intel/Amd 处理器都有备用方案。该类在内部使用cpuid 指令来检查rdtscp 指令的存在。有两个字段,IsRdtscSupported 和 IsRdtscPSupported,告诉处理器是否支持 rdtsc 和 rdtscp。

【讨论】:

  • 我对那段 C# 代码印象深刻。不知道这是可能的。但是,我想知道:使用性能计数器不是更有效吗?参见例如Acquiring high-resolution time stamps。他们明确警告不要使用RDTSC。
  • @stakx 我的思想流派“有人问你绳子,你给他们绳子,然后看着他们”。我是Stopwatch 的一个非常满意的用户。
  • @stakx 然后,我不得不说实话......有几个月我想尝试编写一些汇编代码并从 C# 中调用它们:-)
  • @xanatos - 谢谢你的绳索。我正在移植一些 C++ 代码,并希望尽可能地保持对原始代码的“真实”。我会检查秒表,看看结果是否相似。
  • @xanatos 我认为只有在现代英特尔 CPU 上才有可能。只需要想办法获得 TSC 频率。 “要将周期转换为挂钟时间,我们需要知道 TSC 频率。现代英特尔芯片上的频率缩放不会影响 TSC”github.com/dterei/tsc/blob/master/README.md
【解决方案2】:

没有 P/invoke 的简单版本的 RDTSC。此代码用 asm 代码替换现有方法。查看其他计时器@xanatos

public static class Rdtsc
    {
        // private static readonly byte[] Rdtscp = { 0x0F, 0x01, 0xF9, 0xC3 }; //32 bit timer
        private static readonly byte[] Rdtscp = { 0x0F, 0x01, 0xF9, 0x48, 0xC1, 0xE2, 0x20, 0x48, 0x09, 0xD0, 0xC3 }; //64bit
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static ulong TimestampP()
        {
            Stopwatch.GetTimestamp();
            Stopwatch.GetTimestamp();
            return 0;
        }

        public static unsafe void Init()
        {
            Func<ulong> func = TimestampP;
            var reference = __makeref(func);

            var ptrReference = (IntPtr*)*(IntPtr*)&reference;
            var ptrFunc = (IntPtr*)*ptrReference;
            var ptrTimestampP = (byte*)*(ptrFunc + 0x4);

            foreach (var b in Rdtscp)
                *(ptrTimestampP++) = b;
        }
    }

并获得周期数。调用这样的东西

public static void Main(string[] args)
{  
     CyclesPerSecond = GetCyclesPerSecond();
     for (var i = 0; i < 3; i++)
         Console.WriteLine($"CyclesPerSecond: {GetCyclesPerSecond(),5:0} ");
     for (var i = 0; i < 10; i++)
         Console.WriteLine($"cycles: {ParseDecimal(),5:0.00} ");
}
private static ulong GetCyclesPerSecond()
{
    Rdtsc.Init();

    var sw = Stopwatch.StartNew();
    var startms = Rdtsc.TimestampP();
    do { } while (sw.ElapsedMilliseconds < 1000);
    var endms = Rdtsc.TimestampP();

    return endms - startms;
}

private static double ParseDecimal()
{
     var decStr = "-49823174320.9293800";

     var mincycles = ulong.MaxValue;
     var testDuration = 10000000000UL;
     var testCycles = 0UL;
     var minIterations = 100;
     var pos = 0;
     var startTest = Rdtsc.TimestampP();
     do
     {
         var start = Rdtsc.TimestampP();
         for (var j = 0; j < minIterations; j++) decimal.Parse(decStr);
         var end = Rdtsc.TimestampP();
         var cycles = end - start;
         if (cycles <= mincycles) mincycles = cycles;
         testCycles = Rdtsc.TimestampP() - startTest;
     } while (testCycles < testDuration);
     return mincycles / (double)minIterations;
 }

【讨论】:

  • 非常好的答案,这种功能的每一点性能都很重要。只是要清楚。这是什么平台? [32 或 64 位] 和 [.NET Framework 或 .NET Core] 仅限 Windows?
  • @Joe 计时器只依赖于 64 位/32 位。上面的代码是 64 位的。对于 32 位,您可以使用 `private static readonly byte[] Rdtscp = { 0x0F, 0x01, 0xF9, 0xC3 };`
【解决方案3】:

除了@xanatos 出色的答案外,我需要报告 nanos 中的延迟并成功使用此代码。使用风险自负,特定于 Windows,它似乎容易损坏。

public static string GetRdtscTicksToNanos(ulong ticks)
{
    double calcBaseClock = 1024d * (double)Stopwatch.Frequency;
    double rval = (double)ticks / calcBaseClock;
    return rval.ToString("N9");
}

看来:

Stopwatch.Frequency = QueryPerformanceFrequency = RDTSC_RATE / 1024

见:http://aakinshin.net/blog/post/stopwatch/

【讨论】:

  • 秒表。频率始终为 10^7 (100ns)。与RDTSC频率无关。
【解决方案4】:

不要打扰 RDTSC。它不能从 C# 中有效地调用。

在 C# 中,获得 100ns 精度刻度的最快方法是通过 Stopwatch.GetTimestamp()。

不幸的是,上面介绍的调用 RDTSC 的方法不会胜过它。

这里有一个测试程序来证明这一点:

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine($"Rdtsc ticksPerMillisecond: {ticksPerMillisecond}");

        MethodA_UtcNow();
        MethodB_RdtscP();
        MethodC_Rdtsc();
        MethodD_StopwatchGetTimestamp();
        MethodE_EnvironmentTickCount();
        MethodX_ThreadAtomic();
    }

    private static void MethodA_UtcNow()
    {
        long cnt = 0;
        var doneTime = DateTime.UtcNow.AddSeconds(1).Ticks;
        var startDT = DateTime.Now;
        while (DateTime.UtcNow.Ticks <= doneTime)
        {
            cnt++;
        }
        var endDT = DateTime.Now;
        Console.WriteLine("UtcNow        Time Taken: {0,20}  Total Counted: {1,12}", endDT.Subtract(startDT), cnt);
    }

    private static void MethodE_EnvironmentTickCount()
    {
        long cnt = 0;
        int doneTick = Environment.TickCount + 1000;
        var startDT = DateTime.Now;
        while (Environment.TickCount <= doneTick)
        {
            cnt++;
        }
        var endDT = DateTime.Now;
        Console.WriteLine("TickCount     Time Taken: {0,20}  Total Counted: {1,12}", endDT.Subtract(startDT), cnt);
    }

    private static void MethodB_RdtscP()
    {
        long cnt = 0;
        ulong doneTick = Rdtsc.TimestampP() + ticksPerMillisecond * 1000;
        var startDT = DateTime.Now;
        while (Rdtsc.TimestampP() <= doneTick)
        {
            cnt++;
        }
        var endDT = DateTime.Now;
        Console.WriteLine("RdtscP        Time Taken: {0,20}  Total Counted: {1,12}", endDT.Subtract(startDT), cnt);
    }

    private static void MethodC_Rdtsc()
    {
        long cnt = 0;
        ulong doneTick = Rdtsc.Timestamp() + ticksPerMillisecond * 1000;
        var startDT = DateTime.Now;
        while (Rdtsc.Timestamp() <= doneTick)
        {
            cnt++;
        }
        var endDT = DateTime.Now;
        Console.WriteLine("Rdtsc         Time Taken: {0,20}  Total Counted: {1,12}", endDT.Subtract(startDT), cnt);
    }

    static ulong ticksPerMillisecond = CalibrateTSCTicksPerMillisecond();

    private static ulong CalibrateTSCTicksPerMillisecond()
    {
        ulong rate = 0;
        ulong n = 100;
        for (ulong i = 0; i < n; i++)
        {
            ulong res = CountTSCTicksPerMillisecond();
            if (i == 0 || res < rate)
                rate = res;
        }
        return rate;
    }

    private static ulong CountTSCTicksPerMillisecond()
    {
        ulong tsc1 = Rdtsc.TimestampP();
        long ticks2 = DateTime.UtcNow.Ticks + TimeSpan.TicksPerMillisecond;
        while (DateTime.UtcNow.Ticks < ticks2) ;
        ulong tsc2 = Rdtsc.TimestampP();
        return tsc2 - tsc1;
    }

    private static void MethodD_StopwatchGetTimestamp()
    {
        long cnt = 0;
        long doneTick = Stopwatch.GetTimestamp() + TimeSpan.TicksPerMillisecond * 1000;
        var startDT = DateTime.Now;
        while (Stopwatch.GetTimestamp() <= doneTick)
        {
            cnt++;
        }
        var endDT = DateTime.Now;
        Console.WriteLine("Stopwatch     Time Taken: {0,20}  Total Counted: {1,12}", endDT.Subtract(startDT), cnt);
    }

    private static void MethodX_ThreadAtomic()
    {
        readyEvent.Reset();
        Thread counter = new Thread(CountBool);
        Thread waiter = new Thread(WaitBool);
        counter.Start();
        waiter.Start();
        waiter.Join();
        counter.Join();
    }

    private static void CountBool()
    {
        long cnt = 0;
        readyEvent.WaitOne();
        var startDT = DateTime.Now;
        while (!done)
        {
            cnt++;
        }
        var endDT = DateTime.Now;
        Console.WriteLine("Thread+Atomic Time Taken: {0,20}  Total Counted: {1,12}", endDT.Subtract(startDT), cnt);
    }

    private static volatile bool done = false;
    private static ManualResetEvent readyEvent = new ManualResetEvent(false);

    private static void WaitBool()
    {
        readyEvent.Set();
        Thread.Sleep(1000);
        done = true;
    }

}

Xeon E5 v4 上的输出:

Rdtsc ticksPerMillisecond: 3600200
UtcNow        Time Taken:     00:00:00.9999706  Total Counted:     12561955
RdtscP        Time Taken:     00:00:01.0000589  Total Counted:     37796549
Rdtsc         Time Taken:     00:00:01.0000587  Total Counted:     59091486
Stopwatch     Time Taken:     00:00:01.0000026  Total Counted:     67602657
TickCount     Time Taken:     00:00:01.0085401  Total Counted:    319244516
Thread+Atomic Time Taken:     00:00:01.0123246  Total Counted:   3272391367

换句话说:

  • 对于~16ms 的精度,使用Environment.TickCount。它非常快,每次调用大约需要 3ns。
  • 对于 100ns 精度,使用 Stopwatch.GetTimestamp()。每次调用大约需要 15ns。

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多