forked from stanford-esrg/lzr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.go
212 lines (190 loc) · 5.97 KB
/
bin.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
package bin
import (
"time"
//"context"
"runtime/pprof"
"sync"
"os"
"log"
"github.com/stanford-esrg/lzr"
"fmt"
)
func LZRMain() {
// create a context that can be cancelled
//ctx, cancel := context.WithCancel(context.Background())
start := time.Now()
//read in config
options, ok := lzr.Parse()
if !ok {
fmt.Fprintln(os.Stderr,"Failed to parse command line options, exiting.")
return
}
//For CPUProfiling
if options.CPUProfile != "" {
f, err := os.Create(options.CPUProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
//initalize
ipMeta := lzr.ConstructPacketStateMap( options )
f := lzr.InitFile( options.Filename )
lzr.InitParams()
writingQueue := lzr.ConstructWritingQueue( options.Workers )
pcapIncoming := lzr.ConstructPcapRoutine( options.Workers )
timeoutQueue := lzr.ConstructTimeoutQueue( options.Workers )
retransmitQueue := lzr.ConstructRetransmitQueue( options.Workers )
timeoutIncoming := lzr.PollTimeoutRoutine(
&ipMeta,timeoutQueue, retransmitQueue, options.Workers, options.Timeout, options.RetransmitSec )
incoming := lzr.ConstructIncomingRoutine( options.Workers )
var incomingDone sync.WaitGroup
incomingDone.Add(options.Workers)
done := false
writing := false
timeoutDone := true
// record to file
go func() {
for {
select {
case input := <-writingQueue:
writing = true
f.Record( input, options.Handshakes )
writing = false
}
}
}()
//start all workers
//read from zmap
for i := 0; i < options.Workers; i ++ {
go func( i int ) {
//ExitCondition: incoming channel closed
if (i == options.Workers - 1) {
// a band-aid has been added to check to see if the number of items
// in ipMeta has stayed the same for numHandshakes * timeout*2 time
// if so, close. there is some non-deterministic
// infinite loop condition that needs to be fixed in which ipMeta
// does not empty all the way
var infiniteLoop = false
var ipMetaSize = ipMeta.Count()
var intervalLoop = options.Timeout*lzr.NumHandshakes()*2
go func() {
for {
startTime := time.Now()
for time.Since(startTime) < time.Duration(intervalLoop)*time.Second {
if (ipMeta.HasUpdates()) {
startTime = time.Now()
ipMeta.ResetUpdates()
continue
}
time.Sleep(time.Duration(intervalLoop)*time.Millisecond)
}
if (ipMetaSize == ipMeta.Count()) {
fmt.Fprintln(os.Stderr,"Infinite Loop, Breaking.")
infiniteLoop = true
return
} else {
ipMetaSize = ipMeta.Count()
}
}
}()
for {
if ipMeta.IsEmpty() || infiniteLoop {
done=true
break
}
//slow down to prevent CPU busy looping
time.Sleep(1*time.Second)
fmt.Fprintln(os.Stderr,"Processing:", ipMeta.Count())
}
}
for input := range incoming {
if lzr.ReadZMap() {
toACK := true
toPUSH := false
lzr.SendAck( options, input, &ipMeta, timeoutQueue,
retransmitQueue, writingQueue, toACK, toPUSH, lzr.ACK)
} else {
lzr.SendSyn( input, &ipMeta, timeoutQueue )
}
ipMeta.FinishProcessing( input )
}
incomingDone.Done()
return
}(i)
}
//read from pcap
for i := 0; i < options.Workers; i ++ {
go func( i int ) {
for input := range pcapIncoming {
//fmt.Println("pcap incoming")
//fmt.Println(input)
inMap, startProcessing := ipMeta.IsStartProcessing( input )
//if not in map, return
if !inMap {
continue
}
//if another thread is processing, put input back
if !startProcessing {
pcapIncoming <- input
continue
}
lzr.HandlePcap(options, input, &ipMeta, timeoutQueue,
retransmitQueue, writingQueue )
ipMeta.FinishProcessing( input )
//fmt.Println("finished pcap:")
//fmt.Println(input)
}
}(i)
}
//read from timeout
for i := 0; i < options.Workers; i ++ {
go func( i int ) {
for input := range timeoutIncoming {
inMap, startProcessing := ipMeta.IsStartProcessing( input )
//if another thread is processing, put input back
//if not in map, return
if !inMap {
continue
}
if !startProcessing {
timeoutIncoming <- input
continue
}
timeoutDone = false
lzr.HandleTimeout( options, input, &ipMeta, timeoutQueue, retransmitQueue, writingQueue )
ipMeta.FinishProcessing( input )
}
}(i)
}
//exit gracefully when done
incomingDone.Wait()
for {
if done && len(writingQueue) == 0 && !writing && len(timeoutQueue) == 0 {
if options.MemProfile != "" {
f, err := os.Create(options.MemProfile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
//closing file
f.F.Flush()
t := time.Now()
startTime := time.Now()
timeoutDone = true
// 5 second repeated check if any services have called handleTimeout
for time.Since(startTime) < time.Duration(5)*time.Second {
if !(timeoutDone) {
startTime = time.Now()
timeoutDone = true
}
}
elapsed := t.Sub(start)
lzr.Summarize( elapsed )
return
}
}
} //end of main