1 // Copyright (c) 2020 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
19 "git.fd.io/govpp.git/adapter"
26 // ErrStatDataLenIncorrect is returned when stat data does not match vector
27 // length of a respective data directory
28 ErrStatDataLenIncorrect = fmt.Errorf("stat data length incorrect")
32 MaxWaitInProgress = time.Millisecond * 100
33 CheckDelayInProgress = time.Microsecond * 10
43 statDirScalarIndex = 1
44 statDirCounterVectorSimple = 2
45 statDirCounterVectorCombined = 3
52 dirVector unsafe.Pointer
53 dirSegment unsafe.Pointer
58 // statSegment represents common API for every stats API version
59 type statSegment interface {
60 // GetDirectoryVector returns pointer to memory where the beginning
61 // of the data directory is located.
62 GetDirectoryVector() dirVector
64 // GetStatDirOnIndex accepts directory vector and particular index.
65 // Returns pointer to the beginning of the segment. Also the directory
66 // name as [128]byte and the directory type is returned for easy use
67 // without needing to know the exact segment version.
69 // Note that if the index is equal to 0, the result pointer points to
70 // the same memory address as the argument.
71 GetStatDirOnIndex(v dirVector, index uint32) (dirSegment, dirName, dirType)
73 // GetEpoch re-loads stats header and returns current epoch
74 //and 'inProgress' value
75 GetEpoch() (int64, bool)
77 // CopyEntryData accepts pointer to a directory segment and returns adapter.Stat
78 // based on directory type populated with data
79 CopyEntryData(segment dirSegment) adapter.Stat
81 // UpdateEntryData accepts pointer to a directory segment with data, and stat
83 UpdateEntryData(segment dirSegment, s *adapter.Stat) error
86 // vecHeader represents a vector header
87 type vecHeader struct {
92 func (t dirType) String() string {
93 return adapter.StatType(t).String()
96 func getVersion(data []byte) uint64 {
97 type apiVersion struct {
100 header := (*apiVersion)(unsafe.Pointer(&data[0]))
101 version := &apiVersion{
102 value: atomic.LoadUint64(&header.value),
104 debugf("stats API version loaded: %d", version.value)
108 func vectorLen(v dirVector) dirVector {
109 vec := *(*vecHeader)(unsafe.Pointer(uintptr(v) - unsafe.Sizeof(uint64(0))))
110 return dirVector(&vec.length)
114 func statSegPointer(v dirVector, offset uintptr) dirVector {
115 return dirVector(uintptr(v) + offset)