4zhvml8 发表于 2024-8-17 21:02:17

Linux中Python应用CPU占用高问题排查


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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>其LOAD和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>观察系统资源的消耗来排除其问题,以下记录的是问题排查的过程。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、<span style="color: black;">经过</span><strong style="color: blue;">top</strong>命令查看其CPU占用</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/e7619391cba1426fbfc51a1254097004~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895552&amp;x-signature=tQ6E%2BE55Tlc4lav4plLZf06xJ2U%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;">能够</span>看到其load占用为“20.14, 16.42, 19.16”,三个数字分别<span style="color: black;">暗示</span>cpu在1分钟、5分钟及15分钟的load,cpu的<span style="color: black;">运用</span>率<span style="color: black;">亦</span>偏高,达到了68.8%,占用cpu较高的进程ID为281020。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、<span style="color: black;">经过</span><strong style="color: blue;">ps</strong>查看该应用</p>$ ps aux|grep 281020
    root 212891 0.0 0.0 103328 848 pts/3 S+ 15:41 0:00 grep 281020
    ipcc 281020 39.5 2.6 1025560 425540 ? Sl 01:18 341:29 /usr/bin/python ./ctiRealtimeCdrMain.py default.xml<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果<span style="color: black;">表示</span>该应用为python应用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、查看进程中线程的CPU占用<span style="color: black;">状况</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<strong style="color: blue;">top</strong>中加入-H参数,查看该进程中线程的cpu战胜<span style="color: black;">状况</span>:</p>$ top -H -p 281020<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果如下:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/cb43f4e0985e47d58e714ac2dd406ec7~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895552&amp;x-signature=gWeY%2FAXtgtviMzxoIuMVetab4Dc%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;">能够</span>看到进程281020中线程号为281042的线程战胜的CPU最多。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4、跟踪进程的执行栈</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span><strong style="color: blue;">watch</strong>和<strong style="color: blue;">pstack</strong>命令查看线程中命令的执行<span style="color: black;">状况</span>:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/4c62728b6905490eb3da253afd580f86~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895552&amp;x-signature=aimXuY%2F%2Fb8hJ2EyyVBYvSriZdw8%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;">能够</span>看到从mysql中读取数据占用资源较多。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5、跟踪函数的调用</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">进一步<span style="color: black;">经过</span><strong style="color: blue;">strace</strong>命令进行验证:</p>$ strace -f -p 281042<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果如下:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/1a0cd0b10e1f4760898af639a504cf8b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895552&amp;x-signature=OGEJbU5zZUKVqndJmNiNSC%2B7Iq0%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;">海量</span>的数据在执行数据库的写入(write)及读取(read)操作,上面的pstack命令并<span style="color: black;">无</span>反映出write的问题,那问题应该出<span style="color: black;">此刻</span>数据库的read操作上,初步猜测是数据库的select语句有问题,<span style="color: black;">或</span>是<span style="color: black;">无</span>走索引。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6、<span style="color: black;">检测</span>MySql的慢日志</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">进一步<span style="color: black;">检测</span>MySql的慢日志<span style="color: black;">发掘</span>:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/dc0aff8c2ef04a8e9613be0e677d0fef~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895552&amp;x-signature=y38mrUXv3gbbaJHgZil%2BdpCw4io%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;">海量</span>的<span style="color: black;">查找</span>慢日志,SQL中<span style="color: black;">运用</span>了全表扫描的count、max、min<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>让对方对做优化了。</p>




DH802036 发表于 2024-8-20 03:36:08

系统提示我验证码错误1500次 \~゛,

time 发表于 2024-8-25 23:57:09

外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。

b1gc8v 发表于 2024-8-26 20:24:21

同意、说得对、没错、我也是这么想的等。

j8typz 发表于 2024-9-29 00:46:44

你说得对,我们一起加油,未来可期。

m5k1umn 发表于 2024-10-21 15:38:44

同意、说得对、没错、我也是这么想的等。

1fy07h 发表于 2024-10-24 20:09:55

你的留言真是温暖如春,让我感受到了无尽的支持与鼓励。
页: [1]
查看完整版本: Linux中Python应用CPU占用高问题排查