forked from skydive-project/skydive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.go
124 lines (107 loc) · 3.34 KB
/
set.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
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 flow
import (
"fmt"
"github.com/skydive-project/skydive/common"
)
type FlowSetBandwidth struct {
ABpackets uint64
ABbytes uint64
BApackets uint64
BAbytes uint64
Duration int64
NBFlow uint64
}
func NewFlowSet() *FlowSet {
return &FlowSet{
Flows: make([]*Flow, 0),
}
}
func (fs *FlowSet) Merge(ofs *FlowSet) {
fs.Start = common.MinInt64(fs.Start, ofs.Start)
if fs.Start == 0 {
fs.Start = ofs.Start
}
fs.End = common.MaxInt64(fs.End, ofs.End)
fs.Flows = append(fs.Flows, ofs.Flows...)
}
func (fs *FlowSet) AvgBandwidth() (fsbw FlowSetBandwidth) {
if len(fs.Flows) == 0 {
return
}
fsbw.Duration = fs.End - fs.Start
for _, f := range fs.Flows {
fstart := f.Statistics.Start
fend := f.Statistics.Last
fduration := fend - fstart
if fduration == 0 {
fduration = 1
}
fdurationWindow := uint64(common.MinInt64(fend, fs.End) - common.MaxInt64(fstart, fs.Start))
if fdurationWindow == 0 {
fdurationWindow = 1
}
e := f.Statistics.GetEndpointsType(FlowEndpointType_ETHERNET)
fsbw.ABpackets += uint64(e.AB.Packets * fdurationWindow / uint64(fduration))
fsbw.ABbytes += uint64(e.AB.Bytes * fdurationWindow / uint64(fduration))
fsbw.BApackets += uint64(e.BA.Packets * fdurationWindow / uint64(fduration))
fsbw.BAbytes += uint64(e.BA.Bytes * fdurationWindow / uint64(fduration))
fsbw.NBFlow++
}
return
}
func (fs *FlowSet) Filter(filter *Filter) *FlowSet {
flowset := NewFlowSet()
for _, f := range fs.Flows {
if filter == nil || filter.Eval(f) {
if flowset.Start == 0 || flowset.Start > f.Statistics.Start {
flowset.Start = f.Statistics.Start
}
if flowset.End == 0 || flowset.Start < f.Statistics.Last {
flowset.End = f.Statistics.Last
}
flowset.Flows = append(flowset.Flows, f)
}
}
return flowset
}
func (fs *FlowSet) Bandwidth() (fsbw FlowSetBandwidth) {
for _, f := range fs.Flows {
duration := f.MetricRange.Last - f.MetricRange.Start
// set the duration to the largest flow duration. All the flow should
// be close in term of duration ~= update timer
if fsbw.Duration < duration {
fsbw.Duration = duration
}
fsbw.ABpackets += f.MetricRange.ABPackets
fsbw.ABbytes += f.MetricRange.ABBytes
fsbw.BApackets += f.MetricRange.BAPackets
fsbw.BAbytes += f.MetricRange.BABytes
fsbw.NBFlow++
}
return
}
func (fsbw FlowSetBandwidth) String() string {
return fmt.Sprintf("dt : %d seconds nbFlow %d\n\t\tAB -> BA\nPackets : %8d %8d\nBytes : %8d %8d\n",
fsbw.Duration, fsbw.NBFlow, fsbw.ABpackets, fsbw.BApackets, fsbw.ABbytes, fsbw.BAbytes)
}