?
This document uses PHP Chinese website manual Release
import "runtime/pprof"
概況
索引
子目錄
軟件包 pprof 以 pprof 可視化工具所期望的格式寫入運(yùn)行時(shí)分析數(shù)據(jù)。
分析 Go 程序的第一步是啟用分析。支持使用標(biāo)準(zhǔn)測(cè)試包構(gòu)建的性能分析基準(zhǔn)測(cè)試。例如,以下命令在當(dāng)前目錄中運(yùn)行基準(zhǔn)測(cè)試并將 CPU 和內(nèi)存配置文件寫入 cpu.prof 和 mem.prof:
go test -cpuprofile cpu.prof -memprofile mem.prof -bench .
要為獨(dú)立程序添加等效分析支持,請(qǐng)將以下代碼添加到主函數(shù)中:
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")var memprofile = flag.String("memprofile", "", "write memory profile to `file`")func main() { flag.Parse() if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal("could not create CPU profile: ", err) } if err := pprof.StartCPUProfile(f); err != nil { log.Fatal("could not start CPU profile: ", err) } defer pprof.StopCPUProfile() } // ... rest of the program ... if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { log.Fatal("could not create memory profile: ", err) } runtime.GC() // get up-to-date statistics if err := pprof.WriteHeapProfile(f); err != nil { log.Fatal("could not write memory profile: ", err) } f.Close() }}
還有一個(gè)標(biāo)準(zhǔn)的 HTTP 接口來分析數(shù)據(jù)。添加以下行將在 / debug / pprof / URL 下安裝處理程序以下載實(shí)時(shí)配置文件:
import _ "net/http/pprof"
有關(guān)更多詳細(xì)信息,請(qǐng)參閱 net / http / pprof 軟件包。
配置文件可以通過 pprof 工具進(jìn)行可視化處理:
go tool pprof cpu.prof
pprof 命令行提供了許多命令。通常使用的命令包括打印頂部程序熱點(diǎn)摘要的“top”和打開熱點(diǎn)及其調(diào)用圖的交互圖的“web”。使用“help”獲取所有 pprof 命令的信息。
func Do(ctx context.Context, labels LabelSet, f func(context.Context))
func ForLabels(ctx context.Context, f func(key, value string) bool)
func Label(ctx context.Context, key string) (string, bool)
func Profiles() []*Profile
func SetGoroutineLabels(ctx context.Context)
func StartCPUProfile(w io.Writer) error
func StopCPUProfile()
func WithLabels(ctx context.Context, labels LabelSet) context.Context
func WriteHeapProfile(w io.Writer) error
type LabelSet
func Labels(args ...string) LabelSet
type Profile
func Lookup(name string) *Profile
func NewProfile(name string) *Profile
func (p *Profile) Add(value interface{}, skip int)
func (p *Profile) Count() int
func (p *Profile) Name() string
func (p *Profile) Remove(value interface{})
func (p *Profile) WriteTo(w io.Writer, debug int) error
Bugs
elf.go label.go map.go pprof.go proto.go protobuf.go protomem.go runtime.go
func Do(ctx context.Context, labels LabelSet, f func(context.Context))
使用添加到父標(biāo)簽映射中的給定標(biāo)簽的父上下文副本調(diào)用 f 。標(biāo)簽中的每個(gè)鍵/值對(duì)都按照提供的順序插入到標(biāo)簽貼圖中,覆蓋同一個(gè)鍵的任何以前的值。增強(qiáng)標(biāo)簽貼圖將在 f 調(diào)用期間設(shè)置,并在 f 返回時(shí)恢復(fù)。
func ForLabels(ctx context.Context, f func(key, value string) bool)
ForLabels 通過在上下文中設(shè)置每個(gè)標(biāo)簽來調(diào)用 f 。函數(shù) f 應(yīng)該返回 true 來繼續(xù)迭代或 false 以盡早停止迭代。
func Label(ctx context.Context, key string) (string, bool)
Label 用 ctx 上的給定關(guān)鍵字返回標(biāo)簽的值,以及指示該標(biāo)簽是否存在的布爾值。
func Profiles() []*Profile
Profiles 文件返回所有已知配置文件的一部分,按名稱排序。
func SetGoroutineLabels(ctx context.Context)
SetGoroutineLabels 將當(dāng)前 goroutine 的標(biāo)簽設(shè)置為與 ctx 匹配。這是比 Do 更低級(jí)別的 API,應(yīng)盡可能使用它。
func StartCPUProfile(w io.Writer) error
StartCPUProfile 啟用當(dāng)前進(jìn)程的 CPU 分析。分析時(shí),配置文件將被緩沖并寫入 w 。如果分析已啟用,則 StartCPUProfile 將返回錯(cuò)誤。
在類 Unix 系統(tǒng)上,默認(rèn)情況下,StartCPUProfile 對(duì)于使用 -buildmode = c-archive 或 -buildmode = c-shared 構(gòu)建的 Go 代碼不起作用。StartCPUProfile 依賴于 SIGPROF 信號(hào),但該信號(hào)將被傳送到主程序的 SIGPROF 信號(hào)處理程序(如果有)而不是 Go 所使用的信號(hào)處理程序。要使其工作,請(qǐng)為 syscall.SIGPROF 調(diào)用 os / signal.Notify,但請(qǐng)注意,這樣做可能會(huì)破壞主程序執(zhí)行的任何分析。
func StopCPUProfile()
StopCPUProfile 停止當(dāng)前 CPU 配置文件(如果有)。StopCPUProfile 僅在配置文件的所有寫入完成后才會(huì)返回。
func WithLabels(ctx context.Context, labels LabelSet) context.Context
WithLabels 返回一個(gè)新的 context.Context,添加了給定的標(biāo)簽。標(biāo)簽使用相同的密鑰覆蓋先前的標(biāo)簽。
func WriteHeapProfile(w io.Writer) error
WriteHeapProfile 是 Lookup(“heap”)的縮寫。WriteTo(w,0)。它是為了向后兼容而保存的。
LabelSet 是一組標(biāo)簽。
type LabelSet struct { // contains filtered or unexported fields}
func Labels(args ...string) LabelSet
Labels 需要偶數(shù)個(gè)表示鍵值對(duì)的字符串,并使 LabelSet 包含它們。標(biāo)簽使用相同的密鑰覆蓋先前的標(biāo)簽。
配置文件是堆棧跟蹤的集合,顯示導(dǎo)致特定事件實(shí)例(例如分配)的調(diào)用序列。包可以創(chuàng)建和維護(hù)自己的配置文件; 最常見的用途是跟蹤必須明確關(guān)閉的資源,例如文件或網(wǎng)絡(luò)連接。
配置文件的方法可以同時(shí)從多個(gè) goroutine 調(diào)用。
每個(gè)配置文件都有唯一的名稱。一些配置文件是預(yù)定義的:
goroutine - stack traces of all current goroutines heap - a sampling of all heap allocations threadcreate - stack traces that led to the creation of new OS threads block - stack traces that led to blocking on synchronization primitives mutex - stack traces of holders of contended mutexes
這些預(yù)定義的配置文件在明確的 Add 或 Remove 方法調(diào)用中保持自己和恐慌。
堆概要報(bào)告最近完成的垃圾收集的統(tǒng)計(jì)數(shù)據(jù); 它避免了最近的分配,以避免將配置文件從實(shí)時(shí)數(shù)據(jù)轉(zhuǎn)向垃圾。如果根本沒有垃圾回收,則堆配置文件會(huì)報(bào)告所有已知的分配。此異常主要用于在未啟用垃圾回收的情況下運(yùn)行的程序,通常用于調(diào)試目的。
CPU 配置文件不可用作配置文件。它有一個(gè)特殊的 API,StartCPUProfile 和 StopCPUProfile 函數(shù),因?yàn)樗诜治鲞^程中將輸出流輸出到一個(gè)寫入器。
type Profile struct { // contains filtered or unexported fields}
func Lookup(name string) *Profile
Lookup 將返回具有給定名稱的配置文件,如果不存在此類配置文件,則返回 nil 。
func NewProfile(name string) *Profile
NewProfile 使用給定的名稱創(chuàng)建一個(gè)新的配置文件。如果具有該名稱的配置文件已存在,則 NewProfile 會(huì)發(fā)生混亂。該慣例是使用“導(dǎo)入/路徑”。前綴為每個(gè)包創(chuàng)建單獨(dú)的名稱空間。為了與讀取 pprof 數(shù)據(jù)的各種工具兼容,配置文件名稱不應(yīng)包含空格。
func (p *Profile) Add(value interface{}, skip int)
Add 將當(dāng)前執(zhí)行堆棧添加到與值關(guān)聯(lián)的配置文件。將商店值添加到內(nèi)部映射中,因此值必須適合用作映射鍵,并且在相應(yīng)的調(diào)用 Remove 之前不會(huì)進(jìn)行垃圾收集。如果配置文件已經(jīng)包含值的堆棧,則添加恐慌。
skip 參數(shù)的含義與 runtime.Caller 的 skip 和 controls 開始的地方相同。跳過 skip = 0開始追加函數(shù)的追蹤。例如,給定這個(gè)執(zhí)行堆棧:
Add called from rpc.NewClient called from mypkg.Run called from main.main
跳過 skip = 0時(shí),會(huì)在調(diào)用 Add rpc.NewClient 時(shí)添加堆棧跟蹤。在 skip mypkg.Run 中調(diào)用 NewClient 時(shí),skip skip = 1開始堆棧跟蹤。
func (p *Profile) Count() int
Count 返回配置文件中當(dāng)前執(zhí)行堆棧的數(shù)量。
func (p *Profile) Name() string
Name 返回此配置文件的名稱,該名稱可以傳遞給查找以重新獲取配置文件。
func (p *Profile) Remove(value interface{})
Remove 從配置文件中刪除與值關(guān)聯(lián)的執(zhí)行堆棧。如果該值不在配置文件中,則它是無操作的。
func (p *Profile) WriteTo(w io.Writer, debug int) error
WriteTo 將配置文件的 pprof 格式的快照寫入 w 。如果寫入 w 返回錯(cuò)誤,則 WriteTo 返回該錯(cuò)誤。否則,WriteTo 返回 nil 。
調(diào)試參數(shù)啟用額外的輸出。傳遞 debug = 0僅打印 pprof 所需的十六進(jìn)制地址。傳遞 debug = 1會(huì)添加將地址轉(zhuǎn)換為函數(shù)名和行號(hào)的注釋,以便程序員可以在不使用工具的情況下讀取配置文件。
預(yù)定義的配置文件可以為其他調(diào)試值指定含義; 例如,在打印“goroutine”配置文件時(shí),debug = 2意味著打印 goroutine 堆棧的格式與 Go 程序由于未發(fā)現(xiàn)的恐慌而死時(shí)使用的格式相同。
? 配置文件與用于生成它們的內(nèi)核支持一樣好。
Name | Synopsis |
---|