在 x86 上获取当前时间的指令 [英] Instruction to get the current time on x86

查看:88
本文介绍了在 x86 上获取当前时间的指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有获取当前时间的 x86 指令?

Is there an x86 instruction to get the current time?

基本上......类似于clock_get_time的替代品......具有最小开销的东西......我并不真正关心以任何特定格式获取时间......只要它是我可以的格式使用.

Basically... something like a replacement for clock_get_time ... something with the minimum overhead... where I don't really care about getting the time in any specific format... as long as it's a format I can use.

基本上我正在做一些工作来检测多少物理现实生活时间"已经过去了......我希望能够尽可能频繁地测量时间!

Basically I'm doing some work to "Detect how much PHYSICAL REAL LIFE TIME" has gone by... and I want to be able to measure time as frequently as possible!

我猜你可以想象我正在做一些类似分析应用程序的事情......:)

I guess you can imagine i'm doing something like a profiling app... :)

我真的需要积极有效地访问硬件时间.所以理想情况下......一些 ASM 来获取时间......将它存储在某个地方......然后将其整理成我可以实际处理的某种格式.

I really need aggressively efficient access to the hardware time. So ideally... some ASM to get the time... store it somewhere... then massage it later into some format that I can actually process.

我对 _rdtsc 不感兴趣,因为它衡量经过的周期数.我需要知道执行了多少物理时间......而不是可能因热波动而变化的周期......

I'm not interested in _rdtsc as that measures the number of cycles gone by. I need to know how much physical time has executed... not cycles which can vary due to thermal fluctations or so..

推荐答案

对于分析,通常根据 CPU 时钟周期而不是挂钟时间进行分析最有用.CPU 动态时钟(涡轮增压和节能)使得在测量周期开始之前让 CPU 加速到全速很烦人.

For profiling, often it's most useful to profile in terms of CPU clock cycles, rather than wall-clock time. CPU dynamic clocking (turbo and power saving) makes it annoying to get the CPU ramped up to full speed before the start of a measurement period.

如果您在此之后仍然需要挂钟时间:

If you still need wall-clock time after that:

最近的 x86 CPU 有一个以固定速率运行的 TSC,不管 CPU 频率调整以节省电量.此外,当 CPU 停止时,TSC 不会停止.(即没有工作要做,所以它运行 HLT 指令以在低功耗模式下等待中断.)

Recent x86 CPUs have a TSC that runs at a fixed rate, regardless of CPU frequency adjustment for power-saving. Also, the TSC doesn't stop when the CPU is halted. (i.e. no work to do, so it ran the HLT instruction to wait for an interrupt in low-power mode.)

事实证明,在硬件中有效访问有用的时间源比实际的时钟周期计数器更有用,所以这就是 RDTSC 在引入后几代 CPU 的演变.现在我们又回到使用硬件性能计数器来测量时钟周期了.

It turned out that efficient access to a useful time-source was more useful to have in hardware than an actual clock cycle counter, so that's what RDTSC morphed into, a few CPU generations after its introduction. Now we're back to using hardware performance counters for measuring clock cycles.

在 Linux 中,在 /proc/cpuinfo 的 CPU 功能标志中查找 constant_tscnonstop_tsc.IDK,如果有 CPUID 位.如果没有,请使用 Linux 的代码(如果您可以使用 GPL 代码).

In Linux, look for constant_tsc and nonstop_tsc in the CPU features flags in /proc/cpuinfo. IDK if there are CPUID bits for those. If no, use Linux's code for it (if you can use GPLed code).

在具有这两个关键特性的 CPU 上,Linux 使用 TSC 作为其时钟源 IIRC.

On a CPU with those two key features, Linux uses the TSC as its clocksource, IIRC.

在用户空间中获取当前时间的最低开销方法是计算 RDTSC 滴答和实时之间的转换.在分析时,您可能只存储 64 位 TSC 快照,然后转换为实时.(所以你可以处理 TSC 环绕).RDTSC 只需要大约 24 个周期(Agner Fog 的指令表,Intel Haswell).我认为系统调用的开销将比这高一个数量级.(无论如何,内核都必须在某个地方执行 RDTSC).

The lowest overhead way to get the current time in user-space will be to work out the conversion between RDTSC ticks and real time. While profiling, you might just store 64bit TSC snapshots, and convert to real-time later. (So you can handle TSC wraparound then). RDTSC only takes about 24 cycles (Agner Fog's instruction table, Intel Haswell). I think the overhead of a system call will be an order of magnitude higher than that. (The kernel will have to do a RDTSC in there somewhere anyway).

Agner Fog 记录了他的分析/计时方法,并提供了一些示例代码.我最近没看,但它可能对这个应用程序有用.

Agner Fog has documented his profiling / timing methods, and has some example code. I haven't looked recently, but it might have useful stuff for this application.

这篇关于在 x86 上获取当前时间的指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆