TCP_DEFER_ACCEPT和SO_REUSEPORT

本文介绍TCP_DEFER_ACCEPT特性的使用方法及其带来的性能优势,并详细解析了SO_REUSEPORT如何帮助解决多线程网络服务中的性能瓶颈。

1、使用TCP_DEFER_ACCEPT时,内核并不马上把已经建立成功的TCP连接通过accept()返回对应的fd。而是当TCP连接上有数据接收到后,再通过accept()返回fd。这样的好处是,可以立即使用recv接收一次数据,减少了一次epoll_wait()等待,适当提升一点点性能;

2、在linux3.9及以上的版本,linux内核支持SO_REUSEPORT。这样多个socket可以绑定同一个端口。普通的网络服务程序使用accept()获取不同的TCP/udp连接的FD。其使用方法一是使用一个线程负责accept()接收连接并分发给不同的工作线程。这样到导致线程切换,影响其性能。方法二是每个工作线程均accept(),会出现惊群问题,需要采用锁来解决accept()资源的问题,其性能影响是锁。支持SO_REUSEPORT后,上面两个方法的性能损耗可以得到解决,内核根据当前TCP连接的源目的IP和端口(4-TUPLE)进行HASH计算,从而选择不同的socket。


hecking for C compiler ... found + using GNU C compiler + gcc version: 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2) checking for gcc -pipe switch ... found checking for -Wl,-E switch ... not found checking for gcc builtin atomic operations ... not found checking for C99 variadic macros ... not found checking for gcc variadic macros ... not found checking for gcc builtin 64 bit byteswap ... not found checking for unistd.h ... found checking for inttypes.h ... found checking for limits.h ... found checking for sys/filio.h ... not found checking for sys/param.h ... found checking for sys/mount.h ... found checking for sys/statvfs.h ... found checking for crypt.h ... not found checking for Linux::aarch64 specific features checking for epoll ... not found checking for O_PATH ... not found checking for sendfile() ... not found checking for sendfile64() ... not found checking for sys/prctl.h ... found checking for prctl(PR_SET_DUMPABLE) ... not found checking for prctl(PR_SET_KEEPCAPS) ... not found checking for capabilities ... not found checking for crypt_r() ... not found checking for sys/vfs.h ... found checking for nobody group ... not found checking for nogroup group ... found checking for poll() ... not found checking for /dev/poll ... not found checking for kqueue ... not found checking for crypt() ... not found checking for crypt() in libcrypt ... not found checking for F_READAHEAD ... not found checking for posix_fadvise() ... not found checking for O_DIRECT ... not found checking for F_NOCACHE ... not found checking for directio() ... not found checking for statfs() ... not found checking for statvfs() ... not found checking for dlopen() ... not found checking for dlopen() in libdl ... not found checking for sched_yield() ... not found checking for sched_yield() in librt ... not found checking for sched_setaffinity() ... not found checking for SO_SETFIB ... not found checking for SO_REUSEPORT ... not found checking for SO_ACCEPTFILTER ... not found checking for SO_BINDANY ... not found checking for IP_TRANSPARENT ... not found checking for IP_BINDANY ... not found checking for IP_BIND_ADDRESS_NO_PORT ... not found checking for IP_RECVDSTADDR ... not found checking for IP_SENDSRCADDR ... not found checking for IP_PKTINFO ... not found checking for IPV6_RECVPKTINFO ... not found checking for TCP_DEFER_ACCEPT ... not found checking for TCP_KEEPIDLE ... not found checking for TCP_FASTOPEN ... not found checking for TCP_INFO ... not found checking for accept4() ... not found checking for eventfd() ... not found checking for eventfd() (SYS_eventfd) ... not found checking for int size ... ./configure: error: can not detect int size
最新发布
11-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值