您的位置 首页 golang

Golang使用pprof分析CPU占用

进入分析界面

 // 直接使用 http/pprof方式,也是最常用的方式
go tool pprof 
//也可以使用离线方式,效果时一样的
curl -s  > cpu.prof
go tool pprof cpu.prof  

常用的几种分析方法

  • top(N) 查看热点函数

N可以随意选择,会把CPU占用最大的前N个函数列举出来,如下图:

Golang使用pprof分析CPU占用

pprof top10

其中各列的含义如下,可以根据这些值做对应的分析:

flat :当前函数的执行时间,示例中是250ms。

flat% :当前函数执行占总时间比。示例中进程总耗时是600ms,占比为60.98%。

sum% :每行的sum%是当前行的flat% 加上当前行之前的行的所有flat%的总和,比如第一行为本身的flat%,第二行为当前行的flat%+第一行的flat%。是个统计值,能够看出topN函数占总耗时的比例。比如第二行的sum%=90.24%,说明topN中前两个函数总耗时占CPU总时间的90.24%,占比非常大了。

cum : 是指当前函数及其调用函数的总时间,示例中是340ms。比如a函数调用b函数,然后自身处理部分业务。那cum就是自身业务耗时加b函数从调用到返回的时间。和flat对比就可以看出flat内部函数调用具体耗时位置。

cum% : 同理cum%指cum占总时间的占比,实例中是82.93%。


  • 在上图的命令行中还有以下命令
Golang使用pprof分析CPU占用

pprof 命令

介绍几种常用的命令:

可以可视化的看出函数间的调用关系。web、dot、eog、evince、gif、pdf、png等都是直接输出可视化的调用关系图。只是格式不同,具体可以参考help。每个字命令也可以通过help xx 查看使用方法。

Golang使用pprof分析CPU占用

可视化 pprof

可视化的调用关系图可以直观地看到每个函数的调用关系,以及CPU使用情况:

Golang使用pprof分析CPU占用

profile 函数调用关系图

其中:白色方框中是整体信息,包含采集周期内的总耗时、采集时间戳等。

每个红色方框中,代表当前函数。

方框中的时间指当前函数占用的CPU时间,比例为占用总耗时的比例;

每个线条箭头上的数字表示调用者从调用函数到被调用函数返回的总耗时。比如图中第一个main函数调用demo函数耗时350ms,其中demo函数耗时10ms,demo函数又调用其他函数耗时为340ms,加起来的就是350ms。以此内推。如果红框中没有时间,表明当前函数直接调用了其他函数,没有耗时或者耗时不足以统计。其实可以看出和top命令很类似。不过能直观看出函数调用关系。 线条上也标记了inline调用的类型;

颜色越深,表明函数占用CPU时间越多。

火焰图

以上介绍的工具命令已经能够较直观的进行分析了。golang还有一种神器,更加直观的展示CPU占用的关系。使用方法如下:

  • 安装依赖工具
 //安装go-torch 可以选择对应的版本,安装好之后确保$GOPATH/bin在系统$PATH环境变量中
//,或直接拷贝go-torch 到/usr/bin目录下。windows环境可以直接添加环境变量。
go install github.com/uber/go-torch@latest
// 安装 FlameGraph
// 把 FlameGraph 路径添加到$PATH下,这是个perl写的项目
git clone  github.com/brendangregg/FlameGraph.git
// 安装 graphviz,官网有各平台的安装方法
   
  • 生成火焰图

使用 go-torch -u ,默认是采集30s,也可以加-t 指定时间。具体可以看go-torch help。 下图可以点击每个色块看具体情况。

Golang使用pprof分析CPU占用

火焰图

同样也可以通过离线的cpu.prof文件来产生火焰图,如果生产环境不能安装go-torch工具的话,可以采用这种方式。

 // 首先通过pprof 工具产生cpu.prof文件
curl -s  > cpu.prof
// 然后 利用go-torch工具生成或沿途
go-torch -b cpu.prof  

利用pprof分析CPU占用就介绍到这里,每个业务代码优化cpu的问题不尽相同,排查到热点函数之后就可以有针对性的优化。

文章来源:智云一二三科技

文章标题:Golang使用pprof分析CPU占用

文章地址:https://www.zhihuclub.com/97712.shtml

关于作者: 智云科技

热门文章

评论已关闭

55条评论

  1. Furthermore, Sustanon 250 can be used for both bulking and cutting cycles since testosterone has anabolic as well as androgenic properties

  2. efavirenz increases levels of lomitapide by affecting hepatic intestinal enzyme CYP3A4 metabolism That is a challenge for me as my job is sitting all day at a computer

  3. Company says cancer drug beats tamoxifen C and D CFU of H37Ra C and H37Rv D MOI 10 1 treated with BZA 5 ОјM is shown

  4. acetate femara for fertility when to take As I Lay Dying, who formed in San Diego in 2000, have sold more than a million albums

  5. levitra flagyl vagitorier og menstruasjon The partnership comes as Starbucks pushes into the healthand wellness category and as Danone, owner of the Dannon brand, and other yogurt makers seek to conquer the U 6 mmol l 126 to 101 mg dl, or from diabetic to typical levels

  6. IHC staining of insulin protein in pancreatic tissues from Rab1A and Rab1A mice Extra Corporeal Shock Wave Therapy ESWT

  7. Even Zhao Ling, who was outside, could feel the how much viagra should i take for fun tense atmosphere and the murderous aura emanating from Chu He, but only Cui Shan seemed to be ignorant

  8. As compared with other liposomal formulations, OCT modified daunorubicin plus dihydroartemisinin liposomes displayed higher cytotoxicity, indicating that the OCT could enhance cellular uptake of daunorubicin, which is subsequently confirmed by the flow cytometry Figure 4 A, B and Supplementary Figure S1

  9. Most evidence suggests that soy protein or soy extract can increase bone mineral density BMD or slow BMD loss in women near or beyond menopause

  10. Inhibition of PFKFB3 affects VEGFR2 activation in tip cells and normalizes disorganized angiogenesis in high VEGF conditions 12, 38

  11. Patients should be instructed to read the Medication Guide supplied as required by law when Puretam is dispensed com community profile bitstarz3113058 Bitstarz vpn, bitstarz sign up bonus code

  12. In that case, please try at all costs if it s possible to separate that individual from the rest of the family Krikorian et al Concord grape juice supplementation improves memory function in older adults with mild cognitive impairment

  13. Recent advances in quantifying mammographic density by using volumetric methods have several advantages over visual and two dimensional area based measures 2006; 99 6 355 363

  14. How have other dealt with the onset of such heavy depression or is it possible that this has nothing to do with the drug

  15. President Barack Obama, first lady Michelle Obama, Vice President Joseph Biden, his wife Jill Biden, and White House staff observe a moment of silence to mark the 12th anniversary of the 9 11 attacks, September 11, 2013 on the South Lawn of the White House in Washington, DC

  16. Polyps may be soft and cystic or firm and fibrous; they may be pedunculated or sessile, single or multiple, and vary in size from small with minimal uterine cavity distortion to large, filling the whole cavity Evidence of primary aldosteronism in a predominantly female cohort of normotensive individuals a very high odds ratio for progression into arterial hypertension

  17. zetia seroquel dosage for mania Economists polled by Reuters said the Japanese economy couldwithstand a planned sales tax increase even if growth slows fromthe first quarter s healthy pace, with 14 of the 15 respondentssaying the government should go ahead as planned with the firststage of raising the 5 percent tax to 8 percent in April

  18. There is a need to communicate with and educate young patients regarding fertility issues at diagnosis and a need for future research directed at preserving fertility for young breast cancer survivors

  19. The city council and city attorney are currently focused on crafting a new municipal citation for public pot smoking, despite the fact that cops can already cite people for public consumption under state law

  20. Objective The purpose of this study was to compare the effectiveness of low- dose human chorionic gonadotropin hCG in the late follicular phase to induce ovulation and its endocrine response in patients who had previously failed to ovulate on clomiphene citrate CC alone

  21. Postoperative adjuvant randomized trial comparing chemoendocrine therapy chemotherapy and immunotherapy for patients with stage II breast cancer 5 year results from the Nishinihon Cooperative Study Group of Adjuvant Chemoendocrine Therapy for Breast Cancer ACETBC of Japan We summarize molecular mechanisms involved in creating steroid receptor driven gene expression programs in hormone dependent cancers and will elucidate the interplay between steroid receptor dependent transcription and genetic alterations that contribute to endocrine therapy resistance

  22. This can be compared to the higher rates of successful pregnancy outcome following a tubal reversal

  23. 2002, perhaps because the hydropic sacculus presses against the stapes footplate, enhancing saccular sensitivity to AC sound Presence of comorbid conditions may also limit discussion of treatment options 92 or complicate delivery of treatment such as chemotherapy 40, 93, 94

  24. In sum, acquired FXN deficiency promotes Fe S dependent nuclear dysfunction in primary endothelial cells, displaying a progression of events starting with replication stress and growth arrest and progressing to persistent DDR dependent senescence

  25. James respiratory rate is 18 James Oxygen saturation is 91 There are frank blood suction from the tube There are moderate amount of tracheobronchial secretions Well, you re the right person to get up and start moving

  26. Article Ren J, Chen GG, Liu Y, Su X, Hu B, Leung BC, Wang Y, Ho RL, Yang S, Lu G, Lee CG, Lai PB Cytochrome P450 1A2 Metabolizes 17beta Estradiol to Suppress Hepatocellular Carcinoma

  27. Unintended Acceleration Marketing, Sales Practices andProducts Liability Litigation, 8 10 ml 02151, U

  28. Miguel, USA 2022 04 29 10 00 51 Symptoms of pulmonary congestion include worsening exertional dyspnea and orthopnea; bilateral crackles may be heard on physical examination if pulmonary edema is present

  29. 32, 34 However, genetic and molecular cues of aortic wall stiffening are not fully understood hampering the discovery of therapeutic targets that can slow or reverse arterial stiffening thereby decreasing target organ damage and the risk of developing overt CVD Moderate TBI 9 12

  30. aNPCs were infected with nonspecific shRNA control retroviral vector shNS or shRNA to ERK5 retrovirus as indicated

  31. 9, 2020 Novartis today announced updated median overall survival OS results for Kisqali ribociclib in combination with endocrine therapy, marking the longest survival data ever reported in premenopausal women with hormone receptor positive, human epidermal growth factor receptor 2 negative HR HER2 metastatic breast cancer

  32. Its chemical name is 1isopropyl 3 4 m toluidino 3 pyridyl sulfonyl urea and its structural formula is We tested the hypothesis if suitable modulations of signal proteins could be one of the possible pathways of action of a highly diluted homeopathic drug, Secale cornutum 30C diluted 10 60 times; Sec cor 30

  33. plendil ibuprofeno 50mg ml bula When some the United States and Israel say that all options are on the table and when a miserable regional country Israel says such things, it makes you laugh, Rouhani said in an address to Iran Iraq war veterans, according to Iranian media reports

  34. 30 performed multiple linear regressions to estimate the proportion of hospitalisations that was caused by influenza and provided details on the goodness of fit R 2 of the model

网站地图