在Go语言中,要打印每一行执行的详细信息,可以使用`set -x`命令。

huangapple go评论96阅读模式
英文:

`set -x` for golang: Print every executed line?

问题

有没有类似于shell功能set -x的东西可以用于golang?

我想要看到每一行被执行的代码。

标准库的代码行不应该被打印出来。

英文:

Is there something like shell feature set -x for golang?

I would like to see every line of code which gets executed.

Lines of the standard library should not be printed.

答案1

得分: 3

由于Go是一种编译语言,可执行文件中不包含任何原始源代码,因此无法输出。你可以接近你想要的方法,即以调试模式运行Go项目,并逐行执行代码。

这样,你可以在运行时决定是否跳转到一个函数中,或者只执行它并跳过它,因为调试器无法知道你认为什么是“标准库”,以及哪些应该逐行跟踪,哪些不应该。

另一方面,Go可以使用go协程进行大规模的多线程操作,因此在一分钟内打印每一行执行的代码可能会变得混乱(有时我会同时运行超过一百个协程)。

英文:

Since Go is a compiled language the executable doesn't contain any original source code, so output would be impossible. The closest you can come to the approach you want is to run your Go project in Debug mode and step through every line of code.

This way you can decide while running to jump into a function or just execute it and jump over it, because the debugger won't know what you consider "standard library" and what should traced line-by-line and what not.

On the other hand, Go can be heavily multi-threaded with go routines, so printing every executed line could be a mess in a minute (sometimes I have more than a houndred routines running the same time).

答案2

得分: 3

你可以使用pprof来进行组合:

  • 分析,可以帮助你查看谁调用了什么,以及持续时间有多长

在Go语言中,要打印每一行执行的详细信息,可以使用`set -x`命令。

ofabry/go-callvis也可以帮助查看调用图)

  • 它的Weblist视图显示了每个执行的行及其成本:

在Go语言中,要打印每一行执行的详细信息,可以使用`set -x`命令。

这个指南中查看“交互式分析”。

这不会按顺序显示每行执行的情况,但允许你在运行后进行探索。


请注意,Go 1.20/1.21(2022年第四季度/2023年第二季度)将包含(自#55022被接受):

> ## Go的基于分析的优化(PGO)
>
> 通过pprof和Linux分析器perf等分析工具,可以找出Go程序中的低效之处。这些工具可以确定执行时间大部分消耗在哪些源代码区域。
>
> 与LLVM等其他优化编译器不同,Go编译器尚未执行基于分析的优化(PGO)
PGO使用代码的运行时行为信息来指导编译器优化,如内联、代码布局等。PGO可以提高应用程序性能,提升15-30% [LLVM,AutoFDO]。
>
> 在这个提案中,我们将PGO扩展到Go编译器中。
>
> 具体来说,我们将分析结果合并到编译器的前端,构建一个带有节点和边权重的调用图(称为**WeightedCallGraph**)。然后,内联器使用WeightedCallGraph进行基于分析的内联,积极地内联热函数。
>
> 我们引入了一个与内联器紧密集成的基于分析的代码特化传递,消除热代码路径中的间接方法调用开销。
>
> 此外,我们使用与其关联的分析权重注释IR指令,并将其传播到SSA级别,以便实现基于分析的基本块布局优化,以获得更好的指令缓存和TLB性能。
>
> 最后,我们扩展了Go的链接器,直接使用分析结果进行跨包边界的函数重新排序优化,这也有助于指令缓存和TLB性能。
>
> 我们的PGO消耗的分析文件格式与pprof工具生成的protobuf格式相同。该格式足够丰富,可以携带其他硬件性能计数器信息,如缓存未命中、LBR等。Google的现有perf_data_converter工具可以将Linux perf生成的perf.data文件转换为protobuf格式的profile.proto文件。

Go将提出一种新的PGO编译流程

在Go语言中,要打印每一行执行的详细信息,可以使用`set -x`命令。

英文:

You can combine, using pprof:

  • profiling, which can help you see who calls what, and for how long

在Go语言中,要打印每一行执行的详细信息,可以使用`set -x`命令。

(ofabry/go-callvis can also help to see a call graph)

  • Its Weblist view which shows each executed line and their cost:

在Go语言中,要打印每一行执行的详细信息,可以使用`set -x`命令。

See "Interactive Profiling" in this guide.

This won't display each line executed in order, but allows you to explore after a run what was executed.


Note that Go 1.20/1.21 (Q4 2022/Q2 2023) will include (since #55022 is accepted):

> ## Profile-Guided Optimization (PGO) for Go
>
> Inefficiencies in Go programs can be isolated via profiling tools such as pprof and linux profiler perf. Such tools can pinpoint source code regions where most of the execution time is spent.
>
> Unlike other optimizing compilers such as LLVM, the Go compiler does not yet perform Profile-Guided Optimization(PGO).
PGO uses information about the code’s runtime behavior to guide compiler optimizations such as inlining, code layout etc. PGO can improve application performance in the range 15-30% [LLVM, AutoFDO].
>
> In this proposal, we extend the Go compiler with PGO.
>
> Specifically, we incorporate the profiles into the frontend of the compiler to build a call graph with node & edge weights (called WeightedCallGraph). The Inliner subsequently uses the WeightedCallGraph to perform profile-guided inlining which aggressively inlines hot functions.
>
> We introduce a profile-guided code specialization pass that is tightly integrated with the Inliner and eliminates indirect method call overheads in hot code paths.
>
> Furthermore, we annotate IR instructions with their associated profile weights and propagate these to the SSA-level in order to facilitate profile-guided basic-block layout optimization to benefit from better instruction-cache and TLB performance.
>
> Finally, we extend Go's linker to also consume the profiles directly and perform function reordering optimization across package boundaries -- which also helps instruction-cache and TLB performance.
>
> The format of the profile file consumed by our PGO is identical to the protobuf format produced by the pprof tool. This format is rich enough to carry additional hardware performance counter information such as cache misses, LBR, etc.
Existing perf_data_converter tool from Google can convert a perf.data file produced by the Linux perf into a profile.proto file in protobuf format.

There will be a new compilation flow proposed in Go for PGO

在Go语言中,要打印每一行执行的详细信息,可以使用`set -x`命令。

huangapple
  • 本文由 发表于 2022年7月8日 18:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72910079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定