一、排查uwsgi web服务异常的一般步骤
top命令 简单查看 cpu 内存
查看监控面板(查看组件是否异常 连接数 内存 硬盘)
uwsgitop 拿到busy的pid 使用py-spy查看
py-spy 查看调用栈
elk查看日志 最后根据调用栈和日志分析问题
二、cpu负载异常排查
查看当前进程负载信息
py-spy top --pid 7670 或者
py-spy top-python myprogram.py
生成火焰图分析
py-spy record -o profile.svg --pid 7670 或者
py-spy record -o profile.svg-python myprogram.py
三、内存泄露排查
last_snapshot = None
start_snapshot = None
import gc
import tracemalloc
class BeginShow(APIView):
"""
刚启动程序时执行,记录下创世内存快照
"""
authentication_classes = ()
permission_classes = ()
def get(self, request):
tracemalloc.start()
global last_snapshot
global start_snapshot
last_snapshot = None
start_snapshot = tracemalloc.take_snapshot()
return Response({"start_snapshot":"ok"})
class ShowMem(APIView):
authentication_classes = ()
permission_classes = ()
def get(self, request):
"""
用当前快照分别跟上次快照,创世快照对比,找出没释放的内存差值
"""
dump_string = ""
try:
# gc.collect() # 在快照之前手动回收
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
for stat in top_stats[:50]:
dump_string += "%s\n" % stat
global last_snapshot
global start_snapshot
if last_snapshot:
dump_string += "\ncompare to last:\n"
top_stats = snapshot.compare_to(last_snapshot, 'lineno')
for stat in top_stats[:50]:
dump_string += "%s\n" % stat
if start_snapshot:
dump_string += "\ncompare to start:\n"
top_stats = snapshot.compare_to(start_snapshot, 'lineno')
for stat in top_stats[:50]:
dump_string += "%s\n" % stat
last_snapshot = snapshot
except Exception as e:
dump_string += "%s\n" % str(e)
return Response({"dump_string": dump_string})
先调用 BeginShow
后面再调用 ShowMem 查看内存差值