在Linux中精确到(或更好)微秒的处理时间 [英] Microsecond accurate (or better) process timing in Linux
问题描述
我需要一种非常准确的方式来计时程序的各个部分.我可以为此使用常规的高分辨率时钟,但这将返回挂钟时间,这不是我所需要的:我只需要花时间运行我的进程即可.
I need a very accurate way to time parts of my program. I could use the regular high-resolution clock for this, but that will return wallclock time, which is not what I need: I needthe time spent running only my process.
我清楚地记得看到一个Linux内核补丁,该补丁使我可以将进程的时间设置为纳秒级,只是我忘记为其添加书签,并且也忘记了补丁的名称:(.
I distinctly remember seeing a Linux kernel patch that would allow me to time my processes to nanosecond accuracy, except I forgot to bookmark it and I forgot the name of the patch as well :(.
我还记得它是如何工作的:
I remember how it works though:
在每个上下文切换上,它将读取高分辨率时钟的值,并将最后两个值的差值添加到正在运行的进程的处理时间中.这样可以高分辨率地准确查看过程的实际过程时间.
On every context switch, it will read out the value of a high-resolution clock, and add the delta of the last two values to the process time of the running process. This produces a high-resolution accurate view of the process' actual process time.
使用常规时钟来保持常规处理时间,我相信这是毫秒精度(1000Hz),对于我的目的来说太大了.
The regular process time is kept using the regular clock, which is I believe millisecond accurate (1000Hz), which is much too large for my purposes.
有人知道我在说什么内核补丁吗?我还记得它像一个单词之前或之后的字母-类似于"rtimer"之类的东西,但我记不清了.
Does anyone know what kernel patch I'm talking about? I also remember it was like a word with a letter before or after it -- something like 'rtimer' or something, but I don't remember exactly.
(也欢迎其他建议)
Marko建议的完全公平的调度程序"不是我想要的,但看起来很有希望.我遇到的问题是,我可以用来获取处理时间的调用仍未返回足够细的值.
The Completely Fair Scheduler suggested suggested by Marko is not what I was looking for, but it looks promising. The problem I have with it is that the calls I can use to get process time are still not returning values that are granular enough.
- times()以毫秒为单位返回值21、22.
- clock()返回的值21000、22000,相同的粒度.
- getrusage()返回的值如210002、22001(等等),它们看起来具有更好的准确性,但这些值看起来明显相同.
所以现在我可能遇到的问题是内核具有所需的信息,我只是不知道会返回该信息的系统调用.
So now the problem I'm probably having is that the kernel has the information I need, I just don't know the system call that will return it.
推荐答案
如果您正在寻找这种级别的时序分辨率,则可能正在尝试进行一些微优化.在这种情况下,您应该查看 PAPI .它不仅提供时钟和虚拟(仅用于处理)时序信息,还提供对CPU事件计数器的访问,当您尝试提高性能时,这是必不可少的.
If you are looking for this level of timing resolution, you are probably trying to do some micro-optimization. If that's the case, you should look at PAPI. Not only does it provide both wall-clock and virtual (process only) timing information, it also provides access to CPU event counters, which can be indispensable when you are trying to improve performance.
这篇关于在Linux中精确到(或更好)微秒的处理时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!