服务器性能优化之网络性能优化
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">曾几何时,一切都是<span style="color: black;">那样</span>简单。网卡很慢,<span style="color: black;">仅有</span>一个队列。当数据包到达时,网卡<span style="color: black;">经过</span>DMA复制数据包并发送中断,Linux内核收集这些数据包并完成中断处理。随着网卡越来越快,基于中断的模型可能会因<span style="color: black;">海量</span>传入数据包而<span style="color: black;">引起</span> IRQ 风暴。这将消耗大部分 CPU 功率并冻结系统。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为<span style="color: black;">认识</span>决这个问题,NAPI(中断+轮询)被提议。当内核收到来自网卡的中断时,它<span style="color: black;">起始</span><span style="color: black;">查找</span>设备并尽快收集队列中的数据包。NAPI <span style="color: black;">能够</span>很好地与<span style="color: black;">此刻</span><span style="color: black;">平常</span>的 1 Gbps 网卡<span style="color: black;">协同</span><span style="color: black;">运用</span>。<span style="color: black;">然则</span>,<span style="color: black;">针对</span>10Gbps、20Gbps<span style="color: black;">乃至</span>40Gbps的网卡,NAPI可能还<span style="color: black;">不足</span>。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>仍然<span style="color: black;">运用</span>一个 CPU 和一个队列来接收数据包,这些卡将需要更快的 CPU。幸运的是,<span style="color: black;">此刻</span>多核 CPU 很流行,<span style="color: black;">那样</span><span style="color: black;">为何</span>不并行处理数据包呢?</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/f2b09aa667734f688c2045bff7c0a42a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1725618480&x-signature=23%2BuPM%2B8q%2BgJ69Wl07Mia%2FrO5VA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">1、</span>在优化性能的时候,<span style="color: black;">首要</span>要判断性能的瓶颈在<span style="color: black;">以上</span>的哪个<span style="color: black;">地区</span>。<span style="color: black;">而后</span>对症下药,<span style="color: black;">根据</span>下面的<span style="color: black;">办法</span>来优化</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、<span style="color: black;">加强</span>CPU性能的<span style="color: black;">办法</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">并发。利用多线程、进程。老的线程库效率太低,需要升级用nptl 。进(线)程数不要大于cpu个数 <span style="color: black;">小心</span>用锁。改善架构,<span style="color: black;">尽可能</span><span style="color: black;">不消</span>锁。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">慎用字符串操作,<span style="color: black;">例如</span>sprintf,snprintf,<span style="color: black;">由于</span>%d %s等等都需要CPU资源去做词法分析,数量多的话,<span style="color: black;">亦</span>是不菲的开销,减少系统调用,例如time,<span style="color: black;">重点</span>消耗在用户态和内核态之间的切换。减少遍历操作。把握真实<span style="color: black;">需要</span>,简化实现<span style="color: black;">方法</span>优化架构,字符串协议解包很耗cpu,<span style="color: black;">思虑</span>单独一个线程,一个好的架构,服务器的CPU总消耗总是平均的分布在各个cpu上,CPU的消耗在70%<span style="color: black;">上下</span></span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/61e44987c0fd4eba88af6ec19d6f91e6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1725618480&x-signature=s%2BPV%2BkJux6pTK6vjrgvEdddhiI8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、<span style="color: black;">加强</span>网络IO的<span style="color: black;">办法</span>:<span style="color: black;">运用</span>epoll代替select,<span style="color: black;">运用</span>非阻塞的模式来<span style="color: black;">研发</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、<span style="color: black;">加强</span>磁盘IO的<span style="color: black;">办法</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Linux<span style="color: black;">能够</span>利用空闲内存作文件系统<span style="color: black;">拜访</span>的cache,<span style="color: black;">因此呢</span>系统内存越大存储系统的性能<span style="color: black;">亦</span>越好,利用<span style="color: black;">次序</span>写,减少寻道次数Cache策略,充分利用cpu和内存的资源来缓解磁盘读写压力</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、</span><span style="color: black;">怎样</span>来寻找系统的瓶颈呢?<span style="color: black;">能够</span>利用下面的工具:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1、top</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>查看系统中运行的进程的<span style="color: black;">情况</span>,CPU<span style="color: black;">运用</span><span style="color: black;">情况</span>,系统负载,内存<span style="color: black;">运用</span>等。它是<span style="color: black;">检测</span>系统进程运行<span style="color: black;">情况</span>最方便的工具了,它默认<span style="color: black;">表示</span>部分活动的进程,并且<span style="color: black;">根据</span>进程<span style="color: black;">运用</span>CPU的多少排序。它<span style="color: black;">能够</span><span style="color: black;">表示</span><span style="color: black;">所有</span>CPU的<span style="color: black;">运用</span><span style="color: black;">情况</span>,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">表示</span><span style="color: black;">每一个</span>进程都运行在那个CPU上面。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重点</span><span style="color: black;">能够</span>用top来看<span style="color: black;">那些</span>进程<span style="color: black;">或</span>那类进程占用CPU和内存资源最多,以此<span style="color: black;">快速</span>定位存在性能问题的进程,以及运行<span style="color: black;">反常</span>的进程</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/270b026c2c374c5db57399672a8a1ace~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1725618480&x-signature=I%2FUT1WiUwY9asO5SADN2%2BkCMu8Q%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、free</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">free命令<span style="color: black;">表示</span>系统内存的<span style="color: black;">运用</span><span style="color: black;">情况</span>(<span style="color: black;">理学</span>内存和交换内存)<span style="color: black;">经过</span>这个命令<span style="color: black;">咱们</span><span style="color: black;">能够</span>看到系统进程<span style="color: black;">实质</span><span style="color: black;">运用</span>的物理内存,buffer和cache<span style="color: black;">运用</span>的<span style="color: black;">理学</span>内存</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3、vmstat</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">vmstat是一个很全面的性能分析工具,<span style="color: black;">能够</span>观察到系统的进程状态、内存<span style="color: black;">运用</span>、虚拟内存<span style="color: black;">运用</span>、磁盘的IO、中断、上下问切换、CPU<span style="color: black;">运用</span>等。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4、Strace</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>用来查看一个进程在执行过程中的系统调用和所接收的信号。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/8e529cfd169a4fb085496b72c76197eb~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1725618480&x-signature=bjF8eJGP%2FIdPp%2BoC361XLqicoIU%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">5、tcpdump</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">linux下的抓包工具。<span style="color: black;">能够</span>把抓下来的信息重定向到文件里。<span style="color: black;">而后</span>在windows下用ethereal来分析。很强大</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">6、gprof</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">程序中<span style="color: black;">每一个</span>函数的CPU<span style="color: black;">运用</span>时间。<span style="color: black;">每一个</span>函数的调用次数。并<span style="color: black;">供给</span>简单调用关系图。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span><span style="color: black;">过程</span>:用gcc或g++编译程序时,<span style="color: black;">运用</span>-pg参数→执行编译得到的运行程序,会产生一个gmon.out文件→使用gprof命令。查看结果信息。(gprof -b proc_name gmon.out | less)TG:li9047</span></p>
我深受你的启发,你的话语是我前进的动力。 谷歌外链发布 http://www.fok120.com/
页:
[1]