Remove unnecessary allocation
[govpp.git] / adapter / statsclient / stat_segment_api.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
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:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package statsclient
16
17 import (
18         "fmt"
19         "git.fd.io/govpp.git/adapter"
20         "sync/atomic"
21         "time"
22         "unsafe"
23 )
24
25 var (
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")
29 )
30
31 var (
32         MaxWaitInProgress    = time.Millisecond * 100
33         CheckDelayInProgress = time.Microsecond * 10
34 )
35
36 const (
37         minVersion = 1
38         maxVersion = 2
39 )
40
41 const (
42         statDirIllegal               = 0
43         statDirScalarIndex           = 1
44         statDirCounterVectorSimple   = 2
45         statDirCounterVectorCombined = 3
46         statDirErrorIndex            = 4
47         statDirNameVector            = 5
48         statDirEmpty                 = 6
49 )
50
51 type statDirectoryType int32
52
53 type statDirectoryName []byte
54
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
60
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.
65         //
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)
69
70         // GetEpoch re-loads stats header and returns current epoch
71         //and 'inProgress' value
72         GetEpoch() (int64, bool)
73
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
77
78         // UpdateEntryData accepts pointer to a directory segment with data, and stat
79         // segment to update
80         UpdateEntryData(segment unsafe.Pointer, s *adapter.Stat) error
81 }
82
83 // vecHeader represents a vector header
84 type vecHeader struct {
85         length     uint64
86         vectorData [0]uint8
87 }
88
89 func (t statDirectoryType) String() string {
90         return adapter.StatType(t).String()
91 }
92
93 func getVersion(data []byte) uint64 {
94         type apiVersion struct {
95                 value uint64
96         }
97         header := (*apiVersion)(unsafe.Pointer(&data[0]))
98         version := &apiVersion{
99                 value: atomic.LoadUint64(&header.value),
100         }
101         debugf("stats API version loaded: %d", version.value)
102         return version.value
103 }
104
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)
108 }
109
110 //go:nosplit
111 func statSegPointer(p unsafe.Pointer, offset uintptr) unsafe.Pointer {
112         return unsafe.Pointer(uintptr(p) + offset)
113 }