smb: fix metric types and names, extend 32-bit counters in smb and smbclient (#2710)

Signed-off-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Jan-Otto Kröpke
2026-10-15 15:59:42 +02:00
committed by GitHub
co-authored by Claude
parent 39277da8e5
commit 9015955cfa
13 changed files with 492 additions and 34 deletions
+49
View File
@@ -39,3 +39,52 @@ func (c *Counter) AddValue(value uint32) {
func (c *Counter) Value() float64 {
return c.totalValue
}
// Counter32 extends a raw 32-bit performance counter, such as a
// PERF_COUNTER_COUNTER or the numerator of a PERF_AVERAGE_TIMER, beyond 2^32.
// Unlike Counter, it starts at the raw value and follows a restart of the
// source, so its value equals the raw value until the first wrap. It can only
// detect one wrap between two samples.
type Counter32 struct {
lastValue uint32
totalValue float64
restartBelow uint32
}
// NewCounter32 creates a Counter32 from the first raw value. If restartBelow is
// not zero, a decrease from a raw value below restartBelow is taken as a
// restart of the source instead of a wrap. Use it for counters that cannot
// grow by 2^32-restartBelow between two samples, such as request counts.
func NewCounter32(value float64, restartBelow uint32) Counter32 {
raw := toUint32(value)
return Counter32{
lastValue: raw,
totalValue: float64(raw),
restartBelow: restartBelow,
}
}
// Update adds a raw value and returns the extended value. reset reports that
// the source restarted, for example because a share was reconnected; the raw
// value is then taken as is. Any other decrease is a wrap, unless it starts
// below restartBelow.
func (c *Counter32) Update(value float64, reset bool) float64 {
raw := toUint32(value)
if reset || (raw < c.lastValue && c.lastValue < c.restartBelow) {
c.totalValue = float64(raw)
} else {
c.totalValue += float64(raw - c.lastValue)
}
c.lastValue = raw
return c.totalValue
}
// toUint32 converts through int64, so a sign-extended value keeps its low
// 32 bits on every architecture.
func toUint32(value float64) uint32 {
return uint32(int64(value))
}
+25
View File
@@ -55,3 +55,28 @@ func TestCounter(t *testing.T) {
require.Equal(t, float64(math.MaxUint32)*2-9, c.Value()) //nolint:testifylint
}
func TestCounter32(t *testing.T) {
t.Parallel()
c := utils.NewCounter32(100, 0)
require.Equal(t, 100.0, c.Update(100, false)) //nolint:testifylint
require.Equal(t, 150.0, c.Update(150, false)) //nolint:testifylint
require.Equal(t, float64(math.MaxUint32)-10, c.Update(math.MaxUint32-10, false)) //nolint:testifylint
require.Equal(t, float64(math.MaxUint32)+21, c.Update(20, false)) //nolint:testifylint
require.Equal(t, 5.0, c.Update(5, true)) //nolint:testifylint
require.Equal(t, 15.0, c.Update(15, false)) //nolint:testifylint
// Without restartBelow, any decrease is a wrap.
require.Equal(t, float64(math.MaxUint32)+4, c.Update(3, false)) //nolint:testifylint
}
func TestCounter32RestartBelow(t *testing.T) {
t.Parallel()
c := utils.NewCounter32(50, 1<<31)
require.Equal(t, 3.0, c.Update(3, false)) //nolint:testifylint
require.Equal(t, float64(math.MaxUint32)-10, c.Update(math.MaxUint32-10, false)) //nolint:testifylint
require.Equal(t, float64(math.MaxUint32)+21, c.Update(20, false)) //nolint:testifylint
}