|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate" |
| 5 | + |
| 6 | +import ( |
| 7 | + "math" |
| 8 | + "runtime" |
| 9 | + "sync/atomic" |
| 10 | +) |
| 11 | + |
| 12 | +// atomicCounter is an efficient way of adding to a number which is either an |
| 13 | +// int64 or float64. It is designed to be efficient when adding whole |
| 14 | +// numbers, regardless of whether N is an int64 or float64. |
| 15 | +// |
| 16 | +// Inspired by the Prometheus counter implementation: |
| 17 | +// https://github.com/prometheus/client_golang/blob/14ccb93091c00f86b85af7753100aa372d63602b/prometheus/counter.go#L108 |
| 18 | +type atomicCounter[N int64 | float64] struct { |
| 19 | + // nFloatBits contains only the non-integer portion of the counter. |
| 20 | + nFloatBits atomic.Uint64 |
| 21 | + // nInt contains only the integer portion of the counter. |
| 22 | + nInt atomic.Int64 |
| 23 | +} |
| 24 | + |
| 25 | +// load returns the current value. The caller must ensure all calls to add have |
| 26 | +// returned prior to calling load. |
| 27 | +func (n *atomicCounter[N]) load() N { |
| 28 | + fval := math.Float64frombits(n.nFloatBits.Load()) |
| 29 | + ival := n.nInt.Load() |
| 30 | + return N(fval + float64(ival)) |
| 31 | +} |
| 32 | + |
| 33 | +func (n *atomicCounter[N]) add(value N) { |
| 34 | + ival := int64(value) |
| 35 | + // This case is where the value is an int, or if it is a whole-numbered float. |
| 36 | + if float64(ival) == float64(value) { |
| 37 | + n.nInt.Add(ival) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // Value must be a float below. |
| 42 | + for { |
| 43 | + oldBits := n.nFloatBits.Load() |
| 44 | + newBits := math.Float64bits(math.Float64frombits(oldBits) + float64(value)) |
| 45 | + if n.nFloatBits.CompareAndSwap(oldBits, newBits) { |
| 46 | + return |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// hotColdWaitGroup is a synchronization primitive which enables lockless |
| 52 | +// writes for concurrent writers and enables a reader to acquire exclusive |
| 53 | +// access to a snapshot of state including only completed operations. |
| 54 | +// Conceptually, it can be thought of as a "hot" wait group, |
| 55 | +// and a "cold" wait group, with the ability for the reader to atomically swap |
| 56 | +// the hot and cold wait groups, and wait for the now-cold wait group to |
| 57 | +// complete. |
| 58 | +// |
| 59 | +// Inspired by the prometheus/client_golang histogram implementation: |
| 60 | +// https://github.com/prometheus/client_golang/blob/a974e0d45e0aa54c65492559114894314d8a2447/prometheus/histogram.go#L725 |
| 61 | +// |
| 62 | +// Usage: |
| 63 | +// |
| 64 | +// var hcwg hotColdWaitGroup |
| 65 | +// var data [2]any |
| 66 | +// |
| 67 | +// func write() { |
| 68 | +// hotIdx := hcwg.start() |
| 69 | +// defer hcwg.done(hotIdx) |
| 70 | +// // modify data without locking |
| 71 | +// data[hotIdx].update() |
| 72 | +// } |
| 73 | +// |
| 74 | +// func read() { |
| 75 | +// coldIdx := hcwg.swapHotAndWait() |
| 76 | +// // read data now that all writes to the cold data have completed. |
| 77 | +// data[coldIdx].read() |
| 78 | +// } |
| 79 | +type hotColdWaitGroup struct { |
| 80 | + // startedCountAndHotIdx contains a 63-bit counter in the lower bits, |
| 81 | + // and a 1 bit hot index to denote which of the two data-points new |
| 82 | + // measurements to write to. These are contained together so that read() |
| 83 | + // can atomically swap the hot bit, reset the started writes to zero, and |
| 84 | + // read the number writes that were started prior to the hot bit being |
| 85 | + // swapped. |
| 86 | + startedCountAndHotIdx atomic.Uint64 |
| 87 | + // endedCounts is the number of writes that have completed to each |
| 88 | + // dataPoint. |
| 89 | + endedCounts [2]atomic.Uint64 |
| 90 | +} |
| 91 | + |
| 92 | +// start returns the hot index that the writer should write to. The returned |
| 93 | +// hot index is 0 or 1. The caller must call done(hot index) after it finishes |
| 94 | +// its operation. start() is safe to call concurrently with other methods. |
| 95 | +func (l *hotColdWaitGroup) start() uint64 { |
| 96 | + // We increment h.startedCountAndHotIdx so that the counter in the lower |
| 97 | + // 63 bits gets incremented. At the same time, we get the new value |
| 98 | + // back, which we can use to return the currently-hot index. |
| 99 | + return l.startedCountAndHotIdx.Add(1) >> 63 |
| 100 | +} |
| 101 | + |
| 102 | +// done signals to the reader that an operation has fully completed. |
| 103 | +// done is safe to call concurrently. |
| 104 | +func (l *hotColdWaitGroup) done(hotIdx uint64) { |
| 105 | + l.endedCounts[hotIdx].Add(1) |
| 106 | +} |
| 107 | + |
| 108 | +// swapHotAndWait swaps the hot bit, waits for all start() calls to be done(), |
| 109 | +// and then returns the now-cold index for the reader to read from. The |
| 110 | +// returned index is 0 or 1. swapHotAndWait must not be called concurrently. |
| 111 | +func (l *hotColdWaitGroup) swapHotAndWait() uint64 { |
| 112 | + n := l.startedCountAndHotIdx.Load() |
| 113 | + coldIdx := (^n) >> 63 |
| 114 | + // Swap the hot and cold index while resetting the started measurements |
| 115 | + // count to zero. |
| 116 | + n = l.startedCountAndHotIdx.Swap((coldIdx << 63)) |
| 117 | + hotIdx := n >> 63 |
| 118 | + startedCount := n & ((1 << 63) - 1) |
| 119 | + // Wait for all measurements to the previously-hot map to finish. |
| 120 | + for startedCount != l.endedCounts[hotIdx].Load() { |
| 121 | + runtime.Gosched() // Let measurements complete. |
| 122 | + } |
| 123 | + // reset the number of ended operations |
| 124 | + l.endedCounts[hotIdx].Store(0) |
| 125 | + return hotIdx |
| 126 | +} |
0 commit comments