GODEBUG:用于控制runtime调度变量的环境变量

Golang环境变量之GODEBUG


相关注释在 src/runtime/extern.go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
The GODEBUG variable controls debugging variables within the runtime.
It is a comma-separated list of name=val pairs setting these named variables:

allocfreetrace: setting allocfreetrace=1 causes every allocation to be
profiled and a stack trace printed on each object's allocation and free.

clobberfree: setting clobberfree=1 causes the garbage collector to
clobber the memory content of an object with bad content when it frees
the object.

cpu.*: cpu.all=off disables the use of all optional instruction set extensions.
cpu.extension=off disables use of instructions from the specified instruction set extension.
extension is the lower case name for the instruction set extension such as sse41 or avx
as listed in internal/cpu package. As an example cpu.avx=off disables runtime detection
and thereby use of AVX instructions.

cgocheck: setting cgocheck=0 disables all checks for packages
using cgo to incorrectly pass Go pointers to non-Go code.
Setting cgocheck=1 (the default) enables relatively cheap
checks that may miss some errors. A more complete, but slow,
cgocheck mode can be enabled using GOEXPERIMENT (which
requires a rebuild), see https://pkg.go.dev/internal/goexperiment for details.

dontfreezetheworld: by default, the start of a fatal panic or throw
"freezes the world", preempting all threads to stop all running
goroutines, which makes it possible to traceback all goroutines, and
keeps their state close to the point of panic. Setting
dontfreezetheworld=1 disables this preemption, allowing goroutines to
continue executing during panic processing. Note that goroutines that
naturally enter the scheduler will still stop. This can be useful when
debugging the runtime scheduler, as freezetheworld perturbs scheduler
state and thus may hide problems.

efence: setting efence=1 causes the allocator to run in a mode
where each object is allocated on a unique page and addresses are
never recycled.

gccheckmark: setting gccheckmark=1 enables verification of the
garbage collector's concurrent mark phase by performing a
second mark pass while the world is stopped. If the second
pass finds a reachable object that was not found by concurrent
mark, the garbage collector will panic.

gcpacertrace: setting gcpacertrace=1 causes the garbage collector to
print information about the internal state of the concurrent pacer.

gcshrinkstackoff: setting gcshrinkstackoff=1 disables moving goroutines
onto smaller stacks. In this mode, a goroutine's stack can only grow.

gcstoptheworld: setting gcstoptheworld=1 disables concurrent garbage collection,
making every garbage collection a stop-the-world event. Setting gcstoptheworld=2
also disables concurrent sweeping after the garbage collection finishes.

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. The format of this line is subject to change.
Currently, it is:
gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # MB stacks, #MB globals, # 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
# MB stacks estimated scannable stack size
# MB globals scannable global 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.

harddecommit: setting harddecommit=1 causes memory that is returned to the OS to
also have protections removed on it. This is the only mode of operation on Windows,
but is helpful in debugging scavenger-related issues on other platforms. Currently,
only supported on Linux.

inittrace: setting inittrace=1 causes the runtime to emit a single line to standard
error for each package with init work, summarizing the execution time and memory
allocation. No information is printed for inits executed as part of plugin loading
and for packages without both user defined and compiler generated init work.
The format of this line is subject to change. Currently, it is:
init # @#ms, # ms clock, # bytes, # allocs
where the fields are as follows:
init # the package name
@# ms time in milliseconds when the init started since program start
# clock wall-clock time for package initialization work
# bytes memory allocated on the heap
# allocs number of heap allocations

madvdontneed: setting madvdontneed=0 will use MADV_FREE
instead of MADV_DONTNEED on Linux when returning memory to the
kernel. This is more efficient, but means RSS numbers will
drop only when the OS is under memory pressure. On the BSDs and
Illumos/Solaris, setting madvdontneed=1 will use MADV_DONTNEED instead
of MADV_FREE. This is less efficient, but causes RSS numbers to drop
more quickly.

memprofilerate: setting memprofilerate=X will update the value of runtime.MemProfileRate.
When set to 0 memory profiling is disabled. Refer to the description of
MemProfileRate for the default value.

pagetrace: setting pagetrace=/path/to/file will write out a trace of page events
that can be viewed, analyzed, and visualized using the x/debug/cmd/pagetrace tool.
Build your program with GOEXPERIMENT=pagetrace to enable this functionality. Do not
enable this functionality if your program is a setuid binary as it introduces a security
risk in that scenario. Currently not supported on Windows, plan9 or js/wasm. Setting this
option for some applications can produce large traces, so use with care.

invalidptr: invalidptr=1 (the default) causes the garbage collector and stack
copier to crash the program if an invalid pointer value (for example, 1)
is found in a pointer-typed location. Setting invalidptr=0 disables this check.
This should only be used as a temporary workaround to diagnose buggy code.
The real fix is to not store integers in pointer-typed locations.

sbrk: setting sbrk=1 replaces the memory allocator and garbage collector
with a trivial allocator that obtains memory from the operating system and
never reclaims any memory.

scavtrace: setting scavtrace=1 causes the runtime to emit a single line to standard
error, roughly once per GC cycle, summarizing the amount of work done by the
scavenger as well as the total amount of memory returned to the operating system
and an estimate of physical memory utilization. The format of this line is subject
to change, but currently it is:
scav # KiB work (bg), # KiB work (eager), # KiB total, #% util
where the fields are as follows:
# KiB work (bg) the amount of memory returned to the OS in the background since
the last line
# KiB work (eager) the amount of memory returned to the OS eagerly since the last line
# KiB now the amount of address space currently returned to the OS
#% util the fraction of all unscavenged heap memory which is in-use
If the line ends with "(forced)", then scavenging was forced by a
debug.FreeOSMemory() call.

scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
detailed multiline info every X milliseconds, describing state of the scheduler,
processors, threads and goroutines.

schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
error every X milliseconds, summarizing the scheduler state.

tracebackancestors: setting tracebackancestors=N extends tracebacks with the stacks at
which goroutines were created, where N limits the number of ancestor goroutines to
report. This also extends the information returned by runtime.Stack. Ancestor's goroutine
IDs will refer to the ID of the goroutine at the time of creation; it's possible for this
ID to be reused for another goroutine. Setting N to 0 will report no ancestry information.

tracefpunwindoff: setting tracefpunwindoff=1 forces the execution tracer to
use the runtime's default stack unwinder instead of frame pointer unwinding.
This increases tracer overhead, but could be helpful as a workaround or for
debugging unexpected regressions caused by frame pointer unwinding.

asyncpreemptoff: asyncpreemptoff=1 disables signal-based
asynchronous goroutine preemption. This makes some loops
non-preemptible for long periods, which may delay GC and
goroutine scheduling. This is useful for debugging GC issues
because it also disables the conservative stack scanning used
for asynchronously preempted goroutines.

The net and net/http packages also refer to debugging variables in GODEBUG.
See the documentation for those packages for details.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
GODEBUG 变量控制运行时内的调试变量。
它是一个以逗号分隔的 name=val 对列表,设置这些命名变量:

allocfreetrace:设置 allocfreetrace=1 会导致每次分配
分析和堆栈跟踪打印在每个对象的分配和免费上。

clobberfree:设置 clobberfree=1 会导致垃圾收集器
释放时破坏内容不良的对象的内存内容
物体。

cpu.*: cpu.all=off 禁用所有可选指令集扩展。
cpu.extension=off 禁止使用来自指定指令集扩展的指令。
extension 是指令集扩展的小写名称,例如 sse41 或 avx
如 internal/cpu 包中所列。 例如 cpu.avx=off 禁用运行时检测
从而使用 AVX 指令。

cgocheck:设置 cgocheck=0 禁用对包的所有检查
使用 cgo 错误地将 Go 指针传递给非 Go 代码。
设置 cgocheck=1(默认值)使相对便宜
检查可能会遗漏一些错误。 一个更完整但速度较慢的
可以使用 GOEXPERIMENT 启用 cgocheck 模式(
需要重建),请参阅 https://pkg.go.dev/internal/goexperiment 了解详细信息。

dontfreezetheworld:默认情况下,致命恐慌或抛出的开始
“冻结世界”,抢占所有线程停止所有运行
goroutines,这使得追溯所有 goroutines 成为可能,以及
使他们的状态接近恐慌点。 环境
dontfreezetheworld=1 禁用此抢占,允许 goroutines
在恐慌处理期间继续执行。 请注意,goroutines
自然进入调度器还是会停止。 这在以下情况下很有用
调试运行时调度程序,因为 freezetheworld 扰乱了调度程序
状态,因此可能隐藏问题。

efence:设置 efence=1 会导致分配器以一种模式运行
每个对象都分配在一个唯一的页面上,地址是
从未回收。

gccheckmark:设置 gccheckmark=1 启用验证
垃圾收集器的并发标记阶段通过执行
当世界停止时,第二个标记通过。 如果第二个
pass 找到并发找不到的可达对象
mark,垃圾收集器会 panic

gcpacertrace:设置 gcpacertrace=1 会导致垃圾收集器
打印有关并发定速器的内部状态的信息。

gcshrinkstackoff:设置 gcshrinkstackoff=1 禁用移动 goroutines
到较小的堆栈上。 在这种模式下,goroutine 的堆栈只能增长。

gcstoptheworld:设置 gcstoptheworld=1 禁用并发垃圾收集,
使每个垃圾收集成为一个停止世界的事件。 设置 gcstoptheworld=2
还会在垃圾收集完成后禁用并发清除。

gctrace:设置 gctrace=1 会导致垃圾收集器向标准发出单行
每次收集的错误,总结收集的内存量和
暂停的长度。 此行的格式可能会更改。
目前,它是:
gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # MB stacks, #MB globals, # P
其中字段如下:
gc # GC 编号,在每次 GC 时递增
@#s 程序启动后的秒数
#% 自程序启动以来花费在 GC 上的时间百分比
#+...+# GC 阶段的挂钟/CPU 时间
#->#-># GC 开始时、GC 结束时和活动堆的 MB 堆大小
# MB 目标堆大小
# MB 堆栈估计可扫描堆栈大小
# MB globals 可扫描的全局大小
# P 使用的处理器数量
这些阶段是停止世界(STW)扫描终止,并发
标记和扫描,以及 STW 标记终止。 CPU 时间
for mark/scan 被分解为辅助时间(GC performed in
符合分配)、后台 GC 时间和空闲 GC 时间。
如果该行以“(forced)”结尾,则此 GC 是由
runtime.GC() 调用。

harddecommit:设置 harddecommit=1 会导致返回给操作系统的内存
也取消了保护。 这是 Windows 上唯一的操作模式,
但有助于在其他平台上调试与清道夫相关的问题。 现在,
仅在 Linux 上受支持。

inittrace:设置 inittrace=1 会导致运行时向标准发出一行
每个带有 init work 的包的错误,总结了执行时间和内存
分配。 作为插件加载的一部分执行的初始化没有打印信息
对于没有用户定义和编译器生成的初始化工作的包。
此行的格式可能会更改。 目前,它是:
init # @#ms, # ms clock, # bytes, # allocs
其中字段如下:
init #包名
@# ms 程序启动后 init 启动的时间(以毫秒为单位)
# 包初始化工作的时钟挂钟时间
# 在堆上分配的字节内存
# allocs 堆分配的数量

madvdontneed:设置 madvdontneed=0 将使用 MADV_FREE在将内存返回给
核心。 这样效率更高,但意味着 RSS 数字将
仅当操作系统处于内存压力下时才下降。 在 BSD 上和
Illumos/Solaris,设置 madvdontneed=1 将使用 MADV_DONTNEED 代替
MADV_FREE。 这效率较低,但会导致 RSS 数量下降
更快速。

memprofilerate:设置 memprofilerate=X 将更新 runtime.MemProfileRate 的值。
当设置为 0 时,内存分析被禁用。 参考说明
MemProfileRate 为默认值。

pagetrace:设置 pagetrace=/path/to/file 将写出页面事件的跟踪
可以使用 x/debug/cmd/pagetrace 工具查看、分析和可视化。
使用 GOEXPERIMENT=pagetrace 构建您的程序以启用此功能。 不要
如果您的程序是 setuid 二进制文件,请启用此功能,因为它引入了安全性
在这种情况下的风险。 目前在 Windows、plan9 或 js/wasm 上不受支持。 设置这个
某些应用程序的选项会产生较大的痕迹,因此请小心使用。

invalidptr:invalidptr=1(默认值)导致垃圾收集器和堆栈
如果指针值无效(例如 1),复制器会使程序崩溃
位于指针类型的位置。 设置 invalidptr=0 禁用此检查。
这应该只用作诊断错误代码的临时解决方法。
真正的解决方法是不要将整数存储在指针类型的位置。

sbrk:设置 sbrk=1 替换内存分配器和垃圾收集器
使用从操作系统获取内存的简单分配器,以及
从不回收任何内存。

scavtrace:设置 scavtrace=1 会导致运行时向标准发出单行
错误,大约每个 GC 周期一次,总结由
清道夫以及返回给操作系统的内存总量
以及物理内存利用率的估计。 这一行的格式是主题
改变,但目前是:
scav # KiB work (bg), # KiB work (eager), # KiB total, #% util
其中字段如下:
# KiB work (bg) 自从后台返回给操作系统的内存量
最后一行
# KiB work (eager) 自上一行以来急切返回给操作系统的内存量
# KiB 现在返回给操作系统的地址空间量
#% util 所有未清理的堆内存中正在使用的部分
如果该行以“(强制)”结尾,则清除是由
debug.FreeOSMemory() 调用。

scheddetail:设置 schedtrace=X 和 scheddetail=1 会导致调度程序发出
每 X 毫秒的详细多行信息,描述调度程序的状态,
处理器、线程和协程。

schedtrace:设置 schedtrace=X 会导致调度程序向标准发出单行
每 X 毫秒出错一次,总结调度程序状态。

tracebackancestors:设置 tracebackancestors=N 使用堆栈扩展回溯
创建了哪些 goroutines,其中 N 将祖先 goroutines 的数量限制为
报告。 这也扩展了 runtime.Stack 返回的信息。 祖先的协程
IDs指的是goroutine创建时的ID; 这有可能
被另一个 goroutine 重用的 ID。 将 N 设置为 0 将不报告任何祖先信息。

tracefpunwindoff:设置 tracefpunwindoff=1 强制执行跟踪器
使用运行时的默认堆栈展开器而不是帧指针展开。
这会增加跟踪器的开销,但作为解决方法或用于
调试由帧指针展开引起的意外回归。

asyncpreemptoff: asyncpreemptoff=1 禁用基于信号的
异步 goroutine 抢占。 这使得一些循环
长时间不可抢占,这可能会延迟 GC 和
goroutine 调度。 这对于调试 GC 问题很有用
因为它还禁用了使用的保守堆栈扫描
对于异步抢占的 goroutines。

net 和 net/http 包也引用 GODEBUG 中的调试变量。
有关详细信息,请参阅这些软件包的文档。

go的allocfreetrace有什么作用?


allocfreetrace 是 Go 语言中的运行时调试标志之一。当将其设置为非零值时,Go 运行时系统会跟踪内存分配和释放的堆栈跟踪信息。这个标志主要用于调试和性能分析的目的。

allocfreetrace 启用时,每次进行内存分配和释放操作时,Go 运行时系统会记录分配和释放发生的堆栈跟踪信息。这些信息包括函数调用序列和对应的源代码文件和行号。通过这些信息,可以追踪到哪些部分代码进行了频繁的内存分配和释放,以及这些操作的上下文。

allocfreetrace 主要有以下作用:

  1. 内存分配和释放跟踪:通过分析堆栈跟踪信息,可以了解应用程序中哪些地方进行了频繁的内存分配和释放,从而帮助定位潜在的性能问题和内存泄漏。

  2. 性能分析:通过分析 allocfreetrace 记录的堆栈跟踪信息,可以识别出在哪些代码路径上发生了大量的内存分配和释放操作,从而找到性能瓶颈,进行优化。

需要注意的是,由于 allocfreetrace 会产生额外的开销和信息记录,因此在生产环境中不建议长期启用该标志。它更适用于开发和调试阶段,以帮助开发人员进行性能分析和调试工作。


go的gccheckmark有什么作用?


在 Go 语言中,gccheckmark 是一个与垃圾回收(Garbage Collection)相关的调试标志。当将其设置为非零值时,Go 运行时系统会在垃圾回收过程中进行额外的检查和标记操作,以帮助调试和发现潜在的垃圾回收问题。

具体而言,gccheckmark 的作用包括:

  1. 标记检查:垃圾回收过程中会额外检查标记的正确性。它可以帮助发现标记过程中可能存在的错误或漏标问题。

  2. 标记重复检查:检查是否有重复标记的对象,即对象被多次标记,以避免对同一对象重复进行垃圾回收操作。

  3. 跟踪标记工作量:记录垃圾回收器进行标记的工作量和进度,用于性能分析和优化。

需要注意的是,由于 gccheckmark 增加了垃圾回收器的开销,因此在正式生产环境中不建议长期启用该标志。它主要用于调试和性能分析的目的,以帮助开发人员检查和改进垃圾回收器的行为。


go的gcpacertrace有什么作用?


在 Go 语言中,gcpacertrace 是一个与垃圾回收(Garbage Collection)相关的调试标志。当将其设置为非零值时,Go 运行时系统会在垃圾回收过程中提供额外的跟踪信息,以帮助调试和性能分析。

具体而言,gcpacertrace 的作用包括:

  1. 跟踪 Pacer 行为:垃圾回收器使用 Pacer 来确定内存分配速率和垃圾回收速率。启用 gcpacertrace 可以跟踪 Pacer 的行为,包括调整内存分配速率和触发垃圾回收的时机等。

  2. 性能分析:通过记录 Pacer 的相关信息,可以分析垃圾回收过程中内存分配的速率和垃圾回收的触发情况,帮助定位性能瓶颈并进行优化。

需要注意的是,gcpacertrace 增加了垃圾回收器的开销和输出信息量,因此在正式生产环境中不建议长期启用该标志。它主要用于调试和性能分析的目的,以帮助开发人员理解垃圾回收器的行为和优化垃圾回收性能。