Question

I'm curious how many cycles it takes to change contexts in Linux. I'm specifically using an E5405 Xeon (x64), but I'd love to see how it compares to other platforms as well.

Was it helpful?

Solution

There`s a free app called LMBench written by Larry McVoy and friends. It provides a bunch of OS & HW benchmarks One of the tests is called lat_ctx and it measures contex switch latencies. Google for lmbench and check for yourself on your own HW. Its the only way to get a number meaningful to you.

Gilad

OTHER TIPS

Run vmstat on your machine while doing something that requires heavy context switching. It doesnt tell you how long the actual switch takes, but it will tell you how many switches you do per second.

Then, you have to estimate how much each timeslice spends performing actual code, compared to switching context. Maybe a 100:1 or something? I dont know. 1000:1?

A machine of mine is now doing roughly 3000 switches per second, ie 0.3 ms per timeslice. With a ratio of 100:1 that would mean the actual switch takes 0.003 ms.

But, with multiple cores, threads yielding execution, etc etc, I'm wouldnt draw any conclusion from such a guess :)

I've written code that's able to echo (small) UDP packets at 200k packets per second.

That suggests that it's possible to context switch in not more than 2.5 microseconds, with the actual context switch probably taking somewhat less than that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top