-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClockThread.py
174 lines (131 loc) · 8.95 KB
/
ClockThread.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
import threading
from PrintLogs import printMessage
class ClockThread:
currentTime = 0
def __init__(self, vmList, cloudletList, configurationData, cloudletAllocationPolicy):
self.vmList = vmList
self.cloudletList = cloudletList
self.configurationData = configurationData
self.cloudletAllocationPolicy = cloudletAllocationPolicy
def init(self):
while len(self.vmList) > 0 or len([cloudlet for cloudlet in self.cloudletList if cloudlet.getLength() > 0]) > 0:
threadList = []
for cloudlet in self.cloudletList:
threadList.append(threading.Thread(target=self.clockTick, args=[cloudlet]))
for thread in threadList:
thread.start()
for thread in threadList:
thread.join()
ClockThread.currentTime += 1
def clockTick(self, cloudlet):
if cloudlet.getLength() == 0:
cloudlet.setAllocatedVmId(None)
cloudlet.setPrevAllocatedVmType(None)
return
currentVm = None
for vm in self.vmList:
if vm.getId() == cloudlet.getAllocatedVmId():
currentVm = vm
if currentVm is None:
return
currentRam = float(self.getCurrentRam(cloudlet.getRamDistribution()))
slidingWindowDuration = 30*60
if self.cloudletAllocationPolicy == "LowestSpotScoreFirst":
if currentRam > 0.9 * currentVm.getRam():
state = cloudlet.getOverUtilizedState()
if not state:
cloudlet.setOverUtilizedState(True)
cloudlet.setRamMigrationWindow(ClockThread.currentTime)
if state:
window = cloudlet.getRamMigrationWindow()
if ClockThread.currentTime - window[0] < slidingWindowDuration:
cloudlet.incrementRamMigrationWindowFrequency()
else:
if window[1] >= 0.1 * slidingWindowDuration:
message = "[" + str(
ClockThread.currentTime) + "] Ram utilisation threshold reached. Migrating cloudlet #" + str(
cloudlet.getId()) + " Ram of current allocated VM is " + str(
currentVm.getRam()) + " Current ram of cloudlet " + str(currentRam)
printMessage("CloudletAllocationAndMigration", message)
cloudlet.setMigrationEvent("RAM")
cloudlet.setPrevAllocatedVmType(currentVm.getType())
cloudlet.setRuntimeDistributionOnVm(currentVm.getType(), ClockThread.currentTime, "H_RAM")
cloudlet.setBucket(cloudlet.getBucket() + 1 if cloudlet.getBucket() < 4 else 4)
cloudlet.setUnderUtilizedState(False)
cloudlet.setOverUtilizedState(False)
cloudlet.setAllocatedVmId(None)
return
else:
cloudlet.setRamMigrationWindow(ClockThread.currentTime)
currentBucket = cloudlet.getBucket()
if (currentBucket == 2 and currentRam < 0.9 * 8) or (currentBucket == 3 and currentRam < 0.9 * 32) or (currentBucket == 4 and currentRam < 0.9 * 64):
lowstate = cloudlet.getUnderUtilizedState()
if not lowstate:
cloudlet.setUnderUtilizedState(True)
cloudlet.setLowRamMigrationWindow(ClockThread.currentTime)
if lowstate:
window = cloudlet.getLowRamMigrationWindow()
if ClockThread.currentTime - window[0] < slidingWindowDuration:
cloudlet.incrementLowRamMigrationWindowFrequency()
else:
if window[1] >= 0.1 * slidingWindowDuration:
message = "[" + str(
ClockThread.currentTime) + "] Ram underutilisation threshold reached. Migrating cloudlet #" + str(
cloudlet.getId()) + " Ram of current allocated VM is " + str(
currentVm.getRam()) + " Current ram of cloudlet " + str(currentRam)
printMessage("CloudletAllocationAndMigration", message)
cloudlet.setMigrationEvent("RAM")
cloudlet.setPrevAllocatedVmType(currentVm.getType())
cloudlet.setRuntimeDistributionOnVm(currentVm.getType(), ClockThread.currentTime, "L_RAM")
cloudlet.setBucket(cloudlet.getBucket() - 1 if cloudlet.getBucket() > 1 else 1)
cloudlet.setUnderUtilizedState(False)
cloudlet.setOverUtilizedState(False)
cloudlet.setAllocatedVmId(None)
return
else:
cloudlet.setLowRamMigrationWindow(ClockThread.currentTime)
suitableInstanceTypes = []
for instanceType in self.configurationData.keys():
if cloudlet.getHighestRamUsage() < self.configurationData[instanceType][1] and cloudlet.getBucket() == int(self.configurationData[instanceType][6]):
suitableInstanceTypes.append(instanceType)
minSpotScore = float('inf')
instanceTypeWithMinSpotScore = None
for instanceType in suitableInstanceTypes:
if float(self.configurationData[instanceType][5][int(ClockThread.currentTime / 60)]) < minSpotScore:
minSpotScore = float(self.configurationData[instanceType][5][int(ClockThread.currentTime / 60)])
instanceTypeWithMinSpotScore = instanceType
if cloudlet.getRunningOnDemand():
if float(self.configurationData[instanceTypeWithMinSpotScore][4][int(ClockThread.currentTime/60)]) < self.configurationData[currentVm.getType()][3]:
message = "[" + str(ClockThread.currentTime) + "] Migrating Cloudlet #" + str(cloudlet.getId()) + "from on demand vm #" + str(currentVm.getType()) + " to spot instance of type " + instanceTypeWithMinSpotScore
printMessage("CloudletAllocationAndMigration", message)
cloudlet.setMigrationEvent("SPOT_SCORE")
cloudlet.setPrevAllocatedVmType(currentVm.getType())
cloudlet.setRuntimeDistributionOnVm(currentVm.getType(), ClockThread.currentTime, "SPOT_SCORE", True)
cloudlet.setAllocatedVmId(None)
return
else:
if float(self.configurationData[currentVm.getType()][4][int(ClockThread.currentTime/60)]) > float(self.configurationData[currentVm.getType()][3]):
message = "[" + str(ClockThread.currentTime) + "] Vm #" + str(currentVm.getId()) + " spot price has exceeded on-demand price. Migrating Cloudlet #" + str(cloudlet.getId())
printMessage("CloudletAllocationAndMigration", message)
cloudlet.setMigrationEvent("SPOT_SCORE")
cloudlet.setPrevAllocatedVmType(currentVm.getType())
cloudlet.setRuntimeDistributionOnVm(currentVm.getType(), ClockThread.currentTime, "SPOT_SCORE")
cloudlet.setAllocatedVmId(None)
return
if float(self.configurationData[currentVm.getType()][4][int(ClockThread.currentTime/60)]) > float(self.configurationData[instanceTypeWithMinSpotScore][4][int(ClockThread.currentTime/60)]):
message = "[" + str(ClockThread.currentTime) + "] Vm type " + instanceTypeWithMinSpotScore + " has lower price than Vm #" + str(currentVm.getId()) + " of type " + currentVm.getType() + ". Migrating Cloudlet #" + str(cloudlet.getId())
printMessage("CloudletAllocationAndMigration", message)
cloudlet.setMigrationEvent("SPOT_SCORE")
cloudlet.setPrevAllocatedVmType(currentVm.getType())
cloudlet.setRuntimeDistributionOnVm(currentVm.getType(), ClockThread.currentTime, "SPOT_SCORE")
cloudlet.setAllocatedVmId(None)
return
cloudletUpdatedLength = cloudlet.getLength()-currentVm.getMips()
cloudletUpdatedLength = cloudletUpdatedLength if cloudletUpdatedLength > 0 else 0
if cloudletUpdatedLength == 0:
cloudlet.setRuntimeDistributionOnVm(currentVm.getType(), ClockThread.currentTime, "COMPLETED", cloudlet.getRunningOnDemand())
message = "[" + str(ClockThread.currentTime) + "] Cloudlet #" + str(cloudlet.getId()) + " has finished execution."
printMessage("FinishedExecution", message)
cloudlet.setLength(cloudletUpdatedLength)
def getCurrentRam(self, ramDistribution):
return ramDistribution[int(ClockThread.currentTime/60) % len(ramDistribution)]