What is a Pprof

Pprof is the performance tool of GO, which can record the program running information, CPU and memory usage during the program running. And goroutine runs with a lot of holes. These records are important when tuning performance or locating bugs.

The basic use

There are several ways to use pprof, I’m using the net/ HTTP /pprof that everyone else uses

Here’s my setup for BeeGo

beego.Router("/debug/pprof", &controllers.PprofController{})
beego.Router("/debug/pprof/:app([\\w]+)", &controllers.PprofController{})
Copy the code
package controllers

import (
	"net/http/pprof"

	"github.com/astaxie/beego"
)

type PprofController struct {
	beego.Controller
}

func (this *PprofController) Get(a) {
	switch this.Ctx.Input.Param(":app") {
	default:
		pprof.Index(this.Ctx.ResponseWriter, this.Ctx.Request)
	case "":
		pprof.Index(this.Ctx.ResponseWriter, this.Ctx.Request)
	case "cmdline":
		pprof.Cmdline(this.Ctx.ResponseWriter, this.Ctx.Request)
	case "profile":
		pprof.Profile(this.Ctx.ResponseWriter, this.Ctx.Request)
	case "symbol":
		pprof.Symbol(this.Ctx.ResponseWriter, this.Ctx.Request)
	}
	this.Ctx.ResponseWriter.WriteHeader(200)}Copy the code

Begin to use

Basically, I’m just using this to look at memory usage, so I’m going to write what I did in practice

Let’s start with the following code

func (c *MainController) Get(a) {

	timer := time.NewTimer(time.Second * (30 + 1))
	clearDone := make(chan bool)
	go func(a) {
		fmt.Println("Turn on the timer")
		<-timer.C
		fmt.Println("Timed end")
		clearDone <- true
		fmt.Println("Timer is off. Output to pipe.")
	}()
	c.serverData("The timer is on.") // This code is wrapped as follows
	// self.ServeJSON()
	// self.StopRun()

	<-clearDone

}
Copy the code

If you’re familiar with BeeGo, you’ll know that the StopRun() method will interrupt execution and the code below will not execute.

Let’s just say that the code above is the code in question

Using pprof to troubleshoot the next Start the server enter localhost: 8080 / debug/pprof can see a lot of options The following is the most here are English explanation But here I describe in Chinese

  • Block: block information for the Goroutine, but I’m going to say 0 here, and we’ll talk about how this block looks later.
  • Allocs: A sample of all past memory allocations
  • Heap: Information about heap memory
  • Mutex: Lock information
  • Threadcreate: Information about a thread

I mainly print the call stack information through both goroutine and heap, and then I can see how many coroutines are in use in Goroutine. Heap is the allocation of memory.

I can see the following information when I open the following URL

localhost:8080/debug/pprof/goroutine? debug=1

The first line tells me how many Goroutines I’ve opened so far, and I waited a long time and I still have so many that I can basically tell it’s blocked. I didn’t write a loop, did I

Open the

localhost:8080/debug/pprof/goroutine? debug=2

You can see a bunch of information. It’s all blocked by this one method. See the description in the figure belowchan send, 10 minutesAs I understand it, this chan has blocked for 10 minutes….. And it’s line 20 of the code go back to line 20

clearDone <- true

The definition clearDone has been reclaimed because the method has already been executed. In goroutine, however, data is still being sent to chan.

Command-line mode

This is one of my favorite ways to view it using the command line

go tool pprof localhost:8080/debug/pprof/goroutine

You can see the following information

You’re in command line interaction mode and so on and so forth and you just want to get other urls that you can change

Help you can view the instructions

Top Displays the memory allocation of the top 10 entries

Tree Is displayed as a tree

PNG is output in image format

SVG generates AN SVG file that a browser can recognize

Note if you don’t have Graphviz install it!!

Let’s do another demo

func (c *MainController) Get(a) {

	tick := time.Tick(time.Second / 100)
	var buf []byte
	for range tick {
		buf = append(buf, make([]byte.1024*1024)... }}Copy the code

Starting this example will always eat memory

go tool pprof http://localhost:8080/debug/pprof/heap

Run the command above to obtain stack memory

Use top to see which programs consume the most memory

It is the Get method that consumes the most

Take a closer look at the code and use the List MainController to see the following information

You can also use the Weblist MainController

As you can see, the amount of memory consumed while initializing the array.

You can also use SVG PNG to generate images as described above