????????TCP??TIME_WAIT??????????????????????TIME_WAIT????TIME_WAIT????????2??MSL(Max Segment Lifetime)??????4?????TIME_WAIT???μ?????????????д?????TIME_WAIT???????????????HTTP?????????????????????????????
????net.ipv4.tcp_tw_reuse=1
????net.ipv4.tcp_tw_recycle=1
?????????????TIME_WAIT????????????TIME_WAIT???????
????TCP????????????????RWIN??TCP Receive Window Size????????????????????????TCP???????????Sender????ack???????????????????????????????????????Sender??????Receiver??????ack??Sender???????????????????????????????????????????????TCP???????????????????????????????????ж????????????TCP??????????????????????????????????????????????????????????????????????????£?
????net.core.wmem_default = 8388608
????net.core.rmem_default = 8388608
????net.core.rmem_max = 16777216
????net.core.wmem_max = 16777216
???????????????????RWIN??????ó????????*??·???Sender???buffer??ú?RWIN????????С?????Sender???????????????Receiver???????????????????buffer??С????????????????????????????????????????????????????????????????磬??????????buffer???????????????ack?????Щ?????????????????????磬???????Щbuffer???????????ж???????????ack????buffer??????????????????????TCP??????е???????????????????????????????????????£?????????????????????????????????????????????綪???????????С??????????????LAN????????????????????????????????buffer???и???????紫??????????????????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????UDP???????????????????
????B)UDP????
???????UDP?????????Щ???????????????????MTU——?????????????TCP???????????????·??????????????ν????????????????????·??????????????????????????????70????????????·??????????????????·??????????100??????????????ζ????????????7000???????????????????????????????????????20???????????????2000??????????·?????????????????????????????????????UDP??????????????????????MTU?????????????????????????????????????????????MTU?????????1500??????????4352????802.11????????7981???????????????TCP/UDP????????????????Ч????Payload??????????????IPЭ??????20??????UDP?????8??????TCP???????????????????????????UDP??????????1500-8-20=1472???????????????С?????????????ù???????????????????Щ??????????£??????ЩNB?????????????????????????????????????????????????С??????MTU??????????fragment?????????????????????飬?????????????д??????
??????????£????Socket???????????????setsockopt() ????SO_SNDBUF/SO_RCVBUF???С??TTL??KeepAlive??Щ??????????????????к???????????????Socket?????
??????????UDP??????????????multi-cast??????????????????????????????????????????????Ч???????????????????????????????????????????????????????????????
????C??????????
????????????????????????????????????????????????????????Linux?£??????????ifconfig?????????????????????????overrun??????????????????????????txqueuelen???磨???????1000???????????????Щ???磺ifconfig eth0 txqueuelen 5000??Linux????????????У?ethtool?????????????????????????С????Windows?£?????????????????????е???????е?????????????磺Receive Buffers?? Transmit Buffer?????????????в?????????????Buffer???????????????????????紫??????Ч??
????D??????????????
?????????·???ü?????????????????????????е?TCP?????????????????????????????select???????????????????1024??????????????poll??????????1024???????????select??poll??????????????????????????????????????????????????O(n)???????????epoll???????epoll???????????????????????????????????????callback?????????????????????????????Linux Kernel 2.6?????????????2.5.44?????????????????????е?????????????????????epoll_ctl??????????????????????????????????
?????????????Щ??DNS Lookup?????????С??????磺gethostbyaddr/gethostbyname????????????????????????????????????????????????DNS??????????????????????????????????????????????time out?????????????????hosts?????????????????????????й???????????????????????????????????ζ??顣?????????????棬gethostbyname??????????????????????????????gethostbyname??????????????????????gethostbyname???????????????????????С??????????????GNU??gethostbyname_r()???????????????Щ???????????????????????????磬??????Linux?????NIS??????NFS???Щ?????????????????ú??????????С???
????#p#
????4.4??????
????A??I/O???
????????????select/poll/epoll???????????????????????Unix/Linux?°????е??豸???????????????I/O??????????????????????????I/O?????????á????I/O??????????????I/O????????????????????Unix/Linux?????I/O??????????Linux?μ?I/O???????????????????????????I/O???????????????
??????????????????I/O???????????
?????????????????????????????fctnl????O_NONBLOCK????ɡ?
???????????????select/poll/epoll????????I/O??????????????????????????????I/O??????????????á?
???????????AIO?????????I/O?????????????I/O???е?????I/O?????????????????????????????????????????I/O???????????ó???????????????????????????????????????????????????????????????????????I/O????????
?????????????????κε?????????????I/O????????????????????????????????????CPU??????????????????????????????????????????????Nginx????????Ч???????????epoll??AIO??????????I/O???
??????????Windows?μ?I/O????
????a???????WriteFile?????????????????????????????????????????????????????????????????Overlapped?????????????????????????????????????Overlapped??????Overlapped I/O???????WaitForSingleObject????????????д??ɡ??????????????????????
????b?????????WriteFileEx???????????????????I/O?????????????????callback????????I/O??????????????????????????Windows???callback?????????APC??Asynchronous Procedure Calls????????У??????????ó??????????????????Alterable???????????????е?????????????????????WaitForSingleObjectEx??WaitForMultipleObjectsEx?? MsgWaitForMultipleObjectsEx??SignalObjectAndWait ??SleepEx?????????Alterable??????????????????????wait????????????????
????c???????IOCP–IO Completion Port??IOCP???I/O????????????????У????????????????е?????????????????????????μ???????????????????????????????????μ???????????????????????IOCP??????????????????Linux?μ?AIO???????????????????????÷????????????
????????????????I/O???????????????I/O????????????????У???????????????????cache???????????????????????????????????????????д?????cache??д?????????д???Σ?????cache????????????????????????latency?????????????д??????????????????
????B?????CPU????
????????CPU??????????????????CPU0???????????0??CPU????ù??????????CPU???????????????CPU0???е???????????????????????????????????????????????????????????????????????????????????CPU????????????????CPU0?????????????????????????????????
????????Windows???????????????“?????????”?е?“????”???????????е?“?????????……”??Set Affinity…???????ò???????????????????????Щ?????
????????Linux????????????taskset??????????????????????schedutils????????????apt-get install schedutils??
???????CPU?????????????NUMA??????Non-Uniform Memory Access????????????????????SMP(Symmetric Multi-Processor )???????????????????????е?洢????I/O???????????????′洢?????????????????????ζ??????????NUMA???£????????????????node?????node??????????洢?????????NUMA???Щ?????????????????????????Linux??NUMA??????????Linux?£???NUMA????????????numactl ??????????????????????“myprogram arg1 arg2”??????node 0?????????????node 0 ??1???
????numactl --cpubind=0 --membind=0??1 myprogram arg1 arg2
???????????????????????????????????????node?????????á??????????ó???????????????????node???磺
????$ numactl --membind 1 --cpunodebind 1 --localalloc myapplication