|
| 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" |
| 10 | + "sync/atomic" |
| 11 | + |
| 12 | + "go.opentelemetry.io/otel/attribute" |
| 13 | +) |
| 14 | + |
| 15 | +// atomicCounter is an efficient way of adding to a number which is either an |
| 16 | +// int64 or float64. It is designed to be efficient when adding whole |
| 17 | +// numbers, regardless of whether N is an int64 or float64. |
| 18 | +// |
| 19 | +// Inspired by the Prometheus counter implementation: |
| 20 | +// https://github.com/prometheus/client_golang/blob/14ccb93091c00f86b85af7753100aa372d63602b/prometheus/counter.go#L108 |
| 21 | +type atomicCounter[N int64 | float64] struct { |
| 22 | + // nFloatBits contains only the non-integer portion of the counter. |
| 23 | + nFloatBits atomic.Uint64 |
| 24 | + // nInt contains only the integer portion of the counter. |
| 25 | + nInt atomic.Int64 |
| 26 | +} |
| 27 | + |
| 28 | +// load returns the current value. The caller must ensure all calls to add have |
| 29 | +// returned prior to calling load. |
| 30 | +func (n *atomicCounter[N]) load() N { |
| 31 | + fval := math.Float64frombits(n.nFloatBits.Load()) |
| 32 | + ival := n.nInt.Load() |
| 33 | + return N(fval + float64(ival)) |
| 34 | +} |
| 35 | + |
| 36 | +func (n *atomicCounter[N]) add(value N) { |
| 37 | + ival := int64(value) |
| 38 | + // This case is where the value is an int, or if it is a whole-numbered float. |
| 39 | + if float64(ival) == float64(value) { |
| 40 | + n.nInt.Add(ival) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + // Value must be a float below. |
| 45 | + for { |
| 46 | + oldBits := n.nFloatBits.Load() |
| 47 | + newBits := math.Float64bits(math.Float64frombits(oldBits) + float64(value)) |
| 48 | + if n.nFloatBits.CompareAndSwap(oldBits, newBits) { |
| 49 | + return |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// hotColdWaitGroup is a synchronization primitive which enables lockless |
| 55 | +// writes for concurrent writers and enables a reader to acquire exclusive |
| 56 | +// access to a snapshot of state including only completed operations. |
| 57 | +// Conceptually, it can be thought of as a "hot" wait group, |
| 58 | +// and a "cold" wait group, with the ability for the reader to atomically swap |
| 59 | +// the hot and cold wait groups, and wait for the now-cold wait group to |
| 60 | +// complete. |
| 61 | +// |
| 62 | +// Inspired by the prometheus/client_golang histogram implementation: |
| 63 | +// https://github.com/prometheus/client_golang/blob/a974e0d45e0aa54c65492559114894314d8a2447/prometheus/histogram.go#L725 |
| 64 | +// |
| 65 | +// Usage: |
| 66 | +// |
| 67 | +// var hcwg hotColdWaitGroup |
| 68 | +// var data [2]any |
| 69 | +// |
| 70 | +// func write() { |
| 71 | +// hotIdx := hcwg.start() |
| 72 | +// defer hcwg.done(hotIdx) |
| 73 | +// // modify data without locking |
| 74 | +// data[hotIdx].update() |
| 75 | +// } |
| 76 | +// |
| 77 | +// func read() { |
| 78 | +// coldIdx := hcwg.swapHotAndWait() |
| 79 | +// // read data now that all writes to the cold data have completed. |
| 80 | +// data[coldIdx].read() |
| 81 | +// } |
| 82 | +type hotColdWaitGroup struct { |
| 83 | + // startedCountAndHotIdx contains a 63-bit counter in the lower bits, |
| 84 | + // and a 1 bit hot index to denote which of the two data-points new |
| 85 | + // measurements to write to. These are contained together so that read() |
| 86 | + // can atomically swap the hot bit, reset the started writes to zero, and |
| 87 | + // read the number writes that were started prior to the hot bit being |
| 88 | + // swapped. |
| 89 | + startedCountAndHotIdx atomic.Uint64 |
| 90 | + // endedCounts is the number of writes that have completed to each |
| 91 | + // dataPoint. |
| 92 | + endedCounts [2]atomic.Uint64 |
| 93 | +} |
| 94 | + |
| 95 | +// start returns the hot index that the writer should write to. The returned |
| 96 | +// hot index is 0 or 1. The caller must call done(hot index) after it finishes |
| 97 | +// its operation. start() is safe to call concurrently with other methods. |
| 98 | +func (l *hotColdWaitGroup) start() uint64 { |
| 99 | + // We increment h.startedCountAndHotIdx so that the counter in the lower |
| 100 | + // 63 bits gets incremented. At the same time, we get the new value |
| 101 | + // back, which we can use to return the currently-hot index. |
| 102 | + return l.startedCountAndHotIdx.Add(1) >> 63 |
| 103 | +} |
| 104 | + |
| 105 | +// done signals to the reader that an operation has fully completed. |
| 106 | +// done is safe to call concurrently. |
| 107 | +func (l *hotColdWaitGroup) done(hotIdx uint64) { |
| 108 | + l.endedCounts[hotIdx].Add(1) |
| 109 | +} |
| 110 | + |
| 111 | +// swapHotAndWait swaps the hot bit, waits for all start() calls to be done(), |
| 112 | +// and then returns the now-cold index for the reader to read from. The |
| 113 | +// returned index is 0 or 1. swapHotAndWait must not be called concurrently. |
| 114 | +func (l *hotColdWaitGroup) swapHotAndWait() uint64 { |
| 115 | + n := l.startedCountAndHotIdx.Load() |
| 116 | + coldIdx := (^n) >> 63 |
| 117 | + // Swap the hot and cold index while resetting the started measurements |
| 118 | + // count to zero. |
| 119 | + n = l.startedCountAndHotIdx.Swap((coldIdx << 63)) |
| 120 | + hotIdx := n >> 63 |
| 121 | + startedCount := n & ((1 << 63) - 1) |
| 122 | + // Wait for all measurements to the previously-hot map to finish. |
| 123 | + for startedCount != l.endedCounts[hotIdx].Load() { |
| 124 | + runtime.Gosched() // Let measurements complete. |
| 125 | + } |
| 126 | + // reset the number of ended operations |
| 127 | + l.endedCounts[hotIdx].Store(0) |
| 128 | + return hotIdx |
| 129 | +} |
| 130 | + |
| 131 | +// limitedSyncMap is a sync.Map which enforces the aggregation limit on |
| 132 | +// attribute sets and provides a Len() function. |
| 133 | +type limitedSyncMap struct { |
| 134 | + sync.Map |
| 135 | + aggLimit int |
| 136 | + len int |
| 137 | + lenMux sync.Mutex |
| 138 | +} |
| 139 | + |
| 140 | +func (m *limitedSyncMap) LoadOrStoreAttr(fltrAttr attribute.Set, newValue func(attribute.Set) any) any { |
| 141 | + actual, loaded := m.Load(fltrAttr.Equivalent()) |
| 142 | + if loaded { |
| 143 | + return actual |
| 144 | + } |
| 145 | + // If the overflow set exists, assume we have already overflowed and don't |
| 146 | + // bother with the slow path below. |
| 147 | + actual, loaded = m.Load(overflowSet.Equivalent()) |
| 148 | + if loaded { |
| 149 | + return actual |
| 150 | + } |
| 151 | + // Slow path: add a new attribute set. |
| 152 | + m.lenMux.Lock() |
| 153 | + defer m.lenMux.Unlock() |
| 154 | + |
| 155 | + // re-fetch now that we hold the lock to ensure we don't use the overflow |
| 156 | + // set unless we are sure the attribute set isn't being written |
| 157 | + // concurrently. |
| 158 | + actual, loaded = m.Load(fltrAttr.Equivalent()) |
| 159 | + if loaded { |
| 160 | + return actual |
| 161 | + } |
| 162 | + |
| 163 | + if m.aggLimit > 0 && m.len >= m.aggLimit-1 { |
| 164 | + fltrAttr = overflowSet |
| 165 | + } |
| 166 | + actual, loaded = m.LoadOrStore(fltrAttr.Equivalent(), newValue(fltrAttr)) |
| 167 | + if !loaded { |
| 168 | + m.len++ |
| 169 | + } |
| 170 | + return actual |
| 171 | +} |
| 172 | + |
| 173 | +func (m *limitedSyncMap) Clear() { |
| 174 | + m.lenMux.Lock() |
| 175 | + defer m.lenMux.Unlock() |
| 176 | + m.len = 0 |
| 177 | + m.Map.Clear() |
| 178 | +} |
| 179 | + |
| 180 | +func (m *limitedSyncMap) Len() int { |
| 181 | + m.lenMux.Lock() |
| 182 | + defer m.lenMux.Unlock() |
| 183 | + return m.len |
| 184 | +} |
0 commit comments