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
68 func (d StatType) String() string {
72 case SimpleCounterVector:
73 return "SimpleCounterVector"
74 case CombinedCounterVector:
75 return "CombinedCounterVector"
83 return fmt.Sprintf("UnknownStatType(%d)", d)
86 // StatDir defines directory of stats entries created by PrepareDir.
92 // StatIdentifier holds a stat entry name and index
93 type StatIdentifier struct {
98 // StatEntry represents single stat entry. The type of stat stored in Data
99 // is defined by Type.
100 type StatEntry struct {
106 // Counter represents simple counter with single value, which is usually packet count.
109 // CombinedCounter represents counter with two values, for packet count and bytes count.
110 type CombinedCounter [2]uint64
112 func (s CombinedCounter) Packets() uint64 {
116 func (s CombinedCounter) Bytes() uint64 {
120 // Name represents string value stored under name vector.
123 func (n Name) String() string {
127 // Stat represents some type of stat which is usually defined by StatType.
128 type Stat interface {
129 // IsZero returns true if all of its values equal to zero.
132 // isStat is intentionally unexported to limit implementations of interface to this package,
136 // ScalarStat represents stat for ScalarIndex.
137 type ScalarStat float64
139 // ErrorStat represents stat for ErrorIndex. The array represents workers.
140 type ErrorStat []Counter
142 // SimpleCounterStat represents stat for SimpleCounterVector.
143 // The outer array represents workers and the inner array represents interface/node/.. indexes.
144 // Values should be aggregated per interface/node for every worker.
145 // ReduceSimpleCounterStatIndex can be used to reduce specific index.
146 type SimpleCounterStat [][]Counter
148 // CombinedCounterStat represents stat for CombinedCounterVector.
149 // The outer array represents workers and the inner array represents interface/node/.. indexes.
150 // Values should be aggregated per interface/node for every worker.
151 // ReduceCombinedCounterStatIndex can be used to reduce specific index.
152 type CombinedCounterStat [][]CombinedCounter
154 // NameStat represents stat for NameVector.
157 // EmptyStat represents removed counter directory
158 type EmptyStat string
160 func (ScalarStat) isStat() {}
161 func (ErrorStat) isStat() {}
162 func (SimpleCounterStat) isStat() {}
163 func (CombinedCounterStat) isStat() {}
164 func (NameStat) isStat() {}
165 func (EmptyStat) isStat() {}
167 func (s ScalarStat) IsZero() bool {
170 func (s ErrorStat) IsZero() bool {
174 for _, ss := range s {
181 func (s SimpleCounterStat) IsZero() bool {
185 for _, ss := range s {
186 for _, sss := range ss {
194 func (s CombinedCounterStat) IsZero() bool {
198 for _, ss := range s {
202 for _, sss := range ss {
203 if sss[0] != 0 || sss[1] != 0 {
210 func (s NameStat) IsZero() bool {
214 for _, ss := range s {
221 func (s EmptyStat) IsZero() bool {
225 // ReduceSimpleCounterStatIndex returns reduced SimpleCounterStat s for index i.
226 func ReduceSimpleCounterStatIndex(s SimpleCounterStat, i int) uint64 {
228 for _, w := range s {
234 // ReduceCombinedCounterStatIndex returns reduced CombinedCounterStat s for index i.
235 func ReduceCombinedCounterStatIndex(s CombinedCounterStat, i int) [2]uint64 {
237 for _, w := range s {