How long does a context switch take in Linux?

坚强是说给别人听的谎言 提交于 2020-01-23 10:43:06

问题


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.


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/513692/how-long-does-a-context-switch-take-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!