Linux 网络堆栈是否可以在多核机器上并行运行?
我有一台运行 Linux 2.6.x 的 4 核机器。该机器是单宿主机(即具有 1 个 NIC)。
在这台机器上,我运行 4 个进程(或者 1 个进程中的 4 个线程),这些进程在网络上的 4 个不同主机之间进行网络 I/O。
问题:这4个网络I/O线程能够并行运行吗?我是否可以假设 TCP/IP 堆栈(包括 NIC 设备驱动程序)都是可以利用多个内核完全并行运行的并发代码?在网络 I/O 的任何阶段,这些线程是否会阻止尝试获取 TCP/IP 堆栈中的某些共享资源,从而导致堆栈的一部分(以及位于顶部的 4 个应用程序级线程)部分被占用顺序的而不是完全并行的?
I have a 4-core machine running Linux 2.6.x. The machine is single-homed (i.e., with 1 NIC).
On this machine, I run 4 processes (or, alternatively, 4 threads in 1 process) that do network I/O from/to 4 different hosts on the network.
Question: Will these 4 threads of network I/O be able to run in parallel? Can I assume that the TCP/IP stack (including the NIC device driver) is all concurrent code that can exploit the multiple cores to run completely parallely? At any stage in their network I/O, would these threads ever block trying to acquire some shared resource in the TCP/IP stack, thereby causing portions of stack -- and thus the 4 application-level threads sitting atop -- to be partially sequential and not fully parallel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,所有 4 个线程都会进行网络 IO(自 Linux 2.4 以来大内核锁已经结束)。
您会注意到 64 核计算机上的争用,如本文中分析的那样(使用 Linux 2.6.35): https://www.usenix.org/legacy/events/osdi10/tech/full_papers/Boyd-Wickizer.pdf,但只有 4 个内核和较新的内核,您可能不会注意到任何争用。
PS:即使您的芯片组或网卡只向 CPU 0 发送中断,数据包接收也会分发到所有内核,因为它是在可以分配给任何内核的软件中断中完成的。您可以使用 cat /proc/interrupts 查看网络硬件中断是否分布在所有内核上。
Yes, all 4 threads will do network IO (the big kernel lock is over since Linux 2.4).
You will notice contention on a machine with 64 cores, like the analyzed in this paper (using Linux 2.6.35): https://www.usenix.org/legacy/events/osdi10/tech/full_papers/Boyd-Wickizer.pdf , but with just 4 cores and newer kernels, probably you not notice any contention.
PS: Even if you had a chipset or network card that only sends interrupts to CPU 0, packet reception will be distributed to all cores, because it's done in software interrupts that can be assigned to any core. You can use cat /proc/interrupts to see if network hardware interrupts are being distributed across all cores.