英文:
How do I view the details of a trace produced by `runtime/trace`?
问题
考虑以下程序,它简单地启动了几个 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)
// 启动 goroutine
termChannel := make(chan int)
for i := 0; i < numGoRoutine; i++ {
go doWork(termChannel)
}
// 等待完成
for i := 0; i < numGoRoutine; i++ {
<-termChannel
}
trace.Stop()
}
当该程序终止时,输出的结果是一个名为 /tmp/Trace.out
的二进制文件。接下来,我尝试使用以下命令使用 trace 工具查看跟踪信息。
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 工具查看逐事件的跟踪信息?
英文:
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?
答案1
得分: 2
我可以确认@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。
我尝试使用他们独立的trace2html
工具处理http://localhost:8080/jsontrace的内容,但输出仍然在Firefox中给我报错(首先是"document.registerElement is not a function",修复后还有其他错误不断出现)。
英文:
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)
答案2
得分: 2
如果有人遇到和我一样的问题,结果发现 Chrome 49 中的跟踪查看器也出现了问题。
最近的一个 CL(变更列表)已经修复了这个问题:https://go-review.googlesource.com/#/c/22013/
英文:
In case anyone finds themselves in my shoes, it turns out the trace viewer was broken in Chrome 49, too.
It has been fixed in a recent CL: https://go-review.googlesource.com/#/c/22013/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论