歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> latencytop深度了解你的Linux系統的延遲

latencytop深度了解你的Linux系統的延遲

日期:2017/2/28 16:18:57   编辑:Linux教程

我們在系統調優或者定位問題的時候,經常會發現多線程程序的效率很低,但是又不知道問題出在哪裡,就知道上下文切換很多,但是為什麼上下文切換,是誰導致切換,我們就不知道了。上下文切換可以用dstat這樣的工具查看,比如:

[shell]
$dstat
—-total-cpu-usage—- -dsk/total- -net/total- —paging– —system–
usr sys idl wai hiq siq| read writ| recv send| in out | int csw
9 2 87 2 0 1|7398k 31M| 0 0 | 9.8k 11k| 16k 64k
20 4 69 3 0 4| 26M 56M| 34M 172M| 0 0 | 61k 200k
21 5 64 6 0 3| 26M 225M| 35M 175M| 0 0 | 75k 216k
21 5 66 4 0 4| 25M 119M| 34M 173M| 0 0 | 66k 207k
19 4 68 5 0 3| 23M 56M| 33M 166M| 0 0 | 60k 197k

#或者用systemtap腳本來看
$sudo stap -e ‘global cnt; probe scheduler.cpu_on {cnt<<<1;} probe timer.s(1){printf("%d\n", @count(cnt)); delete cnt;}'
217779
234141
234759
[/shell]

每秒高達200k左右的的上下文切換, 誰能告訴我發生了什麼? 好吧,latencytop來救助了!

它的官網:http://www.latencytop.org/

Skipping audio, slower servers, everyone knows the symptoms of latency. But to know what’s going on in the system, what’s causing the latency, how to fix it… that’s a hard question without good answers right now.

LatencyTOP is a Linux* tool for software developers (both kernel and userspace), aimed at identifying where in the system latency is happening, and what kind of operation/action is causing the latency to happen so that the code can be changed to avoid the worst latency hiccups.
它是Intel貢獻的另外一個性能查看器,還有一個是powertop,都是很不錯的工具.

Latencytop通過在內核上下文切換的時候,記錄被切換的進程的內核棧,然後通過匹配內核棧的函數來判斷是什麼原因導致上下文切換,同時他把幾十種容易引起切換的場景的函數都記錄起來,這樣在判斷系統問題的時候能容易定位到問題。

latencytop分成2個部分,內核部分和應用部分。內核部分負責調用棧的收集並且通過/proc來暴露, 應用部分負責顯示.

工作界面截圖如下:

latencytop在2.6.256後被內核吸收成為其中一部分,只要編譯的時候打開該選項就好,如何確認呢?
[shell]
$ cat /proc/latency_stats
Latency Top version : v0.1
[/shell]
看到這個就好了, 遺憾的是RHEL6竟然帶了latencytop應用部分,而沒有打開編譯選項,讓我們情何以堪呢?
在Ubuntu下可以這麼安裝:

[shell]
$ uname -r
2.6.38-yufeng
$ apt-get install latencytop
$ sudo latencytop #就可以使用了
[/shell]

但是latencytop比較傻的是默認是開圖像界面的,我們很不習慣,我們要文本界面, 自己動手把!

[shell]
$ apt-get source latencytop
$ diff -up Makefile.orig Makefile
— Makefile.orig 2011-03-29 20:10:29.025845447 +0800
+++ Makefile 2011-03-28 14:48:11.232318002 +0800
@@ -1,5 +1,5 @@
# FIXME: Use autoconf ?
-HAS_GTK_GUI = 0
+#HAS_GTK_GUI = 0

DESTDIR =
SBINDIR = /usr/sbin
[/shell]

Copyright © Linux教程網 All Rights Reserved