1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
// Copyright (c) 2019 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package statsclient
import (
"fmt"
"net"
"sync/atomic"
"syscall"
"time"
"unsafe"
"github.com/ftrvxmtrx/fd"
"git.fd.io/govpp.git/adapter"
)
var (
maxWaitInProgress = time.Second * 1
)
type statDirectoryType int32
func (t statDirectoryType) String() string {
return adapter.StatType(t).String()
}
type statSegDirectoryEntry struct {
directoryType statDirectoryType
// unionData can represent: offset, index or value
unionData uint64
offsetVector uint64
name [128]byte
}
type statSegment struct {
sharedHeader []byte
memorySize int64
// oldHeader defines version 0 for stat segment
// and is used for VPP 19.04
oldHeader bool
}
func (c *statSegment) connect(sockName string) error {
addr := &net.UnixAddr{
Net: "unixpacket",
Name: sockName,
}
Log.Debugf("connecting to: %v", addr)
conn, err := net.DialUnix(addr.Net, nil, addr)
if err != nil {
Log.Warnf("connecting to socket %s failed: %s", addr, err)
return err
}
defer func() {
if err := conn.Close(); err != nil {
Log.Warnf("closing socket failed: %v", err)
}
}()
Log.Debugf("connected to socket")
files, err := fd.Get(conn, 1, nil)
if err != nil {
return fmt.Errorf("getting file descriptor over socket failed: %v", err)
}
if len(files) == 0 {
return fmt.Errorf("no files received over socket")
}
defer func() {
for _, f := range files {
if err := f.Close(); err != nil {
Log.Warnf("closing file %s failed: %v", f.Name(), err)
}
}
}()
Log.Debugf("received %d files over socket", len(files))
f := files[0]
info, err := f.Stat()
if err != nil {
return err
}
size := info.Size()
Log.Debugf("fd: name=%v size=%v", info.Name(), size)
data, err := syscall.Mmap(int(f.Fd()), 0, int(size), syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
Log.Warnf("mapping shared memory failed: %v", err)
return fmt.Errorf("mapping shared memory failed: %v", err)
}
Log.Debugf("successfuly mapped shared memory")
c.sharedHeader = data
c.memorySize = size
header := c.readHeader()
Log.Debugf("stat segment header: %+v", header)
// older VPP (19.04) did not have version in stat segment header
// we try to provide fallback support by skipping it in header
if header.version > MaxVersion && header.inProgress > 1 && header.epoch == 0 {
h := c.readHeaderOld()
Log.Warnf("statsclient: falling back to old stat segment version (VPP 19.04): %+v", h)
c.oldHeader = true
}
return nil
}
func (c *statSegment) disconnect() error {
if err := syscall.Munmap(c.sharedHeader); err != nil {
Log.Warnf("unmapping shared memory failed: %v", err)
return fmt.Errorf("unmapping shared memory failed: %v", err)
}
Log.Debugf("successfuly unmapped shared memory")
return nil
}
func (c *statSegment) copyData(dirEntry *statSegDirectoryEntry) adapter.Stat {
switch typ := adapter.StatType(dirEntry.directoryType); typ {
case adapter.ScalarIndex:
return adapter.ScalarStat(dirEntry.unionData)
case adapter.ErrorIndex:
_, errOffset, _ := c.readOffsets()
offsetVector := unsafe.Pointer(&c.sharedHeader[errOffset])
var errData adapter.Counter
if c.oldHeader {
// error were not vector (per-worker) in VPP 19.04
offset := uintptr(dirEntry.unionData) * unsafe.Sizeof(uint64(0))
val := *(*adapter.Counter)(add(offsetVector, offset))
errData = val
} else {
vecLen := vectorLen(offsetVector)
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
offset := uintptr(cb) + uintptr(dirEntry.unionData)*unsafe.Sizeof(adapter.Counter(0))
val := *(*adapter.Counter)(add(unsafe.Pointer(&c.sharedHeader[0]), offset))
errData += val
}
}
return adapter.ErrorStat(errData)
case adapter.SimpleCounterVector:
if dirEntry.unionData == 0 {
Log.Debugf("\toffset is not valid")
break
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
Log.Debugf("\toffset out of range")
break
}
simpleCounter := unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]) // offset
vecLen := vectorLen(simpleCounter)
offsetVector := add(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
data := make([][]adapter.Counter, vecLen)
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
counterVec := unsafe.Pointer(&c.sharedHeader[uintptr(cb)])
vecLen2 := vectorLen(counterVec)
for j := uint64(0); j < vecLen2; j++ {
offset := uintptr(j) * unsafe.Sizeof(adapter.Counter(0))
val := *(*adapter.Counter)(add(counterVec, offset))
data[i] = append(data[i], val)
}
}
return adapter.SimpleCounterStat(data)
case adapter.CombinedCounterVector:
if dirEntry.unionData == 0 {
Log.Debugf("\toffset is not valid")
break
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
Log.Debugf("\toffset out of range")
break
}
combinedCounter := unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]) // offset
vecLen := vectorLen(combinedCounter)
offsetVector := add(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
data := make([][]adapter.CombinedCounter, vecLen)
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
counterVec := unsafe.Pointer(&c.sharedHeader[uintptr(cb)])
vecLen2 := vectorLen(counterVec)
for j := uint64(0); j < vecLen2; j++ {
offset := uintptr(j) * unsafe.Sizeof(adapter.CombinedCounter{})
val := *(*adapter.CombinedCounter)(add(counterVec, offset))
data[i] = append(data[i], val)
}
}
return adapter.CombinedCounterStat(data)
case adapter.NameVector:
if dirEntry.unionData == 0 {
Log.Debugf("\toffset is not valid")
break
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
Log.Debugf("\toffset out of range")
break
}
nameVector := unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]) // offset
vecLen := vectorLen(nameVector)
offsetVector := add(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
data := make([]adapter.Name, vecLen)
for i := uint64(0); i < vecLen; i++ {
cb := *(*uint64)(add(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
if cb == 0 {
Log.Debugf("\tname vector cb out of range")
continue
}
nameVec := unsafe.Pointer(&c.sharedHeader[cb])
vecLen2 := vectorLen(nameVec)
var nameStr []byte
for j := uint64(0); j < vecLen2; j++ {
offset := uintptr(j) * unsafe.Sizeof(byte(0))
val := *(*byte)(add(nameVec, offset))
if val > 0 {
nameStr = append(nameStr, val)
}
}
data[i] = adapter.Name(nameStr)
}
return adapter.NameStat(data)
default:
Log.Warnf("Unknown type %d for stat entry: %q", dirEntry.directoryType, dirEntry.name)
}
return nil
}
type sharedHeaderBase struct {
epoch int64
inProgress int64
directoryOffset int64
errorOffset int64
statsOffset int64
}
type statSegSharedHeader struct {
version uint64
sharedHeaderBase
}
func (c *statSegment) readHeaderOld() (header statSegSharedHeader) {
h := (*sharedHeaderBase)(unsafe.Pointer(&c.sharedHeader[0]))
header.version = 0
header.epoch = atomic.LoadInt64(&h.epoch)
header.inProgress = atomic.LoadInt64(&h.inProgress)
header.directoryOffset = atomic.LoadInt64(&h.directoryOffset)
header.errorOffset = atomic.LoadInt64(&h.errorOffset)
header.statsOffset = atomic.LoadInt64(&h.statsOffset)
return
}
func (c *statSegment) readHeader() (header statSegSharedHeader) {
h := (*statSegSharedHeader)(unsafe.Pointer(&c.sharedHeader[0]))
header.version = atomic.LoadUint64(&h.version)
header.epoch = atomic.LoadInt64(&h.epoch)
header.inProgress = atomic.LoadInt64(&h.inProgress)
header.directoryOffset = atomic.LoadInt64(&h.directoryOffset)
header.errorOffset = atomic.LoadInt64(&h.errorOffset)
header.statsOffset = atomic.LoadInt64(&h.statsOffset)
return
}
func (c *statSegment) readVersion() uint64 {
if c.oldHeader {
return 0
}
header := (*statSegSharedHeader)(unsafe.Pointer(&c.sharedHeader[0]))
version := atomic.LoadUint64(&header.version)
return version
}
func (c *statSegment) readEpoch() (int64, bool) {
if c.oldHeader {
h := c.readHeaderOld()
return h.epoch, h.inProgress != 0
}
header := (*statSegSharedHeader)(unsafe.Pointer(&c.sharedHeader[0]))
epoch := atomic.LoadInt64(&header.epoch)
inprog := atomic.LoadInt64(&header.inProgress)
return epoch, inprog != 0
}
func (c *statSegment) readOffsets() (dir, err, stat int64) {
if c.oldHeader {
h := c.readHeaderOld()
return h.directoryOffset, h.errorOffset, h.statsOffset
}
header := (*statSegSharedHeader)(unsafe.Pointer(&c.sharedHeader[0]))
dirOffset := atomic.LoadInt64(&header.directoryOffset)
errOffset := atomic.LoadInt64(&header.errorOffset)
statOffset := atomic.LoadInt64(&header.statsOffset)
return dirOffset, errOffset, statOffset
}
type statSegAccess struct {
epoch int64
}
func (c *statSegment) accessStart() *statSegAccess {
epoch, inprog := c.readEpoch()
t := time.Now()
for inprog {
if time.Since(t) > maxWaitInProgress {
return nil
}
epoch, inprog = c.readEpoch()
}
return &statSegAccess{
epoch: epoch,
}
}
func (c *statSegment) accessEnd(acc *statSegAccess) bool {
epoch, inprog := c.readEpoch()
if acc.epoch != epoch || inprog {
return false
}
return true
}
type vecHeader struct {
length uint64
vectorData [0]uint8
}
func vectorLen(v unsafe.Pointer) uint64 {
vec := *(*vecHeader)(unsafe.Pointer(uintptr(v) - unsafe.Sizeof(uintptr(0))))
return vec.length
}
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
|