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
51 type statDirectoryType int32
53 type statDirectoryName []byte
55 // statSegment represents common API for every stats API version
56 type statSegment interface {
57 // GetDirectoryVector returns pointer to memory where the beginning
58 // of the data directory is located.
59 GetDirectoryVector() unsafe.Pointer
61 // GetStatDirOnIndex accepts directory vector and particular index.
62 // Returns pointer to the beginning of the segment. Also the directory
63 // name as [128]byte and the directory type is returned for easy use
64 // without needing to know the exact segment version.
66 // Note that if the index is equal to 0, the result pointer points to
67 // the same memory address as the argument.
68 GetStatDirOnIndex(directory unsafe.Pointer, index uint32) (unsafe.Pointer, statDirectoryName, statDirectoryType)
70 // GetEpoch re-loads stats header and returns current epoch
71 //and 'inProgress' value
72 GetEpoch() (int64, bool)
74 // CopyEntryData accepts pointer to a directory segment and returns adapter.Stat
75 // based on directory type populated with data
76 CopyEntryData(segment unsafe.Pointer) adapter.Stat
78 // UpdateEntryData accepts pointer to a directory segment with data, and stat
80 UpdateEntryData(segment unsafe.Pointer, s *adapter.Stat) error
83 // vecHeader represents a vector header
84 type vecHeader struct {
89 func (t statDirectoryType) String() string {
90 return adapter.StatType(t).String()
93 func getVersion(data []byte) uint64 {
94 type apiVersion struct {
97 header := (*apiVersion)(unsafe.Pointer(&data[0]))
98 version := &apiVersion{
99 value: atomic.LoadUint64(&header.value),
101 debugf("stats API version loaded: %d", version.value)
105 func vectorLen(v unsafe.Pointer) unsafe.Pointer {
106 vec := *(*vecHeader)(unsafe.Pointer(uintptr(v) - unsafe.Sizeof(uint64(0))))
107 return unsafe.Pointer(&vec.length)
111 func statSegPointer(p unsafe.Pointer, offset uintptr) unsafe.Pointer {
112 return unsafe.Pointer(uintptr(p) + offset)