doupo2241 2015-11-03 22:10
浏览 75
已采纳

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

Consider the following program, which simply spins up a few goroutines and then waits for them to finish and signal over the channel that they are done.

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()
}

When this program terminates, the output is a binary file called /tmp/Trace.out. Next, I tried to view the trace by using the trace tool as follows.

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

This produces launches a page with a link to View Trace (together with a view other links that give only aggregate data), but clicking on that link results in a blank page. When I view the source of that page, I see the following source, which seems imply that JSON is expected rather than binary.

<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>

How can I view the event-by-event trace using Go tools?

  • 写回答

2条回答 默认 最新

  • dousong5492 2015-11-05 11:21
    关注

    I can confirm @widsvc's comment: you are probably using an incompatible browser like Firefox.

    It works as expected with Chrome.

    Looking under the hood, you can see in Firefox's console this error:

    ReferenceError: tr is not defined trace:7:9

    After a quick search, it appears this uses trace-viewer which is embedded in Chrome: https://www.chromium.org/developers/how-tos/trace-event-profiling-tool.

    I tried using their stand-alone trace2html tool on the content of http://localhost:8080/jsontrace, but the output still gives me errors with Firefox (first "document.registerElement is not a function", and after fixing that one others keep coming)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算