简体   繁体   English

Linux获取上下文切换时间

[英]Linux get context switch timing

I'm implementing some form of internal profiler. 我正在实现某种形式的内部探查器。 Is there a way to know when and for how long a thread is context switched out? 有没有办法知道何时将线程切换到上下文? I know windows has it w the event tracing api and I know perf logs how many context switches happens. 我知道Windows具有事件跟踪API,并且我知道perf记录发生了多少次上下文切换。 Is there a way to do it on linux? 有办法在Linux上做到吗? Needing root privileges is not an issue since it will be an internal tool. 不需要root特权就不是问题,因为它将是一个内部工具。

Sort of. 有点。

See http://man7.org/linux/man-pages/man2/getrusage.2.html about the getrusage() function. 有关getrusage()函数,请参见http://man7.org/linux/man-pages/man2/getrusage.2.html

Notice that the structure it returns has voluntary and involuntary context switch numbers. 注意,它返回的结构具有自愿和非自愿上下文切换号。 Also, you have user and system time. 另外,您还有用户和系统时间。 Other APIs return wall-clock time. 其他API返回壁钟时间。

Any wall-clock time greater than your user and system time is time you weren't running. 任何大于您的用户时间和系统时间的挂钟时间就是您未运行的时间。

Other than that, you could probably use the kernel ftrace ability. 除此之外,您可能还可以使用内核ftrace功能。 See https://www.kernel.org/doc/Documentation/trace/ftrace.txt 参见https://www.kernel.org/doc/Documentation/trace/ftrace.txt

Read http://www.brendangregg.com/blog/2015-07-08/choosing-a-linux-tracer.html for even more options. 阅读http://www.brendangregg.com/blog/2015-07-08/choosing-a-linux-tracer.html了解更多选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM