在golang中用来做监控分析的库包,一般用都是pprof库包… pprof可以在两个地方引入:
    Golang 使用pprof监控性能及GC调优先 - 图1
    其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来。 runtime/pprof可以用来产生dump文件,再使用go tool pprof来分析这运行日志.

    使用net/http/pprof可以做到直接看到当前web服务的状态,包括CPU占用情况和内存使用情况等。

    这次重点说些pprof web显示的模式,我自己主要是关注heap,profile两个数据源。

    关于golang运行环境heap的信息、内存mem等:http://localhost:7777/debug/pprof/heap
    关于profile、CPU计算百分比等:http://localhost:7777/debug/pprof/profile

    上面简单介绍了pprof的监控接口,但怎么查看这些数据? 有这么几个查看方式.
    交互模式, 可以通过用help查看pprof各种命令。 每次运行后拿到的数据是固定的,不会动态更新该数据。
    Golang 使用pprof监控性能及GC调优先 - 图2

    如果你不想使用交互模式,当然这每次都是新数据:
    Golang 使用pprof监控性能及GC调优先 - 图3

    网页查看模式: http://localhost:7777/debug/pprof/
    Golang 使用pprof监控性能及GC调优先 - 图4
    下面是我随便写的一段伪业务代码. 大家可以长时间运行这段代码,会发现内存在缓慢的增长中.

    1. #xiaorui.cc
    2. package main
    3. import (
    4. "flag"
    5. "fmt"
    6. "io/ioutil"
    7. "log"
    8. "net/http"
    9. _ "net/http/pprof"
    10. "sync"
    11. "time"
    12. )
    13. func counter() {
    14. list := []int{1}
    15. c := 1
    16. for i := 0; i < 10000000; i++ {
    17. httpGet()
    18. c = i + 1 + 2 + 3 + 4 - 5
    19. list = append(list, c)
    20. }
    21. fmt.Println(c)
    22. fmt.Println(list[0])
    23. }
    24. func work(wg *sync.WaitGroup) {
    25. for {
    26. counter()
    27. time.Sleep(1 * time.Second)
    28. }
    29. wg.Done()
    30. }
    31. func httpGet() int {
    32. queue := []string{"start..."}
    33. resp, err := http.Get("http://www.163.com")
    34. if err != nil {
    35. // handle error
    36. }
    37. //defer resp.Body.Close()
    38. body, err := ioutil.ReadAll(resp.Body)
    39. if err != nil {
    40. // handle error
    41. }
    42. queue = append(queue, string(body))
    43. return len(queue)
    44. }
    45. func main() {
    46. flag.Parse()
    47. //这里实现了远程获取pprof数据的接口
    48. go func() {
    49. log.Println(http.ListenAndServe("localhost:7777", nil))
    50. }()
    51. var wg sync.WaitGroup
    52. wg.Add(10)
    53. for i := 0; i < 100; i++ {
    54. go work(&wg)
    55. }
    56. wg.Wait()
    57. time.Sleep(3 * time.Second)
    58. }

    下面是pprof统计的heaq信息
    Golang 使用pprof监控性能及GC调优先 - 图5
    另外需要说的一点,pprof可以生成一个svg的矢量图,可以通过这svg图确认代码的流程及调用情况. svg是使用graphviz生成的,mac下直接brew install graphviz就能安装,centos下,yum -y install graphviz .

    下面是pprof help的使用文档.

    1. #xiaorui.cc
    2. $ go tool pprof http://localhost:6060/debug/pprof/heap
    3. Fetching profile from http://localhost:6060/debug/pprof/heap
    4. Saved profile in /Users/ruifengyun/pprof/pprof.localhost:6060.inuse_objects.inuse_space.032.pb.gz
    5. Entering interactive mode (type "help" for commands)
    6. (pprof) help
    7. Commands:
    8. cmd [n] [--cum] [focus_regex]* [-ignore_regex]*
    9. Produce a text report with the top n entries.
    10. Include samples matching focus_regex, and exclude ignore_regex.
    11. Add --cum to sort using cumulative data.
    12. Available commands:
    13. callgrind Outputs a graph in callgrind format
    14. disasm Output annotated assembly for functions matching regexp or address
    15. dot Outputs a graph in DOT format
    16. eog Visualize graph through eog
    17. evince Visualize graph through evince
    18. gif Outputs a graph image in GIF format
    19. gv Visualize graph through gv
    20. list Output annotated source for functions matching regexp
    21. pdf Outputs a graph in PDF format
    22. peek Output callers/callees of functions matching regexp
    23. png Outputs a graph image in PNG format
    24. proto Outputs the profile in compressed protobuf format
    25. ps Outputs a graph in PS format
    26. raw Outputs a text representation of the raw profile
    27. svg Outputs a graph in SVG format
    28. tags Outputs all tags in the profile
    29. text Outputs top entries in text form
    30. top Outputs top entries in text form
    31. tree Outputs a text rendering of call graph
    32. web Visualize graph through web browser
    33. weblist Output annotated source in HTML for functions matching regexp or address
    34. peek func_regex
    35. Display callers and callees of functions matching func_regex.
    36. dot [n] [focus_regex]* [-ignore_regex]* [>file]
    37. Produce an annotated callgraph with the top n entries.
    38. Include samples matching focus_regex, and exclude ignore_regex.
    39. For other outputs, replace dot with:
    40. - Graphic formats: dot, svg, pdf, ps, gif, png (use > to name output file)
    41. - Graph viewer: gv, web, evince, eog
    42. callgrind [n] [focus_regex]* [-ignore_regex]* [>file]
    43. Produce a file in callgrind-compatible format.
    44. Include samples matching focus_regex, and exclude ignore_regex.
    45. weblist func_regex [-ignore_regex]*
    46. Show annotated source with interspersed assembly in a web browser.
    47. list func_regex [-ignore_regex]*
    48. Print source for routines matching func_regex, and exclude ignore_regex.
    49. disasm func_regex [-ignore_regex]*
    50. Disassemble routines matching func_regex, and exclude ignore_regex.
    51. tags tag_regex [-ignore_regex]*
    52. List tags with key:value matching tag_regex and exclude ignore_regex.
    53. quit/exit/^D
    54. Exit pprof.
    55. option=value
    56. The following options can be set individually:
    57. cum/flat: Sort entries based on cumulative or flat data
    58. call_tree: Build context-sensitive call trees
    59. nodecount: Max number of entries to display
    60. nodefraction: Min frequency ratio of nodes to display
    61. edgefraction: Min frequency ratio of edges to display
    62. focus/ignore: Regexp to include/exclude samples by name/file
    63. tagfocus/tagignore: Regexp or value range to filter samples by tag
    64. eg "1mb", "1mb:2mb", ":64kb"
    65. functions: Level of aggregation for sample data
    66. files:
    67. lines:
    68. addresses:
    69. unit: Measurement unit to use on reports
    70. Sample value selection by index:
    71. sample_index: Index of sample value to display
    72. mean: Average sample value over first value
    73. Sample value selection by name:
    74. alloc_space for heap profiles
    75. alloc_objects
    76. inuse_space
    77. inuse_objects
    78. total_delay for contention profiles
    79. mean_delay
    80. contentions
    81. : Clear focus/ignore/hide/tagfocus/tagignore
    82. (pprof)

    根据pprof的统计信息我们可以找到CPU过度计算及内存泄露的大概的点。 现在越来越觉得Golang gc有些让人摸不清头脑. 看来有必要深入学习Golang gc垃圾回收原理.


    Golang 使用pprof监控性能及GC调优先 - 图6