Linux TCP 大部分工作在 softirq 中,而 softirq 既可以無(wú)條件高優(yōu)先級(jí)在 hardirq 后執(zhí)行,也可在 percpu ksoftirqd 內(nèi)核線程中執(zhí)行,而后者飽受詬病,ksoftirqd 本質(zhì)上就是普通 task,它可能被應(yīng)用 task 擠壓。
在 Linux kernel 程序員眼里,softirq 天生高貴,但 top/sar 又容不得 softirq 跑高,程序員的狹隘和偏見(jiàn)使然。
事情的另一面,softirq 也可能擠壓應(yīng)用進(jìn)程,特別在數(shù)據(jù)中心高速傳輸場(chǎng)景,Linux softirq 的調(diào)度方式表現(xiàn)得尤其不可伸縮。softirq 在 hardirq 后最多可以執(zhí)行 10 rounds 以及 2ms,取下限:
/*
* We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
* but break the loop if need_resched() is set or after 2 ms.
* The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
* certain cases, such as stop_machine(), jiffies may cease to
* increment and so we need the MAX_SOFTIRQ_RESTART limit as
* well to make sure we eventually return from this method.
*
* These limits have been established via experimentation.
* The two things to balance is latency against fairness -
* we want to handle softirqs as soon as possible, but they
* should not be able to lock up the box.
*/
#define MAX_SOFTIRQ_TIME msecs_to_jiffies(2)
#define MAX_SOFTIRQ_RESTART 10
這時(shí)間對(duì)數(shù)據(jù)中心短肥管道及其漫長(zhǎng),足以餓死應(yīng)用。對(duì)高速 TCP,如果應(yīng)用程序沒(méi)機(jī)會(huì) write 將數(shù)據(jù)寫入 queue,再高優(yōu)的 softirq 觸發(fā) xmit 也難為無(wú)米之炊。
上周的討論,我建議將一次 round 與連接數(shù)解耦,而每次處理一個(gè)連接的時(shí)間片做成連接數(shù)的減函數(shù)。比方說(shuō) 10 萬(wàn)并發(fā),softirq 最多 2ms,100 萬(wàn)并發(fā)降為 500us,這便提供了伸縮性。
說(shuō)白了還是 Linux 調(diào)度太粗糙。上述應(yīng)用被 softirq 擠占的問(wèn)題,我使能軟中斷線程化就能解決,但應(yīng)用程序會(huì)反過(guò)來(lái)擠占 softirq,眾口難調(diào),你能堆 patch,但解決不了問(wèn)題。還是四象限法靠譜,但要?jiǎng)哟笫中g(shù)。
理論上講,TCP ACK 要比 send data 更高優(yōu),特別是攜帶 SACK 的 ACK,TCP 規(guī)范里明確 receiver 對(duì)非連續(xù)報(bào)文需關(guān)閉 delayed ack 而立即回應(yīng),這說(shuō)明 TCP 希望 sender 更快響應(yīng)異常事件,落實(shí)到實(shí)現(xiàn)就是高優(yōu)處理 ACK,但 Linux kernel 不給力,它沒(méi)能做到高優(yōu)處理 ACK 同時(shí)不餓死應(yīng)用。這也是用戶態(tài)協(xié)議棧實(shí)現(xiàn)高速 TCP 的原因,自己實(shí)現(xiàn)調(diào)度。但另一方面,往大了說(shuō),想要高吞吐,何必用 TCP。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-599156.html
浙江溫州皮鞋濕,下雨進(jìn)水不會(huì)胖。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-599156.html
到了這里,關(guān)于Linux TCP 調(diào)度與伸縮性的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!