views:

1508

answers:

3

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.

+3  A: 

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 :)

truppo
A: 

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.

Alnitak
Are you sure you are switching context and not get several UDP packets from the OS in one go?
Judge Maygarden
good point - will need to think about how I can check that...
Alnitak
+5  A: 

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

gby