erlang 故障排查工具
系统级别perf top, dstat -tam, vtune 都能很好分析beam 瓶颈,本文主要erlang 级别排查:
1. 反编译
确认线上运行代码是否正确,reltools没掌握好,升级偶尔出现问题
decompile(Mod) ->
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(code:which(Mod), [abstract_code]),
io:format("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
2. 进程栈
类似于jstack,发现大量进程挂起,进程数过高,运行慢,hang住等问题用到
pstack(Reg) when is_atom(Reg) ->
case whereis(Reg) of
undefined -> undefined;
Pid -> pstack(Pid)
end;
pstack(Pid) ->
io:format("~s~n", [element(2, process_info(Pid, backtrace))]).
3. etop
分析内存、cpu占用进程,即使数十w进程node 也能正常使用
%进程CPU占用排名
etop() ->
spawn(fun() -> etop:start([{output, text}, {interval, 10}, {lines, 20}, {sort, reductions}]) end). %进程Mem占用排名
etop_mem() ->
spawn(fun() -> etop:start([{output, text}, {interval, 10}, {lines, 20}, {sort, memory}]) end). %停止etop
etop_stop() ->
etop:stop().
4. gc all
进程内存过高时,来一发,看看是内存泄露还是gc不过来
% 对所有process做gc
gc_all() ->
[erlang:garbage_collect(Pid) || Pid <- processes()].
5. fprof
% 对MFA 执行分析,会严重减缓运行,建议只对小量业务执行
% 结果:
% fprof 结果比较详细,能够输出热点调用路径
fprof(M, F, A) ->
fprof:start(),
fprof:apply(M, F, A),
fprof:profile(),
fprof:analyse(),
fprof:stop().
6. eprof
% 对整个节点内所有进程执行eprof, eprof 对线上业务有一定影响,慎用!
% 建议TimeoutSec<10s,且进程数< 1000,否则可能导致节点crash
% 结果:
% 输出每个方法实际执行时间(不会累计方法内其他mod调用执行时间)
% 只能得到mod - Fun 执行次数 执行耗时
eprof_all(TimeoutSec) ->
eprof(processes() -- [whereis(eprof)], TimeoutSec). eprof(Pids, TimeoutSec) ->
eprof:start(),
eprof:start_profiling(Pids),
timer:sleep(TimeoutSec),
eprof:stop_profiling(),
eprof:analyze(total),
eprof:stop().
7. scheduler usage
% 统计下1s每个调度器CPU的实际利用率(因为有spin wait、调度工作, 可能usage 比top显示低很多)
scheduler_usage() ->
scheduler_usage(1000). scheduler_usage(RunMs) ->
erlang:system_flag(scheduler_wall_time, true),
Ts0 = lists:sort(erlang:statistics(scheduler_wall_time)),
timer:sleep(RunMs),
Ts1 = lists:sort(erlang:statistics(scheduler_wall_time)),
erlang:system_flag(scheduler_wall_time, false),
Cores = lists:map(fun({{I, A0, T0}, {I, A1, T1}}) ->
{I, (A1 - A0) / (T1 - T0)} end, lists:zip(Ts0, Ts1)),
{A, T} = lists:foldl(fun({{_, A0, T0}, {_, A1, T1}}, {Ai,Ti}) ->
{Ai + (A1 - A0), Ti + (T1 - T0)} end, {0, 0}, lists:zip(Ts0, Ts1)),
Total = A/T,
io:format("~p~n", [[{total, Total} | Cores]]).
8. 进程调度
% 统计下1s内调度进程数量(含义:第一个数字执行进程数量,第二个数字迁移进程数量)
scheduler_stat() ->
scheduler_stat(1000). scheduler_stat(RunMs) ->
erlang:system_flag(scheduling_statistics, enable),
Ts0 = erlang:system_info(total_scheduling_statistics),
timer:sleep(RunMs),
Ts1 = erlang:system_info(total_scheduling_statistics),
erlang:system_flag(scheduling_statistics, disable),
lists:map(fun({{Key, In0, Out0}, {Key, In1, Out1}}) ->
{Key, In1 - In0, Out1 - Out0} end, lists:zip(Ts0, Ts1)).
9. trace 日志
会把mod 每次调用详细MFA log 下来,args 太大就不好看了
%trace Mod 所有方法的调用
trace(Mod) ->
dbg:tracer(),
dbg:tpl(Mod, '_', []),
dbg:p(all, c). %trace Node上指定 Mod 所有方法的调用, 结果将输出到本地shell
trace(Node, Mod) ->
dbg:tracer(),
dbg:n(Node),
dbg:tpl(Mod, '_', []),
dbg:p(all, c). %停止trace
trace_stop() ->
dbg:stop_clear().
10. 内存高OOM 排查工具
etop 无法应对10w+ 进程节点, 下面代码就没问题了;找到可疑proc后通过pstack、message_queu_len 排查原因
proc_mem_all(SizeLimitKb) ->
Procs = [{undefined, Pid} || Pid<- erlang:processes()],
proc_mem(Procs, SizeLimitKb). proc_mem(SizeLimitKb) ->
Procs = [{Name, Pid} || {_, Name, Pid, _} <- release_handler_1:get_supervised_procs(),
is_process_alive(Pid)],
proc_mem(Procs, SizeLimitKb). proc_mem(Procs, SizeLimitKb) ->
SizeLimit = SizeLimitKb * 1024,
{R, Total} = lists:foldl(fun({Name, Pid}, {Acc, TotalSize}) ->
case erlang:process_info(Pid, total_heap_size) of
{_, Size0} ->
Size = Size0*8,
case Size > SizeLimit of
true -> {[{Name, Pid, Size} | Acc], TotalSize+Size};
false -> {Acc, TotalSize}
end;
_ -> {Acc, TotalSize}
end
end, {[], 0}, Procs),
R1 = lists:keysort(3, R),
{Total, lists:reverse(R1)}.
erlang 故障排查工具的更多相关文章
- MongoDB 常用故障排查工具
1.profile profiling levels: 0,关闭profile:1,只抓取slow查询:2,抓取所有数据. 启动profile并且设置Profile级别: 可以通过mongo shel ...
- SQL Server 2008性能故障排查(一)——概论
原文:SQL Server 2008性能故障排查(一)--概论 备注:本人花了大量下班时间翻译,绝无抄袭,允许转载,但请注明出处.由于篇幅长,无法一篇博文全部说完,同时也没那么快全部翻译完,所以按章节 ...
- 使用strace工具故障排查的5种简单方法
使用strace工具故障排查的5种简单方法 本文源自5 simple ways to troubleshoot using strace strace 是一个非常简单的工具,用来跟踪可执行程序的系统调 ...
- 1个工具,助你提升K8S故障排查效率!
Kubernetes的故障排查一直困扰众多运维团队或DevOps,除了Kubernetes本身的复杂性之外,还有Kubernetes的工作负载是动态的原因.本文将介绍1个工具可以帮助你可视化K8S的网 ...
- Java线上应用故障排查之二:高内存占用
搞Java开发的,经常会碰到下面两种异常: 1.java.lang.OutOfMemoryError: PermGen space 2.java.lang.OutOfMemoryError: Java ...
- paip.hql的调试故障排查流程总结
paip.hql的调试故障排查流程总结 环境.myeclipse7.0 1 Hql的调试工具myeclipxe默认工具.../Hibernate8IDE 1 故障的排除方法overview 1 Hql ...
- 一次线上OOM故障排查经过
转贴:http://my.oschina.net/flashsword/blog/205266 本文是一次线上OOM故障排查的经过,内容比较基础但是真实,主要是记录一下,没有OOM排查经验的同学也可以 ...
- SQL Server 2008性能故障排查(二)——CPU
原文:SQL Server 2008性能故障排查(二)--CPU 承接上一篇:SQL Server 2008性能故障排查(一)--概论 说明一下,CSDN的博客编辑非常不人性化,我在word里面都排好 ...
- JVM 线上故障排查基本操作
# 前言 对于后端程序员,特别是 Java 程序员来讲,排查线上问题是不可避免的.各种 CPU 飚高,内存溢出,频繁 GC 等等,这些都是令人头疼的问题.楼主同样也遇到过这些问题,那么,遇到这些问题该 ...
随机推荐
- Power Management开发的一般流程
本文作为一个提纲挈领的介绍性文档,后面会以此展开,逐渐丰富. 开发流程 针对一个PM feature进行开发,设计模型是第一步.模型设计好之后,还要保留参数接口,可以基于这些参数针对特殊个体进行优化. ...
- 时间格式转换—将后台返回的/Date(1448954018000)/格式转换为正常的时间格式
用JS实现方法: function ChangeDateFormat(cellval) { )); < ? ) : date.getMonth() + ; ? " + date.get ...
- 俄罗斯方块C#版
using System; using System.Windows.Forms; using System.Drawing; using System.Media; class me : Form ...
- Stream与byte[]与Image与string
public byte[] GetByteImage(Image img) { byte[] bt = null; if (!img.Equals(null)) { using (MemoryStre ...
- [转]Pythoin中的Lambda表达式
引用自:http://www.cnblogs.com/evening/archive/2012/03/29/2423554.html 在学习python的过程中,lambda的语法时常会使人感到困惑, ...
- Git中如何利用生成SSH个人公钥访问git仓库
Git中如何利用生成SSH个人公钥访问git仓库方法(这里以coding平台为例): 1. 获取 SSH 协议地址 在项目的代码页面点击 SSH 切换到 SSH 协议, 获得访问地址, 请使用这个地址 ...
- java学习笔记之线程1
1.线程的概念 线程是系统中最小的执行单元,同一进程有多个线程,多个线程共享进程的资源. 线程调用yield()方法使线程从运行状态转入可运行状态,让出资源: 线程调用sleep()方法使线程由运行状 ...
- jQuery进阶
复习: jq无论如何都是一个集合 jq是一个包装集 var arr=$("div").get( )会将所有的DOM对象转换成真正的数组, get( )里边没传参数 兄弟元素: 只要 ...
- Leetcode 18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- CUDA[1] Introductory
Section 0 :Induction of CUDA CUDA是啥?CUDA®: A General-Purpose Parallel Computing Platform and Program ...