/
main.go
289 lines (241 loc) · 5.6 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"github.com/danalex97/nfsTorrent/simulation"
"github.com/danalex97/nfsTorrent/config"
"github.com/danalex97/nfsTorrent/log"
errLog "log"
"runtime/pprof"
"runtime"
"os/signal"
"os"
"flag"
"math/rand"
"time"
"sync"
"fmt"
)
const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
// Flags
var confPath = flag.String(
"conf",
"./confs/small.json",
"The path to configuration .json file.",
)
var extension = flag.Int(
"ext",
MaxInt,
"Use the textesion with ext percent number of leaders.",
)
var timeCDF = flag.Bool(
"cdf",
false,
"Enable printing time cumulative distribution function.",
)
var biased = flag.Int(
"bias",
MaxInt,
"Number of outgoing connections for a biased Tracker.",
)
var verbose = flag.Bool(
"v",
false,
"Verbose output",
)
var leaders = flag.Bool(
"leaders",
false,
"Enable printing leader and follower times.",
)
var pieces = flag.Int(
"pieces",
MaxInt,
"Number of pieces the file has.",
)
var pieceSize = flag.Int(
"pieceSize",
MaxInt,
"The size of a piece from the file.",
)
var logfile = flag.String(
"log",
"",
"The packet log in `.json` format.",
)
var latency = flag.Bool(
"latency",
false,
"Enable control packet latency.",
)
var procs = flag.Int(
"procs",
runtime.NumCPU(),
"GOMAXPROCS",
)
var parallel = flag.Bool(
"parallel",
false,
"Run the simulator's event queue with support for parallel events.",
)
var multi = flag.Int(
"multi",
0,
"The number of subnodes of a virtual node as part of the extension for CacheTorrent protocol.",
)
var choker = flag.String(
"choker",
"TitForTat",
"The strategy used by the Choker. We support TitForTat and Random.",
)
var cpuprofile = flag.String("cpuprofile", "", "Write cpu profile to `file`.")
var memprofile = flag.String("memprofile", "", "Write memory profile to `file`.")
func makeMemprofile() {
// Profiling
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
errLog.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
errLog.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}
func getStats() {
log.Query(log.GetRedundancy)
log.Query(log.GetTraffic)
log.Query(log.GetTime)
if *extension != MaxInt && *leaders {
log.Query(log.GetTimeLeader)
}
if *timeCDF {
log.Query(log.GetTimeCDF)
if *extension != MaxInt && *leaders {
log.Query(log.GetLeaderCDF)
}
}
if *logfile != "" {
log.Query(log.GetLogged)
}
log.Query(log.Stop)
log.Wait()
}
func main() {
runtime.GOMAXPROCS(*procs)
fmt.Println("Running:", os.Args[1:])
// Parsing the flags
flag.Parse()
// Profiling
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
errLog.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
errLog.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
defer makeMemprofile()
// Get profile even on signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
for sig := range c {
fmt.Println("Signal received:", sig)
if *cpuprofile != "" {
pprof.StopCPUProfile()
}
makeMemprofile()
fmt.Println("Partial stats:")
getStats()
os.Exit(0)
}
}()
// Random seed
rand.Seed(time.Now().UTC().UnixNano())
// Set verbosity
log.SetVerbose(*verbose)
// Set log file
log.SetLogfile(*logfile)
var wg sync.WaitGroup
// Set extension
var template interface {}
if *extension == MaxInt {
if *biased == MaxInt {
template = new(simulation.SimulatedNode)
} else {
template = new(simulation.SimulatedBiasedNode)
}
} else {
if *multi == 0 {
if *biased == MaxInt {
template = new(simulation.SimulatedCachedNode)
fmt.Println("Running with CacheTorrent extension.")
} else {
template = new(simulation.SimulatedBiasedCachedNode)
fmt.Println("Running with CacheTorrent extension and biased selection.")
}
} else {
template = new(simulation.SimulatedMultiNode)
fmt.Println("Running with multiTorrent extension.")
}
}
// Run with configuration
s := simulation.NewSimulation(
template,
config.
JSONConfig(*confPath).
WithParams(func(c *config.Conf) {
c.Bias = *biased
c.LeaderPercent = *extension
c.Latency = *latency
c.Parallel = *parallel
c.ChokerStrategy = *choker
if *pieces != MaxInt {
c.Pieces = *pieces
c.StoragePieces = c.Pieces
}
if *multi != 0 {
c.Multi = *multi
c.StoragePieces = c.Pieces / c.Multi
}
if *pieceSize != MaxInt {
c.PieceSize = *pieceSize
}
c.SharedInit = func() {
if *multi == 0 {
wg.Add(1)
} else {
wg.Add(*multi)
}
}
c.SharedCallback = func() {
wg.Done()
}
}),
)
s.Run()
// Initial time required to run SharedInits
time.Sleep(time.Duration(float64(time.Second) * 1))
fmt.Println("Init period done.")
// Wait for all nodes to finish.
if *multi == 0 {
wg.Done()
} else {
for i := 0; i < *multi; i++ {
wg.Done()
}
}
wg.Wait()
if *latency {
// Handle late message delivery
wg.Add(100)
}
s.Stop()
t := s.Time()
fmt.Println("Downloads finished in", t, "milliseconds.")
getStats()
}