与时间测量精度相关的linux补丁?

与时间测量精度相关的linux补丁?

是否有一组针对 Linux 内核的补丁可以解决(系统,而不是用户)进程时间的准确性?

我听说过 RTLinux,但我并不真正需要实际的实时调度约束,我只需要时间测量准确。如果进程切换滑了几毫秒,那就这样吧,但我需要准确测量当前进程中花费的实际时间

答案1

如果您想要使用time内置的 shell,那么 3 位精度是设计限制 - 根据man bash

TIMEFORMAT
          The value of this parameter is used as a format string  specify‐
          ing  how  the timing information for pipelines prefixed with the
          time reserved word should be displayed.  The % character  intro‐
          duces  an  escape  sequence  that is expanded to a time value or
          other information.  The escape sequences and their meanings  are
          as follows; the braces denote optional portions.
          %%        A literal %.
          %[p][l]R  The elapsed time in seconds.
          %[p][l]U  The number of CPU seconds spent in user mode.
          %[p][l]S  The number of CPU seconds spent in system mode.
          %P        The CPU percentage, computed as (%U + %S) / %R.

          The  optional  p is a digit specifying the precision, the number
          of fractional digits after a decimal point.  A value of 0 causes
          no decimal point or fraction to be output.  At most three places
          after the decimal point may be specified; values  of  p  greater
          than  3 are changed to 3.  If p is not specified, the value 3 is
          used.

为了似乎更精确的测量,您可以尝试使用date自定义输出格式来显示纳秒:

Tstart=$(date "+%s.%N")
some_command(s)
Tend=$(date "+%s.%N")
echo "Elapsed: $( Tend-Tstart |bc -l) nanoseconds"

然而,请注意,在两种情况(timedate)中,您都会有一些开销,因为系统需要一些时间来运行启动/停止命令。time由于它是内置的,因此开销较小。所以三位数的限制是有原因的:其余的只是随机垃圾。

也可以看看这个类似的问题,尽管目前接受的答案包含一个小错误。

相关内容