-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflow.go
127 lines (103 loc) · 2.32 KB
/
flow.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
package main
// define data types
import (
"fmt"
"strconv"
"strings"
)
type SortedFlows []*Flow
func (sf SortedFlows) Len() int {
return len(sf)
}
func (sf SortedFlows) Less(i, j int) bool {
return (sf[i].TimeRemaining < sf[j].TimeRemaining)
}
func (sf SortedFlows) Swap(i, j int) {
tmp := sf[i]
sf[i] = sf[j]
sf[j] = tmp
}
type Flow struct {
Start float64
Size uint
Source uint8
Dest uint8
End float64
TimeRemaining float64
OracleFct float64
LastTime float64
FinishEvent *Event
FinishSending bool
Finish bool
}
func flowToString(f *Flow) string {
return fmt.Sprintf("%d %d %d %f %f %f %f %f\n", f.Source, f.Dest, f.Size, f.Start, f.OracleFct, f.End, f.End-f.Start, calculateFlowSlowdown(f))
}
func calculateFlowSlowdown(f *Flow) float64 {
if f.End < f.Start {
panic("flow has negative fct")
}
fct := (f.End - f.Start)
slowdown := fct / f.OracleFct
switch {
case slowdown >= 1:
return slowdown
case slowdown < 0.999:
panic("flow has fct better than oracle")
default:
return 1.000000
}
}
func makeFlow(l string) *Flow {
sp := strings.Split(l, " ")
size, err := strconv.ParseUint(sp[1], 10, 32)
check(err)
src, err := strconv.ParseUint(sp[2], 10, 8)
check(err)
dst, err := strconv.ParseUint(sp[3], 10, 8)
check(err)
time, err := strconv.ParseFloat(sp[4], 64)
check(err)
return &Flow{Start: time, Size: uint(size), Source: uint8(src), Dest: uint8(dst), LastTime: 0, FinishEvent: nil}
}
type EventType int
const (
FlowArrival EventType = iota
FlowSourceFree
FlowDestFree
)
type Event struct {
Time float64
Flow *Flow
Type EventType
Cancelled bool
}
func makeArrivalEvent(f *Flow) *Event {
return &Event{Time: f.Start, Flow: f, Type: FlowArrival, Cancelled: false}
}
func makeCompletionEvent(t float64, f *Flow, ty EventType) *Event {
return &Event{Time: t, Flow: f, Type: ty, Cancelled: false}
}
type EventQueue []*Event
func (e EventQueue) Len() int {
return len(e)
}
func (e EventQueue) Less(i, j int) bool {
return (e[i].Time < e[j].Time)
}
func (e EventQueue) Swap(i, j int) {
tmp := e[i]
e[i] = e[j]
e[j] = tmp
}
func (e *EventQueue) Push(x interface{}) {
ev := x.(*Event)
*e = append(*e, ev)
}
func (e *EventQueue) Pop() interface{} {
old := *e
n := len(old)
ev := old[n-1]
*e = old[0 : n-1]
return ev
}