-
Notifications
You must be signed in to change notification settings - Fork 3
/
sim.py
executable file
·253 lines (215 loc) · 9.97 KB
/
sim.py
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
#!/usr/bin/env python3
'''
Simulator for network bonded energy aware R.A.I.N
Copyright (C) 2013-2014 Marcus Haehnel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import math, argparse, imp, csv, sys
parser = argparse.ArgumentParser(description='Simulate network load and evaluate energy consumption based on card specs')
parser.add_argument('-c','--config',help='The eBond configuration file',required=True)
parser.add_argument('-b','--bwfile',help='The network stats file is in csv format, with timestamp/bandwidth',required=True)
parser.add_argument('-o','--outfile',help='The file that the profile should be written to. CSV Format: timestamp, bandwidth_in, bandwidth_out,power',required=False)
args = parser.parse_args()
cfg = imp.load_source('cfg',args.config)
class Interface:
current = None
time = 0
ifaces = []
def __init__(self, name):
self.ifname = name;
self.bw = float(eval('cfg.%s_BW' % (name)))
self.uplatency = eval('cfg.%s_LATENCY' %(name))
self.bwrange = eval('cfg.%s_RANGE' %(name))
self.profile = eval('cfg.%s_PROFILE' %(name))
self.rounded = eval('cfg.%s_ROUND' % (name))
#Check if profiles are contiguous and sane
cur_send = 0
for k in sorted(self.profile.keys()):
if int(k[0]) != cur_send:
print("ERROR in send profile! Is not contiguous: %s vs. %s" % (str(k), cur_send))
if not k[0] <= k[1] <= self.bw:
print("ERROR strange send range: %s" % str(k))
cur_recv = 0;
for p in sorted(self.profile[k]):
if p[0] != cur_recv:
print("ERROR in recv profile! Is not contiguous: %s vs. %s" % str(p), cur_recv)
if not p[0] <= p[1] <= self.bw:
print("ERROR strange recv range: %s" % str(p))
cur_recv = p[1]
cur_send = k[1];
@staticmethod
def select(bw_up,bw_down):
#Use predictor, go up fast
bw_up *= 1+cfg.PREDICTOR/100.0
bw_down *= 1+cfg.PREDICTOR/100.0
#Keep because of hysteresis?
bw_needed = max(bw_up,bw_down) #TODO: This assumes symmetric up/down
if Interface.current:
#Check for keep time and hysteresis
if bw_needed < max(Interface.current.bwrange) and (bw_needed >= min(Interface.current.bwrange)*cfg.HYSTERESIS/100.0 or Interface.time < cfg.KEEPTIME):
#Reset cooldown timer if we need this interface!
if bw_needed > min(Interface.current.bwrange):
Interface.time = 0
return Interface.current
Interface.time = 0
return min(Interface.ifaces,key=lambda x: x.getPower(bw_up,bw_down) or float("inf"))
def __str__(self):
return ('''iface: {self.ifname} @ {self.bw} MBit/s\n'''
'''Latency: {self.uplatency} ms\n'''
'''Use in range: {self.bwrange[0]} MBit/s - {self.bwrange[1]} MBit/s\n'''
'''Profile: {length}\n'''.format(self=self,length=len(self.profile)))
def getPower(self,bw_up,bw_down):
#nested lists
for snd in sorted(self.profile.keys()):
if float(snd[0]) <= bw_down < float(snd[1]):
for recv in sorted(self.profile[snd]):
if float(recv[0]) <= bw_up < float(recv[1]):
return float(recv[2])
break
if max(bw_up,bw_down) <= self.bw:
return self.rounded
return None
def getMaxBW(self):
return self.bw
def getIFace(self):
return self.ifname
class DataBuffer:
def __init__(self):
self.send = 0
self.recv = 0
self.violations = 0
self.violation_time = 0
def isBuffering(self):
return self.send > 0 or self.recv > 0
def process(self,bw_data,time,iface):
spare_send = iface.getMaxBW()-bw_data[1]
spare_recv = iface.getMaxBW()-bw_data[2]
#if we send/recv more than current channel capacity: buffer
#if we send/recv less, empty buffer
if spare_send < 0:
bw_data[1] = iface.getMaxBW()
self.send += -spare_send*time
else:
bw_data[1] += min(spare_send,self.send/time)
self.send -= min(spare_send*time,self.send)
if spare_recv < 0:
bw_data[2] = iface.getMaxBW()
self.recv += -spare_recv*time
else:
bw_data[2] += min(spare_recv,self.recv/time)
self.recv -= min(spare_recv*time,self.recv)
if self.isBuffering():
self.violations += 1
self.violation_time += time
return bw_data
print("Reading Interfaces:\n================================");
Interface.ifaces = [ Interface(i) for i in cfg.INTERFACES ]
for i in Interface.ifaces:
print(i)
print("===== DONE =====\n");
print("ebond timestep = %s s" % (cfg.INTERVAL))
print("Hysteresis = %s %% of max BW " % (cfg.HYSTERESIS))
print(args.bwfile)
total_time = 0
e_total = 0
e_worst = 0
data_total = [0,0]
violation_time = 0
dbuffer = DataBuffer()
time_iface = { i.getIFace() : 0 for i in Interface.ifaces }
#This is very simple and assumes that the last interface has the worst
#energy characteristics! This might not be an appropriate assumption!
iface_worst = Interface.ifaces[len(Interface.ifaces)-1]
if args.outfile:
profile = open(args.outfile,'w')
still_iface = False
still_time = 0
#Simulation loop
with open(args.bwfile,'rt') as csvfile:
simreader = csv.reader(csvfile, delimiter=',', quotechar="\"");
#read a new line (first line)
last_row = [ float(f) for f in next(simreader) ]
next_step = float(cfg.INTERVAL)
#and select the interface to use at this BW
Interface.current = Interface.select(last_row[1],last_row[2]) or Interface.ifaces[0]
line = 1
while True:
try:
row = next(simreader)
except:
break
line += 1
#number of steps to take before the next possible interface change
#we always take at least one step, step = bandwidth log interval
steps = max(1,math.floor((float(row[0])-last_row[0])/float(cfg.INTERVAL)))
#fast forward data and energy values ... no iface changes
target_time = last_row[0] + steps*cfg.INTERVAL
while True:
time = float(row[0]) - last_row[0]
e_worst += iface_worst.getPower(last_row[1],last_row[2])*time
#Send buffered data from before
initial_last_row = last_row[:]
#Check if some data goes in/out of the buffer due to over/underload
last_row = dbuffer.process(last_row,time,Interface.current)
#calculate data sent/received
data_total[0] += last_row[1]*time
data_total[1] += last_row[2]*time
#and the power used for this interface
cur_p = Interface.current.getPower(last_row[1],last_row[2])
Interface.time += time
if still_iface != Interface.current and still_time > 0:
cur_p += still_iface.getPower(0,0)
still_time -= time
e_total += cur_p*time
time_iface[Interface.current.getIFace()] += time
total_time += time
if line%100 == 0:
if Interface.current.getIFace() == 'eth1':
sys.stdout.write(".")
else:
sys.stdout.write("|")
sys.stdout.flush()
if args.outfile:
profile.write('%s,%s,%s,%s,%s\n' % ( last_row[0], last_row[1], last_row[2], cur_p,dbuffer.isBuffering()))
if (steps != 1 or float(row[0]) >= target_time):
break
last_row = [ float(f) for f in row ]
try:
row = next(simreader)
except:
break
line += 1
#only step if we are on the next interval. If we are
#select iface for this step
#either the best fit or the default
if float(row[0]) >= next_step:
old_iface = Interface.current
Interface.current = Interface.select(initial_last_row[1],initial_last_row[2]) or Interface.ifaces[0]
#WOAH! There is still some bug in how this is handled! Fix it! TODO
if old_iface != Interface.current:
still_time = old_iface.uplatency/1000.0
still_iface = old_iface
time_iface[old_iface.getIFace()] += old_iface.uplatency/1000.0
next_step += float(cfg.INTERVAL)
last_row = [ float(f) for f in row ]
print("DONE")
print("Simulated Time (days): %s" % (total_time/3600/24))
print("Achived Energy: %s MJ (vs %s MJ for only high power card => %s %% saved!)" % (e_total/1000000,e_worst/1000000,(e_worst-e_total)*100/e_worst))
print("Consumed Power: %s Wh (vs %s Wh)" %( str(e_total/3600),str(e_worst/3600)))
print("Interface up share: ")
for key,value in time_iface.items():
print("%s => %s %%" % (key, value*100/total_time))
print("Number of service vialoations due to late power up: %s (%s seconds or %s %% of time)"
%(dbuffer.violations,dbuffer.violation_time,violation_time*100/total_time))
print("Remaining bytes to transfer: %s / %s" %(dbuffer.send,dbuffer.recv))
print("Transfered GByte: %s / %s / %s " % (data_total[0]/1024/8,data_total[1]/1024/8,(data_total[1]+data_total[0])/1024/8))
print("Average Speed MByte/s: %s / %s / %s" % (data_total[0]/8/total_time,data_total[1]/8/total_time,(data_total[1]+data_total[0])/8/total_time))