Skip to content

Commit 7e32d8d

Browse files
committed
cmd/trace: factor out durationHistogram
This code will be useful for the new tracer, and there's no need to duplicate it. This change copies it to internal/trace/traceviewer, adds some comments, and renames it to TimeHistogram. While we're here, let's get rid of the unused String method which has a comment talking about how awful the rendering is. Also, let's get rid of uses of niceDuration. We'd have to bring it with us in the move and I don't think it's worth it. The difference between the default time.Duration rendering and the niceDuration rendering is usually a few extra digits of precision. Yes, it's noisier, but AFAICT it's not substantially worse. It doesn't seem worth the new API, even if it's just internal. We can also always bring it back later. For #60773. For #63960. Change-Id: I795f58f579f1d503c540c3a40bed12e52bce38e2 Reviewed-on: https://go-review.googlesource.com/c/go/+/542001 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Michael Knyszek <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 816ee0b commit 7e32d8d

File tree

3 files changed

+94
-121
lines changed

3 files changed

+94
-121
lines changed

src/cmd/trace/annotations.go

+7-120
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"fmt"
1010
"html/template"
1111
"internal/trace"
12+
"internal/trace/traceviewer"
1213
"log"
13-
"math"
1414
"net/http"
1515
"net/url"
1616
"reflect"
@@ -808,122 +808,9 @@ func newRegionFilter(r *http.Request) (*regionFilter, error) {
808808
}, nil
809809
}
810810

811-
type durationHistogram struct {
812-
Count int
813-
Buckets []int
814-
MinBucket, MaxBucket int
815-
}
816-
817-
// Five buckets for every power of 10.
818-
var logDiv = math.Log(math.Pow(10, 1.0/5))
819-
820-
func (h *durationHistogram) add(d time.Duration) {
821-
var bucket int
822-
if d > 0 {
823-
bucket = int(math.Log(float64(d)) / logDiv)
824-
}
825-
if len(h.Buckets) <= bucket {
826-
h.Buckets = append(h.Buckets, make([]int, bucket-len(h.Buckets)+1)...)
827-
h.Buckets = h.Buckets[:cap(h.Buckets)]
828-
}
829-
h.Buckets[bucket]++
830-
if bucket < h.MinBucket || h.MaxBucket == 0 {
831-
h.MinBucket = bucket
832-
}
833-
if bucket > h.MaxBucket {
834-
h.MaxBucket = bucket
835-
}
836-
h.Count++
837-
}
838-
839-
func (h *durationHistogram) BucketMin(bucket int) time.Duration {
840-
return time.Duration(math.Exp(float64(bucket) * logDiv))
841-
}
842-
843-
func niceDuration(d time.Duration) string {
844-
var rnd time.Duration
845-
var unit string
846-
switch {
847-
case d < 10*time.Microsecond:
848-
rnd, unit = time.Nanosecond, "ns"
849-
case d < 10*time.Millisecond:
850-
rnd, unit = time.Microsecond, "µs"
851-
case d < 10*time.Second:
852-
rnd, unit = time.Millisecond, "ms"
853-
default:
854-
rnd, unit = time.Second, "s "
855-
}
856-
return fmt.Sprintf("%d%s", d/rnd, unit)
857-
}
858-
859-
func (h *durationHistogram) ToHTML(urlmaker func(min, max time.Duration) string) template.HTML {
860-
if h == nil || h.Count == 0 {
861-
return template.HTML("")
862-
}
863-
864-
const barWidth = 400
865-
866-
maxCount := 0
867-
for _, count := range h.Buckets {
868-
if count > maxCount {
869-
maxCount = count
870-
}
871-
}
872-
873-
w := new(strings.Builder)
874-
fmt.Fprintf(w, `<table>`)
875-
for i := h.MinBucket; i <= h.MaxBucket; i++ {
876-
// Tick label.
877-
if h.Buckets[i] > 0 {
878-
fmt.Fprintf(w, `<tr><td class="histoTime" align="right"><a href=%s>%s</a></td>`, urlmaker(h.BucketMin(i), h.BucketMin(i+1)), niceDuration(h.BucketMin(i)))
879-
} else {
880-
fmt.Fprintf(w, `<tr><td class="histoTime" align="right">%s</td>`, niceDuration(h.BucketMin(i)))
881-
}
882-
// Bucket bar.
883-
width := h.Buckets[i] * barWidth / maxCount
884-
fmt.Fprintf(w, `<td><div style="width:%dpx;background:blue;position:relative">&nbsp;</div></td>`, width)
885-
// Bucket count.
886-
fmt.Fprintf(w, `<td align="right"><div style="position:relative">%d</div></td>`, h.Buckets[i])
887-
fmt.Fprintf(w, "</tr>\n")
888-
889-
}
890-
// Final tick label.
891-
fmt.Fprintf(w, `<tr><td align="right">%s</td></tr>`, niceDuration(h.BucketMin(h.MaxBucket+1)))
892-
fmt.Fprintf(w, `</table>`)
893-
return template.HTML(w.String())
894-
}
895-
896-
func (h *durationHistogram) String() string {
897-
const barWidth = 40
898-
899-
labels := []string{}
900-
maxLabel := 0
901-
maxCount := 0
902-
for i := h.MinBucket; i <= h.MaxBucket; i++ {
903-
// TODO: This formatting is pretty awful.
904-
label := fmt.Sprintf("[%-12s%-11s)", h.BucketMin(i).String()+",", h.BucketMin(i+1))
905-
labels = append(labels, label)
906-
if len(label) > maxLabel {
907-
maxLabel = len(label)
908-
}
909-
count := h.Buckets[i]
910-
if count > maxCount {
911-
maxCount = count
912-
}
913-
}
914-
915-
w := new(strings.Builder)
916-
for i := h.MinBucket; i <= h.MaxBucket; i++ {
917-
count := h.Buckets[i]
918-
bar := count * barWidth / maxCount
919-
fmt.Fprintf(w, "%*s %-*s %d\n", maxLabel, labels[i-h.MinBucket], barWidth, strings.Repeat("█", bar), count)
920-
}
921-
return w.String()
922-
}
923-
924811
type regionStats struct {
925812
regionTypeID
926-
Histogram durationHistogram
813+
Histogram traceviewer.TimeHistogram
927814
}
928815

929816
func (s *regionStats) UserRegionURL() func(min, max time.Duration) string {
@@ -933,7 +820,7 @@ func (s *regionStats) UserRegionURL() func(min, max time.Duration) string {
933820
}
934821

935822
func (s *regionStats) add(region regionDesc) {
936-
s.Histogram.add(region.duration())
823+
s.Histogram.Add(region.duration())
937824
}
938825

939826
var templUserRegionTypes = template.Must(template.New("").Parse(`
@@ -966,8 +853,8 @@ var templUserRegionTypes = template.Must(template.New("").Parse(`
966853

967854
type taskStats struct {
968855
Type string
969-
Count int // Complete + incomplete tasks
970-
Histogram durationHistogram // Complete tasks only
856+
Count int // Complete + incomplete tasks
857+
Histogram traceviewer.TimeHistogram // Complete tasks only
971858
}
972859

973860
func (s *taskStats) UserTaskURL(complete bool) func(min, max time.Duration) string {
@@ -979,7 +866,7 @@ func (s *taskStats) UserTaskURL(complete bool) func(min, max time.Duration) stri
979866
func (s *taskStats) add(task *taskDesc) {
980867
s.Count++
981868
if task.complete() {
982-
s.Histogram.add(task.duration())
869+
s.Histogram.Add(task.duration())
983870
}
984871
}
985872

@@ -1169,7 +1056,7 @@ func isUserAnnotationEvent(ev *trace.Event) (taskID uint64, ok bool) {
11691056
var templUserRegionType = template.Must(template.New("").Funcs(template.FuncMap{
11701057
"prettyDuration": func(nsec int64) template.HTML {
11711058
d := time.Duration(nsec) * time.Nanosecond
1172-
return template.HTML(niceDuration(d))
1059+
return template.HTML(d.String())
11731060
},
11741061
"percent": func(dividend, divisor int64) template.HTML {
11751062
if divisor == 0 {

src/cmd/trace/goroutines.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func httpGoroutine(w http.ResponseWriter, r *http.Request) {
169169
var templGoroutine = template.Must(template.New("").Funcs(template.FuncMap{
170170
"prettyDuration": func(nsec int64) template.HTML {
171171
d := time.Duration(nsec) * time.Nanosecond
172-
return template.HTML(niceDuration(d))
172+
return template.HTML(d.String())
173173
},
174174
"percent": func(dividend, divisor int64) template.HTML {
175175
if divisor == 0 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package traceviewer
6+
7+
import (
8+
"fmt"
9+
"html/template"
10+
"math"
11+
"strings"
12+
"time"
13+
)
14+
15+
// TimeHistogram is an high-dynamic-range histogram for durations.
16+
type TimeHistogram struct {
17+
Count int
18+
Buckets []int
19+
MinBucket, MaxBucket int
20+
}
21+
22+
// Five buckets for every power of 10.
23+
var logDiv = math.Log(math.Pow(10, 1.0/5))
24+
25+
// Add adds a single sample to the histogram.
26+
func (h *TimeHistogram) Add(d time.Duration) {
27+
var bucket int
28+
if d > 0 {
29+
bucket = int(math.Log(float64(d)) / logDiv)
30+
}
31+
if len(h.Buckets) <= bucket {
32+
h.Buckets = append(h.Buckets, make([]int, bucket-len(h.Buckets)+1)...)
33+
h.Buckets = h.Buckets[:cap(h.Buckets)]
34+
}
35+
h.Buckets[bucket]++
36+
if bucket < h.MinBucket || h.MaxBucket == 0 {
37+
h.MinBucket = bucket
38+
}
39+
if bucket > h.MaxBucket {
40+
h.MaxBucket = bucket
41+
}
42+
h.Count++
43+
}
44+
45+
// BucketMin returns the minimum duration value for a provided bucket.
46+
func (h *TimeHistogram) BucketMin(bucket int) time.Duration {
47+
return time.Duration(math.Exp(float64(bucket) * logDiv))
48+
}
49+
50+
// ToHTML renders the histogram as HTML.
51+
func (h *TimeHistogram) ToHTML(urlmaker func(min, max time.Duration) string) template.HTML {
52+
if h == nil || h.Count == 0 {
53+
return template.HTML("")
54+
}
55+
56+
const barWidth = 400
57+
58+
maxCount := 0
59+
for _, count := range h.Buckets {
60+
if count > maxCount {
61+
maxCount = count
62+
}
63+
}
64+
65+
w := new(strings.Builder)
66+
fmt.Fprintf(w, `<table>`)
67+
for i := h.MinBucket; i <= h.MaxBucket; i++ {
68+
// Tick label.
69+
if h.Buckets[i] > 0 {
70+
fmt.Fprintf(w, `<tr><td class="histoTime" align="right"><a href=%s>%s</a></td>`, urlmaker(h.BucketMin(i), h.BucketMin(i+1)), h.BucketMin(i))
71+
} else {
72+
fmt.Fprintf(w, `<tr><td class="histoTime" align="right">%s</td>`, h.BucketMin(i))
73+
}
74+
// Bucket bar.
75+
width := h.Buckets[i] * barWidth / maxCount
76+
fmt.Fprintf(w, `<td><div style="width:%dpx;background:blue;position:relative">&nbsp;</div></td>`, width)
77+
// Bucket count.
78+
fmt.Fprintf(w, `<td align="right"><div style="position:relative">%d</div></td>`, h.Buckets[i])
79+
fmt.Fprintf(w, "</tr>\n")
80+
81+
}
82+
// Final tick label.
83+
fmt.Fprintf(w, `<tr><td align="right">%s</td></tr>`, h.BucketMin(h.MaxBucket+1))
84+
fmt.Fprintf(w, `</table>`)
85+
return template.HTML(w.String())
86+
}

0 commit comments

Comments
 (0)