Use pprof remote view httpserver run stack for Golang program, CPU time-consuming and other information

Source: Internet
Author: User

Pprof is a god horse thing?

Pprof-manual page for Pprof (part of Gperftools)

is part of the Gperftools tool.

What is Gperftools again?

These tools is for use by developers so, they can create more robust applications. Especially of use to those developing multi-threaded applications in C + + with templates. Includes Tcmalloc, Heap-checker, Heap-profiler and Cpu-profiler.

A profiling tool that can view stacks, CPU information, and so on.

How to use it in Golang? Let's take a look below.

1. In a httpserver, only one line of code is required

Package Mainimport ("Flag" "Log" "Net/http" _ "net/http/pprof" "Sync" "Time") Func main () {flag. Parse ()//here implements a remote fetch PPROF data Interface go func () {log. Println (http. Listenandserve ("localhost:6060", Nil)} ()var wg sync. Waitgroupwg.add (Ten) for I: = 0; I < 10; i++ {go work (&WG)}WG. Wait ()//wait to see the global Run queue Deplete.time.Sleep (3 * time. Second)}func work (WG *sync. Waitgroup) {time. Sleep (time. Second) var counter intfor I: = 0; i < 1e10; i++ {time. Sleep (Time.millisecond *) COUNTER++}WG. Done ()}

2. Compile and run the program

Go Build Trace_example.go

./trace_example

3. Access via browser

http://localhost:6060/debug/pprof/

Ability to view the overview of a program

4. You can also use the terminal command to view

Then use the Pprof tool-to-look at the heap profile:

Go Tool pprof Http://localhost:6060/debug/pprof/heap

Or to the look in a 30-second CPU profile:

Go Tool pprof Http://localhost:6060/debug/pprof/profile

Or to look at the Goroutine blocking profile:

Go Tool pprof Http://localhost:6060/debug/pprof/block


[Email protected]:~/gocode $go tool pprof Http://localhost:6060/debug/pprof/profileRead http://localhost:6060/ Debug/pprof/symbolgathering CPU profiles from http://localhost:6060/debug/pprof/profile?seconds=30 for the seconds to/ VAR/FOLDERS/2L/195ZCC1N0SN2WJFJWF9HL9D80000GN/T/MMBNPDKV0DBE patient ...  Wrote profile To/var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/t/mmbnpdkv0dwelcome to pprof! For help, type ' help '.        (pprof) Top10total:9 samples 6 66.7% 66.7% 6 66.7% runtime.mach_semaphore_wait 1 11.1% 77.8%        1 11.1% runtime.kevent 1 11.1% 88.9% 1 11.1% runtime.mach_semaphore_signal 1 11.1% 100% 1 11.1% runtime.mach_semaphore_timedwait 0 0.0% 100.0% 7 77.8% System 0 0% 100% 1 1    1.1% notetsleep 0 0.0% 100.0% 1 11.1% ready 0 0.0% 100.0% 2 22.2% runtime.gosched0 0 0.0% 100.0% 1 11.1% runtime.mach_semrelease 0 0% 100% 1 11.1% RUNTIME.NOTETSLEEPG (pprof) webtotal:9 samplesloading Web page file:////var/folders/2l/ 195zcc1n0sn2wjfjwf9hl9d80000gn/t/slfuzhzbdb.0.svg
Web commands generate cool SVG images that are opened in a browser.







In addition to remote pull, you can write your own code to write this data to a file (implementing several routes in the net/http/pprof, turning tracing information on or off).
For specific reference: Http://blog.golang.org/profiling-go-programs
Https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md

Period: Web commands rely on an SVG build package, which you can download via http://www.graphviz.org/Download_macos.php under Mac.

-------------------------------------

2016.11.14 Updated

Agent based on Uninx domain socket, can not take the HTTP

Https://github.com/google/gops

Use pprof remote view httpserver run stack for Golang program, CPU time-consuming and other information

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.