如果CPU的使用率突然暴漲,如何迅速定位是哪個(gè)進(jìn)程、哪段代碼引起的呢?我們需要一個(gè)profiling工具,對(duì)CPU上執(zhí)行的代碼進(jìn)行采樣、統(tǒng)計(jì),告訴我們CPU到底在忙些什么。
perf 就是這樣的工具。我們舉個(gè)例子看看 perf 是怎樣工作的。
首先我們用以下命令模擬出CPU利用率暴漲的現(xiàn)象:
| $ cat /dev/zero > /dev/null |
然后我們看到 CPU 1 的 %system 飆升到95%:
| # sar -P ALL -u 2 2 08:21:16 PM CPU %user %nice %system %iowait %steal %idle 08:21:18 PM all 2.25 0.00 48.25 0.00 0.00 49.50 08:21:18 PM 0 0.50 0.00 1.00 0.00 0.00 98.51 08:21:18 PM 1 4.02 0.00 95.98 0.00 0.00 0.00 |
現(xiàn)在我們用 perf 工具采樣:
| # perf record -a -e cycles -o cycle.perf -g sleep 10 [ perf record: Woken up 18 times to write data ] [ perf record: Captured and wrote 4.953 MB cycle.perf (~216405 samples) ] |
注:”-a”表示對(duì)所有CPU采樣,如果只需針對(duì)特定的CPU,可以使用”-C”選項(xiàng)。
把采樣的數(shù)據(jù)生成報(bào)告:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # perf report -i cycle.perf | more ... # Samples: 40K of event 'cycles' # Event count (approx.): 18491174032 # # Overhead Command Shared Object Symbol # ........ ............... .............................. ................ # 75.65% cat [kernel.kallsyms] [k] __clear_user | --- __clear_user | |--99.56%-- read_zero | vfs_read | sys_read | system_call_fastpath | __GI___libc_read --0.44%-- [...] 2.34% cat [kernel.kallsyms] [k] system_call | --- system_call | |--56.72%-- __write_nocancel | --43.28%-- __GI___libc_read ... |
我們很清楚地看到,CPU利用率有75%來自 cat 進(jìn)程 的 sys_read 系統(tǒng)調(diào)用,perf 甚至精確地告訴了我們是消耗在 read_zero 這個(gè) kernel routine 上。
轉(zhuǎn)自:http://linuxperf.com/?p=36
posted on 2016-01-29 10:45
merlinfang 閱讀(1061)
評(píng)論(0) 編輯 收藏 引用