Files
uTools-Manuals/src/docs/go/runtime.html
2020-06-28 23:41:19 +08:00

270 lines
31 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="container">
<h1 id="pkg-overview">package runtime</h1>
<p><code>import "runtime"</code>
</p><p>runtime包提供和go运行时环境的互操作如控制go程的函数。它也包括用于reflect包的低层次类型信息参见reflect报的文档获取运行时类型系统的可编程接口。</p>
<h3 id="hdr-Environment_Variables">Environment Variables </h3>
<p align="left">下面的环境变量($name或%name%这依赖于主机的操作系统控制go程序的运行时行为。它们的含义和用法可能在各发行版之间改变。</p>
<p align="left">环境变量GOGC设置最初的垃圾收集目标百分比。当新申请的数据和前次垃圾收集剩下的存活数据的比率达到该百分比时就会触发垃圾收集。默认GOGC=100。设置GOGC=off 会完全关闭垃圾收集。runtime/debug包的SetGCPercent函数允许在运行时修改该百分比。参见<a href="http://golang.org/pkg/runtime/debug/#SetGCPercent">http://golang.org/pkg/runtime/debug/#SetGCPercent</a></p>
<p align="left">环境变量GODEBUG控制运行时的debug输出。GODEBUG的值是逗号分隔的name=val对。支持的name如下</p>
<pre>allocfreetrace: 设置其为1会导致每次分配都会被记录剖面会记录每一个对象的分配、释放
及其堆栈踪迹。
efence: 设置其为1会导致分配器运行模式为每个对象申请在独立的页和地址且永不循环利用。
gctrace: 设置其为1会导致垃圾收集器每次收集都向标准错误输出写入单行的数据概述收集的总
内存的大小和暂停的总时间长度。设置其为2会写入同样的概述但也会写入每次收集的两个数据。
gcdead: 设置其为1会导致垃圾收集器摧毁任何它认为已经死掉的执行堆栈。
schedtrace: 设置其为X会导致调度程序每隔X毫秒输出单行信息到标准错误输出概述调度状态。
scheddetail: 设置schedtrace为X并设置其为1会导致调度程序每隔X毫秒输出详细的多行信息
描述调度、进程、线程和go程的状态。</pre>
<p align="left">环境变量GOMAXPROCS限制可以同时运行用户层次的go代码的操作系统进程数。没有对代表go代码的、可以在系统调用中阻塞的go程数的限制那些阻塞的go程不与GOMAXPROCS限制冲突。本包的GOMAXPROCS函数可以查询和修改该限制。</p>
<p align="left">环境变量GOTRACEBACK控制当go程序因为不能恢复的panic或不期望的运行时情况失败时的输出。失败的程序默认会打印所有现存go程的堆栈踪迹省略运行时系统中的函数然后以状态码2退出。如果GOTRACEBACK为0会完全忽略所有go程的堆栈踪迹。如果GOTRACEBACK为1会采用默认行为。如果GOTRACEBACK为2会打印所有现存go程包括运行时函数的堆栈踪迹。如果GOTRACEBACK为crash会打印所有现存go程包括运行时函数的堆栈踪迹并且如果可能会采用操作系统特定的方式崩溃而不是退出。例如在Unix系统里程序会释放SIGABRT信号以触发核心信息转储。</p>
<p align="left">环境变量GOARCH、GOOS、GOPATH和GOROOT构成完整的go环境变量集合。它们影响go程序的构建参见<a href="http://golang.org/cmd/go">http://golang.org/cmd/go</a> and<a href="http://golang.org/pkg/go/build">http://golang.org/pkg/go/build</a>)。</p>
<p align="left">GOARCH、GOOS和GOROOT在编译时被记录并可用本包的常量和函数获取但它们不会影响运行时环境。</p>
<h2 id="pkg-constants">Constants </h2>
<pre>const <span id="Compiler">Compiler</span> = "gc"</pre>
<p>Compiler是编译工具链的名字工具链会构建可执行的二进制文件。已知的工具链是</p>
<pre>gc The 5g/6g/8g compiler suite at code.google.com/p/go.
gccgo The gccgo front end, part of the GCC compiler suite.</pre>
<pre>const <span id="GOARCH">GOARCH</span> <a href="builtin.htm#string">string</a> = theGoarch</pre>
<p>GOARCH是可执行程序的目标处理器架构将要在该架构的机器上执行386、amd64或arm。</p>
<pre>const <span id="GOOS">GOOS</span> <a href="builtin.htm#string">string</a> = theGoos</pre>
<p>GOOS是可执行程序的目标操作系统将要在该操作系统的机器上执行darwin、freebsd、linux等。</p>
<h2 id="pkg-variables">Variables </h2>
<pre>var <span id="MemProfileRate">MemProfileRate</span> <a href="builtin.htm#int">int</a> = 512 * 1024</pre>
<p align="left">MemProfileRate控制会在内存profile里记录和报告的内存分配采样频率。内存profile记录器平均每分配MemProfileRate字节进行一次分配采样。</p>
<p align="left">要在profile里包含每一个申请的块可以将MemProfileRate设为1。要完全关闭profile的记录设置本变量为0。</p>
<p align="left">处理内存profile的工具假设profile记录速度在整个程序的生命期是固定的并等于当前值。修改内存profile的程序应该只进行一次且尽可能早的修改例如在main函数的开始处</p>
<h2 id="Error">type <a href="https://github.com/golang/go/blob/master/src/runtime/error.go?name=release#8" title="View Source">Error</a> </h2>
<pre>type Error interface {
<a href="builtin.htm#error">error</a>
<span class="com">// RuntimeError是一个无操作的函数仅用于区别运行时错误和普通错误。</span>
<span class="com">// 具有RuntimeError方法的错误类型就是运行时错误类型。</span>
<span id="Error.RuntimeError">RuntimeError</span>()
}</pre>
<p>Error接口用来识别运行时错误。</p>
<h2 id="TypeAssertionError">type <a href="https://github.com/golang/go/blob/master/src/runtime/error.go?name=release#19" title="View Source">TypeAssertionError</a> </h2>
<pre>type TypeAssertionError struct {
<span class="com">// 内含隐藏或非导出字段</span>
}</pre>
<p>TypeAssertionError表示一次失败的类型断言。</p>
<h3 id="TypeAssertionError.Error">func (*TypeAssertionError) <a href="https://github.com/golang/go/blob/master/src/runtime/error.go?name=release#28" title="View Source">Error</a> </h3>
<pre class="funcdecl">func (e *<a href="#TypeAssertionError">TypeAssertionError</a>) Error() <a href="builtin.htm#string">string</a></pre>
<h3 id="TypeAssertionError.RuntimeError">func (*TypeAssertionError) <a href="https://github.com/golang/go/blob/master/src/runtime/error.go?name=release#26" title="View Source">RuntimeError</a> </h3>
<pre class="funcdecl">func (*<a href="#TypeAssertionError">TypeAssertionError</a>) RuntimeError()</pre>
<h2 id="GOROOT">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#184" title="View Source">GOROOT</a> </h2>
<pre class="funcdecl">func GOROOT() <a href="builtin.htm#string">string</a></pre>
<p>GOROOT返回Go的根目录。如果存在GOROOT环境变量返回该变量的值否则返回创建Go时的根目录。</p>
<h2 id="Version">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#195" title="View Source">Version</a> </h2>
<pre class="funcdecl">func Version() <a href="builtin.htm#string">string</a></pre>
<p>返回Go的版本字符串。它要么是递交的hash和创建时的日期要么是发行标签如"go1.3"。</p>
<h2 id="NumCPU">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#27" title="View Source">NumCPU</a> </h2>
<pre class="funcdecl">func NumCPU() <a href="builtin.htm#int">int</a></pre>
<p>NumCPU返回本地机器的逻辑CPU个数。</p>
<h2 id="GOMAXPROCS">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#24" title="View Source">GOMAXPROCS</a> </h2>
<pre class="funcdecl">func GOMAXPROCS(n <a href="builtin.htm#int">int</a>) <a href="builtin.htm#int">int</a></pre>
<p>GOMAXPROCS设置可同时执行的最大CPU数并返回先前的设置。  n &lt; 1它就不会更改当前设置。本地机器的逻辑CPU数可通过 NumCPU 查询。本函数在调度程序优化后会去掉。</p>
<h2 id="SetCPUProfileRate">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#141" title="View Source">SetCPUProfileRate</a> </h2>
<pre class="funcdecl">func SetCPUProfileRate(hz <a href="builtin.htm#int">int</a>)</pre>
<p align="left">SetCPUProfileRate设置CPU profile记录的速率为平均每秒hz次。如果hz&lt;=0SetCPUProfileRate会关闭profile的记录。如果记录器在执行该速率必须在关闭之后才能修改。</p>
<p align="left">绝大多数使用者应使用runtime/pprof包或testing包的-test.cpuprofile选项而非直接使用SetCPUProfileRate。</p>
<h2 id="CPUProfile">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#132" title="View Source">CPUProfile</a> </h2>
<pre class="funcdecl">func CPUProfile() []<a href="builtin.htm#byte">byte</a></pre>
<p align="left">CPUProfile返回二进制CPU profile堆栈跟踪数据的下一个chunk函数会阻塞直到该数据可用。 如果profile的记录被关闭且在记录器开着的时候积累的profile数据都被返回了CPUProfile会返回nil。调用者在再次调用本函数之前应先保存返回的数据。</p>
<p align="left">绝大多数使用者应使用 runtime/pprof包或testing包的-test.cpuprofile选项而非直接使用 CPUProfile。</p>
<h2 id="GC">func <a href="https://github.com/golang/go/blob/master/src/runtime/mem.go?name=release#75" title="View Source">GC</a> </h2>
<pre class="funcdecl">func GC()</pre>
<p>GC执行一次垃圾回收。</p>
<h2 id="SetFinalizer">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#177" title="View Source">SetFinalizer</a> </h2>
<pre class="funcdecl">func SetFinalizer(x, f interface{})</pre>
<p align="left">SetFinalizer将x的终止器设置为f。当垃圾收集器发现一个不能接触的即引用计数为零程序中不能再直接或间接访问该对象具有终止器的块时它会清理该关联对象到终止器并在独立go程调用f(x)。这使x再次可以接触但没有了绑定的终止器。如果SetFinalizer没有被再次调用下一次垃圾收集器将视x为不可接触的并释放x。</p>
<p align="left">SetFinalizer(x, nil)会清理任何绑定到x的终止器。</p>
<p align="left">参数x必须是一个指向通过new申请的对象的指针或者通过对复合字面值取址得到的指针。参数f必须是一个函数它接受单个可以直接用x类型值赋值的参数也可以有任意个被忽略的返回值。如果这两条任一条不被满足本函数就会中断程序。</p>
<p align="left">终止器会按依赖顺序执行如果A指向B两者都有终止器且它们无法从其它方面接触只有A的终止器执行A被释放后B的终止器就可以执行。如果一个循环结构包含一个具有终止器的块该循环不能保证会被当垃圾收集终止器也不能保证会执行因为没有尊重依赖关系的顺序。</p>
<p align="left">x的终止器会在x变为不可接触之后的任意时间被调度执行。不保证终止器会在程序退出前执行因此一般终止器只用于在长期运行的程序中释放关联到某对象的非内存资源。例如当一个程序丢弃一个os.File对象时没有调用其Close方法该os.File对象可以使用终止器去关闭对应的操作系统文件描述符。但依靠终止器去刷新内存中的I/O缓冲如bufio.Writer是错误的因为缓冲不会在程序退出时被刷新。</p>
<p align="left">如果*x的大小为0字节不保证终止器会执行。</p>
<p align="left">一个程序会有单独一个go程顺序执行所有的终止器。如果一个终止器必须运行较长时间它应该在内部另开go程执行该任务。</p>
<h2 id="MemStats">type <a href="https://github.com/golang/go/blob/master/src/runtime/mem.go?name=release#13" title="View Source">MemStats</a> </h2>
<pre>type MemStats struct {
<span class="com">// 一般统计</span>
<span id="MemStats.Alloc">Alloc</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 已申请且仍在使用的字节数</span>
<span id="MemStats.TotalAlloc">TotalAlloc</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 已申请的总字节数(已释放的部分也算在内)</span>
<span id="MemStats.Sys">Sys</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 从系统中获取的字节数下面XxxSys之和</span>
<span id="MemStats.Lookups">Lookups</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 指针查找的次数</span>
<span id="MemStats.Mallocs">Mallocs</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 申请内存的次数</span>
<span id="MemStats.Frees">Frees</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 释放内存的次数</span>
<span class="com">// 主分配堆统计</span>
<span id="MemStats.HeapAlloc">HeapAlloc</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 已申请且仍在使用的字节数</span>
<span id="MemStats.HeapSys">HeapSys</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 从系统中获取的字节数</span>
<span id="MemStats.HeapIdle">HeapIdle</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 闲置span中的字节数</span>
<span id="MemStats.HeapInuse">HeapInuse</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 非闲置span中的字节数</span>
<span id="MemStats.HeapReleased">HeapReleased</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 释放到系统的字节数</span>
<span id="MemStats.HeapObjects">HeapObjects</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 已分配对象的总个数</span>
<span class="com">// L低层次、大小固定的结构体分配器统计Inuse为正在使用的字节数Sys为从系统获取的字节数</span>
<span id="MemStats.StackInuse">StackInuse</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 引导程序的堆栈</span>
<span id="MemStats.StackSys">StackSys</span> <a href="builtin.htm#uint64">uint64</a>
<span id="MemStats.MSpanInuse">MSpanInuse</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// mspan结构体</span>
<span id="MemStats.MSpanSys">MSpanSys</span> <a href="builtin.htm#uint64">uint64</a>
<span id="MemStats.MCacheInuse">MCacheInuse</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// mcache结构体</span>
<span id="MemStats.MCacheSys">MCacheSys</span> <a href="builtin.htm#uint64">uint64</a>
<span id="MemStats.BuckHashSys">BuckHashSys</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// profile桶散列表</span>
<span id="MemStats.GCSys">GCSys</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// GC元数据</span>
<span id="MemStats.OtherSys">OtherSys</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 其他系统申请</span>
<span class="com">// 垃圾收集器统计</span>
<span id="MemStats.NextGC">NextGC</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 会在HeapAlloc字段到达该值字节数时运行下次GC</span>
<span id="MemStats.LastGC">LastGC</span> <a href="builtin.htm#uint64">uint64</a> <span class="com">// 上次运行的绝对时间(纳秒)</span>
<span id="MemStats.PauseTotalNs">PauseTotalNs</span> <a href="builtin.htm#uint64">uint64</a>
<span id="MemStats.PauseNs">PauseNs</span> [256]<a href="builtin.htm#uint64">uint64</a> <span class="com">// 近期GC暂停时间的循环缓冲最近一次在[(NumGC+255)%256]</span>
<span id="MemStats.NumGC">NumGC</span> <a href="builtin.htm#uint32">uint32</a>
<span id="MemStats.EnableGC">EnableGC</span> <a href="builtin.htm#bool">bool</a>
<span id="MemStats.DebugGC">DebugGC</span> <a href="builtin.htm#bool">bool</a>
<span class="com">// 每次申请的字节数的统计61是C代码中的尺寸分级数</span>
<span id="MemStats.BySize">BySize</span> [61]struct {
Size <a href="builtin.htm#uint32">uint32</a>
Mallocs <a href="builtin.htm#uint64">uint64</a>
Frees <a href="builtin.htm#uint64">uint64</a>
}
}</pre>
<p>MemStats记录内存申请和分配的统计信息。</p>
<h2 id="ReadMemStats">func <a href="https://github.com/golang/go/blob/master/src/runtime/mem.go?name=release#72" title="View Source">ReadMemStats</a> </h2>
<pre class="funcdecl">func ReadMemStats(m *<a href="#MemStats">MemStats</a>)</pre>
<p>ReadMemStats将内存申请和分配的统计信息填写进m。</p>
<h2 id="MemProfileRecord">type <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#53" title="View Source">MemProfileRecord</a> </h2>
<pre>type MemProfileRecord struct {
<span id="MemProfileRecord.AllocBytes">AllocBytes</span>, <span id="MemProfileRecord.FreeBytes">FreeBytes</span> <a href="builtin.htm#int64">int64</a> <span class="com">// 申请和释放的字节数</span>
<span id="MemProfileRecord.AllocObjects">AllocObjects</span>, <span id="MemProfileRecord.FreeObjects">FreeObjects</span> <a href="builtin.htm#int64">int64</a> <span class="com">// 申请和释放的对象数</span>
<span id="MemProfileRecord.Stack0">Stack0</span> [32]<a href="builtin.htm#uintptr">uintptr</a> <span class="com">// 该记录的调用栈踪迹,以第一个零值成员截止</span>
}</pre>
<p>MemProfileRecord用于描述某个调用栈序列申请和释放的活动对象等信息。</p>
<h3 id="MemProfileRecord.InUseBytes">func (*MemProfileRecord) <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#60" title="View Source">InUseBytes</a> </h3>
<pre class="funcdecl">func (r *<a href="#MemProfileRecord">MemProfileRecord</a>) InUseBytes() <a href="builtin.htm#int64">int64</a></pre>
<p>InUseBytes返回正在使用的字节数AllocBytes FreeBytes</p>
<h3 id="MemProfileRecord.InUseObjects">func (*MemProfileRecord) <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#63" title="View Source">InUseObjects</a> </h3>
<pre class="funcdecl">func (r *<a href="#MemProfileRecord">MemProfileRecord</a>) InUseObjects() <a href="builtin.htm#int64">int64</a></pre>
<p>InUseObjects返回正在使用的对象数AllocObjects - FreeObjects</p>
<h3 id="MemProfileRecord.Stack">func (*MemProfileRecord) <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#69" title="View Source">Stack</a> </h3>
<pre class="funcdecl">func (r *<a href="#MemProfileRecord">MemProfileRecord</a>) Stack() []<a href="builtin.htm#uintptr">uintptr</a></pre>
<p>Stack返回关联至此记录的调用栈踪迹即r.Stack0的前缀。</p>
<h2 id="MemProfile">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#90" title="View Source">MemProfile</a> </h2>
<pre class="funcdecl">func MemProfile(p []<a href="#MemProfileRecord">MemProfileRecord</a>, inuseZero <a href="builtin.htm#bool">bool</a>) (n <a href="builtin.htm#int">int</a>, ok <a href="builtin.htm#bool">bool</a>)</pre>
<p align="left">MemProfile返回当前内存profile中的记录数n。若len(p)&gt;=nMemProfile会将此分析报告复制到p中并返回(n, true)如果len(p)&lt;nMemProfile则不会更改p而只返回(n, false)。</p>
<p align="left">如果inuseZero为真该profile就会包含无效分配记录其中r.AllocBytes&gt;0而r.AllocBytes==r.FreeBytes。这些内存都是被申请后又释放回运行时环境的</p>
<p align="left">大多数调用者应当使用runtime/pprof包或testing包的-test.memprofile标记而非直接调用MemProfile。</p>
<h2 id="Breakpoint">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#8" title="View Source">Breakpoint</a> </h2>
<pre class="funcdecl">func Breakpoint()</pre>
<p>Breakpoint执行一个断点陷阱。</p>
<h2 id="Stack">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#172" title="View Source">Stack</a> </h2>
<pre class="funcdecl">func Stack(buf []<a href="builtin.htm#byte">byte</a>, all <a href="builtin.htm#bool">bool</a>) <a href="builtin.htm#int">int</a></pre>
<p>Stack将调用其的go程的调用栈踪迹格式化后写入到buf中并返回写入的字节数。若all为true函数会在写入当前go程的踪迹信息后将其它所有go程的调用栈踪迹都格式化写入到buf中。</p>
<h2 id="Caller">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#95" title="View Source">Caller</a> </h2>
<pre class="funcdecl">func Caller(skip <a href="builtin.htm#int">int</a>) (pc <a href="builtin.htm#uintptr">uintptr</a>, file <a href="builtin.htm#string">string</a>, line <a href="builtin.htm#int">int</a>, ok <a href="builtin.htm#bool">bool</a>)</pre>
<p align="left">Caller报告当前go程调用栈所执行的函数的文件和行号信息。实参skip为上溯的栈帧数0表示Caller的调用者Caller所在的调用栈由于历史原因skip的意思在Caller和Callers中并不相同。函数的返回值为调用栈标识符、文件名、该调用在文件中的行号。如果无法获得信息ok会被设为false。</p>
<h2 id="Callers">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#102" title="View Source">Callers</a> </h2>
<pre class="funcdecl">func Callers(skip <a href="builtin.htm#int">int</a>, pc []<a href="builtin.htm#uintptr">uintptr</a>) <a href="builtin.htm#int">int</a></pre>
<p>函数把当前go程调用栈上的调用栈标识符填入切片pc中返回写入到pc中的项数。实参skip为开始在pc中记录之前所要跳过的栈帧数0表示Callers自身的调用栈1表示Callers所在的调用栈。返回写入p的项数。</p>
<h2 id="StackRecord">type <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#93" title="View Source">StackRecord</a> </h2>
<pre>type StackRecord struct {
<span id="StackRecord.Stack0">Stack0</span> [32]<a href="builtin.htm#uintptr">uintptr</a> <span class="com">// 该记录的调用栈踪迹,以第一个零值成员截止</span>
}</pre>
<p>StackRecord描述单条调用栈。</p>
<h3 id="StackRecord.Stack">func (*StackRecord) <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#99" title="View Source">Stack</a> </h3>
<pre class="funcdecl">func (r *<a href="#StackRecord">StackRecord</a>) Stack() []<a href="builtin.htm#uintptr">uintptr</a></pre>
<p>Stack返回与记录相关联的调用栈踪迹即r.Stack0的前缀。</p>
<h2 id="Func">type <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#104" title="View Source">Func</a> </h2>
<pre>type Func struct {
<span class="com">// 内含隐藏或非导出字段</span>
}</pre>
<h3 id="FuncForPC">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#110" title="View Source">FuncForPC</a> </h3>
<pre class="funcdecl">func FuncForPC(pc <a href="builtin.htm#uintptr">uintptr</a>) *<a href="#Func">Func</a></pre>
<p>FuncForPC返回一个表示调用栈标识符pc对应的调用栈的*Func如果该调用栈标识符没有对应的调用栈函数会返回nil。每一个调用栈必然是对某个函数的调用。</p>
<h3 id="Func.Name">func (*Func) <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#113" title="View Source">Name</a> </h3>
<pre class="funcdecl">func (f *<a href="#Func">Func</a>) Name() <a href="builtin.htm#string">string</a></pre>
<p>Name返回该调用栈所调用的函数的名字。</p>
<h3 id="Func.FileLine">func (*Func) <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#126" title="View Source">FileLine</a> </h3>
<pre class="funcdecl">func (f *<a href="#Func">Func</a>) FileLine(pc <a href="builtin.htm#uintptr">uintptr</a>) (file <a href="builtin.htm#string">string</a>, line <a href="builtin.htm#int">int</a>)</pre>
<p>FileLine返回该调用栈所调用的函数的源代码文件名和行号。如果pc不是f内的调用栈标识符结果是不精确的。</p>
<h3 id="Func.Entry">func (*Func) <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#118" title="View Source">Entry</a> </h3>
<pre class="funcdecl">func (f *<a href="#Func">Func</a>) Entry() <a href="builtin.htm#uintptr">uintptr</a></pre>
<p>Entry返回该调用栈的调用栈标识符。</p>
<h2 id="NumCgoCall">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#30" title="View Source">NumCgoCall</a> </h2>
<pre class="funcdecl">func NumCgoCall() <a href="builtin.htm#int64">int64</a></pre>
<p>NumCgoCall返回当前进程执行的cgo调用次数。</p>
<h2 id="NumGoroutine">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#33" title="View Source">NumGoroutine</a> </h2>
<pre class="funcdecl">func NumGoroutine() <a href="builtin.htm#int">int</a></pre>
<p>NumGoroutine返回当前存在的Go程数。</p>
<h2 id="Goexit">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#87" title="View Source">Goexit</a> </h2>
<pre class="funcdecl">func Goexit()</pre>
<p align="left">Goexit终止调用它的go程。其它go程不会受影响。Goexit会在终止该go程前执行所有defer的函数。</p>
<p align="left">在程序的main go程调用本函数会终结该go程而不会让main返回。因为main函数没有返回程序会继续执行其它的go程。如果所有其它go程都退出了程序就会崩溃。</p>
<h2 id="Gosched">func <a href="https://github.com/golang/go/blob/master/src/runtime/extern.go?name=release#78" title="View Source">Gosched</a> </h2>
<pre class="funcdecl">func Gosched()</pre>
<p>Gosched使当前go程放弃处理器以让其它go程运行。它不会挂起当前go程因此当前go程未来会恢复执行。</p>
<h2 id="GoroutineProfile">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#122" title="View Source">GoroutineProfile</a> </h2>
<pre class="funcdecl">func GoroutineProfile(p []<a href="#StackRecord">StackRecord</a>) (n <a href="builtin.htm#int">int</a>, ok <a href="builtin.htm#bool">bool</a>)</pre>
<p align="left">GoroutineProfile返回活跃go程的堆栈profile中的记录个数。若len(p) &gt;= n函数就会将profile中的记录复制到p中并返回(n, true)。若len(p) &lt; n则不会修改p而只返回(n, false)。</p>
<p align="left">绝大多数调用者应当使用runtime/pprof包而非直接调用GoroutineProfile。</p>
<h2 id="LockOSThread">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#13" title="View Source">LockOSThread</a> </h2>
<pre class="funcdecl">func LockOSThread()</pre>
<p>将调用的go程绑定到它当前所在的操作系统线程。除非调用的go程退出或调用UnlockOSThread否则它将总是在该线程中执行而其它go程则不能进入该线程。</p>
<h2 id="UnlockOSThread">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#17" title="View Source">UnlockOSThread</a> </h2>
<pre class="funcdecl">func UnlockOSThread()</pre>
<p>将调用的go程解除和它绑定的操作系统线程。若调用的go程未调用LockOSThreadUnlockOSThread不做操作。</p>
<h2 id="ThreadCreateProfile">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#114" title="View Source">ThreadCreateProfile</a> </h2>
<pre class="funcdecl">func ThreadCreateProfile(p []<a href="#StackRecord">StackRecord</a>) (n <a href="builtin.htm#int">int</a>, ok <a href="builtin.htm#bool">bool</a>)</pre>
<p>返回线程创建profile中的记录个数。如果len(p)&gt;=n本函数就会将profile中的记录复制到p中并返回(n, true)。若len(p)&lt;n则不会更改p而只返回(n, false)。</p>
<p align="left">绝大多数使用者应当使用runtime/pprof包而非直接调用ThreadCreateProfile。</p>
<h2 id="BlockProfileRecord">type <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#153" title="View Source">BlockProfileRecord</a> </h2>
<pre>type BlockProfileRecord struct {
<span id="BlockProfileRecord.Count">Count</span> <a href="builtin.htm#int64">int64</a>
<span id="BlockProfileRecord.Cycles">Cycles</span> <a href="builtin.htm#int64">int64</a>
<a href="#StackRecord">StackRecord</a>
}</pre>
<p>BlockProfileRecord用于描述某个调用栈序列发生的阻塞事件的信息。</p>
<h2 id="SetBlockProfileRate">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#149" title="View Source">SetBlockProfileRate</a> </h2>
<pre class="funcdecl">func SetBlockProfileRate(rate <a href="builtin.htm#int">int</a>)</pre>
<p align="left">SetBlockProfileRate控制阻塞profile记录go程阻塞事件的采样频率。对于一个阻塞事件平均每阻塞rate纳秒阻塞profile记录器就采集一份样本。</p>
<p align="left">要在profile中包括每一个阻塞事件需传入rate=1要完全关闭阻塞profile的记录需传入rate&lt;=0。</p>
<h2 id="BlockProfile">func <a href="https://github.com/golang/go/blob/master/src/runtime/debug.go?name=release#166" title="View Source">BlockProfile</a> </h2>
<pre class="funcdecl">func BlockProfile(p []<a href="#BlockProfileRecord">BlockProfileRecord</a>) (n <a href="builtin.htm#int">int</a>, ok <a href="builtin.htm#bool">bool</a>)</pre>
<p align="left">BlockProfile返回当前阻塞profile中的记录个数。如果len(p)&gt;=n本函数就会将此profile中的记录复制到p中并返回(n, true)。如果len(p)&lt;n本函数则不会修改p而只返回(n, false)。</p>
<p align="left">绝大多数使用者应当使用runtime/pprof包或testing包的-test.blockprofile标记 而非直接调用 BlockProfile。</p>
</div>