go - 如何查看 `runtime/trace` 生成的跟踪的详细信息?

标签 go

考虑以下程序,它只是启动几个 goroutine,然后等待它们完成并通过 channel 发出它们已完成的信号。

package main

import (
    "os"
    "runtime/trace"
    "time"
)

func doWork(c chan int) {
    startTime := time.Now()
    i := 0
    for curTime := startTime; curTime.Sub(startTime) < 2; curTime = time.Now() {
        i++
    }
    c <- i
}

func main() {
    numGoRoutine := 10
    traceOutFile, _ := os.OpenFile("/tmp/Trace.out", os.O_WRONLY|os.O_CREATE, os.ModeExclusive|os.ModePerm)
    trace.Start(traceOutFile)

    // Start goroutines
    termChannel := make(chan int)
    for i := 0; i < numGoRoutine; i++ {
        go doWork(termChannel)
    }

    // Wait for completion
    for i := 0; i < numGoRoutine; i++ {
        <-termChannel
    }

    trace.Stop()
}

当这个程序终止时,输出是一个名为 /tmp/Trace.out 的二进制文件。接下来,我尝试使用跟踪工具查看跟踪情况,如下所示。

go tool trace -http=localhost:8080  ./Main /tmp/Trace.out

这会生成一个页面,其中包含指向 View Trace 的链接(以及仅提供聚合数据的 View 其他链接),但单击该链接会导致出现空白页面。当我查看该页面的源代码时,我看到以下源代码,这似乎暗示需要 JSON 而不是二进制文件。

<html>
    <head>
        <link href="/trace_viewer_html" rel="import">
        <script>
            document.addEventListener("DOMContentLoaded", function(event) {
                var viewer = new tr.TraceViewer('/jsontrace');
                document.body.appendChild(viewer);
            });
        </script>
    </head>
    <body>
    </body>
</html>

如何使用 Go 工具查看逐个事件的跟踪记录?

最佳答案

我可以确认@widsvc 的评论:您可能使用了不兼容的浏览器,例如 Firefox。

它与 Chrome 一起正常工作。

深入了解,您可以在 Firefox 的控制台中看到此错误:

ReferenceError: tr is not defined trace:7:9

快速搜索后,它似乎使用了 trace-viewer嵌入在 Chrome 中:https://www.chromium.org/developers/how-tos/trace-event-profiling-tool .

我尝试对 http://localhost:8080/jsontrace 的内容使用他们的独立 trace2html 工具,但输出仍然给我 Firefox 的错误(第一个“document.registerElement 不是一个函数”,并且在修复之后其他人继续出现)

关于go - 如何查看 `runtime/trace` 生成的跟踪的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510046/

相关文章:

go - 使用文件方案读取 url 作为 ReadFile 文件名的惯用方法是什么?

go - Go 是否保证地址不变?

google-app-engine - 重新构建在 docker 容器中运行的 Go (App Engine) 应用程序?

c - 在 Windows 中构建 go 项目时运行 gcc 失败

bash - 带有输入重定向的 exec.Command

go - 马提尼绑定(bind) "cannot return value obtained from unexported field or method"

google-app-engine - 转到 Google 数据存储空值

java - 使用 os.exec 运行的应用程序无法正确加载 java 库

php - golang 上 PHP __METHOD__ 的等价物

go - 此代码中的 T 是什么