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.
22 // DefaultStatsSocket defines a default socket file path for VPP stats API.
23 DefaultStatsSocket = "/run/vpp/stats.sock"
27 ErrStatsDataBusy = errors.New("stats data busy")
28 ErrStatsDirStale = errors.New("stats dir stale")
29 ErrStatsDisconnected = errors.New("stats disconnected")
30 ErrStatsAccessFailed = errors.New("stats access failed")
33 // StatsAPI provides connection to VPP stats API.
34 type StatsAPI interface {
35 // Connect establishes client connection to the stats API.
37 // Disconnect terminates client connection.
40 // ListStats lists indexed names for stats matching patterns.
41 ListStats(patterns ...string) (indexes []StatIdentifier, err error)
42 // DumpStats dumps all stat entries.
43 DumpStats(patterns ...string) (entries []StatEntry, err error)
45 // PrepareDir prepares new stat dir for entries that match any of prefixes.
46 PrepareDir(patterns ...string) (*StatDir, error)
47 // PrepareDirOnIndex prepares new stat dir for entries that match any of indexes.
48 PrepareDirOnIndex(indexes ...uint32) (*StatDir, error)
49 // UpdateDir updates stat dir and all of their entries.
50 UpdateDir(dir *StatDir) error
53 // StatType represents type of stat directory and simply
54 // defines what type of stat data is stored in the stat entry.
58 Unknown StatType = "UnknownStatType"
59 ScalarIndex StatType = "ScalarIndex"
60 SimpleCounterVector StatType = "SimpleCounterVector"
61 CombinedCounterVector StatType = "CombinedCounterVector"
62 ErrorIndex StatType = "ErrorIndex"
63 NameVector StatType = "NameVector"
64 Empty StatType = "Empty"
65 Symlink StatType = "Symlink"
68 // StatDir defines directory of stats entries created by PrepareDir.
74 // StatIdentifier holds a stat entry name and index
75 type StatIdentifier struct {
80 // StatEntry represents single stat entry. The type of stat stored in Data
81 // is defined by Type.
82 type StatEntry struct {
89 // Counter represents simple counter with single value, which is usually packet count.
92 // CombinedCounter represents counter with two values, for packet count and bytes count.
93 type CombinedCounter [2]uint64
95 func (s CombinedCounter) Packets() uint64 {
99 func (s CombinedCounter) Bytes() uint64 {
103 // Name represents string value stored under name vector.
106 func (n Name) String() string {
110 // Stat represents some type of stat which is usually defined by StatType.
111 type Stat interface {
112 // IsZero returns true if all of its values equal to zero.
115 // Type returns underlying type of a stat
118 // isStat is intentionally unexported to limit implementations of interface to this package,
122 // ScalarStat represents stat for ScalarIndex.
123 type ScalarStat float64
125 // ErrorStat represents stat for ErrorIndex. The array represents workers.
126 type ErrorStat []Counter
128 // SimpleCounterStat represents indexed stat for SimpleCounterVector.
129 // The outer array represents workers and the inner array represents interface/node/.. indexes.
130 // Values should be aggregated per interface/node for every worker.
131 // ReduceSimpleCounterStatIndex can be used to reduce specific index.
132 type SimpleCounterStat [][]Counter
134 // CombinedCounterStat represents indexed stat for CombinedCounterVector.
135 // The outer array represents workers and the inner array represents interface/node/.. indexes.
136 // Values should be aggregated per interface/node for every worker.
137 // ReduceCombinedCounterStatIndex can be used to reduce specific index.
138 type CombinedCounterStat [][]CombinedCounter
140 // NameStat represents stat for NameVector.
143 // EmptyStat represents removed counter directory
144 type EmptyStat string
146 func (ScalarStat) isStat() {}
147 func (ErrorStat) isStat() {}
148 func (SimpleCounterStat) isStat() {}
149 func (CombinedCounterStat) isStat() {}
150 func (NameStat) isStat() {}
151 func (EmptyStat) isStat() {}
153 func (s ScalarStat) IsZero() bool {
157 func (s ScalarStat) Type() StatType {
161 func (s ErrorStat) IsZero() bool {
165 for _, ss := range s {
173 func (s ErrorStat) Type() StatType {
177 func (s SimpleCounterStat) IsZero() bool {
181 for _, ss := range s {
182 for _, sss := range ss {
191 func (s SimpleCounterStat) Type() StatType {
192 return SimpleCounterVector
195 func (s CombinedCounterStat) IsZero() bool {
199 for _, ss := range s {
203 for _, sss := range ss {
204 if sss[0] != 0 || sss[1] != 0 {
212 func (s CombinedCounterStat) Type() StatType {
213 return CombinedCounterVector
216 func (s NameStat) IsZero() bool {
220 for _, ss := range s {
228 func (s NameStat) Type() StatType {
232 func (s EmptyStat) IsZero() bool {
236 func (s EmptyStat) Type() StatType {
240 // ReduceSimpleCounterStatIndex returns reduced SimpleCounterStat s for index i.
241 func ReduceSimpleCounterStatIndex(s SimpleCounterStat, i int) uint64 {
243 for _, w := range s {
249 // ReduceCombinedCounterStatIndex returns reduced CombinedCounterStat s for index i.
250 func ReduceCombinedCounterStatIndex(s CombinedCounterStat, i int) [2]uint64 {
252 for _, w := range s {