网站的种类有哪些,做悬赏的网站,信息流投放,开发公司与物业服务合同范本引言#xff1a;好久没分享了#xff0c;不多废话了#xff0c;准备一个专题分三期来分享下golang的性能分析。O 专题目标理解profile基本原理熟悉go常用性能分析工具pprof快速对线上服务的cpu、内存、goroutine的问题进行分析和排查对性能分析#xff0c;golang是采取采样…引言好久没分享了不多废话了准备一个专题分三期来分享下golang的性能分析。O 专题目标理解profile基本原理熟悉go常用性能分析工具pprof快速对线上服务的cpu、内存、goroutine的问题进行分析和排查 对性能分析golang是采取采样分析的方式语言原生支持对运行的程序进行采样收集采样数据通过累加计算并通过提供相应的工具来分析堆栈信息。【对比javajava通过发布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等信息到profiletype 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 eoftrue.// 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 i0; i3; i) C(); } void B() { for (int i0; i5; i) C(); }将得到A AB ABC ABC ABC AC 根据统计信息: 函数累加耗时和调用关系根据这些数据可以构造有向图profiles采样后会以pprof-formatted格式存储起来分析这些数据需要用到golang的分析工具。下一节分享golang常用的分析工具敬请期待。欢迎关注大龄码农