garbage.go

  1. gc 状态
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // gc状态集包含了最近的gc信息
    type GCStats struct {
    LastGC time.Time // 上次gc时间
    NumGC int64 // gc次数
    PauseTotal time.Duration // 所有gc的总暂停时间
    Pause []time.Duration // gc暂时时间历史记录,最近的在最前
    PauseEnd []time.Time // gc暂停结束时间记录,最近的在最前
    PauseQuantiles []time.Duration //
    }
  1. ReadGCStats(stats *GCStats)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // ReadGCStats reads statistics about garbage collection into stats.
    // The number of entries in the pause history is system-dependent;
    // stats.Pause slice will be reused if large enough, reallocated otherwise.
    // ReadGCStats may use the full capacity of the stats.Pause slice.
    // If stats.PauseQuantiles is non-empty, ReadGCStats fills it with quantiles
    // summarizing the distribution of pause time. For example, if
    // len(stats.PauseQuantiles) is 5, it will be filled with the minimum,
    // 25%, 50%, 75%, and maximum pause times.
    func ReadGCStats(stats *GCStats) {

gc会根据PauseQuantiles的长度填充暂停时间

  1. SetGCPercent(percent int) int

    设置gc的比率,默认100,消极gc

  2. FreeOSMemory()

    最大程度的释放系统内存

  3. SetMaxStack(bytes int) int

    设置单个goroutine的最大可用栈空间,64位默认1G,32位250M

  4. SetMaxThreads(threads int) int

    设置最大可用的操作系统级别线程(M)数量

  5. SetPanicOnFault(enabled bool) bool

    设置当前goroutine是否在运行错误的时候进行panic

  6. WriteHeapDump(fd uintptr)

    将堆以及堆对象的描述信息写进指定文件符.改操作会挂起所有线程直到全部写完.所以写入文件符对应的管道另一端不能是当前进程的,可以写入临时文件或者socket里

  7. SetTraceback(level string)

    设置Traceback的层级,”all”是全部

stack.go

  1. PrintStack()

    将runtime.Stack返回的stack track输出到标准错误

  2. Stack()

    分配一个足够大的缓存容纳调用 runtime.Stack的返回.返回当前线程的格式化的stack trace.

stubs.go

1
2
3
4
5
6
7
// Implemented in package runtime.
func readGCStats(*[]time.Duration)
func freeOSMemory()
func setMaxStack(int) int
func setGCPercent(int32) int32
func setPanicOnFault(bool) bool
func setMaxThreads(int) int

固有方法,在runtime里实现