1

Is there any way to execute a block of code in C without suffering a Context Switch?

I have modified perf stat to sample the counters periodically. While this works, it ends up giving me some bad data points such as 0 cycles x instructions.

I believe that the reason for this happening is because of context switching.

Since my code always pulls cycle counter value before instruction counter, if a context switch were to happen after pulling the cycle counter value, and if the process I was profiling executed for some portion of the scheduling quanta, then when I finally pulled the instruction counter value, it would be 'one ahead' of the cycle counter value that I already pulled.

Is there any way to execute the code-block without allowing a context switch to take place?

4

1 回答 1

1

不,没有简单的方法来实现 - 在没有上下文切换的情况下执行代码 - (除非您的代码在内核中运行,但无论如何您都想处理中断)。

您可以使用clock_gettime(2)在 Linux 应用程序中测量时间。仔细阅读时间(7) 。

顺便说一句,您确实需要上下文切换。没有它们,某些计算机可能会过热并损坏。(有些机器用软件处理他们的风扇)。

于 2012-12-14T20:42:03.167 回答