Change module name to go.fd.io/govpp
[govpp.git] / adapter / statsclient / statseg_v2.go
index 10bc5f5..f808112 100644 (file)
 package statsclient
 
 import (
+       "bytes"
+       "encoding/binary"
        "sync/atomic"
        "unsafe"
 
-       "git.fd.io/govpp.git/adapter"
+       "go.fd.io/govpp/adapter"
 )
 
 type statSegmentV2 struct {
@@ -38,6 +40,7 @@ type sharedHeaderV2 struct {
 type statSegDirectoryEntryV2 struct {
        directoryType dirType
        // unionData can represent:
+       // - symlink indexes
        // - index
        // - value
        // - pointer to data
@@ -69,7 +72,7 @@ func (ss *statSegmentV2) GetDirectoryVector() dirVector {
        return ss.adjust(dirVector(&header.dirVector))
 }
 
-func (ss *statSegmentV2) GetStatDirOnIndex(v dirVector, index uint32) (dirSegment, dirName, dirType) {
+func (ss *statSegmentV2) GetStatDirOnIndex(v dirVector, index uint32) (dirSegment, dirName, adapter.StatType) {
        statSegDir := dirSegment(uintptr(v) + uintptr(index)*unsafe.Sizeof(statSegDirectoryEntryV2{}))
        dir := (*statSegDirectoryEntryV2)(statSegDir)
        var name []byte
@@ -79,7 +82,7 @@ func (ss *statSegmentV2) GetStatDirOnIndex(v dirVector, index uint32) (dirSegmen
                        break
                }
        }
-       return statSegDir, name, dir.directoryType
+       return statSegDir, name, getStatType(dir.directoryType, ss.getErrorVector() != nil)
 }
 
 func (ss *statSegmentV2) GetEpoch() (int64, bool) {
@@ -87,18 +90,20 @@ func (ss *statSegmentV2) GetEpoch() (int64, bool) {
        return sh.epoch, sh.inProgress != 0
 }
 
-func (ss *statSegmentV2) CopyEntryData(segment dirSegment) adapter.Stat {
+func (ss *statSegmentV2) CopyEntryData(segment dirSegment, index uint32) adapter.Stat {
        dirEntry := (*statSegDirectoryEntryV2)(segment)
-       if dirEntry.unionData == 0 {
-               debugf("data value or pointer not defined for %s", dirEntry.name)
+       typ := getStatType(dirEntry.directoryType, ss.getErrorVector() != nil)
+       // skip zero pointer value
+       if typ != adapter.ScalarIndex && typ != adapter.Empty && typ != adapter.ErrorIndex && dirEntry.unionData == 0 {
+               debugf("data pointer not defined for %s", dirEntry.name)
                return nil
        }
 
-       switch adapter.StatType(dirEntry.directoryType) {
-       case statDirScalarIndex:
+       switch typ {
+       case adapter.ScalarIndex:
                return adapter.ScalarStat(dirEntry.unionData)
 
-       case statDirErrorIndex:
+       case adapter.ErrorIndex:
                dirVector := ss.getErrorVector()
                if dirVector == nil {
                        debugf("error vector pointer is out of range for %s", dirEntry.name)
@@ -119,7 +124,7 @@ func (ss *statSegmentV2) CopyEntryData(segment dirSegment) adapter.Stat {
                }
                return adapter.ErrorStat(errData)
 
-       case statDirCounterVectorSimple:
+       case adapter.SimpleCounterVector:
                dirVector := ss.adjust(dirVector(&dirEntry.unionData))
                if dirVector == nil {
                        debugf("data vector pointer is out of range for %s", dirEntry.name)
@@ -135,16 +140,26 @@ func (ss *statSegmentV2) CopyEntryData(segment dirSegment) adapter.Stat {
                                continue
                        }
                        counterVectorLength := *(*uint32)(vectorLen(counterVector))
-                       data[i] = make([]adapter.Counter, counterVectorLength)
-                       for j := uint32(0); j < counterVectorLength; j++ {
-                               offset := uintptr(j) * unsafe.Sizeof(adapter.Counter(0))
-                               val := *(*adapter.Counter)(statSegPointer(counterVector, offset))
-                               data[i][j] = val
+                       if index == ^uint32(0) {
+                               data[i] = make([]adapter.Counter, counterVectorLength)
+                               for j := uint32(0); j < counterVectorLength; j++ {
+                                       offset := uintptr(j) * unsafe.Sizeof(adapter.Counter(0))
+                                       data[i][j] = *(*adapter.Counter)(statSegPointer(counterVector, offset))
+                               }
+                       } else {
+                               data[i] = make([]adapter.Counter, 1) // expect single value
+                               for j := uint32(0); j < counterVectorLength; j++ {
+                                       offset := uintptr(j) * unsafe.Sizeof(adapter.Counter(0))
+                                       if index == j {
+                                               data[i][0] = *(*adapter.Counter)(statSegPointer(counterVector, offset))
+                                               break
+                                       }
+                               }
                        }
                }
                return adapter.SimpleCounterStat(data)
 
-       case statDirCounterVectorCombined:
+       case adapter.CombinedCounterVector:
                dirVector := ss.adjust(dirVector(&dirEntry.unionData))
                if dirVector == nil {
                        debugf("data vector pointer is out of range for %s", dirEntry.name)
@@ -160,16 +175,26 @@ func (ss *statSegmentV2) CopyEntryData(segment dirSegment) adapter.Stat {
                                continue
                        }
                        counterVectorLength := *(*uint32)(vectorLen(counterVector))
-                       data[i] = make([]adapter.CombinedCounter, counterVectorLength)
-                       for j := uint32(0); j < counterVectorLength; j++ {
-                               offset := uintptr(j) * unsafe.Sizeof(adapter.CombinedCounter{})
-                               val := *(*adapter.CombinedCounter)(statSegPointer(counterVector, offset))
-                               data[i][j] = val
+                       if index == ^uint32(0) {
+                               data[i] = make([]adapter.CombinedCounter, counterVectorLength)
+                               for j := uint32(0); j < counterVectorLength; j++ {
+                                       offset := uintptr(j) * unsafe.Sizeof(adapter.CombinedCounter{})
+                                       data[i][j] = *(*adapter.CombinedCounter)(statSegPointer(counterVector, offset))
+                               }
+                       } else {
+                               data[i] = make([]adapter.CombinedCounter, 1) // expect single value pair
+                               for j := uint32(0); j < counterVectorLength; j++ {
+                                       offset := uintptr(j) * unsafe.Sizeof(adapter.CombinedCounter{})
+                                       if index == j {
+                                               data[i][0] = *(*adapter.CombinedCounter)(statSegPointer(counterVector, offset))
+                                               break
+                                       }
+                               }
                        }
                }
                return adapter.CombinedCounterStat(data)
 
-       case statDirNameVector:
+       case adapter.NameVector:
                dirVector := ss.adjust(dirVector(&dirEntry.unionData))
                if dirVector == nil {
                        debugf("data vector pointer is out of range for %s", dirEntry.name)
@@ -201,10 +226,26 @@ func (ss *statSegmentV2) CopyEntryData(segment dirSegment) adapter.Stat {
                }
                return adapter.NameStat(data)
 
-       case statDirEmpty:
+       case adapter.Empty:
                return adapter.EmptyStat("<none>")
                // no-op
 
+       case adapter.Symlink:
+               // prevent recursion loops
+               if index != ^uint32(0) {
+                       debugf("received symlink with defined item index")
+                       return nil
+               }
+               i1, i2 := ss.getSymlinkIndexes(dirEntry)
+               // use first index to get the stats directory the symlink points to
+               header := ss.loadSharedHeader(ss.sharedHeader)
+               dirVector := ss.adjust(dirVector(&header.dirVector))
+               statSegDir2 := dirSegment(uintptr(dirVector) + uintptr(i1)*unsafe.Sizeof(statSegDirectoryEntryV2{}))
+
+               // retry with actual stats segment and use second index to get
+               // stats for the required item
+               return ss.CopyEntryData(statSegDir2, i2)
+
        default:
                // TODO: monitor occurrences with metrics
                debugf("Unknown type %d for stat entry: %q", dirEntry.directoryType, dirEntry.name)
@@ -349,3 +390,22 @@ func (ss *statSegmentV2) getErrorVector() dirVector {
        header := ss.loadSharedHeader(ss.sharedHeader)
        return ss.adjust(dirVector(&header.errorVector))
 }
+
+func (ss *statSegmentV2) getSymlinkIndexes(dirEntry *statSegDirectoryEntryV2) (index1, index2 uint32) {
+       var b bytes.Buffer
+       if err := binary.Write(&b, binary.LittleEndian, dirEntry.unionData); err != nil {
+               debugf("error getting symlink indexes for %s: %v", dirEntry.name, err)
+               return
+       }
+       if len(b.Bytes()) != 8 {
+               debugf("incorrect symlink union data length for %s: expected 8, got %d", dirEntry.name, len(b.Bytes()))
+               return
+       }
+       for i := range b.Bytes()[:4] {
+               index1 += uint32(b.Bytes()[i]) << (uint32(i) * 8)
+       }
+       for i := range b.Bytes()[4:] {
+               index2 += uint32(b.Bytes()[i+4]) << (uint32(i) * 8)
+       }
+       return
+}