diff --git a/adapter/stats_api.go b/adapter/stats_api.go index c374539d..bcbd4797 100644 --- a/adapter/stats_api.go +++ b/adapter/stats_api.go @@ -16,6 +16,8 @@ package adapter import ( "errors" + "fmt" + "strings" ) const ( @@ -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. @@ -147,6 +150,16 @@ 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() {} @@ -154,6 +167,7 @@ func (CombinedCounterStat) isStat() {} func (NameStat) isStat() {} func (EmptyStat) isStat() {} func (GaugeStat) isStat() {} +func (HistogramLog2Stat) isStat() {} func (s ScalarStat) IsZero() bool { return s == 0 @@ -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() +} diff --git a/adapter/statsclient/stat_segment_api.go b/adapter/statsclient/stat_segment_api.go index 8827ae88..7e45937e 100644 --- a/adapter/statsclient/stat_segment_api.go +++ b/adapter/statsclient/stat_segment_api.go @@ -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, } diff --git a/adapter/statsclient/statseg_v2.go b/adapter/statsclient/statseg_v2.go index 7b074c75..db67ea32 100644 --- a/adapter/statsclient/statseg_v2.go +++ b/adapter/statsclient/statseg_v2.go @@ -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) @@ -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) @@ -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 {