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

时间:2015-11-03 22:10:03

标签: go

考虑以下程序,它只是旋转几个goroutine,然后等待它们完成并通过通道发出信号,表明它们已完成。

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的链接(以及仅提供聚合数据的其他链接视图),但单击该链接会生成空白页面。当我查看该页面的来源时,我看到以下来源,这似乎意味着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工具查看逐个事件的跟踪?

2 个答案:

答案 0 :(得分:2)

我可以确认@ widsvc的评论:你可能正在使用像Firefox这样的不兼容的浏览器。

Chrome可以正常运行。

在引擎盖下,您可以在Firefox的控制台中看到此错误:

  

ReferenceError:tr未定义trace:7:9

快速搜索后,它会显示嵌入Chrome中的trace-viewerhttps://www.chromium.org/developers/how-tos/trace-event-profiling-tool

我尝试在http://localhost:8080/jsontrace的内容上使用他们的独立trace2html工具,但输出仍然给我错误的Firefox(第一个&#34; document.registerElement不是一个函数&#34 ;并且在确定其他人继续前进之后)

答案 1 :(得分:2)

如果有人发现自己在我的鞋子里,事实证明,跟踪浏览器在Chrome 49中也被打破了。

最近的CL:https://go-review.googlesource.com/#/c/22013/

已修复此问题
相关问题