测量从用户空间到内核空间的延迟的最佳方法是什么?

时间:2017-07-25 14:25:41

标签: c linux performance timer linux-kernel

我必须测量用户空间程序与其交互的驱动程序之间的延迟。我基本上通过这个应用程序发送数据包。 latecny在用户空间中的write与内核中相应的write函数之间

我在用户空间使用了clock_gettimeCLOCK_MONOTONIC  内核(驱动程序)中的getrawmonotonic,当我看到差异时,它是巨大的(大约4毫秒)。所以我肯定使用了错误的方法。

那么,最好的方法是什么?

2 个答案:

答案 0 :(得分:0)

要仅测量从用户到内核空间的单个上下文切换,请尝试使用TSC(时间戳计数器)。它适用于x86和ARM,用户和内核空间。

有关维基百科上的TSC的更多信息:https://en.wikipedia.org/wiki/Time_Stamp_Counter

x86 could be found here的BSD许可实施 和for 64-bit ARM here.

另外,正如评论所建议的那样,考虑使用任何可用的标准工具来衡量往返延迟,即使用内核和返回。

答案 1 :(得分:0)

如果我这样做,我会使用ftrace,它是linux内核提供的性能工具。

它可以跟踪内核中的几乎所有函数。

它首先将信息记录到内存中的环形缓冲区中,因此它很少滑行。

Linux内核源代码中有一个非常好的文档" Documentation / trace / ftrace.txt",你也可以找到它here

1.prepare the environment, configure ftrace.
2.run the application.
3.0.In the application, bind cpu, and give the application priority。
3.1.In the application, write something to the trace_marker, 
3.2.In the application, call the function which you want to test.
4.get the log from the ring buffer.
5.calculate the latency.
相关问题