日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

golang调用java的函数_大话golang性能分析(一):profile基本原理

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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基本原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。