Go -- 中开启gctrace
godebug是一个跨平台的Go程序调试工具,传统的编译型语言调试器使用底层系统调用并读取二进制文件用于调试各类符号。使用起来很麻烦而且很难移植。
godebug使用不同的方法,直接把源码作为目标程序,在每一行插入调试代码,然后编译并运行。结果就是一个全功能的调试器,完全可以移植到不同平台。事实上,由于有了gopherjs,你也可以在浏览器上进行调试:
点击此处尝试原文调试窗口。通过原文的“DEBUG IT”按钮,你可以编辑程序并重新调试它。
工作原理
下面这张是描述上面例子的矢量图:
从中可以看出原始代码转换了两次:第一次godebug插入调试instrumentation,然后gopherjs将结果编译成JavaScript。
让我们看一下instrumentation步骤,以下是godebug插入的调用:
- godebug.EnterFunc:让godebug运行时库知道我们在进入一个函数。由于“next”不停止内部函数调用,运行时库注意这些调用并知道何时跳过这些行。
- godebug.ExitFunc:让godebug运行时库知道我们在离开一个函数,main中省略。
- godebug运行时库:当且仅当用户用命令或一个断点告知其时,使程序暂停并等待输入,暂停时,它会提示用户输入并响应任何命令。
- godebug.Declare:记录变量名和值的映射。该映射通过输出命令使用。
使用godebug
Step 1.安装
Step 2.设置断点
在任何想要断点的地方添加这个标记:
由于断点是源代码的一部分,你可以将自己的逻辑置于他们周围。假设你正在运行一个表格驱动多情况测试,其中一个测试输入“weird string”的情况是失败的,那么你可以将下面这个断点放入测试当中:
godebug test将于标记处暂停程序,方便定位。
Step 3.运行程序
使用godebug运行命令:
或者对测试来说 ,运行测试命令:
默认情况下,godebug只会添加调试instrumentation到package main(为了godebug运行)或者测试下的package(为了godebug测试)。这意味着在默认情况下,你无法进入来自导入包的函数。当需要的时候,执行下面这个行命令:
最后
godebug仍然还是一款需要打磨的新工具,目前存在的已知限制包括:
- 性能开销
- 可能会导致读取冲突,如果你的程序读取自stdin
- 无法附上一个正在运行的进程
- 开始前必须要知道你索要调试的包
其在GitHub上的地址点此进入。
只要在程序执行之前加上环境变量GODEBUG gctrace =1 ,如:GODEBUG gctrace =1 ./xxxx.exe or GODEBUG gctrace =1 go run main.go
程序将会显示gc信息,如下
gc 1 @2.104s 0%: 0.018+1.3+0.076 ms clock, 0.054+0.35/1.0/3.0+0.23 ms cpu, 4->4->3 MB, 5 MB goal, 4 P
gc 2 @2.241s 0%: 0.019+2.4+0.077 ms clock, 0.079+0/2.4/6.4+0.30 ms cpu, 5->6->5 MB, 6 MB goal, 4 P
gc 3 @2.510s 0%: 0.011+3.2+0.063 ms clock, 0.047+0.10/2.9/9.0+0.25 ms cpu, 11->11->10 MB, 12 MB goal, 4 P
gc 4 @3.021s 0%: 0.013+6.6+0.076 ms clock, 0.053+0.34/6.2/18+0.30 ms cpu, 21->21->20 MB, 22 MB goal, 4 P
gc 5 @3.725s 0%: 0.015+15+0.079 ms clock, 0.062+0.35/15/45+0.31 ms cpu, 40->40->39 MB, 41 MB goal, 4 P
gc 6 @4.741s 0%: 0.008+35+0.17 ms clock, 0.035+0.19/35/100+0.70 ms cpu, 76->76->75 MB, 78 MB goal, 4 P
gc 7 @6.688s 0%: 0.020+117+0.34 ms clock, 0.082+11/117/330+1.3 ms cpu, 147->148->146 MB, 151 MB goal, 4 P
gc 8 @68.645s 0%: 0.019+146+0.30 ms clock, 0.078+0.006/146/407+1.2 ms cpu, 285->285->248 MB, 292 MB goal, 4 P
scvg0: inuse: 426, idle: 0, sys: 427, released: 0, consumed: 427 (MB)
gc 9 @175.448s 0%: 0.030+60+0.12 ms clock, 0.12+0.013/60/177+0.51 ms cpu, 484->484->248 MB, 496 MB goal, 4 P
gc 10 @236.621s 0%: 0.006+59+0.11 ms clock, 0.025+0/59/173+0.47 ms cpu, 484->484->248 MB, 496 MB goal, 4 P
gc 11 @285.967s 0%: 0.027+57+0.22 ms clock, 0.11+0/57/163+0.89 ms cpu, 484->484->248 MB, 496 MB goal, 4 P
scvg1: inuse: 331, idle: 175, sys: 507, released: 0, consumed: 507 (MB)
gc 12 @333.817s 0%: 0.009+52+0.18 ms clock, 0.036+0/52/155+0.72 ms cpu, 484->484->248 MB, 496 MB goal, 4 P
gc信息的含义
查看官方的runtime说明:
gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
error at each collection, summarizing the amount of memory collected and the
length of the pause. Setting gctrace=2 emits the same summary but also
repeats each collection. The format of this line is subject to change.
Currently, it is:
gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
where the fields are as follows:
gc # the GC number, incremented at each GC
@#s time in seconds since program start
#% percentage of time spent in GC since program start
#+...+# wall-clock/CPU times for the phases of the GC
#->#-># MB heap size at GC start, at GC end, and live heap
# MB goal goal heap size
# P number of processors used
The phases are stop-the-world (STW) sweep termination, concurrent
mark and scan, and STW mark termination. The CPU times
for mark/scan are broken down in to assist time (GC performed in
line with allocation), background GC time, and idle GC time.
If the line ends with "(forced)", this GC was forced by a
runtime.GC() call and all phases are STW.
Setting gctrace to any value > 0 also causes the garbage collector
to emit a summary when memory is released back to the system.
This process of returning memory to the system is called scavenging.
The format of this summary is subject to change.
Currently it is:
scvg#: # MB released printed only if non-zero
scvg#: inuse: # idle: # sys: # released: # consumed: # (MB)
where the fields are as follows:
scvg# the scavenge cycle number, incremented at each scavenge
inuse: # MB used or partially used spans
idle: # MB spans pending scavenging
sys: # MB mapped from the system
released: # MB released to the system
consumed: # MB allocated from the system
垃圾回收信息gc 1 @2.104s 0%: 0.018+1.3+0.076 ms clock, 0.054+0.35/1.0/3.0+0.23 ms cpu, 4->4->3 MB, 5 MB goal, 4 P。
1 表示第一次执行@2.104s 表示程序执行的总时间0% 垃圾回收时间占用的百分比,(不知道和谁比?难道是和上面的程序执行总时间,这样比较感觉没意义)0.018+1.3+0.076 ms clock 垃圾回收的时间,分别为STW(stop-the-world)清扫的时间, 并发标记和扫描的时间,STW标记的时间0.054+0.35/1.0/3.0+0.23 ms cpu 垃圾回收占用cpu时间4->4->3 MB 堆的大小,gc后堆的大小,存活堆的大小5 MB goal 整体堆的大小4 P 使用的处理器数量
系统内存回收信息,这个很直白,看单词就知道大概意思了scvg0: inuse: 426, idle: 0, sys: 427, released: 0, consumed: 427 (MB)
426 使用多少M内存0 剩下要清除的内存427 系统映射的内存0 释放的系统内存427 申请的系统内存
Go -- 中开启gctrace的更多相关文章
- 在C#中开启事务
1.为什么要开启事务: 举一个简单的例子:在银行业务中,有一条记账原则,即又借有贷.为了保证这种原则,每发生一笔银行业务,就必须保证会计账目上借方科目和贷方科目至少个少一笔,并且这两笔要么同时成功,要 ...
- 在Apache中开启虚拟主机
最近在自学LAMP,在Apache中尝试着开启虚拟主机的时候,遇到了挺多麻烦的,这里也顺便总结一下,在Apache中开启虚拟主机的时候,主要有下面几个步骤: 1.新建一个文件夹作为虚拟主机,用来存储网 ...
- 在虚拟中开启Windows 8.1的Hyper-V平台
VM安装windows8开启Hype-V 今天老魏用VM安装了Windows8.1系统,想用此系统来开发一下Windows Phone8,但是要求确实要开启Hyper-V平台技术,本来是没有任何的问题 ...
- SQLServer数据库中开启CDC导致“事务日志空间被占满,原因为REPLICATION”的原因分析和解决办法
本文出处:http://www.cnblogs.com/wy123/p/6646143.html SQLServer中开启CDC之后,在某些情况下会导致事务日志空间被占满的现象为:在执行增删改语句(产 ...
- Dynamics CRM2011 在Visual Studio中开启Javascript的Xrm.Page智能提示
前面一篇博文:http://blog.csdn.net/vic0228/article/details/49512699 讲到了在Visual Studio中开启xml编辑的智能提示,本篇接着来讲下如 ...
- 在pycharm_2018.2版本中开启Flask的debug的方法 (不要用命令:python **.py启动)
断点后,先ctl+c关闭控制台程序,再点击debuger调试 问题描述:在pycharm_2018.2版本中,我明确开启了debug,代码如下所示: from flask import Flask a ...
- MFC如何在有界面的应用程序中开启控制台窗口
在有界面的应用程序中开启控制台窗口有时候非常有用,尤其是在调试多线程应用程序中,由于通过断点的方式调试程序时会导致线程挂起从而导致各种难于预料的结果.这时候就可以通过开启控制台窗口往窗口输出信息来查看 ...
- 在Ubuntu中开启Soft AP功能
在Ubuntu中开启Soft AP功能 1.查看采用的无线网卡是否支持Soft AP: 注意,可以看到有AP字样,表明支持.楼主比较背,在易迅上挑了个销量最高的netcore nw360,结果无法搭建 ...
- 在 Windows 10 中开启移动 WLAN 热点
本文将介绍如何在 Windows 10 中开启移动 Wi-Fi 热点. This post is written in multiple languages. Please select yours: ...
随机推荐
- Perl学习三:运算符
运算符: 1.operand operator expression statement2.算术运算符 + - * / ** % -字符串参加运算必须转化为整数,若不能转化认为是0. 2.算术比较 & ...
- AtCoder Beginner Contest 098 D - Xor Sum 2
D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...
- ASP.NET(四):ASP.net实现假分页显示数据
导读:在做数据查询的时候,有的时候查询到的数据有很多.通常呢,我们一般都是去拖动右侧边的滚动条.但是,有了分页后,我们就可以不必是使用滚动条,而直接通过分页查看我们想要的数据.在分页的过程中,有分为真 ...
- 九度oj 题目1140:八皇后
题目描述: 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的 ...
- VM上完美运行macos
VM上完美运行macos 作者:方辰昱 时间:十月三号 效果图 简要步骤 下载安装VM 下载镜像文件链接,darwin.iso,unlocker,beamoff.合集下载链接:https://pan. ...
- BZOJ 3282 Tree ——Link-Cut Tree
[题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...
- 【2018.10.2】Note of CXM
1.有一张无向图,现在要给每个点染上黑色或白色,最后每个点的染色代价是它与离这个点最近的不同色节点的距离.求最小代价.所有边权$\geq 0$且互不相同. 分三种情况: 两点都染了色:两点都跟其它点算 ...
- Spoj-NETADMIN Smart Network Administrator
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- poj 3525 求凸包的最大内切圆
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3640 ...
- poj 2891 模线性方程组求解
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8005 ...