aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/trace/v2/goroutines.go
blob: 3cf366635af178f367bb63430f4626f459db423e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2023 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 trace

import (
	"cmp"
	"fmt"
	"html/template"
	"internal/trace"
	"internal/trace/traceviewer"
	tracev2 "internal/trace/v2"
	"log"
	"net/http"
	"slices"
	"sort"
	"strings"
	"time"
)

// GoroutinesHandlerFunc returns a HandlerFunc that serves list of goroutine groups.
func GoroutinesHandlerFunc(summaries map[tracev2.GoID]*trace.GoroutineSummary) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// goroutineGroup describes a group of goroutines grouped by name.
		type goroutineGroup struct {
			Name     string        // Start function.
			N        int           // Total number of goroutines in this group.
			ExecTime time.Duration // Total execution time of all goroutines in this group.
		}
		// Accumulate groups by Name.
		groupsByName := make(map[string]goroutineGroup)
		for _, summary := range summaries {
			group := groupsByName[summary.Name]
			group.Name = summary.Name
			group.N++
			group.ExecTime += summary.ExecTime
			groupsByName[summary.Name] = group
		}
		var groups []goroutineGroup
		for _, group := range groupsByName {
			groups = append(groups, group)
		}
		slices.SortFunc(groups, func(a, b goroutineGroup) int {
			return cmp.Compare(b.ExecTime, a.ExecTime)
		})
		w.Header().Set("Content-Type", "text/html;charset=utf-8")
		if err := templGoroutines.Execute(w, groups); err != nil {
			log.Printf("failed to execute template: %v", err)
			return
		}
	}
}

var templGoroutines = template.Must(template.New("").Parse(`
<html>
<style>` + traceviewer.CommonStyle + `
table {
  border-collapse: collapse;
}
td,
th {
  border: 1px solid black;
  padding-left: 8px;
  padding-right: 8px;
  padding-top: 4px;
  padding-bottom: 4px;
}
</style>
<body>
<h1>Goroutines</h1>
Below is a table of all goroutines in the trace grouped by start location and sorted by the total execution time of the group.<br>
<br>
Click a start location to view more details about that group.<br>
<br>
<table>
  <tr>
    <th>Start location</th>
	<th>Count</th>
	<th>Total execution time</th>
  </tr>
{{range $}}
  <tr>
    <td><code><a href="/goroutine?name={{.Name}}">{{or .Name "(Inactive, no stack trace sampled)"}}</a></code></td>
	<td>{{.N}}</td>
	<td>{{.ExecTime}}</td>
  </tr>
{{end}}
</table>
</body>
</html>
`))

// GoroutineHandler creates a handler that serves information about
// goroutines in a particular group.
func GoroutineHandler(summaries map[tracev2.GoID]*trace.GoroutineSummary) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		goroutineName := r.FormValue("name")

		type goroutine struct {
			*trace.GoroutineSummary
			NonOverlappingStats map[string]time.Duration
			HasRangeTime        bool
		}

		// Collect all the goroutines in the group.
		var (
			goroutines              []goroutine
			name                    string
			totalExecTime, execTime time.Duration
			maxTotalTime            time.Duration
		)
		validNonOverlappingStats := make(map[string]struct{})
		validRangeStats := make(map[string]struct{})
		for _, summary := range summaries {
			totalExecTime += summary.ExecTime

			if summary.Name != goroutineName {
				continue
			}
			nonOverlappingStats := summary.NonOverlappingStats()
			for name := range nonOverlappingStats {
				validNonOverlappingStats[name] = struct{}{}
			}
			var totalRangeTime time.Duration
			for name, dt := range summary.RangeTime {
				validRangeStats[name] = struct{}{}
				totalRangeTime += dt
			}
			goroutines = append(goroutines, goroutine{
				GoroutineSummary:    summary,
				NonOverlappingStats: nonOverlappingStats,
				HasRangeTime:        totalRangeTime != 0,
			})
			name = summary.Name
			execTime += summary.ExecTime
			if maxTotalTime < summary.TotalTime {
				maxTotalTime = summary.TotalTime
			}
		}

		// Compute the percent of total execution time these goroutines represent.
		execTimePercent := ""
		if totalExecTime > 0 {
			execTimePercent = fmt.Sprintf("%.2f%%", float64(execTime)/float64(totalExecTime)*100)
		}

		// Sort.
		sortBy := r.FormValue("sortby")
		if _, ok := validNonOverlappingStats[sortBy]; ok {
			slices.SortFunc(goroutines, func(a, b goroutine) int {
				return cmp.Compare(b.NonOverlappingStats[sortBy], a.NonOverlappingStats[sortBy])
			})
		} else {
			// Sort by total time by default.
			slices.SortFunc(goroutines, func(a, b goroutine) int {
				return cmp.Compare(b.TotalTime, a.TotalTime)
			})
		}

		// Write down all the non-overlapping stats and sort them.
		allNonOverlappingStats := make([]string, 0, len(validNonOverlappingStats))
		for name := range validNonOverlappingStats {
			allNonOverlappingStats = append(allNonOverlappingStats, name)
		}
		slices.SortFunc(allNonOverlappingStats, func(a, b string) int {
			if a == b {
				return 0
			}
			if a == "Execution time" {
				return -1
			}
			if b == "Execution time" {
				return 1
			}
			return cmp.Compare(a, b)
		})

		// Write down all the range stats and sort them.
		allRangeStats := make([]string, 0, len(validRangeStats))
		for name := range validRangeStats {
			allRangeStats = append(allRangeStats, name)
		}
		sort.Strings(allRangeStats)

		err := templGoroutine.Execute(w, struct {
			Name                string
			N                   int
			ExecTimePercent     string
			MaxTotal            time.Duration
			Goroutines          []goroutine
			NonOverlappingStats []string
			RangeStats          []string
		}{
			Name:                name,
			N:                   len(goroutines),
			ExecTimePercent:     execTimePercent,
			MaxTotal:            maxTotalTime,
			Goroutines:          goroutines,
			NonOverlappingStats: allNonOverlappingStats,
			RangeStats:          allRangeStats,
		})
		if err != nil {
			http.Error(w, fmt.Sprintf("failed to execute template: %v", err), http.StatusInternalServerError)
			return
		}
	}
}

func stat2Color(statName string) string {
	color := "#636363"
	if strings.HasPrefix(statName, "Block time") {
		color = "#d01c8b"
	}
	switch statName {
	case "Sched wait time":
		color = "#2c7bb6"
	case "Syscall execution time":
		color = "#7b3294"
	case "Execution time":
		color = "#d7191c"
	}
	return color
}

var templGoroutine = template.Must(template.New("").Funcs(template.FuncMap{
	"percent": func(dividend, divisor time.Duration) template.HTML {
		if divisor == 0 {
			return ""
		}
		return template.HTML(fmt.Sprintf("(%.1f%%)", float64(dividend)/float64(divisor)*100))
	},
	"headerStyle": func(statName string) template.HTMLAttr {
		return template.HTMLAttr(fmt.Sprintf("style=\"background-color: %s;\"", stat2Color(statName)))
	},
	"barStyle": func(statName string, dividend, divisor time.Duration) template.HTMLAttr {
		width := "0"
		if divisor != 0 {
			width = fmt.Sprintf("%.2f%%", float64(dividend)/float64(divisor)*100)
		}
		return template.HTMLAttr(fmt.Sprintf("style=\"width: %s; background-color: %s;\"", width, stat2Color(statName)))
	},
}).Parse(`
<!DOCTYPE html>
<title>Goroutines: {{.Name}}</title>
<style>` + traceviewer.CommonStyle + `
th {
  background-color: #050505;
  color: #fff;
}
th.link {
  cursor: pointer;
}
table {
  border-collapse: collapse;
}
td,
th {
  padding-left: 8px;
  padding-right: 8px;
  padding-top: 4px;
  padding-bottom: 4px;
}
.details tr:hover {
  background-color: #f2f2f2;
}
.details td {
  text-align: right;
  border: 1px solid black;
}
.details td.id {
  text-align: left;
}
.stacked-bar-graph {
  width: 300px;
  height: 10px;
  color: #414042;
  white-space: nowrap;
  font-size: 5px;
}
.stacked-bar-graph span {
  display: inline-block;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  float: left;
  padding: 0;
}
</style>

<script>
function reloadTable(key, value) {
  let params = new URLSearchParams(window.location.search);
  params.set(key, value);
  window.location.search = params.toString();
}
</script>

<h1>Goroutines</h1>

Table of contents
<ul>
	<li><a href="#summary">Summary</a></li>
	<li><a href="#breakdown">Breakdown</a></li>
	<li><a href="#ranges">Special ranges</a></li>
</ul>

<h3 id="summary">Summary</h3>

<table class="summary">
	<tr>
		<td>Goroutine start location:</td>
		<td><code>{{.Name}}</code></td>
	</tr>
	<tr>
		<td>Count:</td>
		<td>{{.N}}</td>
	</tr>
	<tr>
		<td>Execution Time:</td>
		<td>{{.ExecTimePercent}} of total program execution time </td>
	</tr>
	<tr>
		<td>Network wait profile:</td>
		<td> <a href="/io?name={{.Name}}">graph</a> <a href="/io?name={{.Name}}&raw=1" download="io.profile">(download)</a></td>
	</tr>
	<tr>
		<td>Sync block profile:</td>
		<td> <a href="/block?name={{.Name}}">graph</a> <a href="/block?name={{.Name}}&raw=1" download="block.profile">(download)</a></td>
	</tr>
	<tr>
		<td>Syscall profile:</td>
		<td> <a href="/syscall?name={{.Name}}">graph</a> <a href="/syscall?name={{.Name}}&raw=1" download="syscall.profile">(download)</a></td>
		</tr>
	<tr>
		<td>Scheduler wait profile:</td>
		<td> <a href="/sched?name={{.Name}}">graph</a> <a href="/sched?name={{.Name}}&raw=1" download="sched.profile">(download)</a></td>
	</tr>
</table>

<h3 id="breakdown">Breakdown</h3>

The table below breaks down where each goroutine is spent its time during the
traced period.
All of the columns except total time are non-overlapping.
<br>
<br>

<table class="details">
<tr>
<th> Goroutine</th>
<th class="link" onclick="reloadTable('sortby', 'Total time')"> Total</th>
<th></th>
{{range $.NonOverlappingStats}}
<th class="link" onclick="reloadTable('sortby', '{{.}}')" {{headerStyle .}}> {{.}}</th>
{{end}}
</tr>
{{range .Goroutines}}
	<tr>
		<td> <a href="/trace?goid={{.ID}}">{{.ID}}</a> </td>
		<td> {{ .TotalTime.String }} </td>
		<td>
			<div class="stacked-bar-graph">
			{{$Goroutine := .}}
			{{range $.NonOverlappingStats}}
				{{$Time := index $Goroutine.NonOverlappingStats .}}
				{{if $Time}}
					<span {{barStyle . $Time $.MaxTotal}}>&nbsp;</span>
				{{end}}
			{{end}}
			</div>
		</td>
		{{$Goroutine := .}}
		{{range $.NonOverlappingStats}}
			{{$Time := index $Goroutine.NonOverlappingStats .}}
			<td> {{$Time.String}}</td>
		{{end}}
	</tr>
{{end}}
</table>

<h3 id="ranges">Special ranges</h3>

The table below describes how much of the traced period each goroutine spent in
certain special time ranges.
If a goroutine has spent no time in any special time ranges, it is excluded from
the table.
For example, how much time it spent helping the GC. Note that these times do
overlap with the times from the first table.
In general the goroutine may not be executing in these special time ranges.
For example, it may have blocked while trying to help the GC.
This must be taken into account when interpreting the data.
<br>
<br>

<table class="details">
<tr>
<th> Goroutine</th>
<th> Total</th>
{{range $.RangeStats}}
<th {{headerStyle .}}> {{.}}</th>
{{end}}
</tr>
{{range .Goroutines}}
	{{if .HasRangeTime}}
		<tr>
			<td> <a href="/trace?goid={{.ID}}">{{.ID}}</a> </td>
			<td> {{ .TotalTime.String }} </td>
			{{$Goroutine := .}}
			{{range $.RangeStats}}
				{{$Time := index $Goroutine.RangeTime .}}
				<td> {{$Time.String}}</td>
			{{end}}
		</tr>
	{{end}}
{{end}}
</table>
`))