您的位置:首页 > 其它

@线上服务CPU100%问题快速定位实战

2017-09-14 17:09 971 查看
功能问题,通过日志,单步调试相对比较好定位。

性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底。

58到家架构部,运维部,58速运技术部联合进行了一次线上服务CPU问题排查实战演练,同学们反馈有收获,特将实战演练的试题和答案公布出来,希望对大家也有帮助。

题目

某服务器上部署了若干tomcat实例,即若干垂直切分的Java站点服务,以及若干Java微服务(每一个Java站点服务以及Java微服务都对应一个进程),突然收到运维的CPU异常告警。

问:如何定位是哪个服务进程导致CPU过载,哪个线程导致CPU过载,哪段代码导致CPU过载?

(进程 --> 线程 --> 代码块,一步步缩小范围)

步骤一、找到最耗CPU的进程

工具:top

方法

执行top -c ,显示进程运行信息列表

键入P(大写p,或者shift + p),进程按照CPU使用率排序

图示



如上图,最耗CPU的进程PID为10765

步骤二:找到最耗CPU的线程

工具:top

方法

top -Hp 10765 ,显示一个进程的线程运行信息列表(线程肯定是归属于某一个进程的)

键入P(大写p),线程按照CPU使用率排序

图示



如上图,进程10765内,最耗CPU的线程PID为10804

步骤三:将线程PID转化为16进制

工具:printf

方法:printf "%x\n" 10804

图示



如上图,10804对应的16进制是0x2a34,当然,这一步可以用计算器。

之所以要转化为16进制,是因为堆栈里,线程id是用16进制表示的。

步骤四:查看堆栈,找到线程在干嘛

工具:pstack/jstack/grep

方法jstack 10765 | grep '0x2a34' -C5 --color

打印进程堆栈(貌似jstack后面跟的是进程号?)

通过线程id,过滤得到线程堆栈

图示



如上图,找到了耗CPU高的线程对应的线程名称"AsyncLogger-1",以及看到了该线程正在执行代码的堆栈。

转载自:沈剑架构师之路公众号
备注:top命令参数

1. COMMAND-LINE Options
The command-line syntax for top consists of:

-hv | -abcHimMsS -d delay -n iterations -p pid [,pid...]

The typically mandatory switches (`-`) and even whitespace are completely optional.

-a : Sort by memory usage
This switch makes top to sort the processes by allocated memory

-b : Batch mode operation
Starts top in `Batch mode`, which could be useful for sending output from top to other programs or to a file.  In this mode, top will not accept input and runs
until the iterations limit you`ve set with the `-n` command-line option or until killed.

-c : Command line/Program name toggle
Starts  top  with  the  last remembered `c` state reversed.  Thus, if top was displaying command lines, now that field will show program names, and visa versa.
See the `c` interactive command for additional information.

-d : Delay time interval as:  -d ss.tt (seconds.tenths)
Specifies the delay between screen updates, and overrides the corresponding value in one`s personal configuration file or the startup default.  Later this  can
be changed with the `d` or `s` interactive commands.

Fractional  seconds  are honored, but a negative number is not allowed.  In all cases, however, such changes are prohibited if top is running in `Secure mode`,
except for root (unless the `s` command-line option was used).  For additional information on `Secure mode` see topic 5a. SYSTEM Configuration File.

-h : Help
Show library version and the usage prompt, then quit.

-H : Threads toggle
Starts top with the last remembered `H` state reversed.  When this toggle is On, all individual threads will be displayed.  Otherwise, top displays a summation
of all threads in a process.

-i : Idle Processes toggle
Starts top with the last remembered `i` state reversed.  When this toggle is Off, tasks that are idled or zombied will not be displayed.

-m : VIRT/USED toggle
Reports USED (sum of process rss and swap total count) instead of VIRT

-M : Detect memory units
Show memory units (k/M/G) and display floating point values in the memory summary.

-n : Number of iterations limit as:  -n number
Specifies the maximum number of iterations, or frames, top should produce before ending.

-p : Monitor PIDs as:  -pN1 -pN2 ...  or  -pN1, N2 [,...]
Monitor only processes with specified process IDs.  This option can be given up to 20 times, or you can provide a comma delimited list with up to 20 pids.  Co-
mingling both approaches is permitted.

This is a command-line option only.  And should you wish to return to normal operation, it is not necessary to quit and and restart top  --  just issue the `=`
interactive command.

-s : Secure mode operation
Starts top with secure mode forced, even for root.  This mode is far better controlled through the system configuration file (see topic 5. FILES).

-S : Cumulative time mode toggle
Starts  top  with the last remembered `S` state reversed.  When `Cumulative mode` is On, each process is listed with the cpu time that it and its dead children
have used.  See the `S` interactive command for additional information regarding this mode.

-u : Monitor by user as:  -u somebody
Monitor only processes with an effective UID or user name matching that given.

-U : Monitor by user as:  -U somebody
Monitor only processes with a UID or user name matching that given.  This matches real, effective, saved, and filesystem UIDs.

-v : Version
Show library version and the usage prompt, then quit.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: