1 // Copyright (c) 2019 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.
23 // DefaultStatsSocket defines a default socket file path for VPP stats API.
24 DefaultStatsSocket = "/run/vpp/stats.sock"
28 ErrStatsDataBusy = errors.New("stats data busy")
29 ErrStatsDirStale = errors.New("stats dir stale")
30 ErrStatsDisconnected = errors.New("stats disconnected")
31 ErrStatsAccessFailed = errors.New("stats access failed")
34 // StatsAPI provides connection to VPP stats API.
35 type StatsAPI interface {
36 // Connect establishes client connection to the stats API.
38 // Disconnect terminates client connection.
41 // ListStats lists indexed names for stats matching patterns.
42 ListStats(patterns ...string) (indexes []StatIdentifier, err error)
43 // DumpStats dumps all stat entries.
44 DumpStats(patterns ...string) (entries []StatEntry, err error)
46 // PrepareDir prepares new stat dir for entries that match any of prefixes.
47 PrepareDir(patterns ...string) (*StatDir, error)
48 // PrepareDirOnIndex prepares new stat dir for entries that match any of indexes.
49 PrepareDirOnIndex(indexes ...uint32) (*StatDir, error)
50 // UpdateDir updates stat dir and all of their entries.
51 UpdateDir(dir *StatDir) error
54 // StatType represents type of stat directory and simply
55 // defines what type of stat data is stored in the stat entry.
60 ScalarIndex StatType = 1
61 SimpleCounterVector StatType = 2
62 CombinedCounterVector StatType = 3
63 ErrorIndex StatType = 4
64 NameVector StatType = 5
69 func (d StatType) String() string {
73 case SimpleCounterVector:
74 return "SimpleCounterVector"
75 case CombinedCounterVector:
76 return "CombinedCounterVector"
86 return fmt.Sprintf("UnknownStatType(%d)", d)
89 // StatDir defines directory of stats entries created by PrepareDir.
95 // StatIdentifier holds a stat entry name and index
96 type StatIdentifier struct {
101 // StatEntry represents single stat entry. The type of stat stored in Data
102 // is defined by Type.
103 type StatEntry struct {
110 // Counter represents simple counter with single value, which is usually packet count.
113 // CombinedCounter represents counter with two values, for packet count and bytes count.
114 type CombinedCounter [2]uint64
116 func (s CombinedCounter) Packets() uint64 {
120 func (s CombinedCounter) Bytes() uint64 {
124 // Name represents string value stored under name vector.
127 func (n Name) String() string {
131 // Stat represents some type of stat which is usually defined by StatType.
132 type Stat interface {
133 // IsZero returns true if all of its values equal to zero.
136 // Type returns underlying type of a stat
139 // isStat is intentionally unexported to limit implementations of interface to this package,
143 // ScalarStat represents stat for ScalarIndex.
144 type ScalarStat float64
146 // ErrorStat represents stat for ErrorIndex. The array represents workers.
147 type ErrorStat []Counter
149 // SimpleCounterStat represents indexed stat for SimpleCounterVector.
150 // The outer array represents workers and the inner array represents interface/node/.. indexes.
151 // Values should be aggregated per interface/node for every worker.
152 // ReduceSimpleCounterStatIndex can be used to reduce specific index.
153 type SimpleCounterStat [][]Counter
155 // CombinedCounterStat represents indexed stat for CombinedCounterVector.
156 // The outer array represents workers and the inner array represents interface/node/.. indexes.
157 // Values should be aggregated per interface/node for every worker.
158 // ReduceCombinedCounterStatIndex can be used to reduce specific index.
159 type CombinedCounterStat [][]CombinedCounter
161 // NameStat represents stat for NameVector.
164 // EmptyStat represents removed counter directory
165 type EmptyStat string
167 func (ScalarStat) isStat() {}
168 func (ErrorStat) isStat() {}
169 func (SimpleCounterStat) isStat() {}
170 func (CombinedCounterStat) isStat() {}
171 func (NameStat) isStat() {}
172 func (EmptyStat) isStat() {}
174 func (s ScalarStat) IsZero() bool {
178 func (s ScalarStat) Type() StatType {
182 func (s ErrorStat) IsZero() bool {
186 for _, ss := range s {
194 func (s ErrorStat) Type() StatType {
198 func (s SimpleCounterStat) IsZero() bool {
202 for _, ss := range s {
203 for _, sss := range ss {
212 func (s SimpleCounterStat) Type() StatType {
213 return SimpleCounterVector
216 func (s CombinedCounterStat) IsZero() bool {
220 for _, ss := range s {
224 for _, sss := range ss {
225 if sss[0] != 0 || sss[1] != 0 {
233 func (s CombinedCounterStat) Type() StatType {
234 return CombinedCounterVector
237 func (s NameStat) IsZero() bool {
241 for _, ss := range s {
249 func (s NameStat) Type() StatType {
253 func (s EmptyStat) IsZero() bool {
257 func (s EmptyStat) Type() StatType {
261 // ReduceSimpleCounterStatIndex returns reduced SimpleCounterStat s for index i.
262 func ReduceSimpleCounterStatIndex(s SimpleCounterStat, i int) uint64 {
264 for _, w := range s {
270 // ReduceCombinedCounterStatIndex returns reduced CombinedCounterStat s for index i.
271 func ReduceCombinedCounterStatIndex(s CombinedCounterStat, i int) [2]uint64 {
273 for _, w := range s {