天天看點

golang cpuprofile分析http://www.philo.top/2015/05/29/golangProfilingAndGC/ Philogolang調優之clock ticks

http://www.philo.top/2015/05/29/golangProfilingAndGC/

Philo

關注Golang與Docker技術

Home Archives About Donate GeekP weibo github

5月 29 2015 技術

golang調優之clock ticks

本blog的來源

昨天在找工作面試的時候我與面試官聊到了golang的問題。當然讨論的熱點就是調優與GC。

結果面試變成了技術讨論與研究,聊了接近一個小時,真的很開心。

下面的研究内容來自goblog https://blog.golang.org/profiling-go-programs

我也隻是想濃縮一遍上面的内容友善大家研習。當然文章可能比較老了。

是以我在這裡重新走一遍大神之路:

問題來源:

來自論文:http://research.google.com/pubs/pub37122.html

提出的挑戰,在這篇文章中golang的性能是最低的。是以在本blog中就針對這篇文章中的算法進行調優。

先說明我的各種版本号:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      
[#11#[email protected] ~/cpp_pro]$g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
[#12#[email protected] ~/cpp_pro]$go version
go version go1.4.2 darwin/amd64

C++ 成績:
real	0m16.791s
user	0m16.093s
sys	0m0.687s

golang 成績:
real	0m26.582s
user	0m26.393s
sys	0m0.161s
      

pprof運作原理and解釋and調優

1
2
3
4
5
6
7
8
9
10
11
12
      
(pprof) top10
Total: 2525 samples
     298  11.8%  11.8%      345  13.7% runtime.mapaccess1_fast64
     268  10.6%  22.4%     2124  84.1% main.FindLoops
     251   9.9%  32.4%      451  17.9% scanblock
     178   7.0%  39.4%      351  13.9% hash_insert
     131   5.2%  44.6%      158   6.3% sweepspan
     119   4.7%  49.3%      350  13.9% main.DFS
      96   3.8%  53.1%       98   3.9% flushptrbuf
      95   3.8%  56.9%       95   3.8% runtime.aeshash64
      95   3.8%  60.6%      101   4.0% runtime.settype_flush
      88   3.5%  64.1%      988  39.1% runtime.mallocgc
      

pprof子產品通過每秒大概100次的對runtime 中的

stack

進行取樣來進行統計的。下面來解釋一下報表為啥是上面這個樣子。

首先 Total 2525 程式大概運作了25s+

———-這一部分是針對單個函數的統計

col1: 在取樣中作為棧頂的次數

col2: 作為堆頂的百分比,以第一行為例統計關系:298/2525 約等于 11.8% 就好了解了

col3: 排名結果的累加,都是這個位置的數的上面加左面擷取的結果,有了這個就可以大概看出來幾個熱點占用的總比例,非常友善

———-這一部分是對整個堆棧的統計。與上面的差別是不考慮是否在堆棧頂部。

col4: 在sample堆棧中出現的次數,不管是waiting還是return隻要出現就計入統計。

col5: 出現次數百分比,與左邊報表左邊類似。

col6: 略

這種統計方法不但不會影響太多程式性能,而且可以很好的把握程式熱點在何位置。

在Intel Vtune中它會幫你完全統計出函數所用的時間。雖然非常爽但是其實沒有什麼大作用。

有個大概百分比就基本夠用了。

不失為一種定性與定量的中間選擇。其實我在做log系統的時候也可以仿照他的來做。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      
(pprof) list DFS
Total: 2525 samples
ROUTINE ====================== main.DFS in /home/rsc/g/benchgraffiti/havlak/havlak1.go
   119    697 Total samples (flat / cumulative)
     3      3  240: func DFS(currentNode *BasicBlock, nodes []*UnionFindNode, number map[*BasicBlock]int, last []int, current int) int {
     1      1  241:     nodes[current].Init(currentNode, current)
     1     37  242:     number[currentNode] = current
     .      .  243:
     1      1  244:     lastid := current
    89     89  245:     for _, target := range currentNode.OutEdges {
     9    152  246:             if number[target] == unvisited {
     7    354  247:                     lastid = DFS(target, nodes, number, last, lastid+1)
     .      .  248:             }
     .      .  249:     }
     7     59  250:     last[number[currentNode]] = lastid
     1      1  251:     return lastid
(pprof)
      

雖然不需要解釋,但是很容易看出來那句話執行時間是最長的(L:247)

主要熱點問題在于使用了map進行搜尋。

在這個blog中提出了使用[]int的方式給map增加類似索引的東西。效果不錯。((^__^) 嘻嘻……其實我在自己做cache搜尋的時候也這麼做。)

Tip:作者的compiler是6g很老版本的。這裡補充一下go1.4.2的成績:

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
29
30
31
32
      
調優前
$time ./havlak1
# of loops: 76000 (including 1 artificial root node)

real	0m21.686s
user	0m21.578s
sys	0m0.111s

按照上面方法調優後
time ./havlak2
# of loops: 76000 (including 1 artificial root node)

real	0m12.588s
user	0m12.486s
sys	0m0.103s

對比我們上一次測試的源代碼修改了之後的成績,可以看到與C++的成績非常接近了。(GO:26s,CPP:16s)
time ./go_pro
Welcome to LoopTesterApp, Go edition
Constructing Simple CFG...
15000 dummy loops
Constructing CFG...
Performing Loop Recognition
1 Iteration
Another 50 iterations...
..................................................
# of loops: 76000 (including 1 artificial root node)

real	0m18.447s
user	0m18.297s
sys	0m0.134s
      

調優過程源碼:

hg clone https://code.google.com/p/benchgraffiti

總結

在本篇中,我們可以看到簡單的使用pprof子產品就可以針對程式的熱點進行大幅度的性能改進。

當然我依然堅持認為,在項目prototype開發以及alpha版本中不适合任何角度的調優。

隻考慮架構性能已經是最多了(或者說是技術方向性)

但是需要注意的是,pprof本身不會幫你調優,還是要看對golang的熟悉程度。

在這裡雖然用了Index來進行調優,但是我們在實戰的過程當中可能會更加複雜。

也許路還很遠,下一篇GC記憶體調優。Continue。

分享到 Comments

  • golang
  • profiling

Newer golang調優之GC Older 在OSX下使用docker建構hexo環境 喜歡 最新 最早 最熱

  • 0條評論
  • 還沒有評論,沙發等你來搶

帳号管理 caoshulin1989

golang cpuprofile分析http://www.philo.top/2015/05/29/golangProfilingAndGC/ Philogolang調優之clock ticks

分享到:

philo.top正在使用多說

分類

  • 技術61
  • 雜談5

标簽

  • BusyBox1
  • CoreOS1
  • Cow1
  • DES1
  • DaoCloud1
  • Docker2
  • GC1
  • Golang1
  • HomeBrew1
  • Linux13
  • MongoDB1
  • RHEL1
  • RunC1
  • ShadowSocks1
  • Ubuntu1
  • article1
  • atom2
  • blog1
  • bootstrap1
  • busybox1
  • butterfly1
  • career1
  • coreos1
  • css1
  • datatables1
  • delegate1
  • dev1
  • devenv1
  • docker5
  • env1
  • git6
  • godoc1
  • golang18
  • grub1
  • hexo3
  • homebrew1
  • html1
  • html51
  • iterm21
  • java2
  • js8
  • linux3
  • liteIDE1
  • mac2
  • macos1
  • minecraft2
  • mongodb4
  • mysql2
  • node1
  • oop1
  • osx4
  • php1
  • phpExtension1
  • profiling2
  • putty2
  • python4
  • require.js1
  • requirejs1
  • rkt1
  • runc1
  • shell5
  • ss1
  • sublime1
  • test1
  • tray1
  • ubuntu3
  • vim1
  • virtualbox2
  • visualization1
  • web3
  • wiki2
  • yaml1
  • 雜談1

标簽雲

BusyBox CoreOS Cow DES DaoCloud Docker GC Golang HomeBrew Linux MongoDB RHEL RunC ShadowSocks Ubuntu article atom blog bootstrap busybox butterfly career coreos css datatables delegate dev devenv docker env git godoc golang grub hexo homebrew html html5 iterm2 java js linux liteIDE mac macos minecraft mongodb mysql node oop osx php phpExtension profiling putty python require.js requirejs rkt runc shell ss sublime test tray ubuntu vim virtualbox visualization web wiki yaml 雜談

歸檔

  • 八月 20152
  • 七月 20158
  • 六月 20153
  • 五月 20156
  • 四月 20151
  • 三月 20155
  • 二月 20158
  • 一月 20151
  • 十一月 189934

近期文章

  • golang 項目測試方案
  • 大學這些年陪我走過學習開發的硬體們
  • 我的vim golang 開發環境
  • 程式員的投資與投機
  • 秒殺SSD,ubuntu極速開發環境搭建

友情連結

  • Linux中國
  • 主題作者
  • 熱前端
  • 青春華航
  • locez
  • SVNT 七枚核桃

© 2015 jianyingLi

Powered by Hexo . Theme by Landscape-plus . 圖檔托管: 七牛雲存儲 . Home Archives About Donate GeekP

golang cpuprofile分析http://www.philo.top/2015/05/29/golangProfilingAndGC/ Philogolang調優之clock ticks