// Copyright 2014 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Goroutine-related profiles. package main import ( "fmt" "html/template" "internal/trace" "log" "net/http" "reflect" "sort" "strconv" "sync" "time" ) func init() { http.HandleFunc("/goroutines", httpGoroutines) http.HandleFunc("/goroutine", httpGoroutine) } // gtype describes a group of goroutines grouped by start PC. type gtype struct { ID uint64 // Unique identifier (PC). Name string // Start function. N int // Total number of goroutines in this group. ExecTime int64 // Total execution time of all goroutines in this group. } var ( gsInit sync.Once gs map[uint64]*trace.GDesc ) // analyzeGoroutines generates statistics about execution of all goroutines and stores them in gs. func analyzeGoroutines(events []*trace.Event) { gsInit.Do(func() { gs = trace.GoroutineStats(events) }) } // httpGoroutines serves list of goroutine groups. func httpGoroutines(w http.ResponseWriter, r *http.Request) { events, err := parseEvents() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } analyzeGoroutines(events) gss := make(map[uint64]gtype) for _, g := range gs { gs1 := gss[g.PC] gs1.ID = g.PC gs1.Name = g.Name gs1.N++ gs1.ExecTime += g.ExecTime gss[g.PC] = gs1 } var glist []gtype for k, v := range gss { v.ID = k glist = append(glist, v) } sort.Slice(glist, func(i, j int) bool { return glist[i].ExecTime > glist[j].ExecTime }) w.Header().Set("Content-Type", "text/html;charset=utf-8") if err := templGoroutines.Execute(w, glist); err != nil { log.Printf("failed to execute template: %v", err) return } } var templGoroutines = template.Must(template.New("").Parse(`
Goroutines:Goroutine Name: | {{.Name}} |
Number of Goroutines: | {{.N}} |
Execution Time: | {{.ExecTimePercent}} of total program execution time |
Network Wait Time: | graph(download) |
Sync Block Time: | graph(download) |
Blocking Syscall Time: | graph(download) |
Scheduler Wait Time: | graph(download) |
Goroutine | Total | Execution | Network wait | Sync block | Blocking syscall | Scheduler wait | GC sweeping | GC pause | |
---|---|---|---|---|---|---|---|---|---|
{{.ID}} | {{prettyDuration .TotalTime}} | {{prettyDuration .ExecTime}} | {{prettyDuration .IOTime}} | {{prettyDuration .BlockTime}} | {{prettyDuration .SyscallTime}} | {{prettyDuration .SchedWaitTime}} | {{prettyDuration .SweepTime}} {{percent .SweepTime .TotalTime}} | {{prettyDuration .GCTime}} {{percent .GCTime .TotalTime}} |