-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation_simpy.py
227 lines (185 loc) · 5.87 KB
/
simulation_simpy.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
from generators import *
from controllers import *
from applications import AppsCluster
from math import ceil
from monitoring import Monitoring, MultiMonitoring
import numpy as np
from commons import SN1, SN2, SP2, SP3, RP1, RP2, ALL
import sys
import simpy.rt
from controllers import qnTransient
import casadi
import matplotlib.pyplot as plt
from pathlib import Path
import os
import collections
cwd=os.path.dirname(os.path.abspath(__file__))
class systemMnt():
rt = None
def __init__(self):
self.rt=collections.deque(maxlen=1)
def getRT(self):
if(len(self.rt)==0):
return None
else:
return [np.mean(self.rt)]
def optCtrl(optQueue,opt2Queue,P,MU,S,H,tgt,optdt):
sold=None
opt=qnTransient()
opt.buildOpt(MU, P, S,optdt,tgt,H)
while(True):
q=optQueue.get()
opt.model.set_value(opt.initX,q)
obj=0
for h in range(H):
obj+=(opt.stateVar[1,h]-tgt/MU[1,0]*opt.tVar[1,h])**2
#obj+=opt.E_abs[0,h]
if(sold is not None):
opt.model.minimize(obj+0.0*casadi.sumsqr(opt.sVar-sold)+0.000*casadi.sumsqr(opt.sVar))
else:
opt.model.minimize(obj)
sol=opt.model.solve()
opt2Queue.put(sol.value(opt.sVar))
#control loop
def controlLoop(env,cluster,dt):
global optQueue,opt2Queue,optProc,tgt,optdt,H,toUpdate,cores_i
#idx=1.0/10
# if(optQueue==None):
# optQueue = multiprocessing.Queue()
# opt2Queue = multiprocessing.Queue()
#
# P = np.matrix([[0,1,0],[0,0,1],[0,0,1]])
# MU = np.matrix([cluster.X0[0],cluster.srateAvg[0],0]).T
# S = np.matrix([0, -1,0]).T
#
# optProc = multiprocessing.Process(target=optCtrl, args=(optQueue,opt2Queue,P,MU,S,H,tgt,optdt,))
# optProc.start()
while True:
yield env.timeout(dt)
# if(toUpdate):
# toUpdate=False
# optProc.kill()
# optQueue = multiprocessing.Queue()
# opt2Queue = multiprocessing.Queue()
#
# P = np.matrix([[0,1,0],[0,0,1],[0,0,1]])
# MU = np.matrix([cluster.X0[0],cluster.srateAvg[0],0]).T
# S = np.matrix([0, -1,0]).T
#
# optProc = multiprocessing.Process(target=optCtrl, args=(optQueue,opt2Queue,P,MU,S,H,tgt,optdt,))
# optProc.start()
q=cluster.sampleQueue()[0]
#opt control with optimaztion
# optQueue.put([0,q,0])
#
# opts=opt2Queue.get()
# print(opts)
# cluster.cores=[opts[1]]
#
#opt control
cluster.cores=[max(q/tgt,0.0001)]
cluster.updateCores(cluster.cores)
# if(mnt.getRT() is not None):
# print("Control",cluster.cores,mnt.getRT(),env.now)
# c1.control(env.now)
# cluster.cores=c1.cores
# cluster.updateCores(cluster.cores)
cores_i.append(cluster.cores[0])
threads_i.append(cluster.getSwThreads()[0])
if(len(RT)>1):
T=np.linspace(0,len(RT),len(RT))
IAvg=np.divide(np.cumsum(RT),T)
print("Control",cluster.cores,mnt.getRT(),env.now,IAvg[-1])
def monitoringLoop(env,cluster,dt):
global mnt,MU,users,cores,cores_i,RT,threads_i
while True:
yield env.timeout(dt)
rts=cluster.sampleRT(True)
if(not np.isnan(rts)):
mnt.rt.append(rts)
RT.append(rts)
users.append(cluster.X0)
cores.append(np.mean(cores_i))
cores_i=[]
threads.append(np.mean(threads_i))
threads_i=[]
#print("Monitoring",cluster.cores,mnt.getRT(),users[-1],env.now)
def simulation(env,cluster,dt,g):
global toUpdate,it
it=0
while True:
print("update trate",g.f(it))
print(env.now*100/(mtDt*simStep))
cluster.updateTRate(g.f(it))
cluster.sampleRT(True)
toUpdate=True
yield env.timeout(dt)
it+=1
users=[]
cores=[]
cores_i=[]
threads=[]
threads_i=[]
RT=[]
tgt=3.0
H=10
sold=None
holdingTime=100
changePoints=80
simStep=changePoints*holdingTime
optQueue=None
opt2Queue=None
optProc=None
toUpdate=False
#optdt=10**(-1) # dt all'interno del problema di ottimo
mtDt=10**(-1)
optdt=10**(-1)
ctrlPeriod=optdt
#applications names
Names=["App1"];
#average service rate per applications
srateAvg=[10];
#arrival rates per applications
X0=[1]
#reserved cpus quaota per applications
cores_init=[1]
#monitor object
mnt=systemMnt()
#workload generator
g = MultiGenerator([SN1])
c1 = CTControllerScaleXNode(ctrlPeriod, cores_init, 100, BCs=[0.05], DCs=[0.05])
c1.cores=cores_init
c1.setSLA([tgt*1/srateAvg[0]])
c1.monitoring=mnt
HCores=[]
Huser=[]
Hrt=[]
env=simpy.rt.RealtimeEnvironment(factor=1.2,strict=True)
#env=simpy.Environment()
cluster=AppsCluster(appNames=Names,srateAvg=srateAvg,initCores=cores_init,isDeterministic=False,X0=X0,env=env)
env.process(simulation(env,cluster,holdingTime*mtDt,g))
env.process(monitoringLoop(env,cluster,mtDt))
env.process(controlLoop(env,cluster,ctrlPeriod))
env.run(until=mtDt*simStep)
Path("%s/experiments/"%(cwd)).mkdir(parents=True, exist_ok=True)
T=np.linspace(0,len(RT),len(RT))
IAvg=np.divide(np.cumsum(RT),T)
plt.figure()
plt.plot(RT)
plt.axhline(y = tgt*1.0/srateAvg[0], color = 'r', linestyle = '--')
#plt.ylim([0,tgt*5/srateAvg[0]])
plt.plot(IAvg)
plt.savefig("./experiments/rt.pdf")
fig, ax = plt.subplots()
ax2 = ax.twinx()
ax.plot(users, color="r",label="#arate(req/s)")
ax.set_xlabel('time (k*30s)')
ax.set_ylabel('Workload (req/s)')
ax.legend(loc="upper left")
ax2.plot(cores, color="b",label="#hw_cores")
ax2.plot(threads, color="y",label="#sw_cores")
ax2.set_ylabel('Allocation')
ax2.legend(loc="upper right")
plt.savefig("./experiments/wrall.pdf")
plt.show()
#print(np.mean(RT))