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 アプリケーション内の時間を測定できます。time(7)を注意深く読んでください。

ところで、コンテキストスイッチが必要です。それらがないと、一部のコンピューターは物理的に過熱し、物理的に壊れる可能性があります。(一部のマシンはファンをソフトウェアで処理します)。

于 2012-12-14T20:42:03.167 に答える