Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions adapter/stats_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package adapter

import (
"errors"
"fmt"
"strings"
)

const (
Expand Down Expand Up @@ -64,6 +66,7 @@ const (
Empty StatType = "Empty"
Symlink StatType = "Symlink"
GaugeIndex StatType = "GaugeIndex"
HistogramLog2 StatType = "HistogramLog2"
)

// StatDir defines directory of stats entries created by PrepareDir.
Expand Down Expand Up @@ -147,13 +150,24 @@ type NameStat []Name
// EmptyStat represents removed counter directory
type EmptyStat string

// HistogramLog2Bin represents histogram data.
// Counts[j] represents the histogram bucket with values in range [2^(MinExp + j), 2^(MinExp + j + 1) - 1].
type HistogramLog2Bin struct {
MinExp uint64
Counts []uint64
}

// HistogramLog2Stat represents stat for a log2 histogram. The array represents workers.
type HistogramLog2Stat []HistogramLog2Bin

func (ScalarStat) isStat() {}
func (ErrorStat) isStat() {}
func (SimpleCounterStat) isStat() {}
func (CombinedCounterStat) isStat() {}
func (NameStat) isStat() {}
func (EmptyStat) isStat() {}
func (GaugeStat) isStat() {}
func (HistogramLog2Stat) isStat() {}

func (s ScalarStat) IsZero() bool {
return s == 0
Expand Down Expand Up @@ -268,3 +282,35 @@ func (s GaugeStat) IsZero() bool {
func (s GaugeStat) Type() StatType {
return GaugeIndex
}

func (s HistogramLog2Stat) IsZero() bool {
if s == nil {
return true
}
for _, bins := range s {
for _, c := range bins.Counts {
if c != 0 {
return false
}
}
}
return true
}

func (s HistogramLog2Stat) Type() StatType {
return HistogramLog2
}

func (s HistogramLog2Stat) String() string {
var b strings.Builder
for i, bin := range s {
fmt.Fprintf(&b, "\n [thread %d]: min_exp=%d", i, bin.MinExp)
var cumulative uint64
for j, count := range bin.Counts {
cumulative += count
fmt.Fprintf(&b, "\n <= %d: %d (cumulative: %d)",
uint64(1)<<(bin.MinExp+uint64(j)), count, cumulative)
}
}
return b.String()
}
1 change: 1 addition & 0 deletions adapter/statsclient/stat_segment_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var dirTypeMapping = map[dirType]adapter.StatType{
4: adapter.NameVector,
5: adapter.Empty,
6: adapter.Symlink,
7: adapter.HistogramLog2,
9: adapter.GaugeIndex,
}

Expand Down
44 changes: 44 additions & 0 deletions adapter/statsclient/statseg_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func (ss *statSegmentV2) CopyEntryData(segment dirSegment, index uint32) adapter
// stats for the required item
return ss.CopyEntryData(statSegDir2, i2)

case adapter.HistogramLog2:
return ss.copyHistogramLog2Data(dirEntry)

case adapter.GaugeIndex:
return adapter.GaugeStat(dirEntry.unionData)

Expand Down Expand Up @@ -374,6 +377,14 @@ func (ss *statSegmentV2) UpdateEntryData(segment dirSegment, stat *adapter.Stat)
}
}

case adapter.HistogramLog2Stat:
histStat := ss.copyHistogramLog2Data(dirEntry)
if histStat == nil {
debugf("failed to read histogram log2 data for %s", dirEntry.name)
return ErrStatDataLenIncorrect
}
*stat = histStat

case adapter.GaugeStat:
*stat = adapter.GaugeStat(dirEntry.unionData)

Expand All @@ -385,6 +396,39 @@ func (ss *statSegmentV2) UpdateEntryData(segment dirSegment, stat *adapter.Stat)
return nil
}

func (ss *statSegmentV2) copyHistogramLog2Data(dirEntry *statSegDirectoryEntryV2) adapter.Stat {
dirVector := ss.adjust(dirVector(&dirEntry.unionData))
if dirVector == nil {
debugf("data vector pointer is out of range for %s", dirEntry.name)
return nil
}
vecLen := *(*uint32)(vectorLen(dirVector))
data := make(adapter.HistogramLog2Stat, vecLen)
// Iterate over each worker's vector of bins
for i := uint32(0); i < vecLen; i++ {
counterVectorOffset := statSegPointer(dirVector, uintptr(i+1)*unsafe.Sizeof(uint64(0)))
counterVector := ss.adjust(vectorLen(counterVectorOffset))
if counterVector == nil {
debugf("histogram log2 pointer out of range for thread %d", i)
continue
}
counterVectorLength := *(*uint32)(vectorLen(counterVector))
if counterVectorLength < 1 {
continue
}

// Per thread vectors: bins[0] = min_exp, bins[1:] = bin counts.
data[i].MinExp = *(*uint64)(statSegPointer(counterVector, 0))
binCount := counterVectorLength - 1
data[i].Counts = make([]uint64, binCount)
for j := uint32(0); j < binCount; j++ {
offset := uintptr(j+1) * unsafe.Sizeof(uint64(0))
data[i].Counts[j] = *(*uint64)(statSegPointer(counterVector, offset))
}
}
return data
}

// Adjust data pointer using shared header and base and return
// the pointer to a data segment
func (ss *statSegmentV2) adjust(data dirVector) dirVector {
Expand Down
Loading