-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtrace.go
122 lines (108 loc) · 2.63 KB
/
trace.go
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
// Copyright (c) 2021 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 core
import (
"sort"
"sync"
"time"
"go.fd.io/govpp/api"
)
// default buffer size
const bufferSize = 100
// Trace is the default trace API implementation.
type Trace struct {
*sync.RWMutex
wg sync.WaitGroup
records []*api.Record
buffer chan *api.Record
index int
closeFunc func()
}
// NewTrace initializes the trace object, always bound to a GoVPP connection.
// The size limits the number of records stored.
// Initializing a new trace for the same connection replaces the old one and
// discards all values already collected.
func NewTrace(c *Connection, size int) (t *Trace) {
t = &Trace{
RWMutex: &sync.RWMutex{},
records: make([]*api.Record, size),
buffer: make(chan *api.Record, bufferSize),
}
c.traceLock.Lock()
c.trace = t
c.traceLock.Unlock()
t.closeFunc = func() {
c.trace = nil // no more records
close(t.buffer)
}
go func() {
for {
record, ok := <-t.buffer
if !ok {
return
}
if t.index < len(t.records) {
t.Lock()
t.records[t.index] = record
t.index++
t.Unlock()
}
t.wg.Done()
}
}()
return
}
func (t *Trace) GetRecords() (list []*api.Record) {
// it is supposed to wait until all API messages sent to the
// buffer are processed before returning the list
t.wg.Wait()
list = make([]*api.Record, t.index)
t.RLock()
copy(list, t.records[:t.index])
t.RUnlock()
sort.Slice(list, func(i, j int) bool {
return list[i].Timestamp.Before(list[j].Timestamp)
})
return list
}
func (t *Trace) GetRecordsForChannel(chId uint16) (list []*api.Record) {
records := t.GetRecords()
for _, record := range records {
if record.ChannelID == chId {
list = append(list, record)
}
}
return list
}
func (t *Trace) Clear() {
t.Lock()
t.records = make([]*api.Record, len(t.records))
t.index = 0
t.Unlock()
}
func (t *Trace) Close() {
t.closeFunc()
}
func (t *Trace) registerNew() (now time.Time, enabled bool) {
if t != nil {
t.wg.Add(1)
enabled = true
}
return time.Now(), enabled
}
func (t *Trace) send(record *api.Record) {
if t != nil {
t.buffer <- record
}
}