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