Change module name to go.fd.io/govpp
[govpp.git] / adapter / statsclient / stat_segment_api.go
index 0ca0c8b..2c2950f 100644 (file)
@@ -16,10 +16,11 @@ package statsclient
 
 import (
        "fmt"
-       "git.fd.io/govpp.git/adapter"
        "sync/atomic"
        "time"
        "unsafe"
+
+       "go.fd.io/govpp/adapter"
 )
 
 var (
@@ -38,25 +39,37 @@ const (
        maxVersion = 2
 )
 
-const (
-       statDirIllegal               = 0
-       statDirScalarIndex           = 1
-       statDirCounterVectorSimple   = 2
-       statDirCounterVectorCombined = 3
-       statDirErrorIndex            = 4
-       statDirNameVector            = 5
-       statDirEmpty                 = 6
-)
+var dirTypeMapping = map[dirType]adapter.StatType{
+       1: adapter.ScalarIndex,
+       2: adapter.SimpleCounterVector,
+       3: adapter.CombinedCounterVector,
+       4: adapter.NameVector,
+       5: adapter.Empty,
+       6: adapter.Symlink,
+}
 
-type statDirectoryType int32
+var dirTypeMappingLegacy = map[dirType]adapter.StatType{
+       1: adapter.ScalarIndex,
+       2: adapter.SimpleCounterVector,
+       3: adapter.CombinedCounterVector,
+       4: adapter.ErrorIndex,
+       5: adapter.NameVector,
+       6: adapter.Empty,
+       7: adapter.Symlink,
+}
 
-type statDirectoryName []byte
+type (
+       dirVector  unsafe.Pointer
+       dirSegment unsafe.Pointer
+       dirName    []byte
+       dirType    int32
+)
 
 // statSegment represents common API for every stats API version
 type statSegment interface {
        // GetDirectoryVector returns pointer to memory where the beginning
        // of the data directory is located.
-       GetDirectoryVector() unsafe.Pointer
+       GetDirectoryVector() dirVector
 
        // GetStatDirOnIndex accepts directory vector and particular index.
        // Returns pointer to the beginning of the segment. Also the directory
@@ -65,19 +78,21 @@ type statSegment interface {
        //
        // Note that if the index is equal to 0, the result pointer points to
        // the same memory address as the argument.
-       GetStatDirOnIndex(directory unsafe.Pointer, index uint32) (unsafe.Pointer, statDirectoryName, statDirectoryType)
+       GetStatDirOnIndex(v dirVector, index uint32) (dirSegment, dirName, adapter.StatType)
 
        // GetEpoch re-loads stats header and returns current epoch
        //and 'inProgress' value
        GetEpoch() (int64, bool)
 
        // CopyEntryData accepts pointer to a directory segment and returns adapter.Stat
-       // based on directory type populated with data
-       CopyEntryData(segment unsafe.Pointer) adapter.Stat
+       // based on directory type populated with data. The index is an optional parameter
+       // (used by symlinks) returning stats for item on the given index only.
+       // Use ^uint32(0) as an empty index (since 0 is a valid value).
+       CopyEntryData(segment dirSegment, index uint32) adapter.Stat
 
        // UpdateEntryData accepts pointer to a directory segment with data, and stat
        // segment to update
-       UpdateEntryData(segment unsafe.Pointer, s *adapter.Stat) error
+       UpdateEntryData(segment dirSegment, s *adapter.Stat) error
 }
 
 // vecHeader represents a vector header
@@ -86,10 +101,6 @@ type vecHeader struct {
        vectorData [0]uint8
 }
 
-func (t statDirectoryType) String() string {
-       return adapter.StatType(t).String()
-}
-
 func getVersion(data []byte) uint64 {
        type apiVersion struct {
                value uint64
@@ -102,12 +113,25 @@ func getVersion(data []byte) uint64 {
        return version.value
 }
 
-func vectorLen(v unsafe.Pointer) unsafe.Pointer {
+func vectorLen(v dirVector) dirVector {
        vec := *(*vecHeader)(unsafe.Pointer(uintptr(v) - unsafe.Sizeof(uint64(0))))
-       return unsafe.Pointer(&vec.length)
+       return dirVector(&vec.length)
+}
+
+func getStatType(dirTypeNum dirType, useLegacyMapping bool) (dirTyp adapter.StatType) {
+       var exists bool
+       if useLegacyMapping {
+               dirTyp, exists = dirTypeMappingLegacy[dirTypeNum]
+       } else {
+               dirTyp, exists = dirTypeMapping[dirTypeNum]
+       }
+       if exists {
+               return dirTyp
+       }
+       return adapter.Unknown
 }
 
 //go:nosplit
-func statSegPointer(p unsafe.Pointer, offset uintptr) unsafe.Pointer {
-       return unsafe.Pointer(uintptr(p) + offset)
+func statSegPointer(v dirVector, offset uintptr) dirVector {
+       return dirVector(uintptr(v) + offset)
 }