golang调用java的函数_大话golang性能分析(一):profile基本原理
引言:好久沒分享了,不多廢話了,準備一個專題分三期來分享下golang的性能分析。
O 專題目標
- 理解profile基本原理
- 熟悉go常用性能分析工具pprof
- 快速對線上服務的cpu、內存、goroutine的問題進行分析和排查
對性能分析,golang是采取采樣分析的方式,語言原生支持對運行的程序進行采樣,收集采樣數據通過累加計算并通過提供相應的工具來分析堆棧信息。【對比java:java通過發布jdk周邊工具jps jmap jstack等,在問題分析前先將內存中的數據進行dump的方式進行分析】
一 profile原理
A Profile is a collection of stack traces showing the call sequences that led to instances of a particular event, such as allocation. Packages can create and maintain their own profiles; the most common use is for tracking resources that must be explicitly closed, such as files or network connections.
go默認會初始化六種profile, 每種profile存儲的實際內容被抽象為 ?countProfile?,存儲棧幀地址,堆棧地址通過調用?runtime.CallersFrames(stk)?可以獲取堆棧信息,下面主要講最常用的三種方式:內存、CPU和協程
type countProfile interface { Len() int Stack(i int) []uintptr}// A countProfile is a set of stack traces to be printed as counts grouped by stack trace. There are multiple implementations:all that matters is that we can find out how many traces there are and obtain each trace in turn.- 內存采樣
go程序啟動后,runtime會按照一定頻率對內存的分配進行采樣記錄,當內存分配每達到一定值(默認是512KB,參數由?runtime.MemProfileRate?設定), runtime就會記錄下當前這次內存分配的大小、stack等信息到profile
type MemProfileRecord struct { AllocBytes, FreeBytes int64 // number of bytes allocated, freed AllocObjects, FreeObjects int64 // number of objects allocated, freed Stack0 [32]uintptr // stack trace for this record; ends at first 0 entry}- CPU采樣
cpu的采樣是通過調用函數?StartCPUProfile?來啟動采樣的,結束調用?StopCPUProfile?調用鏈如下StartCPUProfile?->?runtime.SetCPUProfileRate?->?sighandler?;采樣頻率是100hz
// The runtime routines allow a variable profiling rate,// but in practice operating systems cannot trigger signals// at more than about 500 Hz, and our processing of the// signal is not cheap (mostly getting the stack trace).// 100 Hz is a reasonable choice: it is frequent enough to// produce useful data, rare enough not to bog down the// system, and a nice round number to make it easy to// convert sample counts to seconds. Instead of requiring// each client to specify the frequency, we hard code it.const hz = 100// readProfile, provided by the runtime, returns the next chunk of// binary CPU profiling stack trace data, blocking until data is available.// If profiling is turned off and all the profile data accumulated while it was// on has been returned, readProfile returns eof=true.// The caller must save the returned data and tags before calling readProfile again.func readProfile() (data []uint64, tags []unsafe.Pointer, eof bool)- goroutine采樣
GMP模型中,G goroutine P context M thread,采樣數據來源于P,運行中的協程上下文堆棧。P會維護當前執行隊列,隊列中是M對應的G隊列。自行檢索GPM原理(關鍵字:竊取、61分之一全局隊列)
如何將采樣內容匯總?
假設我們在程序執行的某個時刻取樣得到一個棧幀序列是ABC,可以得到的信息包括:此刻運行的函數是C,是從函數B調用到C的。當取樣很多次后進行統計,就可以得到調用的信息。比如對下面這段代碼的取樣:
- void A() { B(); for (int i=0; i<3; i++) C(); } void B() { for (int i=0; i<5; i++) C(); }
- 將得到
- A AB ABC ABC ABC AC 根據統計信息: 函數累加耗時和調用關系,根據這些數據可以構造有向圖
profiles采樣后會以pprof-formatted格式存儲起來,分析這些數據需要用到golang的分析工具。
下一節分享golang常用的分析工具,敬請期待。歡迎關注"大齡碼農
總結
以上是生活随笔為你收集整理的golang调用java的函数_大话golang性能分析(一):profile基本原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卡巴斯基安全浏览器_卡巴斯基杀毒软件被曝
- 下一篇: themyleaf 图片上传_javaE