mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
commit
b66d32f763
@ -1,114 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to dump the GOSSAFUNC graph for a method)
|
||||
[#]: via: (https://dave.cheney.net/2020/06/19/how-to-dump-the-gossafunc-graph-for-a-method)
|
||||
[#]: author: (Dave Cheney https://dave.cheney.net/author/davecheney)
|
||||
|
||||
How to dump the GOSSAFUNC graph for a method
|
||||
======
|
||||
|
||||
The Go compiler’s SSA backend contains a facility to produce HTML debugging output of the compilation phases. This post covers how to print the SSA output for function _and_ methods.
|
||||
|
||||
Let’s start with a sample program which contains a function, a value method, and a pointer method:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Numbers struct {
|
||||
vals []int
|
||||
}
|
||||
|
||||
func (n *Numbers) Add(v int) {
|
||||
n.vals = append(n.vals, v)
|
||||
}
|
||||
|
||||
func (n Numbers) Average() float64 {
|
||||
sum := 0.0
|
||||
for _, num := range n.vals {
|
||||
sum += float64(num)
|
||||
}
|
||||
return sum / float64(len(n.vals))
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
var numbers Numbers
|
||||
numbers.Add(200)
|
||||
numbers.Add(43)
|
||||
numbers.Add(-6)
|
||||
fmt.Println(numbers.Average())
|
||||
}
|
||||
```
|
||||
|
||||
Control of the SSA debugging output is via the `GOSSAFUNC` environment variable. This variable takes the name of the function to dump. This is _not_ the functions fully qualified name. For `func main` above the name of the function is `main` _not_ `main.main`.
|
||||
|
||||
```
|
||||
% env GOSSAFUNC=main go build
|
||||
runtime
|
||||
dumped SSA to ../../go/src/runtime/ssa.html
|
||||
t
|
||||
dumped SSA to ./ssa.html
|
||||
```
|
||||
|
||||
In this example `GOSSAFUNC=main` matched both `main.main` and a function called `runtime.main`.[1][1] This is a little unfortunate, but in practice probably not a big deal as, if you’re performance tuning your code, it won’t be in a giant spaghetti blob in `func main`.
|
||||
|
||||
What is more likely is your code will be in a _method_, so you’ve probably landed on this post looking for the correct incantation to dump the SSA output for a method.
|
||||
|
||||
To print the SSA debug for the pointer method `func (n *Numbers) Add`, the equivalent function name is`(*Numbers).Add`:[2][2]
|
||||
|
||||
```
|
||||
% env "GOSSAFUNC=(*Numbers).Add" go build
|
||||
t
|
||||
dumped SSA to ./ssa.html
|
||||
```
|
||||
|
||||
To print the SSA debug for a value method `func (n Numbers) Average`, the equivalent function name is `(*Numbers).Average` _even though this is a value method_:
|
||||
|
||||
```
|
||||
% env "GOSSAFUNC=(*Numbers).Average" go build
|
||||
t
|
||||
dumped SSA to ./ssa.html
|
||||
```
|
||||
|
||||
1. If you didn’t build Go from source then the path to the `runtime` package may be read only and you might receive an error. Please don’t use the `sudo` hammer to fix this.[][3]
|
||||
2. Please pay attention to the shell quoting.[][4]
|
||||
|
||||
|
||||
|
||||
### Related posts:
|
||||
|
||||
1. [Accidental method value][5]
|
||||
2. [Never edit a method, always rewrite it][6]
|
||||
3. [How to find out which Go version built your binary][7]
|
||||
4. [Declaration scopes in Go][8]
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://dave.cheney.net/2020/06/19/how-to-dump-the-gossafunc-graph-for-a-method
|
||||
|
||||
作者:[Dave Cheney][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://dave.cheney.net/author/davecheney
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: tmp.kLSHpQXzhr#easy-footnote-bottom-1-4188 (If you didn’t build Go from source then the path to the <code>runtime</code> package may be read only and you might receive an error. Please don’t use the <code>sudo</code> hammer to fix this.)
|
||||
[2]: tmp.kLSHpQXzhr#easy-footnote-bottom-2-4188 (Please pay attention to the shell quoting.)
|
||||
[3]: tmp.kLSHpQXzhr#easy-footnote-1-4188
|
||||
[4]: tmp.kLSHpQXzhr#easy-footnote-2-4188
|
||||
[5]: https://dave.cheney.net/2014/05/19/accidental-method-value (Accidental method value)
|
||||
[6]: https://dave.cheney.net/2017/11/30/never-edit-a-method-always-rewrite-it (Never edit a method, always rewrite it)
|
||||
[7]: https://dave.cheney.net/2017/06/20/how-to-find-out-which-go-version-built-your-binary (How to find out which Go version built your binary)
|
||||
[8]: https://dave.cheney.net/2016/12/15/declaration-scopes-in-go (Declaration scopes in Go)
|
@ -0,0 +1,99 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to dump the GOSSAFUNC graph for a method)
|
||||
[#]: via: (https://dave.cheney.net/2020/06/19/how-to-dump-the-gossafunc-graph-for-a-method)
|
||||
[#]: author: (Dave Cheney https://dave.cheney.net/author/davecheney)
|
||||
|
||||
如何转储一个方法的 GOSSAFUNC 图
|
||||
======
|
||||
|
||||
Go 编译器的 SSA 后端包含一种工具,可以生成编译阶段的 HTML 调试输出。这篇文章介绍了如何为函数_和_方法打印 SSA 输出。
|
||||
|
||||
让我们从一个包含函数、值方法和指针方法的示例程序开始:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Numbers struct {
|
||||
vals []int
|
||||
}
|
||||
|
||||
func (n *Numbers) Add(v int) {
|
||||
n.vals = append(n.vals, v)
|
||||
}
|
||||
|
||||
func (n Numbers) Average() float64 {
|
||||
sum := 0.0
|
||||
for _, num := range n.vals {
|
||||
sum += float64(num)
|
||||
}
|
||||
return sum / float64(len(n.vals))
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
var numbers Numbers
|
||||
numbers.Add(200)
|
||||
numbers.Add(43)
|
||||
numbers.Add(-6)
|
||||
fmt.Println(numbers.Average())
|
||||
}
|
||||
```
|
||||
|
||||
通过 `GOSSAFUNC` 环境变量控制 SSA 调试输出。此变量含有要转储的函数的名称。这_不是_函数的完全限定名。对于上面的 `func main`,函数名称为 `main` _而不是_ `main.main`。
|
||||
|
||||
```
|
||||
% env GOSSAFUNC=main go build
|
||||
runtime
|
||||
dumped SSA to ../../go/src/runtime/ssa.html
|
||||
t
|
||||
dumped SSA to ./ssa.html
|
||||
```
|
||||
|
||||
在这个例子中,`GOSSAFUNC=main` 同时匹配了 `main.main` 和一个名为 `runtime.main` 的函数。[1][1]这有点不走运,但是实际上可能没什么大不了的,因为如果你要对代码进行性能调整,它就不会出现在 `func main` 中的巨大的意大利面块中。
|
||||
|
||||
你的代码更有可能在_方法_中,你可能已经看到这篇文章,并寻找能够转储方法的 SSA 输出。
|
||||
|
||||
要为指针方法 `func (n *Numbers) Add` 打印 SSA 调试,等效函数名为 `(*Numbers).Add`:[2][2]
|
||||
|
||||
```
|
||||
% env "GOSSAFUNC=(*Numbers).Add" go build
|
||||
t
|
||||
dumped SSA to ./ssa.html
|
||||
```
|
||||
|
||||
要为值方法 `func (n Numbers) Average` 打印 SSA 调试,等效函数名为 `(*Numbers).Average`,_即使这是一个值方法_:
|
||||
|
||||
```
|
||||
% env "GOSSAFUNC=(*Numbers).Average" go build
|
||||
t
|
||||
dumped SSA to ./ssa.html
|
||||
```
|
||||
|
||||
1. 如果你没有从源码构建 Go,那么 `runtime` 软件包的路径可能是只读的,并且可能会收到错误消息。请不要使用 `sudo` 来解决此问题。
|
||||
2. 请注意 shell 引用
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://dave.cheney.net/2020/06/19/how-to-dump-the-gossafunc-graph-for-a-method
|
||||
|
||||
作者:[Dave Cheney][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://dave.cheney.net/author/davecheney
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: tmp.kLSHpQXzhr#easy-footnote-bottom-1-4188 (If you didn’t build Go from source then the path to the <code>runtime</code> package may be read only and you might receive an error. Please don’t use the <code>sudo</code> hammer to fix this.)
|
||||
[2]: tmp.kLSHpQXzhr#easy-footnote-bottom-2-4188 (Please pay attention to the shell quoting.)
|
Loading…
Reference in New Issue
Block a user