Linux 中微秒精确(或更好)的进程计时 [英] Microsecond accurate (or better) process timing in Linux

查看:23
本文介绍了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:

在每次上下文切换时,它会读出一个高分辨率时钟的值,并将最后两个值的 delta 添加到正在运行的进程的进程时间.这会生成流程实际处理时间的高分辨率准确视图.

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 建议的 Completely Fair Scheduler 不是我想要的,但看起来很有希望.我遇到的问题是,我可以用来获取处理时间的调用仍然没有返回足够细化的值.

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.

http://icl.cs.utk.edu/papi/

这篇关于Linux 中微秒精确(或更好)的进程计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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