From 944920c6966ef4e463f95066af563edd27ea7a94 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 21 May 2015 12:19:38 +0200 Subject: [PATCH] Ensure alignment of struct members used in sync.atomic functions. --- prometheus/histogram.go | 10 +++++++--- prometheus/value.go | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 27e9c5f..cba2929 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -213,6 +213,13 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr } type histogram struct { + // sumBits contains the bits of the float64 representing the sum of all + // observations. sumBits and count have to go first in the struct to + // guarantee alignment for atomic operations. + // http://golang.org/pkg/sync/atomic/#pkg-note-BUG + sumBits uint64 + count uint64 + SelfCollector // Note that there is no mutex required. @@ -222,9 +229,6 @@ type histogram struct { counts []uint64 labelPairs []*dto.LabelPair - - sumBits uint64 // The bits of the float64 representing the sum of all observations. - count uint64 } func (h *histogram) Desc() *Desc { diff --git a/prometheus/value.go b/prometheus/value.go index 107d43e..b54ac11 100644 --- a/prometheus/value.go +++ b/prometheus/value.go @@ -43,11 +43,15 @@ var errInconsistentCardinality = errors.New("inconsistent label cardinality") // ValueType. This is a low-level building block used by the library to back the // implementations of Counter, Gauge, and Untyped. type value struct { + // valBits containst the bits of the represented float64 value. It has + // to go first in the struct to guarantee alignment for atomic + // operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG + valBits uint64 + SelfCollector desc *Desc valType ValueType - valBits uint64 // These are the bits of the represented float64 value. labelPairs []*dto.LabelPair }