Skip to content

Commit 1ba25fa

Browse files
mknyszekdmitshur
authored andcommitted
[release-branch.go1.17] runtime: simplify histogram buckets considerably
There was an off-by-one error in the time histogram buckets calculation that caused the linear sub-buckets distances to be off by 2x. The fix was trivial, but in writing tests I realized there was a much simpler way to express the calculation for the histogram buckets, and took the opportunity to do that here. The new bucket calculation also fixes the bug. For #50732. Fixes #50734. Change-Id: Idae89986de1c415ee4e148f778e0e101ca003ade Reviewed-on: https://go-review.googlesource.com/c/go/+/380094 Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Michael Knyszek <mknyszek@google.com> Run-TryBot: Michael Knyszek <mknyszek@google.com> (cherry picked from commit 2e9dcb5) Reviewed-on: https://go-review.googlesource.com/c/go/+/384621 TryBot-Result: Gopher Robot <gobot@golang.org>
1 parent 7d70adf commit 1ba25fa

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

src/runtime/export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,8 @@ func (th *TimeHistogram) Record(duration int64) {
12171217
(*timeHistogram)(th).record(duration)
12181218
}
12191219

1220+
var TimeHistogramMetricsBuckets = timeHistogramMetricsBuckets
1221+
12201222
func SetIntArgRegs(a int) int {
12211223
lock(&finlock)
12221224
old := intArgRegs

src/runtime/histogram.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const (
4747
// │ └---- Next 4 bits -> sub-bucket 1
4848
// └------- Bit 5 set -> super-bucket 2
4949
//
50-
// Following this pattern, bucket 45 will have the bit 48 set. We don't
50+
// Following this pattern, super-bucket 44 will have the bit 47 set. We don't
5151
// have any buckets for higher values, so the highest sub-bucket will
5252
// contain values of 2^48-1 nanoseconds or approx. 3 days. This range is
5353
// more than enough to handle durations produced by the runtime.
@@ -139,36 +139,30 @@ func float64NegInf() float64 {
139139
func timeHistogramMetricsBuckets() []float64 {
140140
b := make([]float64, timeHistTotalBuckets+1)
141141
b[0] = float64NegInf()
142-
for i := 0; i < timeHistNumSuperBuckets; i++ {
143-
superBucketMin := uint64(0)
144-
// The (inclusive) minimum for the first non-negative bucket is 0.
145-
if i > 0 {
146-
// The minimum for the second bucket will be
147-
// 1 << timeHistSubBucketBits, indicating that all
148-
// sub-buckets are represented by the next timeHistSubBucketBits
149-
// bits.
150-
// Thereafter, we shift up by 1 each time, so we can represent
151-
// this pattern as (i-1)+timeHistSubBucketBits.
152-
superBucketMin = uint64(1) << uint(i-1+timeHistSubBucketBits)
153-
}
154-
// subBucketShift is the amount that we need to shift the sub-bucket
155-
// index to combine it with the bucketMin.
156-
subBucketShift := uint(0)
157-
if i > 1 {
158-
// The first two super buckets are exact with respect to integers,
159-
// so we'll never have to shift the sub-bucket index. Thereafter,
160-
// we shift up by 1 with each subsequent bucket.
161-
subBucketShift = uint(i - 2)
162-
}
142+
// Super-bucket 0 has no bits above timeHistSubBucketBits
143+
// set, so just iterate over each bucket and assign the
144+
// incrementing bucket.
145+
for i := 0; i < timeHistNumSubBuckets; i++ {
146+
bucketNanos := uint64(i)
147+
b[i+1] = float64(bucketNanos) / 1e9
148+
}
149+
// Generate the rest of the super-buckets. It's easier to reason
150+
// about if we cut out the 0'th bucket, so subtract one since
151+
// we just handled that bucket.
152+
for i := 0; i < timeHistNumSuperBuckets-1; i++ {
163153
for j := 0; j < timeHistNumSubBuckets; j++ {
164-
// j is the sub-bucket index. By shifting the index into position to
165-
// combine with the bucket minimum, we obtain the minimum value for that
166-
// sub-bucket.
167-
subBucketMin := superBucketMin + (uint64(j) << subBucketShift)
168-
169-
// Convert the subBucketMin which is in nanoseconds to a float64 seconds value.
154+
// Set the super-bucket bit.
155+
bucketNanos := uint64(1) << (i + timeHistSubBucketBits)
156+
// Set the sub-bucket bits.
157+
bucketNanos |= uint64(j) << i
158+
// The index for this bucket is going to be the (i+1)'th super bucket
159+
// (note that we're starting from zero, but handled the first super-bucket
160+
// earlier, so we need to compensate), and the j'th sub bucket.
161+
// Add 1 because we left space for -Inf.
162+
bucketIndex := (i+1)*timeHistNumSubBuckets + j + 1
163+
// Convert nanoseconds to seconds via a division.
170164
// These values will all be exactly representable by a float64.
171-
b[i*timeHistNumSubBuckets+j+1] = float64(subBucketMin) / 1e9
165+
b[bucketIndex] = float64(bucketNanos) / 1e9
172166
}
173167
}
174168
b[len(b)-1] = float64Inf()

src/runtime/histogram_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,43 @@ func TestTimeHistogram(t *testing.T) {
6868

6969
dummyTimeHistogram = TimeHistogram{}
7070
}
71+
72+
func TestTimeHistogramMetricsBuckets(t *testing.T) {
73+
buckets := TimeHistogramMetricsBuckets()
74+
75+
nonInfBucketsLen := TimeHistNumSubBuckets * TimeHistNumSuperBuckets
76+
expBucketsLen := nonInfBucketsLen + 2 // Count -Inf and +Inf.
77+
if len(buckets) != expBucketsLen {
78+
t.Fatalf("unexpected length of buckets: got %d, want %d", len(buckets), expBucketsLen)
79+
}
80+
// Check the first non-Inf 2*TimeHistNumSubBuckets buckets in order, skipping the
81+
// first bucket which should be -Inf (checked later).
82+
//
83+
// Because of the way this scheme works, the bottom TimeHistNumSubBuckets
84+
// buckets are fully populated, and then the next TimeHistNumSubBuckets
85+
// have the TimeHistSubBucketBits'th bit set, while the bottom are once
86+
// again fully populated.
87+
for i := 1; i <= 2*TimeHistNumSubBuckets+1; i++ {
88+
if got, want := buckets[i], float64(i-1)/1e9; got != want {
89+
t.Errorf("expected bucket %d to have value %e, got %e", i, want, got)
90+
}
91+
}
92+
// Check some values.
93+
idxToBucket := map[int]float64{
94+
0: math.Inf(-1),
95+
33: float64(0x10<<1) / 1e9,
96+
34: float64(0x11<<1) / 1e9,
97+
49: float64(0x10<<2) / 1e9,
98+
58: float64(0x19<<2) / 1e9,
99+
65: float64(0x10<<3) / 1e9,
100+
513: float64(0x10<<31) / 1e9,
101+
519: float64(0x16<<31) / 1e9,
102+
expBucketsLen - 2: float64(0x1f<<43) / 1e9,
103+
expBucketsLen - 1: math.Inf(1),
104+
}
105+
for idx, bucket := range idxToBucket {
106+
if got, want := buckets[idx], bucket; got != want {
107+
t.Errorf("expected bucket %d to have value %e, got %e", idx, want, got)
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)