-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathselector.go
80 lines (74 loc) · 2.47 KB
/
selector.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
package main
import (
"fmt"
"time"
)
const (
SelectAll = iota
CountBasedSampling
TimeBasedSampling
)
type Selector struct {
Name string
SelectionProcessName string
CacheName string
Interval uint32
Space uint32
TotalPacketCount uint64
NextSampleStartPacketCount uint64
NextSampleEndPacketCount uint64
NextSampleStartTime time.Time
NextSampleEndTime time.Time
LastPacketTime time.Time
cachePointer *Cache
Algorithm uint16
}
func (selector Selector) String() string {
s := fmt.Sprintf("Name: %s, SelectorocessName: %s, Algorithm: %d, Interval: %d, Space: %d, cachePointer: %p\n", selector.Name, selector.SelectionProcessName, selector.Algorithm, selector.Interval, selector.Space, selector.cachePointer)
return s
}
func (selector *Selector) associateCache(caches []Cache) {
for i := 0; i < len(caches); i++ {
if selector.CacheName == caches[i].Name {
selector.cachePointer = &caches[i]
break
}
}
}
func (s *Selector) selectPacket(packetTime time.Time) bool {
if s.Algorithm == SelectAll {
return true
} else if s.Algorithm == CountBasedSampling {
if s.TotalPacketCount == 0 {
s.NextSampleStartPacketCount = 1
s.NextSampleEndPacketCount = s.NextSampleStartPacketCount + uint64(s.Interval)
}
s.TotalPacketCount++
if s.NextSampleStartPacketCount <= s.TotalPacketCount && s.TotalPacketCount < s.NextSampleEndPacketCount {
return true
} else {
if s.NextSampleEndPacketCount <= s.TotalPacketCount {
s.NextSampleStartPacketCount = s.NextSampleEndPacketCount + uint64(s.Space)
s.NextSampleEndPacketCount = s.NextSampleStartPacketCount + uint64(s.Interval)
}
return false
}
} else if s.Algorithm == TimeBasedSampling {
if s.TotalPacketCount == 0 {
s.NextSampleStartTime = packetTime
s.NextSampleEndTime = s.NextSampleStartTime.Add(time.Duration(s.Interval))
}
s.TotalPacketCount++
s.LastPacketTime = packetTime
if s.NextSampleStartTime.Equal(s.LastPacketTime) || (s.NextSampleStartTime.After(s.LastPacketTime) && s.NextSampleEndTime.Before(s.LastPacketTime)) {
return true
} else {
if s.NextSampleEndTime.Equal(s.LastPacketTime) || s.NextSampleEndTime.After(s.LastPacketTime) {
s.NextSampleStartTime = s.NextSampleEndTime.Add(time.Duration(s.Space))
s.NextSampleEndTime = s.NextSampleStartTime.Add(time.Duration(s.Interval))
}
return false
}
}
return false
}