A tour of the Go trace package

栏目: IT技术 · 发布时间: 6年前

内容简介::information_source:Go provides us a tool to enable tracing during the runtime and get a detailed view of the execution of our program. This tool can be enabled by flagThe flow of the tool is quite straightforward. Each event, such as memory allocation; al

A tour of the Go trace package

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

:information_source: This article is based on Go 1.13.

Go provides us a tool to enable tracing during the runtime and get a detailed view of the execution of our program. This tool can be enabled by flag -trace with the tests, from pprof to get live tracing, or anywhere in our code thanks to the trace package . This tool can be even more powerful since you can enhance it with your own traces. Let’s review how it works.

Flow

The flow of the tool is quite straightforward. Each event, such as memory allocation; all the phases of garbage collector; goroutines when they run, pause, etc. is statically recorded by the Go standard library and formatted to be displayed later. However, before the recording starts, Go first “stops the world” and takes a snapshot of the current goroutines and their states.

You can find more details about this phase in my article “ Go: How Does Go Stop the World?

This will later allow Go to construct the cycle of life of each goroutine properly. Here is the flow:

A tour of the Go trace package

Initialization phase before tracing

Then, collected events are pushed to a buffer that is later flushed to a list of full buffers when the max capacity is reached. Here is a diagram of this flow:

A tour of the Go trace package

Tracing collect events per P

The tracer now needs a way to dump those traces to the output. For that, Go spawns a goroutine dedicated to that when the tracing starts. This goroutine will dump data when available and will park the goroutine until the next ones. Here is a representation of it:

A tour of the Go trace package

A dedicated goroutine reads and dump the traces

The flow is now pretty clear, so let’s review the recorded traces.

Traces

Once the tracing is generated, the visualization can be done with running the command go tool trace my-output.out . Let’s take some traces as example:

A tour of the Go trace package

Tracing from go tool

Most of them are quite straightforward. The traces related to the garbage collector stand under the blue trace GC :

A tour of the Go trace package

Traces of the garbage collector

Here is a quick review:

STW
GC (idle)
MARK ASSIST
GXX runtime.bgsweep
GXX runtime.gcBgMarkWorker

You can find more information about those traces in my article “ Go: How Does the Garbage Collector Watch Your Application?

However, some traces are not easy to understand. Let’s review them to get a better understanding:

  • proc start is called when a processor is associated with a thread. It happens when a new thread is started or when we resume from a syscall.

A tour of the Go trace package

  • proc stop is called when a thread is disassociated from the current processor. It happens when the thread is blocked in a syscall or when a thread exits.

A tour of the Go trace package

  • syscall is called when a goroutine is making a system call:

A tour of the Go trace package

  • unblock is called when a goroutine is unblocked from a syscall — the label (sysexit) would be displayed in that case, from a blocked channel, etc:

A tour of the Go trace package

The traces can be enhanced since Go allows you to define and visualize your own trace along with the ones from the standard library.

User traces

The traces we can define have two hierarchical levels:

  • at the top level with the task, with a start and an end.
  • at a sub level with the region.

Here is a simple example of them:

Those new traces can be visualized directly from the tool via the menu User-defined tasks:

A tour of the Go trace package

Custom task and regions

It’s also possible to record some arbitrary log to the task:

ctx, task := trace.NewTask(context.Background(), "main start")
trace.Log(ctx, "category", "I/O file")
trace.Log(ctx, "goroutine", "2")

Those logs will be found under the goroutine that sets up the task:

A tour of the Go trace package

Custom logs in the tracing

The tasks can also be embedded in other tasks by deriving the context of the parent task and so on.

However, tracing all those events live from production — thanks to pprof — could slightly deteriorate the performance while you are collecting them.

Performance impact

A simple benchmark could help to understand the impact of the tracing. One will run with the flag -trace and another one without. Here are the results with a benchmark of the function ioutil.ReadFile() that generates quite a lot of events:

name         time/op
ReadFiles-8  48.1µs ± 0%name         time/op
ReadFiles-8  63.5µs ± 0%  // with tracing

In this case, the impact is about ~35% and could vary depending on the application. However, some tools such as StackDriver allows having continuous profiling on production while keeping a light overhead on our application.


以上所述就是小编给大家介绍的《A tour of the Go trace package》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Android编程权威指南(第3版)

Android编程权威指南(第3版)

比尔·菲利普斯 (Bill Phillips)、克里斯·斯图尔特 (Chris Stewart)、克莉丝汀·马西卡诺 (Kristin Marsicano) / 王明发 / 人民邮电出版社 / 2017-6 / 129.00元

Big Nerd Ranch 是美国一家专业的移动开发技术培训机构。本书主要以其Android 训练营教学课程为基础,融合了几位作者多年的心得体会,是一本完全面向实战的Android 编程权威指南。全书共36 章,详细介绍了8 个Android 应用的开发过程。通过这些精心设计的应用,读者可掌握很多重要的理论知识和开发技巧,获得宝贵的开发经验。 第3 版较之前版本增加了对数据绑定等新工具的介......一起来看看 《Android编程权威指南(第3版)》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具