如何获取 Win32 中的 CPU 周期计数?
在 Win32 中,有没有办法获得唯一的 cpu 周期计数或类似的东西,对于多个进程/语言/系统/等来说是统一的。
我正在创建一些日志文件,但必须生成多个日志文件,因为我们托管 .NET 运行时,并且我希望避免从一个日志文件调用另一个日志文件来记录日志。 因此,我想我只需生成两个文件,将它们组合起来,然后对它们进行排序,以获得涉及跨世界调用的连贯时间线。
但是,GetTickCount 不会随着每次调用而增加,因此这是不可靠的。 是否有更好的号码,以便我在排序时能够以正确的顺序接到电话?
编辑:感谢 @Greg 让我走上了 QueryPerformanceCounter 的道路,这确实做到了窍门。
In Win32, is there any way to get a unique cpu cycle count or something similar that would be uniform for multiple processes/languages/systems/etc.
I'm creating some log files, but have to produce multiple logfiles because we're hosting the .NET runtime, and I'd like to avoid calling from one to the other to log. As such, I was thinking I'd just produce two files, combine them, and then sort them, to get a coherent timeline involving cross-world calls.
However, GetTickCount does not increase for every call, so that's not reliable. Is there a better number, so that I get the calls in the right order when sorting?
Edit: Thanks to @Greg that put me on the track to QueryPerformanceCounter, which did the trick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 RDTSC CPU 指令(假设为 x86)。 该指令提供 CPU 周期计数器,但请注意,它会很快增加到最大值,然后重置为 0。正如 Wikipedia 文章提到的,您最好使用 QueryPerformanceCounter 函数。
You can use the RDTSC CPU instruction (assuming x86). This instruction gives the CPU cycle counter, but be aware that it will increase very quickly to its maximum value, and then reset to 0. As the Wikipedia article mentions, you might be better off using the QueryPerformanceCounter function.
System.Diagnostics.Stopwatch.GetTimestamp() 返回自时间原点(可能是计算机启动时,但我不确定)以来的 CPU 周期数,并且我从未见过它在两次调用之间没有增加。
CPU 周期对于每台计算机都是特定的,因此您不能使用它来合并两台计算机之间的日志文件。
System.Diagnostics.Stopwatch.GetTimestamp() return the number of CPU cycle since a time origin (maybe when the computer start, but I'm not sure) and I've never seen it not increased between 2 calls.
The CPU Cycles will be specific for each computer so you can't use it to merge log file between 2 computers.
RDTSC 输出可能取决于当前内核的时钟频率,对于现代 CPU 来说,该时钟频率既不是恒定的,也不是在多核机器中一致的。
使用系统时间,如果处理来自多个系统的源,请使用 NTP 时间源。 通过这种方式,您可以获得可靠、一致的时间读数; 如果开销对于您的目的来说太多,请使用 HPET 计算自最后已知的可靠时间读数比单独使用 HPET 更好。
RDTSC output may depend on the current core's clock frequency, which for modern CPUs is neither constant nor, in a multicore machine, consistent.
Use the system time, and if dealing with feeds from multiple systems use an NTP time source. You can get reliable, consistent time readings that way; if the overhead is too much for your purposes, using the HPET to work out time elapsed since the last known reliable time reading is better than using the HPET alone.
使用 GetTickCount 并在合并日志文件时添加另一个计数器。 不会为您提供不同日志文件之间的完美顺序,但它至少会以正确的顺序保留每个文件中的所有日志。
Use the GetTickCount and add another counter as you merge the log files. Won't give you perfect sequence between the different log files, but it will at least keep all logs from each file in the correct order.
使用 RDTSC。
像 QueryPerformanceCounter 这样的过程只是在底层调用 RDTSC。
RDTSC 大约 16 年以来一直不依赖于动态频率变化。 来自Intel 64 和 IA-32 架构软件开发人员手册(第 3B 卷):
RDTSCP 是一条相关指令,它还提供与读取的时间戳相关的处理器 ID:
根据您要测量的内容,将栅栏与对 RDTSC 的调用结合使用可能是合适的:
上述内容也适用于 AMD 处理器:
AMD 处理器至少从 Bulldozer 开始就拥有不变的 TSC。 来自AMD 系列 15h 型号 30h-3Fh 处理器的 BIOS 和内核开发人员指南 (BKDG):
参考文献:
https://www. intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer -references/40332.pdf
https://www.amd.com/content/dam/amd/en/documents/archived-tech-docs/design-guides/25481.pdf
Use RDTSC.
Procedures like QueryPerformanceCounter are just calling RDTSC under the hood.
RDTSC has not been dependent on dynamic frequency changes for about 16 years. From The Intel 64 and IA-32 Architectures Software Developer’s Manual (Vol. 3B):
RDTSCP is a related instruction that also provides the processor ID relevant to the timestamp that was read:
Depending on what you're measuring, it may be appropriate to use fences in conjunction with calls to RDTSC:
The above also applies to AMD processors:
AMD processors have had an invariant TSC since at least as far back as Bulldozer. From The BIOS and Kernel Developer’s Guide (BKDG) for AMD Family 15h Models 30h-3Fh Processors:
References:
https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/40332.pdf
https://www.amd.com/content/dam/amd/en/documents/archived-tech-docs/design-guides/25481.pdf
这是一篇有趣的文章! 说不要使用 RDTSC,而是使用 < a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904" rel="nofollow noreferrer">QueryPerformanceCounter。
结论:
Heres an interesting article! says not to use RDTSC, but to instead use QueryPerformanceCounter.
Conclusion: