go pprof详细理解及使用

Golang性能分析工具pprof--远程分析时无法定位源代码行数问题解决方案 golang使用pprof定位代码行问题 阅读详情

一、概述

        在Go语言中,我们可以通过标准库的代码包runtime和runtime/pprof中的程序来生成三种包含实时性数据的概要文件,分别是CPU概要文件、内存概要文件和程序阻塞概要文件。

cpu概要文件

在默认情况下,Go语言的运行时系统会以100 Hz的的频率对CPU使用情况进行取样。

 

内存概要文件

内存概要文件用于保存在用户程序执行期间的内存使用情况。这里所说的内存使用情况,其实就是程序运行过程中堆内存的分配情况。

 

程序阻塞概要文件

程序阻塞概要文件用于保存用户程序中的Goroutine阻塞事件的记录。

 

名称

说明

取样频率

goroutine

活跃Goroutine的信息的记录。

仅在获取时取样一次。

threadcreate

系统线程创建情况的记录。

仅在获取时取样一次。

heap

堆内存分配情况的记录。

默认每分配512K字节时取样一次。

block

Goroutine阻塞事件的记录。

默认每发生一次阻塞事件时取样一次。

 

 

 

 

 

 

二、引入pprof

import  _ "net/http/pprof"

go func() {
    http.ListenAndServe("0.0.0.0:6060", nil)
}()

  三、pprof使用

  1. 浏览器访问

在浏览器输入http://ip:6060/debug/pprof可以看到

profiles下分别是block(阻塞情况)、goroutine数量、heap(堆)、mutex(锁)和threadcreate(创建的物理线程)

 

点击heap可以看到如下堆内存及GC相关的参数信息

看到这里是调用Go底层的runtime.MemStats结构体获取,该结构体相关字段解释如下

Alloc uint64 //golang语言框架堆空间分配的字节数
TotalAlloc uint64 //从服务开始运行至今分配器为分配的堆空间总 和,只有增加,释放的时候不减少
Sys uint64 //服务现在系统使用的内存
Lookups uint64 //被runtime监视的指针数
Mallocs uint64 //服务malloc heap objects的次数
Frees uint64 //服务回收的heap objects的次数
HeapAlloc uint64 //服务分配的堆内存字节数
HeapSys uint64 //系统分配的作为运行栈的内存
HeapIdle uint64 //申请但是未分配的堆内存或者回收了的堆内存(空闲)字节数
HeapInuse uint64 //正在使用的堆内存字节数
HeapReleased uint64 //返回给OS的堆内存,类似C/C++中的free。
HeapObjects uint64 //堆内存块申请的量
StackInuse uint64 //正在使用的栈字节数
StackSys uint64 //系统分配的作为运行栈的内存
MSpanInuse uint64 //用于测试用的结构体使用的字节数
MSpanSys uint64 //系统为测试用的结构体分配的字节数
MCacheInuse uint64 //mcache结构体申请的字节数(不会被视为垃圾回收)
MCacheSys uint64 //操作系统申请的堆空间用于mcache的字节数
BuckHashSys uint64 //用于剖析桶散列表的堆空间
GCSys uint64 //垃圾回收标记元信息使用的内存
OtherSys uint64 //golang系统架构占用的额外空间
NextGC uint64 //垃圾回收器检视的内存大小
LastGC uint64 // 垃圾回收器最后一次执行时间。
PauseTotalNs uint64 // 垃圾回收或者其他信息收集导致服务暂停的次数。
PauseNs [256]uint64 //一个循环队列,记录最近垃圾回收系统中断的时间
PauseEnd [256]uint64 //一个循环队列,记录最近垃圾回收系统中断的时间开始点。
NumForcedGC uint32 //服务调用runtime.GC()强制使用垃圾回收的次数。
GCCPUFraction float64 //垃圾回收占用服务CPU工作的时间总和。如果有100个goroutine,垃圾回收的时间为1S,那么就占用了100S。
BySize //内存分配器使用情况

2. go tool pprof命令行查看

go tool pprof --help命令可以查看该命令行的详细使用说明。

> go tool pprof --help
     usage:

Produce output in the specified format.

   pprof <format> [options] [binary] <source> ...

Omit the format to get an interactive shell whose commands can be used
to generate various views of a profile

   pprof [options] [binary] <source> ...

Omit the format and provide the "-http" flag to get an interactive web
interface at the specified host:port that can be used to navigate through
various views of a profile.

   pprof -http [host]:[port] [options] [binary] <source> ...

Details:
  Output formats (select at most one):
    -callgrind       Outputs a graph in callgrind format
    -comments        Output all profile comments
    -disasm          Output assembly listings annotated with samples
    -dot             Outputs a graph in DOT format
    -eog             Visualize graph through eog
    -evince          Visualize graph through evince
    -gif             Outputs a graph image in GIF format
    -gv              Visualize graph through gv
    -kcachegrind     Visualize report in KCachegrind
    -list            Output annotated source for functions matching regexp
    -pdf             Outputs a graph in PDF format
    -peek            Output callers/callees of functions matching regexp
    -png             Outputs a graph image in PNG format
    -proto           Outputs the profile in compressed protobuf format
    -ps              Outputs a graph in PS format
    -raw             Outputs a text representation of the raw profile
    -svg             Outputs a graph in SVG format
    -tags            Outputs all tags in the profile
    -text            Outputs top entries in text form
    -top             Outputs top entries in text form
    -topproto        Outputs top entries in compressed protobuf format
    -traces          Outputs all profile samples in text form
    -tree            Outputs a text rendering of call graph
    -web             Visualize graph through web browser
    -weblist         Display annotated source in a web browser

  Options:
    -call_tree       Create a context-sensitive call tree
    -compact_labels  Show minimal headers
    -divide_by       Ratio to divide all samples before visualization
    -drop_negative   Ignore negative differences
    -edgefraction    Hide edges below <f>*total
    -focus           Restricts to samples going through a node matching regexp
    -hide            Skips nodes matching regexp
    -ignore          Skips paths going through any nodes matching regexp
    -mean            Average sample value over first value (count)
    -nodecount       Max number of nodes to show
    -nodefraction    Hide nodes below <f>*total
    -normalize       Scales profile based on the base profile.
    -output          Output filename for file-based outputs
    -positive_percentages Ignore negative samples when computing percentages
    -prune_from      Drops any functions below the matched frame.
    -relative_percentages Show percentages relative to focused subgraph
    -sample_index    Sample value to report (0-based index or name)
    -show            Only show nodes matching regexp
    -source_path     Search path for source files
    -tagfocus        Restricts to samples with tags in range or matched by regexp
    -taghide         Skip tags matching this regexp
    -tagignore       Discard samples with tags in range or matched by regexp
    -tagshow         Only consider tags matching this regexp
    -trim            Honor nodefraction/edgefraction/nodecount defaults
    -unit            Measurement units to display

  Option groups (only set one per group):
    cumulative       
      -cum             Sort entries based on cumulative weight
      -flat            Sort entries based on own weight
    granularity      
      -addresses       Aggregate at the function level.
      -addressnoinlines Aggregate at the function level, including functions' addresses in the output.
      -files           Aggregate at the file level.
      -functions       Aggregate at the function level.
      -lines           Aggregate at the source code line level.
      -noinlines       Aggregate at the function level.

  Source options:
    -seconds              Duration for time-based profile collection
    -timeout              Timeout in seconds for profile collection
    -buildid              Override build id for main binary
    -add_comment          Free-form annotation to add to the profile
                          Displayed on some reports or with pprof -comments
    -base source          Source of profile to use as baseline
    profile.pb.gz         Profile in compressed protobuf format
    legacy_profile        Profile in legacy pprof format
    http://host/profile   URL for profile handler to retrieve
    -symbolize=           Controls source of symbol information
      none                  Do not attempt symbolization
      local                 Examine only local binaries
      fastlocal             Only get function names from local binaries
      remote                Do not examine local binaries
      force                 Force re-symbolization
    Binary                  Local path or build id of binary for symbolization


  Misc options:
   -http              Provide web based interface at host:port.
                      Host is optional and 'localhost' by default.
                      Port is optional and a randomly available port by default.
   -tools             Search path for object tools

  Legacy convenience options:
   -inuse_space           Same as -sample_index=inuse_space
   -inuse_objects         Same as -sample_index=inuse_objects
   -alloc_space           Same as -sample_index=alloc_space
   -alloc_objects         Same as -sample_index=alloc_objects
   -total_delay           Same as -sample_index=delay
   -contentions           Same as -sample_index=contentions
   -mean_delay            Same as -mean -sample_index=delay

  Environment Variables:
   PPROF_TMPDIR       Location for saved profiles (default $HOME/pprof)
   PPROF_TOOLS        Search path for object-level tools
   PPROF_BINARY_PATH  Search path for local binary files
                      default: $HOME/pprof/binaries
                      finds binaries by $name and $buildid/$name
   * On Windows, %USERPROFILE% is used instead of $HOME

从以上我们可以看到该命令可以指定输出的格式化类型,以及可选参数。我们只针对其中常用的进行探讨。

如果想输出图片相关格式需要先安装graphviz,graphviz安装地址如下:

linux源码安装地址:https://graphviz.gitlab.io/_pages/Download/Download_source.html

windows下载地址:https://graphviz.gitlab.io/_pages/Download/Download_windows.html

可参考:https://blog.youkuaiyun.com/skh2015java/article/details/102748065

 

查看内存使用情况

> go tool pprof  http://ip:6060/debug/pprof/heap

会进入如下gdb交互模式:

> help可以查看使用说明

> top 可以查看前10个的内存分配情况

> tree 以树状显示

> png 以图片格式输出

> svg 生成浏览器可以识别的svg文件

 

cpu概要文件(cpu使用情况)

> go tool pprof  http://ip:6060/debug/pprof/profile

默认需要等30s

常用操作同上

 

感谢:

http://wiki.jikexueyuan.com/project/go-command-tutorial/0.12.html

http://www.iargs.cn/?p=62

使用go pprof进行golang程序内存分析 use pprof to fix oom 阅读详情

相关推荐

golang性能分析 pprof的使用 graphviz

golang性能分析 pprof的使用 graphviz

Mr_XiMu的博客 2357

go 调试利器之pprof指标分析

本文通过源码分析了go pprof指标分析工具,及其使用方法。

weixin_38597669的博客 4082

Go语言性能优化:从GC调优到pprof分析

通过GC调优和pprof分析,可以有效地优化Go语言的性能。在实际项目中,建议结合使用这两种方法,定期进行性能测试和分析,找到性能瓶颈并进行针对性的优化。同时,保持代码的可读性和可维护性也是优化过程中需要注意的重要方面。希望本文的分享能够帮助大家更好地优化Go语言应用的性能。

shejizuopin的博客 1247

Go性能分析工具pprof详解

pprof是Go官方提供的性能分析工具,可以分析程序的运行情况,并且提供可视化的功能。本文主要涉及pprof的使用和具体示例的讲解。

nil 1万+

golang pprof性能调试工具

摘要:pprof是Go语言的性能分析工具,可用于CPU、内存、协程等性能分析。使用分为数据采集和分析两步:采集可通过http端口、test命令或代码导出profile文件;分析时使用go tool pprof命令,支持top文本分析和web/flame可视化分析。关键指标包括flat(函数直接耗时)、cum(累计耗时)及其百分比。可视化分析中,节点大小表示flat值,颜色深浅表示cum值,线条粗细反映调用关系。建议通过-http参数启动web服务查看火焰图等可视化结果,更直观定位性能瓶颈。

JineD的博客 205

Linux 隐藏进程

这种方式属于DKoM(Direct Kernel Object Manipulation)攻击方式了,即直接内核对象操作技术。关于第一种hook方式可以参考上一篇文章。最后推荐一个rootkit工具。

chi's blog 2051

Go性能调优及相关工具使用(四)——性能调优工具pprof的使用

性能调优实战,pprof工具使用。

qq_54353206的博客 3489

识别产品外观的合格软件_工业视觉工业质检视觉识别机器视觉视觉检测智能制造工业检测缺陷...

随着工业物联网技术的迅猛发展,掀起了以云计算、大数据、以及人工智能 AI 等信息技术正与传统工业深入融合,由此衍生的 “智能制造4.0” 理念,正在为全球工业带来深远变革。中国的制造业巨头也纷纷借此发力,向智能化、数字化制造演进,实施战略转型。如何高效科学的管理和分析制造业务链上的生产价值,推进制造企业生产工艺优化与产品质量提升是每一个制造企业在数字化、智能化转型过程中的必经之路。业务发展带来的挑...

weixin_42502419的博客 754

golang性能分析工具pprof介绍

profile在计算机领域,我们可以将其理解为当前应用程序运行状态的画像。当程序性能不佳时,我们希望知道应用在 什么地方耗费了多少 CPU、memory等资源,golang是非常注重性能的语言,其内置的pprof就是为了分析调优程序运行性能而生。CPU profile:当前程序的CPU使用情况,pprof按照一定频率去采集应用程序在CPU和寄存器上面的数据Memory Profile(Heap Profile):当前程序的内存使用情况,可查看heap和alloc的情况。

技术控的笔记 8294

IP白名单校验

支持格式: 1、127.0.0.1 // 指定固定IP 2、127.0.0.1-127.0.0.128 // 可使用“-”来表示一个IP区间,支持跨IP段(一般不会出现这种情况,跨IP段则配置多个规则) 3、127.0.* // 2~4位IP段可...

2201

J1939协议实用指南与J1939数据记录方案

“没错,这是一份SAE J1939简单、实用指南。”—虹科 **开篇:**在这篇介绍中,我们介绍了J1939协议的基本知识,其中包括PGN和SPN。因为这是一篇偏向应用的简介,所以您还将会学习到如何通过DBC文件解码J1939数据、如何记录J1939、典型的应用案例和实用技巧。下面,来了解下这份简单易懂的J1939介绍吧~ 什么是J1939? 简而言之,SAE J1939其实是一套标准,重型车辆ECU间就是按照这套标准在CAN总线上进行通信的。当今大多数车辆都通过CAN(Controller Area Ne

1万+

[Go]理解golang项目性能分析工具PProf

PProf 是以 profile.proto来分析数据 可以采集程序执行时候的命令分析 可以采集HTTP服务时的运行时数据分析 可以通过go test 测试用例分析 有下面这些监控分析功能: CPU 分析 , 内存分析 , 阻塞分析 , 互斥锁分析 , Groutine分析 如果是http服务 , 那么直接引入这个包就可以实现了_ "net/http/pprof" 如果使用的gin框架 , 需...

程序员老狼专注开发aigc或客服系统应用 330

实战指南:基于汇川PLC与EtherNET/IP的无线以太网通信配置详解

本文提供了一份详细的实战指南,讲解如何配置汇川PLC基于EtherNET/IP协议的无线以太网通信。通过硬件选型(如达泰DTD419MB模块)、软件设置(使用AutoShop配置IP与I/O映射)及通信测试,实现PLC间稳定、灵活的数据交换,有效解决工业现场布线难题,提升部署与维护效率。

lake5的博客 616

golang pprof and heap and stack

golang-栈内存空间 golang pprof 使用简介 go pprof详细理解及使用

sky527759的博客 388

提示词驱动的智能合同生成与审核

第1章:核心概念与背景 1.1.1 问题背景 1.1.1.1 合同生成与审核的需求 合同作为商业和法律领域中的基本工具,其生成和审核过程至关重要。传统的合同生成往往依赖于法律专业人士的手工撰写,这不仅耗时耗力,而且容易出现错误。此外,合

AI天才研究院 1002

高通IPA硬件加速介绍

‌ 高通IPA (Internet Protocol Accelerator)模块是一种硬件加速模块,由高通公司(Qualcomm)提供,主要用于优化移动设备上的网络数据传输,特别是通过 LTE 、 5G 等移动网络的数据传输‌‌。IPA模块通过硬件加速,显著提高了数据传输的速度和效率,同时降低了设备的功耗,并提供了更稳定的网络连接,减少了数据传输中的丢包和延迟‌。

四儿家的小祖宗的博客 1627

Golang使用pprof监控性能

前言 之前一直研究过golang 的 pprof的性能分析,但是没搞出火焰图,后面查了一些资料学会了生成火焰图。今天我就用直接简单暴力的方式的教程来说明怎么用Golang的pprof监控和生成火焰图。 简单使用pprof 一.在代码里面写监听接口口。 #必须要 import "net/http/pprof" go func() { if err := http.Listen...

baogui8961的博客 949

学Simulink--环境感知场景实例:基于Simulink的毫米波雷达多目标跟踪仿真

本文介绍了基于Simulink的毫米波雷达多目标跟踪仿真系统,重点分析了FMCW雷达信号处理流程(Range/Doppler FFT、CFAR检测)和多目标跟踪算法(JPDA数据关联、EKF状态估计)。通过构建城市道路交叉场景,验证了系统在跟踪精度(位置误差<0.2m)、实时性(单帧处理24ms)和鲁棒性(ID切换率<5%)方面的性能优势,并提出了UKF滤波、多传感器融合等优化方向。该仿真系统为自动驾驶环境感知提供了实用的毫米波雷达跟踪解决方案。

amy_mhd的博客 92

Go性能调优:深入理解和应用Go pprof

在开发高性能的Go应用程序时,性能调优是一个重要的环节。Go语言提供了一种强大的性能分析工具——pprof,它可以帮助开发人员定位应用程序中的性能瓶颈。本文将介绍如何使用Go pprof进行性能分析,并提供一些常见的优化技巧,帮助您更好地调优Go应用程序。Go pprof是Go语言的一个性能分析工具,它使用了一种称为"profiling"的技术来监测应用程序的运行时性能。通过收集和分析性能数据,pprof可以提供详细的函数调用图、内存使用情况、CPU利用率等信息,帮助我们找出代码中的性能问题。

DTCloud 427

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Go 程序调试总结

pprof有两个包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来 实例 package main import ( "log" "net" "net/http" "time" ) func run() { http.ListenAndSer...

cyq6239075的博客 2468

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

深入理解Go性能分析工具pprof在high-performance-go项目中的应用

在Go语言开发过程中,你是否遇到过这样的困境:代码运行缓慢,但不知道瓶颈在哪里?内存使用量异常增长,却无法定位问题根源?并发程序出现阻塞,但难以确定具体原因? 性能优化(Performance Optimization)是每个开发者都会面临的挑战。Go语言作为一门高性能的编程语言,提供了强大的性能分析工具pprof(Profiling Tool),能够帮助我们精准定位性能问题。本文将深入探讨pp

gitblog_01060的博客 378

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

golang性能分析,pprof的使用,graphviz,火焰图

golang中的pprof的使用,graphviz

猛犸象 1万+

Golang 使用pprof监控性能及GC调优先

   在golang中用来做监控分析的库包,一般用都是pprof库包… pprof可以在两个地方引入:    其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来。 runtime/pprof可以用来产生dump文件,再使用go tool pprof来分析这运行日志.    使用net/http/pprof可以做到直接看到当前w...

冷月醉雪的博客 3567

golang pprof 关键使用技巧

pprof 安装及使用 golang pprof 用于对线上运行程序 性能监控与分析 工具安装 go get -u github.com/google/pprof 代码注入 import _ "net/http/pprof" 启动服务(6060端口为例) go run ... 获取监控文件(heap为例) curl http://localhost:6060/debug/pprof/heap > heap.profile pprof web监控示例 pprof -http=: ./heap.p

谢小军的专栏 2045

pprof & 火焰图go-torch

引言 最近学习go的性能分析工具,pprof和go-torch,这篇文章是本人对两个公爵安装,使用的总结。 pprof 参考 https://studygolang.com/articles/12970 step1 mac上安装graphviz,可以通过brew安装,用于生成.svg格式图形数据 brew install graphviz step2 开启一个pprof的服务 step3 利用g...

leehzl的博客 2504

go pprof 详细教程与案例分析

go pprof简介    profile 一般被称为 性能分析,词典上的翻译是 概况(名词)或者 描述…的概况(动词)。对于计算机程序来说,它的 profile,就是一个程序在运行时的各种概况信息,包括 cpu 占用情况,内存情况,线程情况,线程阻塞情况等等。知道了程序的这些信息,也就能容易的定位程序中的问题和故障原因 。pprof是Go的性能分析工具,在程序运行过程中,可以记录程序的运行信息,可以是CPU使用情况、内存使用情况、goroutin

weixin_39137890的博客 7262
上一篇: graphviz安装
下一篇: Golang sync.Pool详解
思维的深度
博客等级 码龄11年 2万+粉丝 391原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值