runtime
GOGCGODEBUGGOMAXPROCSGORACEGOTRACEBACK
GOROOTGOPATHGOOSGOARCHGOSSAFUNC
src/runtime/extern.goenvs
1
2
3
4
5
6
7
8
9
10
11
12
13
// The bootstrap sequence is:
//
// call osinit
// call schedinit
// make & queue new G
// call runtime·mstart
//
// The new G calls runtime·main.
func schedinit() {
...
goenvs()
...
}
GOGC
GOGC100100%GCGOGC=offGCdebug.SetGCPercent
GODEBUG
GODEBUG,
GODEBUG=‘gctrace=1,inittrace=1’ go run main.go
在调度系统初始化的时候,首先解析环境变量,然后解析调试参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// The bootstrap sequence is:
//
// call osinit
// call schedinit
// make & queue new G
// call runtime·mstart
//
// The new G calls runtime·main.
func schedinit() {
...
goenvs()
parsedebugvars()
...
}
dbgvars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// src/runtime/runtime1.go
var dbgvars = []dbgVar{
{"allocfreetrace", &debug.allocfreetrace},
{"clobberfree", &debug.clobberfree},
{"cgocheck", &debug.cgocheck},
{"efence", &debug.efence},
{"gccheckmark", &debug.gccheckmark},
{"gcpacertrace", &debug.gcpacertrace},
{"gcshrinkstackoff", &debug.gcshrinkstackoff},
{"gcstoptheworld", &debug.gcstoptheworld},
{"gctrace", &debug.gctrace},
{"invalidptr", &debug.invalidptr},
{"madvdontneed", &debug.madvdontneed},
{"sbrk", &debug.sbrk},
{"scavtrace", &debug.scavtrace},
{"scheddetail", &debug.scheddetail},
{"schedtrace", &debug.schedtrace},
{"tracebackancestors", &debug.tracebackancestors},
{"asyncpreemptoff", &debug.asyncpreemptoff},
{"inittrace", &debug.inittrace},
}
allocfreetrace
allocfreetrace=1
clobberfree
clobberfree=1
cgocheck
cgocheck=0cgocheck=1cgocheck=2
efence
efence=1
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
gcpacertrace=1
gcshrinkstackoff
gcshrinkstackoff=1goroutinegoroutine
gcstoptheworld
gcstoptheworld=1GCSTWgcstoptheworld=2
gctrace
gctrace=1GC
gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
每个字段的解释如下:
gc@#s#%GCGC#+#+# ms clockGC#+#/#/#+# ms cpu#->#-># MBGC# MB goalGC# PP
GC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import (
"fmt"
"time"
)
var data []byte
func main() {
data = make([]byte, 0, 100)
fmt.Println(data)
time.Sleep(3 * time.Minute)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@b89af2baca14:/WORKDIR/gostudy/hello# GODEBUG='gctrace=1' go run main.go
gc 1 @0.015s 1%: 0.074+2.1+0.041 ms clock, 0.29+0.14/0.13/0+0.16 ms cpu, 4->5->1 MB, 5 MB goal, 4 P
gc 2 @0.024s 1%: 0.012+1.0+0.002 ms clock, 0.050+0.10/0.13/0.24+0.010 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 3 @0.033s 1%: 0.017+1.8+0.004 ms clock, 0.070+0.29/0/0+0.018 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 4 @0.041s 1%: 0.022+0.44+0.001 ms clock, 0.088+0.10/0.25/0.007+0.007 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 5 @0.049s 1%: 0.023+2.1+0.001 ms clock, 0.093+0.037/0.76/0.036+0.006 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 6 @0.055s 1%: 0.017+0.53+0.28 ms clock, 0.068+0.060/0.29/0.37+1.1 ms cpu, 4->4->1 MB, 5 MB goal, 4 P
gc 7 @0.058s 2%: 0.12+0.38+0.001 ms clock, 0.49+0.10/0.24/0.17+0.004 ms cpu, 4->4->1 MB, 5 MB goal, 4 P
gc 8 @0.060s 2%: 0.044+0.52+0.001 ms clock, 0.17+0.096/0.28/0.090+0.004 ms cpu, 4->4->1 MB, 5 MB goal, 4 P
gc 9 @0.065s 2%: 0.074+0.29+0.002 ms clock, 0.29+0.096/0.16/0.23+0.009 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
# command-line-arguments
gc 1 @0.005s 2%: 0.007+0.86+0.010 ms clock, 0.030+0.068/0.42/0.88+0.043 ms cpu, 4->5->4 MB, 5 MB goal, 4 P
# command-line-arguments
gc 1 @0.001s 17%: 0.14+0.68+0.11 ms clock, 0.57+0.044/0.49/0.35+0.45 ms cpu, 4->5->5 MB, 5 MB goal, 4 P
gc 2 @0.003s 11%: 0.002+1.5+0.094 ms clock, 0.011+0.068/0.65/0.75+0.37 ms cpu, 9->9->8 MB, 10 MB goal, 4 P
[]
GC forced
gc 10 @120.397s 0%: 0.028+0.49+0.002 ms clock, 0.11+0/0.29/0.27+0.009 ms cpu, 3->3->0 MB, 4 MB goal, 4 P
root@b89af2baca14:/WORKDIR/gostudy/hello#
inittrace
inittrace=1runtimepackage
init # @#ms, # ms clock, # bytes, # allocs
每个字段的意义如下:
init #@# ms# clockbytesallocs
madvdontneed
madvdontneed=0MADV_FREEMADV_DONTNEEDRSS
memprofilerate
memprofilerate=Xruntime.MemProfileRate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// MemProfileRate controls the fraction of memory allocations
// that are recorded and reported in the memory profile.
// The profiler aims to sample an average of
// one allocation per MemProfileRate bytes allocated.
//
// To include every allocated block in the profile, set MemProfileRate to 1.
// To turn off profiling entirely, set MemProfileRate to 0.
//
// The tools that process the memory profiles assume that the
// profile rate is constant across the lifetime of the program
// and equal to the current value. Programs that change the
// memory profiling rate should do so just once, as early as
// possible in the execution of the program (for example,
// at the beginning of main).
var MemProfileRate int = defaultMemProfileRate(512 * 1024)
invalidptr
invalidptr=1nvalidptr=0
sbrk
sbrk=1
scavtrace
scavtrace=1GC
scav # # KiB work, # KiB total, #% util
scav #KiB workKiB total#% util
(forced)debug.FreeOSMemory()
scheddetail
schedtrace=Xscheddetail=1X ms
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.
asyncpreemptoff
asyncpreemptoff=1GCgoroutineGCgoroutine
GOMAXPROCS
GOMAXPROCSGOMAXPROCS
GORACE
-race
GOTRACEBACK
GOTRACEBACKGogoroutine2goroutine
GOTRACEBACK=nonegoroutineGOTRACEBACK=single (默认)GOTRACEBACK=allgoroutineGOTRACEBACK=systemallgorotuineGOTRACEBACK=crashsystemUnixSIGABRT