/
metrics.go
233 lines (210 loc) · 8.38 KB
/
metrics.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
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
//go:generate go run ../scripts/gendecoder.go -output metrics_gendecoder.go
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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 ofthe 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 specificlanguage governing permissions and
* limitations under the License.
*
*/
package topology
import (
json "encoding/json"
"github.com/skydive-project/skydive/common"
)
// InterfaceMetric the interface packets counters
// easyjson:json
// gendecoder
type InterfaceMetric struct {
Collisions int64 `json:"Collisions,omitempty"`
Multicast int64 `json:"Multicast,omitempty"`
RxBytes int64 `json:"RxBytes,omitempty"`
RxCompressed int64 `json:"RxCompressed,omitempty"`
RxCrcErrors int64 `json:"RxCrcErrors,omitempty"`
RxDropped int64 `json:"RxDropped,omitempty"`
RxErrors int64 `json:"RxErrors,omitempty"`
RxFifoErrors int64 `json:"RxFifoErrors,omitempty"`
RxFrameErrors int64 `json:"RxFrameErrors,omitempty"`
RxLengthErrors int64 `json:"RxLengthErrors,omitempty"`
RxMissedErrors int64 `json:"RxMissedErrors,omitempty"`
RxOverErrors int64 `json:"RxOverErrors,omitempty"`
RxPackets int64 `json:"RxPackets,omitempty"`
TxAbortedErrors int64 `json:"TxAbortedErrors,omitempty"`
TxBytes int64 `json:"TxBytes,omitempty"`
TxCarrierErrors int64 `json:"TxCarrierErrors,omitempty"`
TxCompressed int64 `json:"TxCompressed,omitempty"`
TxDropped int64 `json:"TxDropped,omitempty"`
TxErrors int64 `json:"TxErrors,omitempty"`
TxFifoErrors int64 `json:"TxFifoErrors,omitempty"`
TxHeartbeatErrors int64 `json:"TxHeartbeatErrors,omitempty"`
TxPackets int64 `json:"TxPackets,omitempty"`
TxWindowErrors int64 `json:"TxWindowErrors,omitempty"`
Start int64 `json:"Start,omitempty"`
Last int64 `json:"Last,omitempty"`
}
// InterfaceMetricMetadataDecoder implements a json message raw decoder
func InterfaceMetricMetadataDecoder(raw json.RawMessage) (common.Getter, error) {
var metric InterfaceMetric
if err := json.Unmarshal(raw, &metric); err != nil {
return nil, err
}
return &metric, nil
}
// GetStart returns start time
func (im *InterfaceMetric) GetStart() int64 {
return im.Start
}
// SetStart set start time
func (im *InterfaceMetric) SetStart(start int64) {
im.Start = start
}
// GetLast returns last time
func (im *InterfaceMetric) GetLast() int64 {
return im.Last
}
// SetLast set last tome
func (im *InterfaceMetric) SetLast(last int64) {
im.Last = last
}
// Add sum two metrics and return a new Metrics object
func (im *InterfaceMetric) Add(m common.Metric) common.Metric {
om := m.(*InterfaceMetric)
return &InterfaceMetric{
Collisions: im.Collisions + om.Collisions,
Multicast: im.Multicast + om.Multicast,
RxBytes: im.RxBytes + om.RxBytes,
RxCompressed: im.RxCompressed + om.RxCompressed,
RxCrcErrors: im.RxCrcErrors + om.RxCrcErrors,
RxDropped: im.RxDropped + om.RxDropped,
RxErrors: im.RxErrors + om.RxErrors,
RxFifoErrors: im.RxFifoErrors + om.RxFifoErrors,
RxFrameErrors: im.RxFrameErrors + om.RxFrameErrors,
RxLengthErrors: im.RxLengthErrors + om.RxLengthErrors,
RxMissedErrors: im.RxMissedErrors + om.RxMissedErrors,
RxOverErrors: im.RxOverErrors + om.RxOverErrors,
RxPackets: im.RxPackets + om.RxPackets,
TxAbortedErrors: im.TxAbortedErrors + om.TxAbortedErrors,
TxBytes: im.TxBytes + om.TxBytes,
TxCarrierErrors: im.TxCarrierErrors + om.TxCarrierErrors,
TxCompressed: im.TxCompressed + om.TxCompressed,
TxDropped: im.TxDropped + om.TxDropped,
TxErrors: im.TxErrors + om.TxErrors,
TxFifoErrors: im.TxFifoErrors + om.TxFifoErrors,
TxHeartbeatErrors: im.TxHeartbeatErrors + om.TxHeartbeatErrors,
TxPackets: im.TxPackets + om.TxPackets,
TxWindowErrors: im.TxWindowErrors + om.TxWindowErrors,
Start: im.Start,
Last: im.Last,
}
}
// Sub subtracts two metrics and return a new metrics object
func (im *InterfaceMetric) Sub(m common.Metric) common.Metric {
om := m.(*InterfaceMetric)
return &InterfaceMetric{
Collisions: im.Collisions - om.Collisions,
Multicast: im.Multicast - om.Multicast,
RxBytes: im.RxBytes - om.RxBytes,
RxCompressed: im.RxCompressed - om.RxCompressed,
RxCrcErrors: im.RxCrcErrors - om.RxCrcErrors,
RxDropped: im.RxDropped - om.RxDropped,
RxErrors: im.RxErrors - om.RxErrors,
RxFifoErrors: im.RxFifoErrors - om.RxFifoErrors,
RxFrameErrors: im.RxFrameErrors - om.RxFrameErrors,
RxLengthErrors: im.RxLengthErrors - om.RxLengthErrors,
RxMissedErrors: im.RxMissedErrors - om.RxMissedErrors,
RxOverErrors: im.RxOverErrors - om.RxOverErrors,
RxPackets: im.RxPackets - om.RxPackets,
TxAbortedErrors: im.TxAbortedErrors - om.TxAbortedErrors,
TxBytes: im.TxBytes - om.TxBytes,
TxCarrierErrors: im.TxCarrierErrors - om.TxCarrierErrors,
TxCompressed: im.TxCompressed - om.TxCompressed,
TxDropped: im.TxDropped - om.TxDropped,
TxErrors: im.TxErrors - om.TxErrors,
TxFifoErrors: im.TxFifoErrors - om.TxFifoErrors,
TxHeartbeatErrors: im.TxHeartbeatErrors - om.TxHeartbeatErrors,
TxPackets: im.TxPackets - om.TxPackets,
TxWindowErrors: im.TxWindowErrors - om.TxWindowErrors,
Start: im.Start,
Last: im.Last,
}
}
// IsZero returns true if all the values are equal to zero
func (im *InterfaceMetric) IsZero() bool {
// sum as these numbers can't be <= 0
return (im.Collisions +
im.Multicast +
im.RxBytes +
im.RxCompressed +
im.RxCrcErrors +
im.RxDropped +
im.RxErrors +
im.RxFifoErrors +
im.RxFrameErrors +
im.RxLengthErrors +
im.RxMissedErrors +
im.RxOverErrors +
im.RxPackets +
im.TxAbortedErrors +
im.TxBytes +
im.TxCarrierErrors +
im.TxCompressed +
im.TxDropped +
im.TxErrors +
im.TxFifoErrors +
im.TxHeartbeatErrors +
im.TxPackets +
im.TxWindowErrors) == 0
}
func (im *InterfaceMetric) applyRatio(ratio float64) *InterfaceMetric {
return &InterfaceMetric{
Collisions: int64(float64(im.Collisions) * ratio),
Multicast: int64(float64(im.Multicast) * ratio),
RxBytes: int64(float64(im.RxBytes) * ratio),
RxCompressed: int64(float64(im.RxCompressed) * ratio),
RxCrcErrors: int64(float64(im.RxCrcErrors) * ratio),
RxDropped: int64(float64(im.RxDropped) * ratio),
RxErrors: int64(float64(im.RxErrors) * ratio),
RxFifoErrors: int64(float64(im.RxFifoErrors) * ratio),
RxFrameErrors: int64(float64(im.RxFrameErrors) * ratio),
RxLengthErrors: int64(float64(im.RxLengthErrors) * ratio),
RxMissedErrors: int64(float64(im.RxMissedErrors) * ratio),
RxOverErrors: int64(float64(im.RxOverErrors) * ratio),
RxPackets: int64(float64(im.RxPackets) * ratio),
TxAbortedErrors: int64(float64(im.TxAbortedErrors) * ratio),
TxBytes: int64(float64(im.TxBytes) * ratio),
TxCarrierErrors: int64(float64(im.TxCarrierErrors) * ratio),
TxCompressed: int64(float64(im.TxCompressed) * ratio),
TxDropped: int64(float64(im.TxDropped) * ratio),
TxErrors: int64(float64(im.TxErrors) * ratio),
TxFifoErrors: int64(float64(im.TxFifoErrors) * ratio),
TxHeartbeatErrors: int64(float64(im.TxHeartbeatErrors) * ratio),
TxPackets: int64(float64(im.TxPackets) * ratio),
TxWindowErrors: int64(float64(im.TxWindowErrors) * ratio),
Start: im.Start,
Last: im.Last,
}
}
// Split splits a metric into two parts
func (im *InterfaceMetric) Split(cut int64) (common.Metric, common.Metric) {
if cut <= im.Start {
return nil, im
} else if cut >= im.Last || im.Start == im.Last {
return im, nil
}
duration := float64(im.Last - im.Start)
ratio1 := float64(cut-im.Start) / duration
ratio2 := float64(im.Last-cut) / duration
m1 := im.applyRatio(ratio1)
m1.Last = cut
m2 := im.applyRatio(ratio2)
m2.Start = cut
return m1, m2
}