Golang Pprof Records

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Runtime/pprof

We are going to add a method call to the PPROF package so that the program can log the heap memory allocation state of the program at run time to the file (or it can be written elsewhere, such as a network, etc.) for further analysis.

If your go program is just an application, such as calculating the Fabonacci sequence, then you cannot use the NET/HTTP/PPROF package and you need to use Runtime/pprof. The specific approach is to use the pprof. Startcpuprofile and Pprof.stopcpuprofile. (The pprof of the Go language is the porting of Google C + + Profiler.)

For example, the following:

Import (
     "Flag"
     "FMT"
     "OS"
     "Runtime/pprof"
)
var cpuprofile = flag. String ("Cpuprofile", "", "Write CPU profile to file")
Func Main () {
     Flag. Parse ()
     If *cpuprofile! = "" {
         F, err: = OS. Create (*cpuprofile)
         If err! = Nil {
             Fmt. PRINTLN (ERR)
         }
         Pprof. Startcpuprofile (f)
         Defer pprof. Stopcpuprofile ()
     }
     // ...
}

Add a--cpuprofile parameter when running the program, such as Fabonacci--cpuprofile=fabonacci.prof

The CPU information will be recorded in the xxx.prof when the program is running.

The above content is from:

http://blog.csdn.net/yhcharles/article/details/16820485

Http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html

Http://blog.golang.org/profiling-go-programs

Go Tool pprof

To prepare the run environment for Go tool pprof, it is necessary to use Perl to run this command directly.

Under Windows you can install ActivePerl (http://www.activestate.com/activeperl), which I downloaded here: ActivePerl 5.16.3 for Windows (64-bit, x64).

If Perl is not installed, the following error will be reported:

D:\gocodes\src\logdemo>go Tool Pprof
Go Tool:perl not found

D:\gocodes\src\logdemo>go tool Pprof LogDemo.exe a.prof
Go Tool:perl not found

Once installed, you can see some statistics by entering commands as follows:

The example here is to use the go tool pprof your-executable-name profile-filename pprof Command mode to analyze the data

   1:d:\gocodes\src\logdemo>go tool Pprof LogDemo.exe a.prof
   2:welcome to pprof!  For help, type ' help '.
   3: (pprof) Top
   4:total:236 samples
   5:   8.1%   8.1%  13.6% RUNTIME.MALLOCGC
   6:   5.1%  13.1%-  11.0% FMT. *PP). Printarg
   7:   5.1%  18.2%      236 100.0% Text
   8:        9   3.8%  22.0%        9   3.8% Runtime.memmove
   9:        8   3.4%  25.4%  23.3% os. ( *file). Writeconsole
  Ten:        8   3.4%  28.8%        8   3.4% runtime.indexbytebody
  One:        7   3.0%  31.8%      144  61.0% FMT. Fprintln
  :        7   3.0%  34.7%   4.7% runtime.makeslice
          6   2.5%  37.3%  10.6% Bufio. ( *reader). Readslice
  :        5   2.1%  39.4%        6   2.5% fmt. ( *FMT). padstring
  : (PPROF)

The top command can see the most time-consuming function. The meaning of each field in its output format is:

    1. Number of times the sample point falls in the function
    2. Percentage of the sample point falling in the function
    3. Cumulative percentage of previous item
    4. The total number of times the sample point falls in the function and the function it calls
    5. The percentage of total number of times the sample point falls in the function and the function it calls
    6. Name of function

http://blog.csdn.net/yhcharles/article/details/16820485

Of course we can also look at the memory consumption rankings for each function/method in the following way.

D:\gocodes\src\logdemo>go tool pprof LogDemo.exe--text a.prof
total:236 samples
8.1% 8.1% 13.6% RUNTIME.MALLOCGC
5.1% 13.1%-11.0% FMT. (*PP). Printarg
5.1% 18.2% 236 100.0% Text
9 3.8% 22.0% 9 3.8% runtime.memmove
8 3.4% 25.4% 23.3% os. (*file). Writeconsole
8 3.4% 28.8% 8 3.4% runtime.indexbytebody
7 3.0% 31.8% 144 61.0% FMT. Fprintln
7 3.0% 34.7% 4.7% Runtime.makeslice
6 2.5% 37.3% 10.6% Bufio. (*reader). Readslice
5 2.1% 39.4% 6 2.5% fmt. (*FMT). padstring
5 2.1% 41.5% 6 2.5% Runtime.entersyscall
5 2.1% 43.6% 5 2.1% runtime.memclr
5 2.1% 45.8% 6 2.5% Unicode/utf8. Decoderune
4 1.7% 47.5% 9 3.8% Asserte2tret
4 1.7% 49.2% 7 3.0% copyout
4 1.7% 50.8% 12.7% FMT. (*PP). Doprint
4 1.7% 52.5% 12.7% gostringsize
4 1.7% 54.2% bayi 34.3% os. (*file). Write
4 1.7% 55.9% 6.8% Runtime.cgocall
4 1.7% 57.6% 9 3.8% Runtime.deferreturn
4 1.7% 59.3% 16.9% runtime.slicebytetostring
4 1.7% 61.0% 4 1.7% sync/atomic. AddUint32
4 1.7% 62.7% 8.5% syscall. Syscall6
4 1.7% 64.4% 6.4% unicode/utf16. Encode
3 1.3% 65.7% 11.9% Bufio. (*reader). ReadLine
3 1.3% 66.9% 9 3.8% copyin
3 1.3% 68.2% 5 2.1% Endcgo
3 1.3% 69.5% 6 2.5% fmt. (*cache). Put
3 1.3% 70.8% 5 2.1% fmt. (*FMT). Init
3 1.3% 72.0% 4.7% FMT. (*PP). fmtstring
3 1.3% 73.3% 9 3.8% fmt. (*PP). Free
3 1.3% 74.6% 6 2.5% Runtime.deferproc
3 1.3% 75.8% 6 2.5% runtime.mal
3 1.3% 77.1% 3 1.3% runtime.markallocated
3 1.3% 78.4% 6 2.5% sync. (*mutex). Lock
3 1.3% 79.7% 7 3.0% sync. (*mutex). Unlock
3 1.3% 80.9% 3 1.3% sync/atomic.compareandswapuint32
3 1.3% 82.2% 4 1.7% syscall. (*lazyproc). Addr
2 0.8% 83.1% 3 1.3% cnew
2 0.8% 83.9% 2 0.8% fmt. (*FMT). clearflags
2 0.8% 84.7% 146 61.9% FMT. Println
2 0.8% 85.6% 3 1.3% freedefer
2 0.8% 86.4% 2 0.8% popdefer
2 0.8% 87.3% 2 0.8% runtime. Mspanlist_isempty
2 0.8% 88.1% 2 0.8% runtime.memcopy64
2 0.8% 89.0% 8.9% syscall. Writeconsole
2 0.8% 89.8% 2 0.8% unicode/utf8.decoderuneinternal
1 0.4% 90.3% 4.2% Bufio. (*reader). Fill
1 0.4% 90.7% 9 3.8% bytes. Indexbyte
1 0.4% 91.1% 2 0.8% fmt. (*cache). Get
1 0.4% 91.5% 8 3.4% fmt. (*FMT). fmt_s
1 0.4% 91.9% 1 0.4% fmt. (*FMT). truncate
1 0.4% 92.4% 1 0.4% newdefer
1 0.4% 92.8% 32.6% os. (*file). Write
1 0.4% 93.2% 4.2% Runtime. Mcentral_alloclist
1 0.4% 93.6% 1 0.4% runtime. Mspanlist_insert
1 0.4% 94.1% 4.2% runtime.asserte2t
1 0.4% 94.5% 1 0.4% runtime.casp
1 0.4% 94.9% 4 1.7% Runtime.cnewarray
1 0.4% 95.3% 4.2% runtime.convt2e
1 0.4% 95.8% 1 0.4% runtime.efacethash
1 0.4% 96.2% 1 0.4% runtime.exitsyscall
1 0.4% 96.6% 3 1.3% runtime.new
1 0.4% 97.0% 1 0.4% runtime.stdcall
1 0.4% 97.5% 1 0.4% runtime.strcopy
1 0.4% 97.9% 2 0.8% runtime.unlockosthread
1 0.4% 98.3% 1 0.4% Save
1 0.4% 98.7% 1 0.4% syscall. (*lazyproc). Find
1 0.4% 99.2% 6 2.5% syscall. Read
1 0.4% 99.6% 2 0.8% Unicode/utf8. Fullrune
1 0.4% 100.0% 1 0.4% unlockosthread
0 0.0% 100.0% 7 3.0% Mcentral_grow
0 0.0% 100.0% 3 1.3% mheap_alloclocked
0 0.0% 100.0% 1 0.4% mheap_freelocked
0 0.0% 100.0% 7.2% fmt.newprinter
0 0.0% 100.0% 4 1.7% Makeslice1
0 0.0% 100.0% 9 3.8% os. (*file). Read
0 0.0% 100.0% 9 3.8% os. (*file). Read
0 0.0% 100.0% 4.2% runtime. Mcache_refill
0 0.0% 100.0% 7 3.0% Runtime. Mheap_alloc
0 0.0% 100.0% 2 0.8% runtime.asserte2t2
0 0.0% 100.0% 236 100.0% runtime.gosched0
0 0.0% 100.0% 1 0.4% runtime.lock
0 0.0% 100.0% 236 100.0% Runtime.main
0 0.0% 100.0% 1 0.4% runtime.nanotime
0 0.0% 100.0% 1 0.4% syscall. (*lazyproc). Mustfind
0 0.0% 100.0% 5 2.1% syscall. ReadFile

D:\gocodes\src\logdemo>

Use the chart to show

If you want to see not only the memory consumption ranking of each method/function, but also the call relationship between them, you need to install Graphviz or Ghostview, which is relatively graphviz users, here is an example.

http://blog.raphaelzhang.com/2014/01/memory-leak-detection-in-go/

Download Address: http://www.graphviz.org/Download..php i downloaded graphviz-2.36.msi.

Once installed, do not forget to set environment variables. For example, I installed the following directory: C:\Program Files (x86) \graphviz2.36\bin need to set it to the PATH environment variable.

How to generate a chart presentation:

    1. Use the go tool pprof your-executable-name --dot profile-filename > heap.gv command to generate a GV file that can be seen in Graphviz, and view the memory consumption of each method/function while viewing the call relationship between them
    2. After generating a GV file by dot -Tpng heap.gv > heap.png generating a PNG graphic file that calls network and memory consumption graphs

Note that you enter the Web directly on the pprof command line, and if it is not a Web program, the following error is reported when it is generated:

D:\gocodes\src\logdemo>go tool Pprof LogDemo.exe a.prof
Welcome to pprof! For help, type ' help '.
(pprof) Web
total:236 samples
Dropping nodes with <= 1 samples; Edges with <= 0 abs (samples)
Loading Web page file:///c:\users\guohon~1\appdata\local\temp\m9inayn4pq.0.svg
' Uname ' is not an internal or external command, nor is it a running program
or batch file.
The system could not find the path specified.
The system could not find the path specified.
' Google-chrome ' is not an internal or external command, nor is it a running program
or batch file.
' Firefox ' is not an internal or external command, nor is it a running program
or batch file.
Could not load Web browser.
(PPROF)

Original link Address http://blog.csdn.net/susubuhui/article/details/39117763

It is important to note that the return value is ignored, especially when the return value is a pointer type, and if the return value allocates memory, the loop is called, causing the memory to be repeatedly allocated but not freed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.