if err := http.ListenAndServe(":6060", nil); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
for {
for _, v := range animal.AllAnimals {
v.Live()
}
time.Sleep(time.Second)
}
}
_ "net/http/pprof"
runtime.GOMAXPROCS(1)
runtime.SetMutexProfileFraction(1)
runtime.SetBlockProfileRate(1)
命令如下:
Microsoft Windows [版本 10.0.22000.978] (c) Microsoft Corporation。保留所有权利。
C:\Users\90760>go tool pprof -h usage:
Produce output in the specified format.
pprof [options] [binary] ...
Omit the format to get an interactive shell whose commands can be used to generate various views of a profile
pprof [options] [binary] ...
Omit the format and provide the "-http" flag to get an interactive web interface at the specified host:port that can be used to navigate through various views of a profile.
pprof -http [host]:[port] [options] [binary] ...
Details: Output formats (select at most one): -callgrind Outputs a graph in callgrind format -comments Output all profile comments -disasm Output assembly listings annotated with samples -dot Outputs a graph in DOT format -eog Visualize graph through eog -evince Visualize graph through evince -gif Outputs a graph image in GIF format -gv Visualize graph through gv -kcachegrind Visualize report in KCachegrind -list Output annotated source for functions matching regexp -pdf Outputs a graph in PDF format -peek Output callers/callees of functions matching regexp -png Outputs a graph image in PNG format -proto Outputs the profile in compressed protobuf format -ps Outputs a graph in PS format -raw Outputs a text representation of the raw profile -svg Outputs a graph in SVG format -tags Outputs all tags in the profile -text Outputs top entries in text form -top Outputs top entries in text form -topproto Outputs top entries in compressed protobuf format -traces Outputs all profile samples in text form -tree Outputs a text rendering of call graph -web Visualize graph through web browser -weblist Display annotated source in a web browser
Options: -call_tree Create a context-sensitive call tree -compact_labels Show minimal headers -divide_by Ratio to divide all samples before visualization -drop_negative Ignore negative differences -edgefraction Hide edges below *total -focus Restricts to samples going through a node matching regexp -hide Skips nodes matching regexp -ignore Skips paths going through any nodes matching regexp -intel_syntax Show assembly in Intel syntax -mean Average sample value over first value (count) -nodecount Max number of nodes to show -nodefraction Hide nodes below *total -noinlines Ignore inlines. -normalize Scales profile based on the base profile. -output Output filename for file-based outputs -prune_from Drops any functions below the matched frame. -relative_percentages Show percentages relative to focused subgraph -sample_index Sample value to report (0-based index or name) -show Only show nodes matching regexp -show_from Drops functions above the highest matched frame. -source_path Search path for source files -tagfocus Restricts to samples with tags in range or matched by regexp -taghide Skip tags matching this regexp -tagignore Discard samples with tags in range or matched by regexp -tagshow Only consider tags matching this regexp -trim Honor nodefraction/edgefraction/nodecount defaults -trim_path Path to trim from source paths before search -unit Measurement units to display
Option groups (only set one per group): granularity -functions Aggregate at the function level. -filefunctions Aggregate at the function level. -files Aggregate at the file level. -lines Aggregate at the source code line level. -addresses Aggregate at the address level. sort -cum Sort entries based on cumulative weight -flat Sort entries based on own weight
Source options: -seconds Duration for time-based profile collection -timeout Timeout in seconds for profile collection -buildid Override build id for main binary -add_comment Free-form annotation to add to the profile Displayed on some reports or with pprof -comments -diff_base source Source of base profile for comparison -base source Source of base profile for profile subtraction profile.pb.gz Profile in compressed protobuf format legacy_profile Profile in legacy pprof format http://host/profile URL for profile handler to retrieve -symbolize= Controls source of symbol information none Do not attempt symbolization local Examine only local binaries fastlocal Only get function names from local binaries remote Do not examine local binaries force Force re-symbolization Binary Local path or build id of binary for symbolization -tls_cert TLS client certificate file for fetching profile and symbols -tls_key TLS private key file for fetching profile and symbols -tls_ca TLS CA certs file for fetching profile and symbols
Misc options: -http Provide web interface at host:port. Host is optional and 'localhost' by default. Port is optional and a randomly available port by default. -no_browser Skip opening a browser for the interactive web UI. -tools Search path for object tools
Legacy convenience options: -inuse_space Same as -sample_index=inuse_space -inuse_objects Same as -sample_index=inuse_objects -alloc_space Same as -sample_index=alloc_space -alloc_objects Same as -sample_index=alloc_objects -total_delay Same as -sample_index=delay -contentions Same as -sample_index=contentions -mean_delay Same as -mean -sample_index=delay
Environment Variables: PPROF_TMPDIR Location for saved profiles (default $HOME/pprof) PPROF_TOOLS Search path for object-level tools PPROF_BINARY_PATH Search path for local binary files default: $HOME/pprof/binaries searches $name, $path, $buildid/$name, $path/$buildid * On Windows, %USERPROFILE% is used instead of $HOME
C:\Users\90760>go tool pprof http://localhost:6060/debug/pprof/allocs Fetching profile over HTTP from http://localhost:6060/debug/pprof/allocs Saved profile in C:\Users\90760\pprof\pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.001.pb.gz Type: alloc_space Time: Oct 29, 2022 at 4:33pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 2750.04MB, 99.95% of 2751.54MB total Dropped 15 nodes (cum <= 13.76MB) flat flat% sum% cum cum% 2046.04MB 74.36% 74.36% 2046.04MB 74.36% github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Steal 704MB 25.59% 99.95% 704MB 25.59% github.com/wolfogre/go-pprof-practice/animal/canidae/dog.(*Dog).Run (inline) 0 0% 99.95% 704MB 25.59% github.com/wolfogre/go-pprof-practice/animal/canidae/dog.(*Dog).Live 0 0% 99.95% 2046.04MB 74.36% github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Live 0 0% 99.95% 2750.04MB 99.95% main.main 0 0% 99.95% 2750.04MB 99.95% runtime.main (pprof) top 5 Showing nodes accounting for 2750.04MB, 99.95% of 2751.54MB total Dropped 15 nodes (cum <= 13.76MB) Showing top 5 nodes out of 6 flat flat% sum% cum cum% 2046.04MB 74.36% 74.36% 2046.04MB 74.36% github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Steal 704MB 25.59% 99.95% 704MB 25.59% github.com/wolfogre/go-pprof-practice/animal/canidae/dog.(*Dog).Run (inline) 0 0% 99.95% 704MB 25.59% github.com/wolfogre/go-pprof-practice/animal/canidae/dog.(*Dog).Live 0 0% 99.95% 2046.04MB 74.36% github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Live 0 0% 99.95% 2750.04MB 99.95% main.main (pprof) list command list requires an argument (pprof) list Steal Total: 2.69GB ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Steal in D:\code\goproject\src\go-pprof-practice\animal\muridae\mouse\mouse.go 2GB 2GB (flat, cum) 74.36% of Total . . 45: . . 46:func (m *Mouse) Steal() { . . 47: log.Println(m.Name(), "steal") . . 48: max := constant.Gi . . 49: for len(m.buffer)*constant.Mi < max { 2GB 2GB 50: m.buffer = append(m.buffer, [constant.Mi]byte{}) . . 51: } . . 52:} (pprof) list dog Total: 2.69GB ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/canidae/dog.(*Dog).Live in D:\code\goproject\src\go-pprof-practice\animal\canidae\dog\dog.go 0 704MB (flat, cum) 25.59% of Total . . 16:func (d *Dog) Live() { . . 17: d.Eat() . . 18: d.Drink() . . 19: d.Shit() . . 20: d.Pee() . 704MB 21: d.Run() . . 22: d.Howl() . . 23:} . . 24: . . 25:func (d *Dog) Eat() { . . 26: log.Println(d.Name(), "eat") ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/canidae/dog.(*Dog).Run in D:\code\goproject\src\go-pprof-practice\animal\canidae\dog\dog.go 704MB 704MB (flat, cum) 25.59% of Total . . 38: log.Println(d.Name(), "pee") . . 39:} . . 40: . . 41:func (d *Dog) Run() { . . 42: log.Println(d.Name(), "run") 704MB 704MB 43: _ = make([]byte, 16*constant.Mi) . . 44:} . . 45: . . 46:func (d *Dog) Howl() { . . 47: log.Println(d.Name(), "howl") . . 48:} (pprof) c
Microsoft Windows [版本 10.0.22000.978] (c) Microsoft Corporation。保留所有权利。
C:\Users\90760>go tool pprof http://localhost:6060/debug/pprof/block Fetching profile over HTTP from http://localhost:6060/debug/pprof/block Saved profile in C:\Users\90760\pprof\pprof.contentions.delay.001.pb.gz Type: delay Time: Oct 29, 2022 at 4:55pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 643.37s, 100% of 643.39s total Dropped 4 nodes (cum <= 3.22s) Showing top 10 nodes out of 15 flat flat% sum% cum cum% 299.42s 46.54% 46.54% 299.42s 46.54% runtime.chanrecv1 298.85s 46.45% 92.99% 298.85s 46.45% sync.(*Mutex).Lock (inline) 45.10s 7.01% 100% 45.10s 7.01% runtime.selectgo 0 0% 100% 298.85s 46.45% github.com/wolfogre/go-pprof-practice/animal/canidae/wolf.(*Wolf).Howl 0 0% 100% 298.85s 46.45% github.com/wolfogre/go-pprof-practice/animal/canidae/wolf.(*Wolf).Live 0 0% 100% 299.05s 46.48% github.com/wolfogre/go-pprof-practice/animal/felidae/cat.(*Cat).Live 0 0% 100% 299.05s 46.48% github.com/wolfogre/go-pprof-practice/animal/felidae/cat.(*Cat).Pee 0 0% 100% 597.90s 92.93% main.main 0 0% 100% 45.47s 7.07% net/http.(*ServeMux).ServeHTTP 0 0% 100% 45.49s 7.07% net/http.(*conn).serve (pprof) top live Focus expression matched no samples Active filters: focus=live Showing nodes accounting for 0, 0% of 643.39s total flat flat% sum% cum cum% (pprof) list cat Total: 643.39s ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/felidae/cat.(*Cat).Live in D:\code\goproject\src\go-pprof-practice\animal\felidae\cat\cat.go 0 299.05s (flat, cum) 46.48% of Total . . 14: . . 15:func (c *Cat) Live() { . . 16: c.Eat() . . 17: c.Drink() . . 18: c.Shit() . 299.05s 19: c.Pee() . . 20: c.Climb() . . 21: c.Sneak() . . 22:} . . 23: . . 24:func (c *Cat) Eat() { ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/felidae/cat.(*Cat).Pee in D:\code\goproject\src\go-pprof-practice\animal\felidae\cat\cat.go 0 299.05s (flat, cum) 46.48% of Total . . 34:} . . 35: . . 36:func (c *Cat) Pee() { . . 37: log.Println(c.Name(), "pee") . . 38: . 299.05s 39: <-time.After(time.Second) . . 40:} . . 41: . . 42:func (c *Cat) Climb() { . . 43: log.Println(c.Name(), "climb") . . 44:} (pprof)
Microsoft Windows [版本 10.0.22000.978] (c) Microsoft Corporation。保留所有权利。
C:\Users\90760>go tool pprof http://localhost:6060/debug/pprof/goroutine Fetching profile over HTTP from http://localhost:6060/debug/pprof/goroutine Saved profile in C:\Users\90760\pprof\pprof.goroutine.001.pb.gz Type: goroutine Time: Oct 29, 2022 at 5:08pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 53, 98.15% of 54 total Showing top 10 nodes out of 32 flat flat% sum% cum cum% 51 94.44% 94.44% 51 94.44% runtime.gopark 1 1.85% 96.30% 1 1.85% runtime.asyncPreempt2 1 1.85% 98.15% 1 1.85% runtime/pprof.runtime_goroutineProfileWithLabels 0 0% 98.15% 50 92.59% github.com/wolfogre/go-pprof-practice/animal/canidae/wolf.(*Wolf).Drink.func1 0 0% 98.15% 1 1.85% github.com/wolfogre/go-pprof-practice/animal/felidae/tiger.(*Tiger).Eat 0 0% 98.15% 1 1.85% github.com/wolfogre/go-pprof-practice/animal/felidae/tiger.(*Tiger).Live 0 0% 98.15% 1 1.85% internal/poll.(*FD).Accept 0 0% 98.15% 1 1.85% internal/poll.(*FD).acceptOne 0 0% 98.15% 1 1.85% internal/poll.(*pollDesc).wait 0 0% 98.15% 1 1.85% internal/poll.execIO (pprof) list Drink Total: 54 ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/canidae/wolf.(*Wolf).Drink.func1 in D:\code\goproject\src\go-pprof-practice\animal\canidae\wolf\wolf.go 0 50 (flat, cum) 92.59% of Total . . 29: . . 30:func (w *Wolf) Drink() { . . 31: log.Println(w.Name(), "drink") . . 32: for i := 0; i < 10; i++ { . . 33: go func() { . 50 34: time.Sleep(30 * time.Second) . . 35: }() . . 36: } . . 37:} . . 38: . . 39:func (w *Wolf) Shit() { (pprof) exit
C:\Users\90760>go tool pprof http://localhost:6060/debug/pprof/heap Fetching profile over HTTP from http://localhost:6060/debug/pprof/heap Saved profile in C:\Users\90760\pprof\pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.002.pb.gz Type: inuse_space Time: Oct 29, 2022 at 5:11pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 1GB, 99.90% of 1GB total Dropped 13 nodes (cum <= 0.01GB) flat flat% sum% cum cum% 1GB 99.90% 99.90% 1GB 99.90% github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Steal 0 0% 99.90% 1GB 99.90% github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Live 0 0% 99.90% 1GB 99.90% main.main 0 0% 99.90% 1GB 99.90% runtime.main (pprof) list Steal Total: 1GB ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Steal in D:\code\goproject\src\go-pprof-practice\animal\muridae\mouse\mouse.go 1GB 1GB (flat, cum) 99.90% of Total . . 45: . . 46:func (m *Mouse) Steal() { . . 47: log.Println(m.Name(), "steal") . . 48: max := constant.Gi . . 49: for len(m.buffer)*constant.Mi < max { 1GB 1GB 50: m.buffer = append(m.buffer, [constant.Mi]byte{}) . . 51: } . . 52:} (pprof) list Live Total: 1GB ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Live in D:\code\goproject\src\go-pprof-practice\animal\muridae\mouse\mouse.go 0 1GB (flat, cum) 99.90% of Total . . 18: m.Eat() . . 19: m.Drink() . . 20: m.Shit() . . 21: m.Pee() . . 22: m.Hole() . 1GB 23: m.Steal() . . 24:} . . 25: . . 26:func (m *Mouse) Eat() { . . 27: log.Println(m.Name(), "eat") . . 28:} (pprof) list Steal Total: 1GB ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/muridae/mouse.(*Mouse).Steal in D:\code\goproject\src\go-pprof-practice\animal\muridae\mouse\mouse.go 1GB 1GB (flat, cum) 99.90% of Total . . 45: . . 46:func (m *Mouse) Steal() { . . 47: log.Println(m.Name(), "steal") . . 48: max := constant.Gi . . 49: for len(m.buffer)*constant.Mi < max { 1GB 1GB 50: m.buffer = append(m.buffer, [constant.Mi]byte{}) . . 51: } . . 52:} (pprof) exit
C:\Users\90760>go tool pprof http://localhost:6060/debug/pprof/profile?seconds=60 Fetching profile over HTTP from http://localhost:6060/debug/pprof/profile?seconds=60 Saved profile in C:\Users\90760\pprof\pprof.samples.cpu.001.pb.gz Type: cpu Time: Oct 29, 2022 at 5:14pm (CST) Duration: 60.11s, Total samples = 18.24s (30.34%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 18.05s, 98.96% of 18.24s total Dropped 64 nodes (cum <= 0.09s) flat flat% sum% cum cum% 18.05s 98.96% 98.96% 18.08s 99.12% github.com/wolfogre/go-pprof-practice/animal/felidae/tiger.(*Tiger).Eat 0 0% 98.96% 18.08s 99.12% github.com/wolfogre/go-pprof-practice/animal/felidae/tiger.(*Tiger).Live 0 0% 98.96% 18.18s 99.67% main.main 0 0% 98.96% 18.18s 99.67% runtime.main (pprof) list Eat Total: 18.24s ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/canidae/dog.(*Dog).Eat in D:\code\goproject\src\go-pprof-practice\animal\canidae\dog\dog.go 0 30ms (flat, cum) 0.16% of Total . . 21: d.Run() . . 22: d.Howl() . . 23:} . . 24: . . 25:func (d *Dog) Eat() { . 30ms 26: log.Println(d.Name(), "eat") . . 27:} . . 28: . . 29:func (d *Dog) Drink() { . . 30: log.Println(d.Name(), "drink") . . 31:} ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/felidae/cat.(*Cat).Eat in D:\code\goproject\src\go-pprof-practice\animal\felidae\cat\cat.go 0 10ms (flat, cum) 0.055% of Total . . 20: c.Climb() . . 21: c.Sneak() . . 22:} . . 23: . . 24:func (c *Cat) Eat() { . 10ms 25: log.Println(c.Name(), "eat") . . 26:} . . 27: . . 28:func (c *Cat) Drink() { . . 29: log.Println(c.Name(), "drink") . . 30:} ROUTINE ======================== github.com/wolfogre/go-pprof-practice/animal/felidae/tiger.(*Tiger).Eat in D:\code\goproject\src\go-pprof-practice\animal\felidae\tiger\tiger.go 18.05s 18.08s (flat, cum) 99.12% of Total . . 19:} . . 20: . . 21:func (t *Tiger) Eat() { . . 22: log.Println(t.Name(), "eat") . . 23: loop := 10000000000 18.05s 18.08s 24: for i := 0; i < loop; i++ { . . 25: // do nothing . . 26: } . . 27:} . . 28: . . 29:func (t *Tiger) Drink() { (pprof) web failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in %PATH% (pprof) top Showing nodes accounting for 18.05s, 98.96% of 18.24s total Dropped 64 nodes (cum <= 0.09s) flat flat% sum% cum cum% 18.05s 98.96% 98.96% 18.08s 99.12% github.com/wolfogre/go-pprof-practice/animal/felidae/tiger.(*Tiger).Eat 0 0% 98.96% 18.08s 99.12% github.com/wolfogre/go-pprof-practice/animal/felidae/tiger.(*Tiger).Live 0 0% 98.96% 18.18s 99.67% main.main 0 0% 98.96% 18.18s 99.67% runtime.main (pprof) web failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in %PATH% (pprof) exit
C:\Users\90760>go tool pprof http://localhost:6060/debug/pprof/threadcreate Fetching profile over HTTP from http://localhost:6060/debug/pprof/threadcreate Saved profile in C:\Users\90760\pprof\pprof.threadcreate.001.pb.gz Type: threadcreate Time: Oct 29, 2022 at 5:24pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 1, 14.29% of 7 total Showing top 10 nodes out of 11 flat flat% sum% cum cum% 1 14.29% 14.29% 1 14.29% runtime.allocm 0 0% 14.29% 1 14.29% net/http.(*ServeMux).ServeHTTP 0 0% 14.29% 1 14.29% net/http.(*conn).serve 0 0% 14.29% 1 14.29% net/http.HandlerFunc.ServeHTTP 0 0% 14.29% 1 14.29% net/http.serverHandler.ServeHTTP 0 0% 14.29% 1 14.29% net/http/pprof.Profile 0 0% 14.29% 1 14.29% runtime.SetCPUProfileRate 0 0% 14.29% 1 14.29% runtime.newm 0 0% 14.29% 1 14.29% runtime.setProcessCPUProfiler 0 0% 14.29% 1 14.29% runtime.setcpuprofilerate (pprof) list command list requires an argument (pprof) exit
C:\Users\90760>go tool pprof -http=:8000 http://localhost:6060/debug/pprof/profile Fetching profile over HTTP from http://localhost:6060/debug/pprof/profile Saved profile in C:\Users\90760\pprof\pprof.samples.cpu.003.pb.gz Serving web UI on http://localhost:8000 Failed to execute dot. Is Graphviz installed? exec: "dot": executable file not found in %PATH%