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

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

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)} ()varWG Sync. Waitgroupwg.add (Ten) forI: =0; I <Ten; 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)varCounterint forI: =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

Qpzhang@qpzhangdemac-mini:~/gocode $go Tool pprof http://Localhost:6060/debug/pprof/profileRead http://Localhost:6060/debug/pprof/symbolGathering CPU Profile fromhttp//localhost:6060/debug/pprof/profile?seconds=30 for-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:9Samples6  66.7%66.7%6  66.7%runtime.mach_semaphore_wait1  11.1%77.8%1  11.1%runtime.kevent1  11.1%88.9%1  11.1%runtime.mach_semaphore_signal1  11.1%100.0%1  11.1%runtime.mach_semaphore_timedwait0   0.0%100.0%7  77.8%System0   0.0%100.0%1  11.1%Notetsleep0   0.0%100.0%1  11.1% Ready0   0.0%100.0%2  22.2%runtime.gosched00   0.0%100.0%1  11.1%Runtime.mach_semrelease0   0.0%100.0%1  11.1%RUNTIME.NOTETSLEEPG (pprof) Webtotal:9samplesloading 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-programshttps://github.com/hyper-carrot/go_command_tutorial/blob/ MASTER/0.12.MD period: Web commands rely on an SVG build package, under Mac, you can download through http://www.graphviz.org/Download_macos.php.


Transferred from:http://www.cnblogs.com/zhangqingping/p/4345071.html

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.